summary refs log tree commit diff stats
path: root/lib/address.go
diff options
context:
space:
mode:
authorYash Srivastav <yash111998@gmail.com>2019-06-08 01:05:23 +0530
committerDrew DeVault <sir@cmpwn.com>2019-06-07 16:22:01 -0400
commitfca7321639f77bbf825dc897156d7a21993a2c69 (patch)
tree5e503bf4ebd9672eef2af5cbad49b5b73405e548 /lib/address.go
parent6d491569c0f95bec03cc837446c2836acabe99ae (diff)
downloadaerc-fca7321639f77bbf825dc897156d7a21993a2c69.tar.gz
Message list: implement index-format option
Diffstat (limited to 'lib/address.go')
-rw-r--r--lib/address.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/address.go b/lib/address.go
new file mode 100644
index 0000000..b557195
--- /dev/null
+++ b/lib/address.go
@@ -0,0 +1,40 @@
+package lib
+
+import (
+	"bytes"
+	"fmt"
+	"regexp"
+	"strings"
+
+	"github.com/emersion/go-imap"
+)
+
+var (
+	atom *regexp.Regexp = regexp.MustCompile("^[a-z0-9!#$%7'*+-/=?^_`{}|~ ]+$")
+)
+
+func FormatAddresses(addrs []*imap.Address) string {
+	val := bytes.Buffer{}
+	for i, addr := range addrs {
+		val.WriteString(FormatAddress(addr))
+		if i != len(addrs)-1 {
+			val.WriteString(", ")
+		}
+	}
+	return val.String()
+}
+
+func FormatAddress(addr *imap.Address) string {
+	if addr.PersonalName != "" {
+		if atom.MatchString(addr.PersonalName) {
+			return fmt.Sprintf("%s <%s@%s>",
+				addr.PersonalName, addr.MailboxName, addr.HostName)
+		} else {
+			return fmt.Sprintf("\"%s\" <%s@%s>",
+				strings.ReplaceAll(addr.PersonalName, "\"", "'"),
+				addr.MailboxName, addr.HostName)
+		}
+	} else {
+		return fmt.Sprintf("<%s@%s>", addr.MailboxName, addr.HostName)
+	}
+}