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.
This commit is contained in:
Ben Burwell 2019-12-30 09:41:32 -05:00 committed by Drew DeVault
parent b360cca977
commit 0c9f59bad4

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"mime"
"net/mail" "net/mail"
"os/exec" "os/exec"
"strings" "strings"
@ -139,13 +140,22 @@ func readCompletions(r io.Reader) ([]string, error) {
parts := strings.SplitN(line, "\t", 3) parts := strings.SplitN(line, "\t", 3)
if addr, err := mail.ParseAddress(parts[0]); err == nil { if addr, err := mail.ParseAddress(parts[0]); err == nil {
if len(parts) > 1 { 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) { func (c *Completer) handleErr(err error) {
if c.errHandler != nil { if c.errHandler != nil {
c.errHandler(err) c.errHandler(err)