-
Notifications
You must be signed in to change notification settings - Fork 0
/
headerParser.go
87 lines (69 loc) · 1.4 KB
/
headerParser.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package email
import (
"net/mail"
"strings"
"time"
)
type headerParser struct {
header *mail.Header
err error
}
func (hp headerParser) parseAddress(s string) (mailAddress *mail.Address) {
if hp.err != nil {
return nil
}
if strings.Trim(s, " \n") != "" {
mailAddress, hp.err = mail.ParseAddress(s)
return mailAddress
}
return nil
}
func (hp headerParser) parseAddressList(s string) (mailAddress []*mail.Address) {
if hp.err != nil {
return
}
if strings.Trim(s, " \n") != "" {
mailAddress, hp.err = mail.ParseAddressList(s)
return
}
return
}
func (hp headerParser) parseTime(s string) (mailTime time.Time) {
s = strings.Trim(s, " ")
if hp.err != nil || s == "" {
return
}
formats := []string{
time.RFC1123Z,
"Mon, 2 Jan 2006 15:04:05 -0700",
time.RFC1123Z + " (MST)",
"Mon, 2 Jan 2006 15:04:05 -0700 (MST)",
}
for _, format := range formats {
mailTime, hp.err = time.Parse(format, s)
if hp.err == nil {
return
}
}
return
}
func (hp headerParser) parseMessageId(idString string) string {
if hp.err != nil {
return ""
}
return strings.Trim(idString, "<> ")
}
func (hp headerParser) parseMessageIdList(idString string) (result []string) {
if hp.err != nil {
return
}
for _, p := range strings.Split(idString, " ") {
if strings.Trim(p, " \n") != "" {
id := hp.parseMessageId(p)
if len(id) > 0 {
result = append(result, id)
}
}
}
return
}