-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmail.go
293 lines (264 loc) · 6.17 KB
/
mail.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Package mail implements a parser for electronic mail messages as specified
// in RFC5322.
//
// We allow both CRLF and LF to be used in the input, possibly mixed.
package emlparser
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"mime/quotedprintable"
"regexp"
"strings"
"time"
"github.com/nelsonken/emlparser-go/decoder"
)
var benc = base64.URLEncoding
func mkId(s []byte) string {
h := sha1.New()
h.Write(s)
hash := h.Sum(nil)
ed := benc.EncodeToString(hash)
return ed[0:20]
}
type HeaderInfo struct {
FullHeaders []Header // all headers
OptHeaders []Header // unprocessed headers
MessageId string
Id string
Date time.Time
From []Address
Sender Address
ReplyTo []Address
To []Address
Cc []Address
Bcc []Address
Subject string
Comments []string
Keywords []string
ContentType string
InReply []string
References []string
}
type Message struct {
HeaderInfo
Body []byte
Text string
Html string
Attachments []Attachment
Parts []Part
}
type Attachment struct {
Filename string
Data []byte
}
type Header struct {
Key, Value string
}
func Parse(s []byte) (m Message, e error) {
r, e := ParseRaw(s)
if e != nil {
return
}
return Process(r)
}
func Process(r RawMessage) (m Message, e error) {
m.FullHeaders = []Header{}
m.OptHeaders = []Header{}
for _, rh := range r.RawHeaders {
h := Header{string(rh.Key), string(rh.Value)}
m.FullHeaders = append(m.FullHeaders, h)
switch string(rh.Key) {
case `Content-Type`:
m.ContentType = string(rh.Value)
case `Message-ID`:
v := bytes.Trim(rh.Value, `<>`)
m.MessageId = string(v)
m.Id = mkId(v)
case `In-Reply-To`:
ids := strings.Fields(string(rh.Value))
for _, id := range ids {
m.InReply = append(m.InReply, strings.Trim(id, `<> `))
}
case `References`:
ids := strings.Fields(string(rh.Value))
for _, id := range ids {
m.References = append(m.References, strings.Trim(id, `<> `))
}
case `Date`:
m.Date = ParseDate(string(rh.Value))
case `From`:
m.From, e = parseAddressList(rh.Value)
case `Sender`:
m.Sender, e = ParseAddress(rh.Value)
case `Reply-To`:
m.ReplyTo, e = parseAddressList(rh.Value)
case `To`:
m.To, e = parseAddressList(rh.Value)
case `Cc`:
m.Cc, e = parseAddressList(rh.Value)
case `Bcc`:
m.Bcc, e = parseAddressList(rh.Value)
case `Subject`:
subject, err := decoder.Parse(rh.Value)
if err != nil {
fmt.Println("Failed decode subject", err)
}
m.Subject = string(subject)
case `Comments`:
m.Comments = append(m.Comments, string(rh.Value))
case `Keywords`:
ks := strings.Split(string(rh.Value), ",")
for _, k := range ks {
m.Keywords = append(m.Keywords, strings.TrimSpace(k))
}
default:
m.OptHeaders = append(m.OptHeaders, h)
}
if e != nil {
return
}
}
if m.Sender == nil && len(m.From) > 0 {
m.Sender = m.From[0]
}
if m.ContentType != `` {
parts, er := parseBody(m.ContentType, r.Body)
if er != nil {
e = er
return
}
for _, part := range parts {
switch {
case strings.Contains(part.Type, "text/plain"):
data, err := decoder.UTF8(part.Charset, part.Data)
if err != nil {
m.Text = string(part.Data)
} else {
m.Text = string(data)
}
case strings.Contains(part.Type, "text/html"):
data, err := decoder.UTF8(part.Charset, part.Data)
if err != nil {
m.Html = string(part.Data)
} else {
m.Html = string(data)
}
default:
if cd, ok := part.Headers["Content-Disposition"]; ok {
if strings.Contains(cd[0], "attachment") {
filename := regexp.MustCompile("(?msi)name=\"(.*?)\"").FindStringSubmatch(cd[0]) //.FindString(cd[0])
if len(filename) < 2 {
fmt.Println("failed get filename from header content-disposition")
break
}
dfilename, err := decoder.Parse([]byte(filename[1]))
if err != nil {
fmt.Println("Failed decode filename of attachment", err)
} else {
filename[1] = string(dfilename)
}
if encoding, ok := part.Headers["Content-Transfer-Encoding"]; ok {
switch strings.ToLower(encoding[0]) {
case "base64":
part.Data, er = base64.StdEncoding.DecodeString(string(part.Data))
if er != nil {
fmt.Println(er, "failed decode base64")
}
case "quoted-printable":
part.Data, _ = ioutil.ReadAll(quotedprintable.NewReader(bytes.NewReader(part.Data)))
}
}
m.Attachments = append(m.Attachments, Attachment{filename[1], part.Data})
}
}
}
}
m.Parts = parts
m.ContentType = parts[0].Type
m.Text = string(parts[0].Data)
} else {
m.Text = string(r.Body)
}
return
}
type RawHeader struct {
Key, Value []byte
}
type RawMessage struct {
RawHeaders []RawHeader
Body []byte
}
func isWSP(b byte) bool {
return b == ' ' || b == '\t'
}
func ParseRaw(s []byte) (m RawMessage, e error) {
// parser states
const (
READY = iota
HKEY
HVWS
HVAL
)
const (
CR = '\r'
LF = '\n'
)
CRLF := []byte{CR, LF}
state := READY
kstart, kend, vstart := 0, 0, 0
done := false
m.RawHeaders = []RawHeader{}
for i := 0; i < len(s); i++ {
b := s[i]
switch state {
case READY:
if b == CR && i < len(s)-1 && s[i+1] == LF {
// we are at the beginning of an empty header
m.Body = s[i+2:]
done = true
goto Done
}
if b == LF {
m.Body = s[i+1:]
done = true
goto Done
}
// otherwise this character is the first in a header
// key
kstart = i
state = HKEY
case HKEY:
if b == ':' {
kend = i
state = HVWS
}
case HVWS:
if !isWSP(b) {
vstart = i
state = HVAL
}
case HVAL:
if b == CR && i < len(s)-2 && s[i+1] == LF && !isWSP(s[i+2]) {
v := bytes.Replace(s[vstart:i], CRLF, nil, -1)
hdr := RawHeader{s[kstart:kend], v}
m.RawHeaders = append(m.RawHeaders, hdr)
state = READY
i++
} else if b == LF && i < len(s)-1 && !isWSP(s[i+1]) {
v := bytes.Replace(s[vstart:i], CRLF, nil, -1)
hdr := RawHeader{s[kstart:kend], v}
m.RawHeaders = append(m.RawHeaders, hdr)
state = READY
}
}
}
Done:
if !done {
e = errors.New("unexpected EOF")
}
return
}