-
Notifications
You must be signed in to change notification settings - Fork 0
/
incoming_mail.go
165 lines (132 loc) · 4.11 KB
/
incoming_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
package ohdiary
import (
"net/http"
"net/mail"
"time"
"bytes"
"strings"
"encoding/base64"
"regexp"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"appengine"
"appengine/datastore"
)
func incomingMail(w http.ResponseWriter, r *http.Request) {
// TODO: discard messages which are too big
c := appengine.NewContext(r)
defer r.Body.Close()
var b bytes.Buffer
if _, err := b.ReadFrom(r.Body); err != nil {
c.Errorf("Failed to read stream body: %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
msg, err2 := mail.ReadMessage(strings.NewReader(b.String()))
if err2 != nil {
c.Errorf("Failed to read message: %s", err2)
http.Error(w, err2.Error(), http.StatusInternalServerError)
return
}
addresses, err := msg.Header.AddressList("To")
if err != nil {
c.Errorf("Failed to parse addresses: %s", err)
return
}
var diaryEntryKey *datastore.Key
for _, address := range addresses {
token := tokenFromEmailAddress(address.Address)
c.Infof("Received mail with token %s", token)
q := datastore.NewQuery("Diary").Filter("Token =", token).KeysOnly()
diaryKeys, err3 := q.GetAll(c, nil)
if err3 != nil {
c.Errorf("Failed to read diary: %s", err3)
http.Error(w, err3.Error(), http.StatusInternalServerError)
continue
}
if len(diaryKeys) == 0 {
continue
}
diaryEntryKey = datastore.NewIncompleteKey(c, "DiaryEntry", diaryKeys[0])
}
if diaryEntryKey == nil {
c.Errorf("No diary found")
return
}
content, err := parseMailBody(c, msg)
if err != nil {
c.Errorf("Failed to parse mail body: %s", err)
return
}
if len(content) == 0 {
c.Infof("No content found in posted mail")
return
}
subject := msg.Header.Get("Subject")
diaryEntry := DiaryEntry {
CreatedAt: extractDiaryEntryDate(subject),
Content: stripSignature(content),
}
_, err5 := datastore.Put(c, diaryEntryKey, &diaryEntry)
if err5 != nil {
c.Errorf("Failed to insert diary entry %s", err5)
http.Error(w, err5.Error(), http.StatusInternalServerError)
return
}
c.Infof("Added new diary entry for key %s", diaryEntryKey)
}
func extractDiaryEntryDate(subject string) time.Time {
subjectParsingExpression := regexp.MustCompile("It's (.*) - How did your day go?")
entryDate := subjectParsingExpression.FindAllStringSubmatch(subject, -1)[0][1]
entryCreatedAt, err := time.Parse("Monday, Jan 2", entryDate)
if err != nil {
entryCreatedAt = time.Now().UTC()
}
// apply current year to parsed value
return time.Date(time.Now().UTC().Year(), entryCreatedAt.Month(), entryCreatedAt.Day(), 0, 0, 0, 0, time.UTC)
}
func stripSignature(body string) string {
// if you were cool => http://www.cs.cmu.edu/~vitor/papers/sigFilePaper_finalversion.ps
signatureMatcher := regexp.MustCompile("-- ?[\n\r]")
return signatureMatcher.Split(body, -1)[0]
}
func parseMailBody(c appengine.Context, msg *mail.Message) (string, error) {
mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
if err != nil {
c.Errorf("Failed to parse content type %s", err)
return "", err
}
if strings.HasPrefix(mediaType, "multipart/") {
mr := multipart.NewReader(msg.Body, params["boundary"])
for {
p, err := mr.NextPart()
if err == io.EOF {
return "", nil
}
if err != nil {
c.Errorf("Failed to read next part of message: %v", p)
return "", err
}
slurp, err := ioutil.ReadAll(p)
if err != nil {
c.Errorf("Reading all for slurp %s", err)
return "", err
}
transferEncoding := p.Header.Get("Content-Transfer-Encoding")
if (transferEncoding == "base64") {
decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewReader(slurp))
decodedSlurp, err := ioutil.ReadAll(decoder)
if err != nil {
c.Errorf("Failed to decode block")
return "", err
}
return string(decodedSlurp), nil
}
c.Infof("Part %q: %q\n", p.Header.Get("Content-Type"), slurp)
return string(slurp), nil
}
}
return "", nil
}