-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
56 lines (48 loc) · 1.31 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package imap
import (
"fmt"
"strings"
"github.com/paulrosania/go-mail"
)
func quoteString(s string) string {
return fmt.Sprintf("%q", s)
}
func headerToImapString(h *mail.Header, key string) string {
for _, fld := range h.Fields {
if fld.Name() == key {
return fmt.Sprintf("%q", fld.Value)
}
}
return "NIL"
}
func headerToEnvelopeString(h *mail.Header) string {
return fmt.Sprintf("(%s %s %s %s %s %s %s %s %s %s)",
headerToImapString(h, "Date"),
headerToImapString(h, "Subject"),
addressHeaderToStructureList(h.Get("From")),
addressHeaderToStructureList(h.Get("Sender")),
addressHeaderToStructureList(h.Get("Reply-To")),
addressHeaderToStructureList(h.Get("To")),
addressHeaderToStructureList(h.Get("Cc")),
addressHeaderToStructureList(h.Get("Bcc")),
headerToImapString(h, "In-Reply-To"),
headerToImapString(h, "Message-Id"))
}
func addressHeaderToStructureList(header string) string {
if header == "" {
return "NIL"
}
ap := mail.NewAddressParser(header)
addrs := make([]string, 0, len(ap.Addresses))
for _, a := range ap.Addresses {
name := a.Name(false)
if name == "" {
name = "NIL"
} else {
name = quoteString(name)
}
str := fmt.Sprintf("(%s NIL %q %q)", name, a.Localpart, a.Domain)
addrs = append(addrs, str)
}
return fmt.Sprintf("(%s)", strings.Join(addrs, " "))
}