-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail_message.go
587 lines (527 loc) · 16.5 KB
/
mail_message.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
// Copyright 2016 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Model and methods for distributing a message sent to a mailing list.
package main
import (
"bytes"
"crypto/md5"
"encoding/base32"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/mail"
"os"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/jhillyerd/go.enmime"
"github.com/nu7hatch/gouuid"
)
const (
parentList = "MAILINGLIST_PARENTS"
studentList = "MAILINGLIST_STUDENTS"
)
var listMap = map[string]string{"[email protected]": parentList, "[email protected]": parentList,
"[email protected]": studentList, "[email protected]": studentList}
var base32Codec = base32.StdEncoding.WithPadding(base32.NoPadding)
type MailMessage struct {
from *mail.Address
to []*mail.Address
subject string
body *enmime.MIMEBody
lists []string
allRecipients []*User
attachmentDir string
attachments []string
inlines []string
}
// Processes the incoming message and redistributes it to the appropriate recipients.
func (message *MailMessage) Handle() {
log.Println("Message received:")
log.Printf("From: %v", message.from)
log.Printf("To: %v", message.to)
log.Printf("Subject: %s", message.subject)
log.Printf("Body: %s", message.body.Html)
log.Printf("Attachment count: %d", len(message.body.Attachments))
log.Printf("Inline count: %d", len(message.body.Inlines))
if message.handleReplyForwarding() {
return
}
senderUser, err := GetUserByEmail(message.from.Address)
if err != nil {
message.handleError(err, 0)
return
}
message.lists, err = message.getListsAndCheckPermission(senderUser)
if err != nil {
message.handleError(err, 0)
return
}
if len(message.lists) == 0 {
log.Printf("Message is not addressed to any known mailing lists; ignoring.")
return
}
log.Printf("Lists addressed to: %v", message.lists)
if message.body.Html == "" {
err = errors.New("Rejected message with blank HTML body; can't process plain-text messages. " +
"Please re-send as HTML (try using a different client).")
message.handleError(err, 0)
return
}
message.allRecipients, err = message.getRecipients()
if err != nil {
message.handleError(err, 0)
return
}
err = message.saveAttachments()
if err != nil {
err = fmt.Errorf("Error saving attachments: %v", err)
message.handleError(err, 0)
return
}
recipientAddresses := make([]string, len(message.allRecipients))
for i, recipient := range message.allRecipients {
recipientAddresses[i] = recipient.Email
}
log.Printf("Redistributing message to %d recipients: %v", len(message.allRecipients), recipientAddresses)
var actualRecipients []*User
if message.isDebug() {
// Only send the message to the original sender.
actualRecipients = []*User{senderUser}
} else {
actualRecipients = message.allRecipients
}
for index, recipient := range actualRecipients {
err = message.forwardEmail(recipient)
if err != nil {
err = fmt.Errorf("Error sending message to %s: %v", recipient.Email, err)
message.handleError(err, index)
return
}
// Sleep between sending messages to avoid exceeding the SES rate limit.
time.Sleep(time.Millisecond * time.Duration(config.GetInt("send_interval_ms")))
}
message.postToSlack()
if !message.isDebug() {
err = message.postToBlog(senderUser)
if err != nil {
err = fmt.Errorf("Error posting message to blog after distributing to list: %v", err)
message.handleError(err, len(actualRecipients))
return
}
}
}
// Parses the mailing lists from the original recipients. Returns an error if the sender doesn't have permission.
func (message *MailMessage) getListsAndCheckPermission(senderUser *User) ([]string, error) {
var lists []string
for _, toEmail := range message.to {
if list, ok := listMap[strings.ToLower(toEmail.Address)]; ok {
if !senderUser.HasPermission(list + "_SEND") {
return nil, fmt.Errorf("Sender '%s' does not have permission to mail list '%s'.",
message.from.Address, toEmail)
}
lists = append(lists, list)
}
}
return lists, nil
}
// Returns the list of all addresses to distribute the message to, given the list addresses the original
// message was sent to.
func (message *MailMessage) getRecipients() ([]*User, error) {
recipientSet := make(map[string]*User) // Simulates a set for deduplication
for _, list := range message.lists {
users, err := GetUsersByPermission(list + "_RECEIVE")
if err != nil {
return nil, err
}
for _, user := range users {
recipientSet[user.Email] = user
}
}
var recipients []*User
for _, recipient := range recipientSet {
recipients = append(recipients, recipient)
}
sort.Slice(recipients, func(i, j int) bool {
return recipients[i].Email < recipients[j].Email
})
return recipients, nil
}
// Adds a list-specific prefix to the original subject.
func (message *MailMessage) getFormattedSubject() string {
prefix := "[Team 254"
if len(message.lists) == 1 {
if message.lists[0] == parentList {
prefix += " Parents"
} else if message.lists[0] == studentList {
prefix += " Students"
}
}
return prefix + "] " + message.subject
}
// Returns true if the subject line contains "DEBUG".
func (message *MailMessage) isDebug() bool {
return strings.Contains(message.subject, "DEBUG")
}
// Saves attachments to a local directory that is served via HTTP.
func (message *MailMessage) saveAttachments() error {
messageId, _ := uuid.NewV4()
message.attachmentDir = messageId.String()
basePath := fmt.Sprintf("%s/%s", config.GetString("attachment_save_path"), message.attachmentDir)
err := os.MkdirAll(basePath, 0755)
if err != nil {
return err
}
if len(message.body.Attachments) != 0 {
for _, attachment := range message.body.Attachments {
filePath := fmt.Sprintf("%s/%s", basePath, attachment.FileName())
err = ioutil.WriteFile(filePath, attachment.Content(), 0644)
if err != nil {
return err
}
message.attachments = append(message.attachments, attachment.FileName())
}
}
if len(message.body.Inlines) != 0 {
for _, inline := range message.body.Inlines {
filePath := fmt.Sprintf("%s/%s", basePath, inline.FileName())
err = ioutil.WriteFile(filePath, inline.Content(), 0644)
if err != nil {
return err
}
message.inlines = append(message.inlines, inline.FileName())
// Rewrite the image tag in the HTML body to link to the inline image.
cid := inline.Header().Get("X-Attachment-Id")
inlineImageUrl := fmt.Sprintf("%s/%s/%s", config.GetString("attachment_base_url"),
message.attachmentDir, inline.FileName())
imageRe := regexp.MustCompile(fmt.Sprintf("<img src=[\"'](cid:%s)[\"']", cid))
matches := imageRe.FindStringSubmatch(message.body.Html)
if matches == nil {
return fmt.Errorf("Could not find content ID '%s' in message body.", cid)
}
message.body.Html = strings.Replace(message.body.Html, matches[1], inlineImageUrl, -1)
}
}
doc, err := goquery.NewDocumentFromReader(strings.NewReader(message.body.Html))
if err != nil {
return err
}
var goqueryErr error = nil
doc.Find("img").Each(func(i int, s *goquery.Selection) {
if i == 0 {
err = os.MkdirAll(basePath+"/images", 0755)
if err != nil {
goqueryErr = err
return
}
}
src, exists := s.Attr("src")
if exists && !strings.Contains(src, config.GetString("attachment_base_url")) {
resp, err := http.Get(src)
if err != nil {
goqueryErr = err
return
}
defer resp.Body.Close()
lastIndexOfSlash := strings.LastIndex(src, "/")
var fileName string
if lastIndexOfSlash != -1 && lastIndexOfSlash < len(src)-1 {
fileName = fmt.Sprintf("%d_%s.jpg", i, src[(lastIndexOfSlash+1):len(src)])
} else {
fileName = fmt.Sprintf("%d.jpg", i)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
goqueryErr = err
return
}
err = ioutil.WriteFile(fmt.Sprintf("%s/images/%s", basePath, fileName), body, 0644)
if err != nil {
goqueryErr = err
return
}
s.SetAttr("src", fmt.Sprintf("%s/%s/images/%s", config.GetString("attachment_base_url"), message.attachmentDir, fileName))
}
})
if goqueryErr != nil {
return goqueryErr
}
html, err := doc.Find("body").Html()
if err != nil {
return err
}
message.body.Html = html
return nil
}
// Sends the reformatted original message on to the given recipient.
func (message *MailMessage) forwardEmail(recipient *User) error {
location, _ := time.LoadLocation("America/Los_Angeles")
sendTime := time.Now().In(location)
attachmentBaseUrl := fmt.Sprintf("%s/%s", config.GetString("attachment_base_url"), message.attachmentDir)
data := struct {
Body string
IsDebug bool
AllRecipients []*User
Date string
AttachmentBaseUrl string
Attachments []string
UnsubscribeLink string
}{
message.body.Html,
message.isDebug(),
message.allRecipients,
sendTime.Format("January 2, 2006"),
attachmentBaseUrl,
message.attachments,
recipient.UnsubscribeLink(),
}
template, err := template.ParseFiles("message.html")
if err != nil {
return err
}
var buffer bytes.Buffer
err = template.Execute(&buffer, data)
if err != nil {
return err
}
encodedFromAddress := strings.ToLower(base32Codec.EncodeToString([]byte(message.from.Address)))
email := &ses.SendEmailInput{
Source: aws.String(fmt.Sprintf("\"%s\" <r-%s@%s>", message.from.Name, encodedFromAddress,
config.GetString("host_name"))),
Destination: &ses.Destination{
ToAddresses: []*string{aws.String(recipient.Email)},
},
ReplyToAddresses: []*string{aws.String(message.from.Address)},
Message: &ses.Message{
Subject: &ses.Content{
Data: aws.String(message.getFormattedSubject()),
},
Body: &ses.Body{
Html: &ses.Content{
Data: aws.String(buffer.String()),
},
},
},
}
_, err = sesService.SendEmail(email)
return err
}
func (message *MailMessage) postToBlog(senderUser *User) error {
// Format the message as an HTML blog post.
attachmentBaseUrl := fmt.Sprintf("%s/%s", config.GetString("attachment_base_url"), message.attachmentDir)
data := struct {
Body string
AttachmentBaseUrl string
Attachments []string
}{message.body.Html, attachmentBaseUrl, message.attachments}
template, err := template.ParseFiles("blog_post.html")
if err != nil {
return err
}
var buffer bytes.Buffer
err = template.Execute(&buffer, data)
if err != nil {
return err
}
body := buffer.String()
// Determine which lists the message was sent to, so that it can be posted in the appropriate category.
sentToStudents := "0"
sentToParents := "0"
for _, list := range message.lists {
if list == studentList {
sentToStudents = "1"
} else if list == parentList {
sentToParents = "1"
}
}
url := config.GetString("blog_post_url")
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBufferString(body))
if err != nil {
return err
}
// Populate post metadata.
location, _ := time.LoadLocation("America/Los_Angeles")
sendTime := time.Now().In(location)
dateString := sendTime.Format("Mon, 2 Jan 2006 15:04:05 MST")
req.Header.Set("Date", dateString)
req.Header.Set("User-Agent", "cheesy-mail")
authDigest := md5.Sum([]byte(dateString + message.subject + body + os.Getenv("TEAM254_SECRET")))
req.Header.Set("Authorization", hex.EncodeToString(authDigest[:]))
req.Header.Set("Poof-Title", message.subject)
req.Header.Set("Poof-User", strconv.Itoa(senderUser.Id))
req.Header.Set("Poof-Students", sentToStudents)
req.Header.Set("Poof-Parents", sentToParents)
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Printf("Error posting blog: %s", string(body))
return fmt.Errorf("Post failed: status code %d for URL %s", resp.StatusCode, url)
}
return nil
}
// Sends email data to a Slack webhook to post on the appropriate channel.
func (message *MailMessage) postToSlack() {
// Determine which channel-specific Slack webhook to post to. Messages sent only to the parents list shouldn't be
// posted at all.
webhook_url := ""
if message.isDebug() {
webhook_url = config.GetString("slack_webhook_url_debug")
} else {
for _, list := range message.lists {
if list == studentList {
webhook_url = config.GetString("slack_webhook_url_students")
}
}
}
if webhook_url == "" {
return
}
body := fmt.Sprintf(
"<!channel>\n*Subject: %s*\n_From: %s_\n\n%s", message.subject, message.from.Name, message.body.Text,
)
data := struct {
Text string `json:"text"`
}{body}
jsonData, err := json.Marshal(data)
if err != nil {
log.Printf("Error: %v", err)
return
}
req, err := http.NewRequest("POST", webhook_url, strings.NewReader(string(jsonData)))
if err != nil {
log.Printf("Error: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("Error: %v", err)
return
}
defer resp.Body.Close()
}
// Sends a message containing the error to the original message author (and CCs the admin).
func (message *MailMessage) handleError(err error, numSent int) {
log.Printf("Error: %v", err)
data := struct {
ErrorMessage string
NumSent int
NumTotal int
}{err.Error(), numSent, len(message.allRecipients)}
template, err := template.ParseFiles("error_message.html")
if err != nil {
log.Printf("Error: %v", err)
}
var buffer bytes.Buffer
err = template.Execute(&buffer, data)
if err != nil {
log.Printf("Error: %v", err)
}
email := &ses.SendEmailInput{
Source: aws.String(fmt.Sprintf("%s <%s>", "Mailing List Admin", config.GetString("admin_address"))),
Destination: &ses.Destination{
ToAddresses: []*string{aws.String(message.from.Address)},
CcAddresses: []*string{aws.String(config.GetString("admin_address"))},
},
Message: &ses.Message{
Subject: &ses.Content{
Data: aws.String(fmt.Sprintf("Failed to send message \"%s\"", message.subject)),
},
Body: &ses.Body{
Html: &ses.Content{
Data: aws.String(buffer.String()),
},
},
},
}
_, err = sesService.SendEmail(email)
if err != nil {
log.Printf("Error sending error notification to %s: %v", message.from.Address, err)
}
}
// Forwards replies sent to r:[encoded reply address]@[list domain] to the appropriate user. Returns true if such a
// message was detected, even if it couldn't be handled properly.
func (message *MailMessage) handleReplyForwarding() bool {
replyRe := regexp.MustCompile(fmt.Sprintf("^r-([a-z2-7]+)@%s$", config.GetString("host_name")))
var replyAddresses []string
for _, address := range message.to {
matches := replyRe.FindStringSubmatch(address.Address)
if len(matches) > 0 {
replyAddress, err := base32Codec.DecodeString(strings.ToUpper(matches[1]))
if err != nil {
fmt.Println("error:", err)
err = fmt.Errorf("Error decoding reply address: %v", err)
message.handleError(err, 0)
return true
}
replyAddresses = append(replyAddresses, string(replyAddress))
}
}
if len(replyAddresses) == 0 {
// This message does not represent a reply to a previous message sent out on the list.
return false
}
if strings.Contains(strings.ToLower(message.subject), "automatic reply") {
// Do not forward automatic replies
return true
}
log.Printf("Decoded reply-to addresses: %v", replyAddresses)
data := struct {
From *mail.Address
HtmlBody string
TextBody string
}{message.from, message.body.Html, message.body.Text}
template, err := template.ParseFiles("reply.html")
if err != nil {
message.handleError(err, 0)
return true
}
var buffer bytes.Buffer
err = template.Execute(&buffer, data)
if err != nil {
message.handleError(err, 0)
return true
}
encodedFromAddress := strings.ToLower(base32Codec.EncodeToString([]byte(message.from.Address)))
email := &ses.SendEmailInput{
Source: aws.String(fmt.Sprintf("\"%s\" <r-%s@%s>", message.from.Name, encodedFromAddress,
config.GetString("host_name"))),
Destination: &ses.Destination{
ToAddresses: aws.StringSlice(replyAddresses),
CcAddresses: []*string{aws.String(config.GetString("admin_address"))},
},
ReplyToAddresses: []*string{aws.String(message.from.Address)},
Message: &ses.Message{
Subject: &ses.Content{
Data: aws.String(message.subject),
},
Body: &ses.Body{
Html: &ses.Content{
Data: aws.String(buffer.String()),
},
},
},
}
_, err = sesService.SendEmail(email)
if err != nil {
err = fmt.Errorf("Error forwarding reply: %v", err)
message.handleError(err, 0)
}
return true
}