about summary refs log tree commit diff stats
path: root/commands
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-05-25 11:56:56 -0400
committerDrew DeVault <sir@cmpwn.com>2019-05-25 11:56:56 -0400
commitcef784bf520470315e93835e9c2828d69de0d5c9 (patch)
tree1fd81d5ddcd630b4d2da5e733a85f864cc2bd0b1 /commands
parentc4c8648cc716d40f5f6558b5f2bf375d8f9a36d9 (diff)
downloadaerc-cef784bf520470315e93835e9c2828d69de0d5c9.tar.gz
Fix special characters in address.PersonalName
Diffstat (limited to 'commands')
-rw-r--r--commands/account/reply.go40
-rw-r--r--commands/compose/send.go22
2 files changed, 35 insertions, 27 deletions
diff --git a/commands/account/reply.go b/commands/account/reply.go
index cd07ec9..579d671 100644
--- a/commands/account/reply.go
+++ b/commands/account/reply.go
@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"io"
 	gomail "net/mail"
+	"regexp"
 	"strings"
 
 	"git.sr.ht/~sircmpwn/getopt"
@@ -21,6 +22,25 @@ func init() {
 	register("reply", Reply)
 }
 
+var (
+	atom *regexp.Regexp = regexp.MustCompile("^[a-z0-9!#$%7'*+-/=?^_`{}|~ ]+$")
+)
+
+func formatAddress(addr *imap.Address) string {
+	if addr.PersonalName != "" {
+		if atom.MatchString(addr.PersonalName) {
+			return fmt.Sprintf("%s <%s@%s>",
+				addr.PersonalName, addr.MailboxName, addr.HostName)
+		} else {
+			return fmt.Sprintf("\"%s\" <%s@%s>",
+				strings.ReplaceAll(addr.PersonalName, "\"", "'"),
+				addr.MailboxName, addr.HostName)
+		}
+	} else {
+		return fmt.Sprintf("<%s@%s>", addr.MailboxName, addr.HostName)
+	}
+}
+
 func Reply(aerc *widgets.Aerc, args []string) error {
 	opts, optind, err := getopt.Getopts(args[1:], "aq")
 	if err != nil {
@@ -70,26 +90,14 @@ func Reply(aerc *widgets.Aerc, args []string) error {
 	}
 	if replyAll {
 		for _, addr := range msg.Envelope.Cc {
-			if addr.PersonalName != "" {
-				cc = append(cc, fmt.Sprintf("%s <%s@%s>",
-					addr.PersonalName, addr.MailboxName, addr.HostName))
-			} else {
-				cc = append(cc, fmt.Sprintf("<%s@%s>",
-					addr.MailboxName, addr.HostName))
-			}
+			cc = append(cc, formatAddress(addr))
 		}
 		for _, addr := range msg.Envelope.To {
 			address := fmt.Sprintf("%s@%s", addr.MailboxName, addr.HostName)
 			if address == us.Address {
 				continue
 			}
-			if addr.PersonalName != "" {
-				to = append(to, fmt.Sprintf("%s <%s@%s>",
-					addr.PersonalName, addr.MailboxName, addr.HostName))
-			} else {
-				to = append(to, fmt.Sprintf("<%s@%s>",
-					addr.MailboxName, addr.HostName))
-			}
+			to = append(to, formatAddress(addr))
 		}
 	}
 
@@ -103,8 +111,8 @@ func Reply(aerc *widgets.Aerc, args []string) error {
 	composer := widgets.NewComposer(
 		aerc.Config(), acct.AccountConfig(), acct.Worker()).
 		Defaults(map[string]string{
-			"To":          strings.Join(to, ","),
-			"Cc":          strings.Join(cc, ","),
+			"To":          strings.Join(to, ", "),
+			"Cc":          strings.Join(cc, ", "),
 			"Subject":     subject,
 			"In-Reply-To": msg.Envelope.MessageId,
 		}).
diff --git a/commands/compose/send.go b/commands/compose/send.go
index 2c2e294..7956aee 100644
--- a/commands/compose/send.go
+++ b/commands/compose/send.go
@@ -2,7 +2,6 @@ package compose
 
 import (
 	"crypto/tls"
-	"errors"
 	"fmt"
 	"io"
 	"net/mail"
@@ -14,6 +13,7 @@ import (
 	"github.com/emersion/go-smtp"
 	"github.com/gdamore/tcell"
 	"github.com/miolini/datacounter"
+	"github.com/pkg/errors"
 
 	"git.sr.ht/~sircmpwn/aerc/widgets"
 	"git.sr.ht/~sircmpwn/aerc/worker/types"
@@ -37,7 +37,7 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
 
 	uri, err := url.Parse(config.Outgoing)
 	if err != nil {
-		return err
+		return errors.Wrap(err, "url.Parse(outgoing)")
 	}
 	var (
 		scheme string
@@ -55,7 +55,7 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
 
 	header, rcpts, err := composer.PrepareHeader()
 	if err != nil {
-		return err
+		return errors.Wrap(err, "PrepareHeader")
 	}
 
 	if config.From == "" {
@@ -63,7 +63,7 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
 	}
 	from, err := mail.ParseAddress(config.From)
 	if err != nil {
-		return err
+		return errors.Wrap(err, "ParseAddress(config.From)")
 	}
 
 	var (
@@ -101,7 +101,7 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
 			}
 			conn, err = smtp.Dial(host)
 			if err != nil {
-				return 0, err
+				return 0, errors.Wrap(err, "smtp.Dial")
 			}
 			defer conn.Close()
 			if sup, _ := conn.Extension("STARTTLS"); sup {
@@ -114,7 +114,7 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
 				if err = conn.StartTLS(&tls.Config{
 					ServerName: serverName,
 				}); err != nil {
-					return 0, err
+					return 0, errors.Wrap(err, "StartTLS")
 				}
 			} else {
 				if starttls {
@@ -136,7 +136,7 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
 				ServerName: serverName,
 			})
 			if err != nil {
-				return 0, err
+				return 0, errors.Wrap(err, "smtp.DialTLS")
 			}
 			defer conn.Close()
 		}
@@ -144,21 +144,21 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
 		// TODO: sendmail
 		if saslClient != nil {
 			if err = conn.Auth(saslClient); err != nil {
-				return 0, err
+				return 0, errors.Wrap(err, "conn.Auth")
 			}
 		}
 		// TODO: the user could conceivably want to use a different From and sender
 		if err = conn.Mail(from.Address); err != nil {
-			return 0, err
+			return 0, errors.Wrap(err, "conn.Mail")
 		}
 		for _, rcpt := range rcpts {
 			if err = conn.Rcpt(rcpt); err != nil {
-				return 0, err
+				return 0, errors.Wrap(err, "conn.Rcpt")
 			}
 		}
 		wc, err := conn.Data()
 		if err != nil {
-			return 0, err
+			return 0, errors.Wrap(err, "conn.Data")
 		}
 		defer wc.Close()
 		ctr := datacounter.NewWriterCounter(wc)