-
Notifications
You must be signed in to change notification settings - Fork 19
/
eazye.go
621 lines (541 loc) · 15.5 KB
/
eazye.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package eazye
import (
"bytes"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/mail"
"strings"
"time"
"github.com/mxk/go-imap/imap"
"github.com/paulrosania/go-charset/charset"
_ "github.com/paulrosania/go-charset/data"
qprintable "github.com/sloonz/go-qprintable"
"golang.org/x/net/html"
)
// MailboxInfo holds onto the credentials and other information
// needed for connecting to an IMAP server.
type MailboxInfo struct {
Host string
TLS bool
InsecureSkipVerify bool
User string
Pwd string
Folder string
// Read only mode, false (original logic) if not initialized
ReadOnly bool
}
// GetAll will pull all emails from the email folder and return them as a list.
func GetAll(info MailboxInfo, markAsRead, delete bool) ([]Email, error) {
// call chan, put 'em in a list, return
var emails []Email
responses, err := GenerateAll(info, markAsRead, delete)
if err != nil {
return emails, err
}
for resp := range responses {
if resp.Err != nil {
return emails, resp.Err
}
emails = append(emails, resp.Email)
}
return emails, nil
}
// GenerateAll will find all emails in the email folder and pass them along to the responses channel.
func GenerateAll(info MailboxInfo, markAsRead, delete bool) (chan Response, error) {
return generateMail(info, "ALL", nil, markAsRead, delete)
}
// GenerateCommand will find all emails in the email folder matching the IMAP command and pass them along to the responses channel.
func GenerateCommand(info MailboxInfo, IMAPCommand string, markAsRead, delete bool) (chan Response, error) {
return generateMail(info, IMAPCommand, nil, markAsRead, delete)
}
// GetCommand will pull all emails that match the provided IMAP Command.
// Examples of IMAP Commands include TO/FROM/BCC, some examples are here http://www.marshallsoft.com/ImapSearch.htm
func GetCommand(info MailboxInfo, IMAPCommand string, markAsRead, delete bool) ([]Email, error) {
responses, err := GenerateCommand(info, IMAPCommand, markAsRead, delete)
return responseToList(responses, err)
}
// GetUnread will find all unread emails in the folder and return them as a list.
func GetUnread(info MailboxInfo, markAsRead, delete bool) ([]Email, error) {
responses, err := GenerateUnread(info, markAsRead, delete)
return responseToList(responses, err)
}
func responseToList(responses chan Response, err error) ([]Email, error) {
// call chan, put 'em in a list, return
var emails []Email
if err != nil {
return emails, err
}
for resp := range responses {
if resp.Err != nil {
return emails, resp.Err
}
emails = append(emails, resp.Email)
}
return emails, nil
}
// GenerateUnread will find all unread emails in the folder and pass them along to the responses channel.
func GenerateUnread(info MailboxInfo, markAsRead, delete bool) (chan Response, error) {
return generateMail(info, "UNSEEN", nil, markAsRead, delete)
}
// GetSince will pull all emails that have an internal date after the given time.
func GetSince(info MailboxInfo, since time.Time, markAsRead, delete bool) ([]Email, error) {
var emails []Email
responses, err := GenerateSince(info, since, markAsRead, delete)
if err != nil {
return emails, err
}
for resp := range responses {
if resp.Err != nil {
return emails, resp.Err
}
emails = append(emails, resp.Email)
}
return emails, nil
}
// GenerateSince will find all emails that have an internal date after the given time and pass them along to the
// responses channel.
func GenerateSince(info MailboxInfo, since time.Time, markAsRead, delete bool) (chan Response, error) {
return generateMail(info, "", &since, markAsRead, delete)
}
// MarkAsUnread will set the UNSEEN flag on a supplied slice of UIDs
func MarkAsUnread(info MailboxInfo, uids []uint32) error {
client, err := newIMAPClient(info)
if err != nil {
return err
}
defer func() {
client.Close(true)
client.Logout(30 * time.Second)
}()
for _, u := range uids {
err := alterEmail(client, u, "\\SEEN", false)
if err != nil {
return err //return on first failure
}
}
return nil
}
// DeleteEmails will delete emails from the supplied slice of UIDs
func DeleteEmails(info MailboxInfo, uids []uint32) error {
client, err := newIMAPClient(info)
if err != nil {
return err
}
defer func() {
client.Close(true)
client.Logout(30 * time.Second)
}()
for _, u := range uids {
err := deleteEmail(client, u)
if err != nil {
return err //return on first failure
}
}
return nil
}
// ValidateMailboxInfo attempts to login to the supplied IMAP account to ensure the info is correct
func ValidateMailboxInfo(info MailboxInfo) error {
client, err := newIMAPClient(info)
defer func() {
client.Close(true)
client.Logout(30 * time.Second)
}()
return err
}
// Email is a simplified email struct containing the basic pieces of an email. If you want more info,
// it should all be available within the Message attribute.
type Email struct {
Message *mail.Message
From *mail.Address `json:"from"`
To []*mail.Address `json:"to"`
InternalDate time.Time `json:"internal_date"`
Precedence string `json:"precedence"`
Subject string `json:"subject"`
HTML []byte `json:"html"`
Text []byte `json:"text"`
IsMultiPart bool `json:"is_multipart"`
UID uint32 `json:"uid"`
}
var (
styleTag = []byte("style")
scriptTag = []byte("script")
headTag = []byte("head")
metaTag = []byte("meta")
doctypeTag = []byte("doctype")
shapeTag = []byte("v:shape")
imageDataTag = []byte("v:imagedata")
commentTag = []byte("!")
nonVisibleTags = [][]byte{
styleTag,
scriptTag,
headTag,
metaTag,
doctypeTag,
shapeTag,
imageDataTag,
commentTag,
}
)
func VisibleText(body io.Reader) ([][]byte, error) {
var (
text [][]byte
skip bool
err error
)
z := html.NewTokenizer(body)
for {
tt := z.Next()
switch tt {
case html.ErrorToken:
if err = z.Err(); err == io.EOF {
return text, nil
}
return text, err
case html.TextToken:
if !skip {
tmp := bytes.TrimSpace(z.Text())
if len(tmp) == 0 {
continue
}
tagText := make([]byte, len(tmp))
copy(tagText, tmp)
text = append(text, tagText)
}
case html.StartTagToken, html.EndTagToken:
tn, _ := z.TagName()
for _, nvTag := range nonVisibleTags {
if bytes.Equal(tn, nvTag) {
skip = (tt == html.StartTagToken)
break
}
}
}
}
return text, nil
}
// VisibleText will return any visible text from an HTML
// email body.
func (e *Email) VisibleText() ([][]byte, error) {
// if theres no HTML, just return text
if len(e.HTML) == 0 {
return [][]byte{e.Text}, nil
}
return VisibleText(bytes.NewReader(e.HTML))
}
// String is to spit out a somewhat pretty version of the email.
func (e *Email) String() string {
return fmt.Sprintf(`
----------------------------
From: %s
To: %s
Internal Date: %s
Precedence: %s
Subject: %s
HTML: %s
Text: %s
----------------------------
`,
e.From,
e.To,
e.InternalDate,
e.Precedence,
e.Subject,
string(e.HTML),
string(e.Text),
)
}
// Response is a helper struct to wrap the email responses and possible errors.
type Response struct {
Email Email
Err error
}
// newIMAPClient will initiate a new IMAP connection with the given creds.
func newIMAPClient(info MailboxInfo) (*imap.Client, error) {
var client *imap.Client
var err error
if info.TLS {
config := new(tls.Config)
config.InsecureSkipVerify = info.InsecureSkipVerify
client, err = imap.DialTLS(info.Host, config)
if err != nil {
return client, err
}
} else {
client, err = imap.Dial(info.Host)
if err != nil {
return client, err
}
}
_, err = client.Login(info.User, info.Pwd)
if err != nil {
return client, err
}
_, err = imap.Wait(client.Select(info.Folder, info.ReadOnly))
if err != nil {
return client, err
}
return client, nil
}
const dateFormat = "02-Jan-2006"
// findEmails will run a find the UIDs of any emails that match the search.:
func findEmails(client *imap.Client, search string, since *time.Time) (*imap.Command, error) {
var specs []imap.Field
if len(search) > 0 {
specs = append(specs, search)
}
if since != nil {
sinceStr := since.Format(dateFormat)
specs = append(specs, "SINCE", sinceStr)
}
// get headers and UID for UnSeen message in src inbox...
cmd, err := imap.Wait(client.UIDSearch(specs...))
if err != nil {
return &imap.Command{}, fmt.Errorf("uid search failed: %s", err)
}
return cmd, nil
}
var GenerateBufferSize = 100
func generateMail(info MailboxInfo, search string, since *time.Time, markAsRead, delete bool) (chan Response, error) {
responses := make(chan Response, GenerateBufferSize)
client, err := newIMAPClient(info)
if err != nil {
close(responses)
return responses, fmt.Errorf("uid search failed: %s", err)
}
go func() {
defer func() {
client.Close(true)
client.Logout(30 * time.Second)
close(responses)
}()
var cmd *imap.Command
// find all the UIDs
cmd, err = findEmails(client, search, since)
if err != nil {
responses <- Response{Err: err}
return
}
// gotta fetch 'em all
getEmails(client, cmd, markAsRead, delete, responses)
}()
return responses, nil
}
func getEmails(client *imap.Client, cmd *imap.Command, markAsRead, delete bool, responses chan Response) {
seq := &imap.SeqSet{}
msgCount := 0
for _, rsp := range cmd.Data {
for _, uid := range rsp.SearchResults() {
msgCount++
seq.AddNum(uid)
}
}
// nothing to request?! why you even callin me, foolio?
if seq.Empty() {
return
}
fCmd, err := imap.Wait(client.UIDFetch(seq, "INTERNALDATE", "BODY[]", "UID", "RFC822.HEADER"))
if err != nil {
responses <- Response{Err: fmt.Errorf("unable to perform uid fetch: %s", err)}
return
}
var email Email
for _, msgData := range fCmd.Data {
msgFields := msgData.MessageInfo().Attrs
// make sure is a legit response before we attempt to parse it
// deal with unsolicited FETCH responses containing only flags
// I'm lookin' at YOU, Gmail!
// http://mailman13.u.washington.edu/pipermail/imap-protocol/2014-October/002355.html
// http://stackoverflow.com/questions/26262472/gmail-imap-is-sometimes-returning-bad-results-for-fetch
if _, ok := msgFields["RFC822.HEADER"]; !ok {
continue
}
email, err = NewEmail(msgFields)
if err != nil {
responses <- Response{Err: fmt.Errorf("unable to parse email: %s", err)}
return
}
responses <- Response{Email: email}
if !markAsRead {
err = removeSeen(client, imap.AsNumber(msgFields["UID"]))
if err != nil {
responses <- Response{Err: fmt.Errorf("unable to remove seen flag: %s", err)}
return
}
}
if delete {
err = deleteEmail(client, imap.AsNumber(msgFields["UID"]))
if err != nil {
responses <- Response{Err: fmt.Errorf("unable to delete email: %s", err)}
return
}
}
}
return
}
func deleteEmail(client *imap.Client, UID uint32) error {
return alterEmail(client, UID, "\\DELETED", true)
}
func removeSeen(client *imap.Client, UID uint32) error {
return alterEmail(client, UID, "\\SEEN", false)
}
func alterEmail(client *imap.Client, UID uint32, flag string, plus bool) error {
flg := "-FLAGS"
if plus {
flg = "+FLAGS"
}
fSeq := &imap.SeqSet{}
fSeq.AddNum(UID)
_, err := imap.Wait(client.UIDStore(fSeq, flg, flag))
if err != nil {
return err
}
return nil
}
func hasEncoding(word string) bool {
return strings.Contains(word, "=?") && strings.Contains(word, "?=")
}
func isEncodedWord(word string) bool {
return strings.HasPrefix(word, "=?") && strings.HasSuffix(word, "?=") && strings.Count(word, "?") == 4
}
func parseSubject(subject string) string {
if !hasEncoding(subject) {
return subject
}
dec := mime.WordDecoder{}
sub, _ := dec.DecodeHeader(subject)
return sub
}
// NewEmail will parse an imap.FieldMap into an Email. This
// will expect the message to container the internaldate and the body with
// all headers included.
func NewEmail(msgFields imap.FieldMap) (Email, error) {
var email Email
// parse the header
var message bytes.Buffer
message.Write(imap.AsBytes(msgFields["RFC822.HEADER"]))
message.Write([]byte("\n\n"))
rawBody := imap.AsBytes(msgFields["BODY[]"])
message.Write(rawBody)
msg, err := mail.ReadMessage(&message)
if err != nil {
return email, fmt.Errorf("unable to read header: %s", err)
}
from, err := mail.ParseAddress(msg.Header.Get("From"))
if err != nil {
return email, fmt.Errorf("unable to parse from address: %s", err)
}
to, err := mail.ParseAddressList(msg.Header.Get("To"))
if err != nil {
to = []*mail.Address{}
}
email = Email{
Message: msg,
InternalDate: imap.AsDateTime(msgFields["INTERNALDATE"]),
Precedence: msg.Header.Get("Precedence"),
From: from,
To: to,
Subject: parseSubject(msg.Header.Get("Subject")),
UID: imap.AsNumber(msgFields["UID"]),
}
// chunk the body up into simple chunks
email.HTML, email.Text, email.IsMultiPart, err = parseBody(msg.Header, rawBody)
return email, err
}
var headerSplitter = []byte("\r\n\r\n")
// parseBody will accept a a raw body, break it into all its parts and then convert the
// message to UTF-8 from whatever charset it may have.
func parseBody(header mail.Header, body []byte) (html []byte, text []byte, isMultipart bool, err error) {
var mediaType string
var params map[string]string
mediaType, params, err = mime.ParseMediaType(header.Get("Content-Type"))
if err != nil {
return
}
if strings.HasPrefix(mediaType, "multipart/") {
isMultipart = true
mr := multipart.NewReader(bytes.NewReader(body), params["boundary"])
for {
p, err := mr.NextPart()
if err == io.EOF {
break
}
if err != nil {
break
}
slurp, err := ioutil.ReadAll(p)
if err != nil {
// error and no results to use
if len(slurp) == 0 {
break
}
}
partMediaType, partParams, err := mime.ParseMediaType(p.Header.Get("Content-Type"))
if err != nil {
break
}
var htmlT, textT []byte
htmlT, textT, err = parsePart(partMediaType, partParams["charset"], p.Header.Get("Content-Transfer-Encoding"), slurp)
if len(htmlT) > 0 {
html = htmlT
} else {
text = textT
}
}
} else {
splitBody := bytes.SplitN(body, headerSplitter, 2)
if len(splitBody) < 2 {
err = errors.New("unexpected email format. (single part and no \\r\\n\\r\\n separating headers/body")
return
}
body = splitBody[1]
html, text, err = parsePart(mediaType, params["charset"], header.Get("Content-Transfer-Encoding"), body)
}
return
}
func parsePart(mediaType, charsetStr, encoding string, part []byte) (html, text []byte, err error) {
// deal with charset
if strings.ToLower(charsetStr) == "iso-8859-1" {
var cr io.Reader
cr, err = charset.NewReader("latin1", bytes.NewReader(part))
if err != nil {
return
}
part, err = ioutil.ReadAll(cr)
if err != nil {
return
}
}
// deal with encoding
var body []byte
switch strings.ToLower(encoding) {
case "quoted-printable":
dec := qprintable.NewDecoder(qprintable.WindowsTextEncoding, bytes.NewReader(part))
body, err = ioutil.ReadAll(dec)
if err != nil {
return
}
case "base64":
decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewReader(part))
body, err = ioutil.ReadAll(decoder)
if err != nil {
return
}
default:
body = part
}
// deal with media type
mediaType = strings.ToLower(mediaType)
switch {
case strings.Contains(mediaType, "text/html"):
html = body
case strings.Contains(mediaType, "text/plain"):
text = body
}
return
}