about summary refs log tree commit diff stats
path: root/models/models.go
diff options
context:
space:
mode:
authorReto Brunner <reto@labrat.space>2020-08-19 12:01:45 +0200
committerReto Brunner <reto@labrat.space>2020-08-20 19:18:57 +0200
commitc84630714405a1e93766a6a6c023801302a3ea66 (patch)
tree156e9f45c6847a123f677594c8aff3a82322bce8 /models/models.go
parentfe1cabb077cf6c6cb3de122b3f5532acbeba8c85 (diff)
downloadaerc-c84630714405a1e93766a6a6c023801302a3ea66.tar.gz
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.
Diffstat (limited to 'models/models.go')
-rw-r--r--models/models.go34
1 files changed, 10 insertions, 24 deletions
diff --git a/models/models.go b/models/models.go
index d61b774..b7d7e05 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