From f204c736759f4d6647a50aa94a415545c0d3a8f7 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Mon, 11 Mar 2024 17:31:24 +0100 Subject: compose,viewer: do not always wrap people names in quotes Currently, names of recipients in From, To, Cc and Bcc headers are (almost) always wrapped in quotes. Even if they are pure ASCII and contain one upper case letter. E.g.: From: "Foo" There is no valid reason to add these quotes unless the name contains special characters as specified by RFC 5322, section 3.2.3: specials = "(" / ")" / ; Special characters that do "<" / ">" / ; not appear in atext "[" / "]" / ":" / ";" / "@" / "\" / "," / "." / DQUOTE Adapt the check accordingly. Link: https://datatracker.ietf.org/doc/html/rfc5322#section-3.2.3 Signed-off-by: Robin Jarry Tested-by: Bence Ferdinandy --- lib/format/format.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/format/format.go b/lib/format/format.go index a8600009..430abf75 100644 --- a/lib/format/format.go +++ b/lib/format/format.go @@ -2,7 +2,6 @@ package format import ( "fmt" - "regexp" "strings" "time" "unicode" @@ -10,24 +9,23 @@ import ( "github.com/emersion/go-message/mail" ) -// AddressForHumans formats the address. If the address's name -// contains non-ASCII characters it will be quoted but not encoded. +const rfc5322specials string = `()<>[]:;@\,."` + +// AddressForHumans formats the address. // Meant for display purposes to the humans, not for sending over the wire. func AddressForHumans(a *mail.Address) string { if a.Name != "" { - if atom.MatchString(a.Name) { - return fmt.Sprintf("%s <%s>", a.Name, a.Address) - } else { + if strings.ContainsAny(a.Name, rfc5322specials) { return fmt.Sprintf("\"%s\" <%s>", strings.ReplaceAll(a.Name, "\"", "'"), a.Address) + } else { + return fmt.Sprintf("%s <%s>", a.Name, a.Address) } } else { return fmt.Sprintf("<%s>", a.Address) } } -var atom *regexp.Regexp = regexp.MustCompile("^[a-z0-9!#$%7'*+-/=?^_`{}|~ ]+$") - // FormatAddresses formats a list of addresses into a human readable string func FormatAddresses(l []*mail.Address) string { formatted := make([]string, len(l)) -- cgit v1.2.3-54-g00ecf