aboutsummaryrefslogtreecommitdiffstats
path: root/lib/format
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2024-03-11 17:31:24 +0100
committerRobin Jarry <robin@jarry.cc>2024-04-13 22:37:22 +0200
commitf204c736759f4d6647a50aa94a415545c0d3a8f7 (patch)
tree51a5febd494ed4ac0d9427ffbeb18a8bd536a835 /lib/format
parent953355acc950c53ee95469bad2ee9e9c206970a3 (diff)
downloadaerc-f204c736759f4d6647a50aa94a415545c0d3a8f7.tar.gz
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" <foo@baz.org> 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 <robin@jarry.cc> Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
Diffstat (limited to 'lib/format')
-rw-r--r--lib/format/format.go14
1 files 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))