From 0c9f59bad49d5b7073efd1acd7c0299a02b25509 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Mon, 30 Dec 2019 09:41:32 -0500 Subject: Handle MIME encoded addresses in address book When addresses contain special characters, net/mail MIME-encodes them to a valid RFC 5322 address for use in headers. However, these are not pleasant for human consumption, so we decode them for use in the completion list. Aerc properly encodes addresses when the message is sent. This patch also removes surrounding white space from contact names, if present. --- completer/completer.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'completer') diff --git a/completer/completer.go b/completer/completer.go index baa897d..f6900ee 100644 --- a/completer/completer.go +++ b/completer/completer.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "log" + "mime" "net/mail" "os/exec" "strings" @@ -139,13 +140,22 @@ func readCompletions(r io.Reader) ([]string, error) { parts := strings.SplitN(line, "\t", 3) if addr, err := mail.ParseAddress(parts[0]); err == nil { if len(parts) > 1 { - addr.Name = parts[1] + addr.Name = strings.TrimSpace(parts[1]) } - completions = append(completions, addr.String()) + decoded, err := decodeMIME(addr.String()) + if err != nil { + return nil, fmt.Errorf("could not decode MIME string: %w", err) + } + completions = append(completions, decoded) } } } +func decodeMIME(s string) (string, error) { + var d mime.WordDecoder + return d.DecodeHeader(s) +} + func (c *Completer) handleErr(err error) { if c.errHandler != nil { c.errHandler(err) -- cgit 1.4.1-2-gfad0