forked from sg3des/eml
/
header_list.go
179 lines (156 loc) · 3.45 KB
/
header_list.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package eml
import (
"fmt"
"mime"
"strings"
"time"
"github.com/Schidstorm/eml/decoder"
)
type HeaderList map[string][]string
func (h HeaderList) FirstByKey(key string) (string, bool) {
if list, ok := h[key]; ok && len(list) > 0 {
return list[0], true
}
return "", false
}
func (h HeaderList) Add(key, value string) {
if _, ok := h[key]; !ok {
h[key] = []string{}
}
h[key] = append(h[key], value)
}
func (h HeaderList) Set(key string, value []string) {
h[key] = value
}
func (h HeaderList) ContentType() string {
if header, ok := h.FirstByKey("Content-Type"); ok {
return header
}
return "text/plain"
}
type MediaType struct {
Type string
Params map[string]string
}
func (h HeaderList) MediaType() MediaType {
mediaType, params, _ := mime.ParseMediaType(h.ContentType())
return MediaType{
Type: mediaType,
Params: params,
}
}
func (h HeaderList) MessageId() string {
if header, ok := h.FirstByKey("Message-ID"); ok {
return strings.Trim(header, `<>`)
}
return ""
}
func (h HeaderList) InReply() []string {
if header, ok := h.FirstByKey("In-Reply-To"); ok {
ids := strings.Fields(string(header))
var inReply []string
for _, id := range ids {
inReply = append(inReply, strings.Trim(id, `<> `))
}
return inReply
}
return nil
}
func (h HeaderList) References() []string {
if header, ok := h.FirstByKey("References"); ok {
ids := strings.Fields(string(header))
var references []string
for _, id := range ids {
references = append(references, strings.Trim(id, `<> `))
}
return references
}
return nil
}
func (h HeaderList) Date() time.Time {
if header, ok := h.FirstByKey("Date"); ok {
return ParseDate(header)
}
return time.Unix(0, 0)
}
func (h HeaderList) From() []Address {
if header, ok := h.FirstByKey("From"); ok {
if l, err := parseAddressList([]byte(header)); err == nil {
return l
}
}
return nil
}
func (h HeaderList) Sender() Address {
if header, ok := h.FirstByKey("Sender"); ok {
if l, err := ParseAddress([]byte(header)); err == nil {
if l == nil {
from := h.From()
if len(from) > 0 {
return from[0]
}
}
return l
}
}
return nil
}
func (h HeaderList) ReplyTo() []Address {
if header, ok := h.FirstByKey("Reply-To"); ok {
if l, err := parseAddressList([]byte(header)); err == nil {
return l
}
}
return nil
}
func (h HeaderList) To() []Address {
if header, ok := h.FirstByKey("To"); ok {
if l, err := parseAddressList([]byte(header)); err == nil {
return l
}
}
return nil
}
func (h HeaderList) Cc() []Address {
if header, ok := h.FirstByKey("Cc"); ok {
if l, err := parseAddressList([]byte(header)); err == nil {
return l
}
}
return nil
}
func (h HeaderList) Bcc() []Address {
if header, ok := h.FirstByKey("Bcc"); ok {
if l, err := parseAddressList([]byte(header)); err == nil {
return l
}
}
return nil
}
func (h HeaderList) Subject() string {
if header, ok := h.FirstByKey("Subject"); ok {
subject, err := decoder.Parse([]byte(header))
if err != nil {
fmt.Println("Failed decode subject", err)
}
return string(subject)
}
return ""
}
func (h HeaderList) Comments() []string {
if header, ok := h.FirstByKey("Comments"); ok {
return []string{header}
}
return nil
}
func (h HeaderList) Keywords() []string {
if header, ok := h.FirstByKey("Keywords"); ok {
ks := strings.Split(header, ",")
var keywords []string
for _, k := range ks {
keywords = append(keywords, strings.TrimSpace(k))
}
return keywords
}
return nil
}