From c84630714405a1e93766a6a6c023801302a3ea66 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Wed, 19 Aug 2020 12:01:45 +0200 Subject: base models.Address on the mail.Address type This allows us to hook into the std libs implementation of parsing related stuff. For this, we need to get rid of the distinction between a mailbox and a host to just a single "address" field. However this is already the common case. All but one users immediately concatenated the mbox/domain to a single address. So this in effects makes it simpler for most cases and we simply do the transformation in the special case. --- models/models.go | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) (limited to 'models') diff --git a/models/models.go b/models/models.go index d61b774a..b7d7e05d 100644 --- a/models/models.go +++ b/models/models.go @@ -1,9 +1,9 @@ package models import ( - "bytes" "fmt" "io" + gomail "net/mail" "regexp" "strings" "time" @@ -134,38 +134,24 @@ type Envelope struct { MessageId string } -type Address struct { - Name string - Mailbox string - Host string -} +type Address gomail.Address var atom *regexp.Regexp = regexp.MustCompile("^[a-z0-9!#$%7'*+-/=?^_`{}|~ ]+$") -func (a Address) Format() string { +// String 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 (a *Address) Format() string { if a.Name != "" { if atom.MatchString(a.Name) { - return fmt.Sprintf("%s <%s@%s>", a.Name, a.Mailbox, a.Host) + return fmt.Sprintf("%s <%s>", a.Name, a.Address) } else { - return fmt.Sprintf("\"%s\" <%s@%s>", - strings.ReplaceAll(a.Name, "\"", "'"), - a.Mailbox, a.Host) + return fmt.Sprintf("\"%s\" <%s>", + strings.ReplaceAll(a.Name, "\"", "'"), a.Address) } } else { - return fmt.Sprintf("<%s@%s>", a.Mailbox, a.Host) - } -} - -// FormatAddresses formats a list of addresses, separating each by a comma -func FormatAddresses(addrs []*Address) string { - val := bytes.Buffer{} - for i, addr := range addrs { - val.WriteString(addr.Format()) - if i != len(addrs)-1 { - val.WriteString(", ") - } + return fmt.Sprintf("<%s>", a.Address) } - return val.String() } // OriginalMail is helper struct used for reply/forward -- cgit