-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.go
193 lines (163 loc) · 5.25 KB
/
email.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
package main
// The most arcane part of the whole thing, because apparently,
// to parse email with golang you need to actually know the IMAP standard
// by heart, because none of this is properly documented.
//
// Oh well.
import (
"bytes"
"fmt"
"io"
"log"
"strings"
"time"
"github.com/emersion/go-imap/v2"
"github.com/emersion/go-imap/v2/imapclient"
"github.com/emersion/go-message/mail"
"golang.org/x/net/html/charset"
)
// Because the actual types are truly mindbending, we consolidate the interesting
// parts of the email into a simpler structure before handing that over to the parsers.
type NotificationEmail struct {
From string
Subj string
Date time.Time
Text string
Html string
}
// The annoying part: taking the message apart into its html and txt bodies.
// It is my intuition, (the documentation is exceedingly lacking)
// that the BodySection[] map always contains exactly one element
// when the message was downloaded with Collect() as above.
// Whose key is a struct.
// And bizarrely, it's not a nil struct.
// So we have to curse to high heavens and loop through sections.
func parseEmail(message *imapclient.FetchMessageBuffer) *NotificationEmail {
notification := NotificationEmail{
From: message.Envelope.From[0].Addr(),
Subj: message.Envelope.Subject,
Date: message.Envelope.Date,
}
for _, bodyPart := range message.BodySection {
mr, err := mail.CreateReader(bytes.NewReader(bodyPart))
if err != nil {
// Looking at the createreader code, if there was
// an error, it's probably a borked message anyway.
return nil
}
partLoop:
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
log.Printf("failed to read message part: %v", err)
return nil
}
// We ignore attachments and stuff...
switch h := p.Header.(type) {
case *mail.InlineHeader:
chunkBytes, _ := io.ReadAll(p.Body)
contentType, contentTypeParams, err := h.ContentType()
if err != nil {
continue partLoop
}
// In case we got utf-8, that's where it ends.
text := string(chunkBytes)
// But encodings that are not utf-8 should be converted to utf-8.
// We're assuming they didn't lie to us. (they can)
cs := contentTypeParams["charset"]
if strings.ToUpper(cs) != "UTF-8" {
enc, _, certain := charset.DetermineEncoding(chunkBytes, h.Get("Content-Type"))
if certain && enc != nil {
decodedText, err := enc.NewDecoder().Bytes(chunkBytes)
if err != nil {
log.Printf("failed to decode message, skipping.")
continue partLoop
}
text = string(decodedText)
}
}
switch contentType {
case "text/plain":
notification.Text = text
case "text/html":
notification.Html = text
}
}
}
}
return ¬ification
}
// Reach into the IMAP server and ask it for all unread emails matching a certain From address.
func fetchMail(client *imapclient.Client, fromAddress string) []*NotificationEmail {
searchResult, err := client.Search(
&imap.SearchCriteria{
Header: []imap.SearchCriteriaHeaderField{
{Key: "From", Value: fromAddress},
},
NotFlag: []imap.Flag{
"\\Seen",
}}, &imap.SearchOptions{}).Wait()
if err != nil {
log.Fatalf("search failed: %v", err)
}
fetchOptions := &imap.FetchOptions{
Flags: true,
Envelope: true,
BodyStructure: &imap.FetchItemBodyStructure{Extended: true},
BodySection: []*imap.FetchItemBodySection{{}},
}
messages, err := client.Fetch(searchResult.All, fetchOptions).Collect()
if err != nil {
log.Fatalf("failed to fetch: %v", err)
}
log.Printf("unread messages from %s: %d", fromAddress, len(messages))
parsedMessages := make([]*NotificationEmail, 0, len(messages))
for _, message := range messages {
result := parseEmail(message)
if result != nil {
parsedMessages = append(parsedMessages, result)
}
}
return parsedMessages
}
// Log into the IMAP server, fetch all interesting emails,
// and produce a slice of structures containing the important parts we're looking for.
func acquireEmail(cfg NotifyRSSConfig) []*NotificationEmail {
var err error
dialTone := fmt.Sprintf("%s:%d", cfg.Mail.Host, cfg.Mail.Port)
var client *imapclient.Client
switch strings.ToLower(cfg.Mail.Connection) {
case "ssl":
client, err = imapclient.DialTLS(dialTone, nil)
case "starttls":
client, err = imapclient.DialStartTLS(dialTone, nil)
case "plain":
client, err = imapclient.DialInsecure(dialTone, nil)
default:
log.Fatalf("ssl parameter must be one of 'plain', 'ssl', 'starttls'")
}
if err != nil {
log.Fatalf("connection failure: %v", err)
}
defer client.Close()
if err := client.Login(cfg.Mail.User, cfg.Mail.Pass).Wait(); err != nil {
log.Fatalf("failed to login: %v", err)
}
mailbox, err := client.Select(cfg.Mail.Folder, &imap.SelectOptions{ReadOnly: true}).Wait()
if err != nil {
log.Fatalf("failed to reach %s: %v", cfg.Mail.Folder, err)
}
log.Printf("%s contains %v messages", cfg.Mail.Folder, mailbox.NumMessages)
var notifications []*NotificationEmail
if mailbox.NumMessages > 0 {
for _, notifier := range SupportedNotifiers {
notifications = append(notifications, fetchMail(client, notifier.From)...)
}
}
if err := client.Logout().Wait(); err != nil {
log.Fatalf("failed to logout: %v", err)
}
return notifications
}