forked from carlostrub/sisyphus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
301 lines (241 loc) · 5.82 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
294
295
296
297
298
299
300
301
package sisyphus
import (
"bufio"
"errors"
"fmt"
"math"
"mime/quotedprintable"
"net/mail"
"os"
"path/filepath"
"regexp"
"strings"
"unicode/utf8"
log "github.com/sirupsen/logrus"
"github.com/carlostrub/maildir"
"github.com/kennygrant/sanitize"
)
// Maildir represents the address to a Maildir directory
type Maildir string
// Mail includes the key of a mail in Maildir
type Mail struct {
Key string
Subject, Body *string
Junk, New bool
DryRun bool
}
// CreateDirs creates all the required dirs -- if not already there.
func (d Maildir) CreateDirs() error {
dir := string(d)
log.WithFields(log.Fields{
"dir": dir,
}).Info("Create missing directories")
err := os.MkdirAll(filepath.Join(dir, ".Junk", "cur"), 0700)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Join(dir, "new"), 0700)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Join(dir, "cur"), 0700)
return err
}
// Index loads all mail keys from the Maildir directory for processing.
func (d Maildir) Index() (m []*Mail, err error) {
dir := string(d)
log.WithFields(log.Fields{
"dir": dir,
}).Info("Start indexing mails")
dirs := []string{dir, filepath.Join(dir, ".Junk")}
for _, val := range dirs {
j, err := maildir.Dir(val).Keys()
if err != nil {
return m, err
}
for _, v := range j {
var new Mail
new.Key = v
if val == filepath.Join(dir, ".Junk") {
new.Junk = true
}
m = append(m, &new)
}
}
log.WithFields(log.Fields{
"dir": dir,
}).Info("All mails indexed")
return m, nil
}
// Load reads a mail's subject and body
func (m *Mail) Load(dir Maildir) (err error) {
var message *mail.Message
switch {
case m.Junk:
dir = Maildir(filepath.Join(string(dir), ".Junk"))
case m.New:
dir = Maildir(filepath.Join(string(dir), "new"))
}
message, err = maildir.Dir(dir).Message(m.Key)
if err != nil {
return err
}
// get Subject
if m.Subject != nil {
return errors.New("there is already a subject")
}
subject := message.Header.Get("Subject")
m.Subject = &subject
// get Body
bQ := quotedprintable.NewReader(message.Body)
var b []string
bScanner := bufio.NewScanner(bQ)
for bScanner.Scan() {
raw := bScanner.Text()
b = append(b, raw)
}
body := strings.Join(b, " ")
if m.Body != nil {
return errors.New("there is already a body")
}
m.Body = &body
return nil
}
// Unload removes a mail's subject and body from the internal cache
func (m *Mail) Unload(dir Maildir) (err error) {
m.Subject = nil
m.Body = nil
return nil
}
func trimStringFromBase64(s string) string {
if idx := strings.Index(s, "Content-Transfer-Encoding: base64"); idx != -1 {
return s[:idx-1]
}
return s
}
func cleanString(i string) (s string) {
s = sanitize.Accents(i)
s = sanitize.HTML(s)
s = strings.ToLower(s)
bad := []string{
"boundary=", "charset", "content-transfer-encoding",
"content-type", "image/jpeg", "multipart/alternative",
"multipart/related", "name=", "nextpart", "quoted-printable",
"text/html", "text/plain", "this email must be viewed in html mode",
"this is a multi-part message in mime format",
"windows-1251", "windows-1252", "!", "#", "$", "%", "&", "'",
"(", ")", "*", "+", ",", ". ", "<", "=", ">", "?", "@", "[",
"\"", "\\", "\n", "\t", "]", "^", "_", "{", "|", "}",
}
for _, b := range bad {
s = strings.Replace(s, b, " ", -1)
}
for i := 0; i < 10; i++ {
s = strings.Replace(s, " ", " ", -1)
}
return s
}
// Clean cleans the mail's subject and body
func (m *Mail) Clean() error {
if m.Subject != nil {
s := trimStringFromBase64(*m.Subject)
s = cleanString(s)
m.Subject = &s
}
if m.Body != nil {
b := trimStringFromBase64(*m.Body)
b = cleanString(b)
m.Body = &b
}
return nil
}
// wordlist takes a string of space separated text and returns a list of unique
// words in a space separated string
func wordlist(s string) (l []string, err error) {
list := make(map[string]int)
raw := strings.Split(s, " ")
var clean []string
// use regexp compile for use in the loop that follows
regexMatcher, err := regexp.Compile("(^[a-z]+$)")
if err != nil {
return l, err
}
for _, w := range raw {
str := w
for len(str) > 0 {
r, size := utf8.DecodeLastRuneInString(str)
if size > 2 {
clean = append(clean, fmt.Sprintf("%c", r))
}
str = str[:len(str)-size]
}
// no long or too short words
length := len(w)
if length < 4 || length > 10 {
continue
}
// no numbers, special characters, etc. -- only words
match := regexMatcher.MatchString(w)
if !match {
continue
} else {
clean = append(clean, w)
}
}
// only the first 1000 words count
maxWords := int(math.Min(1000, float64(len(clean))))
for i := 0; i < maxWords; i++ {
w := clean[i]
list[w]++
}
for word := range list {
l = append(l, word)
}
return l, nil
}
// Wordlist prepares the mail for training
func (m *Mail) Wordlist() (w []string, err error) {
var s string
if m.Subject != nil {
s = s + " " + *m.Subject
}
if m.Body != nil {
s = s + " " + *m.Body
}
w, err = wordlist(s)
return w, err
}
// cleanWordlist combines Clean and Wordlist in one internal function
func (m *Mail) cleanWordlist() (w []string, err error) {
err = m.Clean()
if err != nil {
return w, err
}
w, err = m.Wordlist()
return w, err
}
// LoadMails loads all mails from a given slice of Maildirs
func LoadMails(d []Maildir) (mails map[Maildir][]*Mail, err error) {
mails = make(map[Maildir][]*Mail)
// create missing directories and write index
for _, val := range d {
var m []*Mail
m, err = val.Index()
if err != nil {
return mails, err
}
mails[val] = m
}
return mails, nil
}
// LoadMaildirs creates Maildirs and required directories, if missing
func LoadMaildirs(d []Maildir) (err error) {
// create missing directories and write index
for _, val := range d {
err := val.CreateDirs()
if err != nil {
return err
}
}
return nil
}