aboutsummaryrefslogtreecommitdiffstats
path: root/lib/format/format.go
diff options
context:
space:
mode:
authorReto Brunner <reto@labrat.space>2020-11-10 19:57:09 +0100
committerReto Brunner <reto@labrat.space>2020-11-14 15:40:13 +0100
commitfc9ccc30008e564c1dea23b3bfe480ca5a10c070 (patch)
tree80acc77569557d73cbd073def0dabd6472653b5d /lib/format/format.go
parentfb67d1f5a3f6fe875d74581aff59c79817a7b9f4 (diff)
downloadaerc-fc9ccc30008e564c1dea23b3bfe480ca5a10c070.tar.gz
remove models.Address in favor of go-message Address
We made a new type out of go-message/mail.Address without any real reason. This suddenly made it necessary to convert from one to the other without actually having any benefit whatsoever. This commit gets rid of the additional type
Diffstat (limited to 'lib/format/format.go')
-rw-r--r--lib/format/format.go39
1 files changed, 30 insertions, 9 deletions
diff --git a/lib/format/format.go b/lib/format/format.go
index 787821a2..e19ca319 100644
--- a/lib/format/format.go
+++ b/lib/format/format.go
@@ -2,25 +2,28 @@ package format
import (
"errors"
+ "fmt"
"mime"
gomail "net/mail"
+ "regexp"
"strings"
"time"
"unicode"
"git.sr.ht/~sircmpwn/aerc/models"
"github.com/emersion/go-message"
+ "github.com/emersion/go-message/mail"
)
-func ParseAddress(address string) (*models.Address, error) {
+func ParseAddress(address string) (*mail.Address, error) {
addrs, err := gomail.ParseAddress(address)
if err != nil {
return nil, err
}
- return (*models.Address)(addrs), nil
+ return (*mail.Address)(addrs), nil
}
-func ParseAddressList(s string) ([]*models.Address, error) {
+func ParseAddressList(s string) ([]*mail.Address, error) {
if len(s) == 0 {
// we don't consider an empty list to be an error
return nil, nil
@@ -33,17 +36,35 @@ func ParseAddressList(s string) ([]*models.Address, error) {
return nil, err
}
- addrs := make([]*models.Address, len(list))
+ addrs := make([]*mail.Address, len(list))
for i, a := range list {
- addrs[i] = (*models.Address)(a)
+ addrs[i] = (*mail.Address)(a)
}
return addrs, nil
}
-func FormatAddresses(l []*models.Address) string {
+// AddressForHumans formats the address. If the address's name
+// contains non-ASCII characters it will be quoted but not encoded.
+// 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 {
+ return fmt.Sprintf("\"%s\" <%s>",
+ strings.ReplaceAll(a.Name, "\"", "'"), a.Address)
+ }
+ } else {
+ return fmt.Sprintf("<%s>", a.Address)
+ }
+}
+
+var atom *regexp.Regexp = regexp.MustCompile("^[a-z0-9!#$%7'*+-/=?^_`{}|~ ]+$")
+
+func FormatAddresses(l []*mail.Address) string {
formatted := make([]string, len(l))
for i, a := range l {
- formatted[i] = a.Format()
+ formatted[i] = AddressForHumans(a)
}
return strings.Join(formatted, ", ")
}
@@ -130,7 +151,7 @@ func ParseMessageFormat(format string, timeFmt string, ctx Ctx) (string,
return "", nil,
errors.New("no envelope available for this message")
}
- var addr *models.Address
+ var addr *mail.Address
if len(envelope.ReplyTo) == 0 {
if len(envelope.From) == 0 {
return "", nil,
@@ -171,7 +192,7 @@ func ParseMessageFormat(format string, timeFmt string, ctx Ctx) (string,
return "", nil,
errors.New("found no address for sender")
}
- addr := envelope.From[0].Format()
+ addr := AddressForHumans(envelope.From[0])
retval = append(retval, 's')
args = append(args, addr)
case 'F':