-
Notifications
You must be signed in to change notification settings - Fork 22
/
message.go
330 lines (287 loc) · 7.53 KB
/
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
package data
import (
"bytes"
"crypto/rand"
"encoding/base64"
"io"
"log"
"mime"
"strings"
"time"
)
// LogHandler is called for each log message. If nil, log messages will
// be output using log.Printf instead.
var LogHandler func(message string, args ...interface{})
func logf(message string, args ...interface{}) {
if LogHandler != nil {
LogHandler(message, args...)
} else {
log.Printf(message, args...)
}
}
// MessageID represents the ID of an SMTP message including the hostname part
type MessageID string
// NewMessageID generates a new message ID
func NewMessageID(hostname string) (MessageID, error) {
size := 32
rb := make([]byte, size)
_, err := rand.Read(rb)
if err != nil {
return MessageID(""), err
}
rs := base64.URLEncoding.EncodeToString(rb)
return MessageID(rs + "@" + hostname), nil
}
// Messages represents an array of Messages
// - TODO is this even required?
type Messages []Message
// Message represents a parsed SMTP message
type Message struct {
ID MessageID
From *Path
To []*Path
Content *Content
Created time.Time
MIME *MIMEBody // FIXME refactor to use Content.MIME
Raw *SMTPMessage
}
// Path represents an SMTP forward-path or return-path
type Path struct {
Relays []string
Mailbox string
Domain string
Params string
}
// Content represents the body content of an SMTP message
type Content struct {
Headers map[string][]string
Body string
Size int
MIME *MIMEBody
}
// SMTPMessage represents a raw SMTP message
type SMTPMessage struct {
From string
To []string
Data string
Helo string
}
// MIMEBody represents a collection of MIME parts
type MIMEBody struct {
Parts []*Content
}
// Parse converts a raw SMTP message to a parsed MIME message
func (m *SMTPMessage) Parse(hostname string) *Message {
var arr []*Path
for _, path := range m.To {
arr = append(arr, PathFromString(path))
}
id, _ := NewMessageID(hostname)
msg := &Message{
ID: id,
From: PathFromString(m.From),
To: arr,
Content: ContentFromString(m.Data),
Created: time.Now(),
Raw: m,
}
if msg.Content.IsMIME() {
logf("Parsing MIME body")
msg.MIME = msg.Content.ParseMIMEBody()
}
// find headers
var hasMessageID bool
var receivedHeaderName string
var returnPathHeaderName string
for k := range msg.Content.Headers {
if strings.ToLower(k) == "message-id" {
hasMessageID = true
continue
}
if strings.ToLower(k) == "received" {
receivedHeaderName = k
continue
}
if strings.ToLower(k) == "return-path" {
returnPathHeaderName = k
continue
}
}
if !hasMessageID {
msg.Content.Headers["Message-ID"] = []string{string(id)}
}
if len(receivedHeaderName) > 0 {
msg.Content.Headers[receivedHeaderName] = append(msg.Content.Headers[receivedHeaderName], "from "+m.Helo+" by "+hostname+" (MailHog)\r\n id "+string(id)+"; "+time.Now().Format(time.RFC1123Z))
} else {
msg.Content.Headers["Received"] = []string{"from " + m.Helo + " by " + hostname + " (MailHog)\r\n id " + string(id) + "; " + time.Now().Format(time.RFC1123Z)}
}
if len(returnPathHeaderName) > 0 {
msg.Content.Headers[returnPathHeaderName] = append(msg.Content.Headers[returnPathHeaderName], "<"+m.From+">")
} else {
msg.Content.Headers["Return-Path"] = []string{"<" + m.From + ">"}
}
return msg
}
// Bytes returns an io.Reader containing the raw message data
func (m *SMTPMessage) Bytes() io.Reader {
var b = new(bytes.Buffer)
b.WriteString("HELO:<" + m.Helo + ">\r\n")
b.WriteString("FROM:<" + m.From + ">\r\n")
for _, t := range m.To {
b.WriteString("TO:<" + t + ">\r\n")
}
b.WriteString("\r\n")
b.WriteString(m.Data)
return b
}
// FromBytes returns a SMTPMessage from raw message bytes (as output by SMTPMessage.Bytes())
func FromBytes(b []byte) *SMTPMessage {
msg := &SMTPMessage{}
var headerDone bool
for _, l := range strings.Split(string(b), "\n") {
if !headerDone {
if strings.HasPrefix(l, "HELO:<") {
l = strings.TrimPrefix(l, "HELO:<")
l = strings.TrimSuffix(l, ">\r")
msg.Helo = l
continue
}
if strings.HasPrefix(l, "FROM:<") {
l = strings.TrimPrefix(l, "FROM:<")
l = strings.TrimSuffix(l, ">\r")
msg.From = l
continue
}
if strings.HasPrefix(l, "TO:<") {
l = strings.TrimPrefix(l, "TO:<")
l = strings.TrimSuffix(l, ">\r")
msg.To = append(msg.To, l)
continue
}
if strings.TrimSpace(l) == "" {
headerDone = true
continue
}
}
msg.Data += l + "\n"
}
return msg
}
// Bytes returns an io.Reader containing the raw message data
func (m *Message) Bytes() io.Reader {
var b = new(bytes.Buffer)
for k, vs := range m.Content.Headers {
for _, v := range vs {
b.WriteString(k + ": " + v + "\r\n")
}
}
b.WriteString("\r\n")
b.WriteString(m.Content.Body)
return b
}
// IsMIME detects a valid MIME header
func (content *Content) IsMIME() bool {
header, ok := content.Headers["Content-Type"]
if !ok {
return false
}
return strings.HasPrefix(header[0], "multipart/")
}
// ParseMIMEBody parses SMTP message content into multiple MIME parts
func (content *Content) ParseMIMEBody() *MIMEBody {
var parts []*Content
if hdr, ok := content.Headers["Content-Type"]; ok {
if len(hdr) > 0 {
boundary := extractBoundary(hdr[0])
var p []string
if len(boundary) > 0 {
p = strings.Split(content.Body, "--"+boundary)
logf("Got boundary: %s", boundary)
} else {
logf("Boundary not found: %s", hdr[0])
}
for _, s := range p {
if len(s) > 0 {
part := ContentFromString(strings.Trim(s, "\r\n"))
if part.IsMIME() {
logf("Parsing inner MIME body")
part.MIME = part.ParseMIMEBody()
}
parts = append(parts, part)
}
}
}
}
return &MIMEBody{
Parts: parts,
}
}
// PathFromString parses a forward-path or reverse-path into its parts
func PathFromString(path string) *Path {
var relays []string
email := path
if strings.Contains(path, ":") {
x := strings.SplitN(path, ":", 2)
r, e := x[0], x[1]
email = e
relays = strings.Split(r, ",")
}
mailbox, domain := "", ""
if strings.Contains(email, "@") {
x := strings.SplitN(email, "@", 2)
mailbox, domain = x[0], x[1]
} else {
mailbox = email
}
return &Path{
Relays: relays,
Mailbox: mailbox,
Domain: domain,
Params: "", // FIXME?
}
}
// ContentFromString parses SMTP content into separate headers and body
func ContentFromString(data string) *Content {
logf("Parsing Content from string: '%s'", data)
x := strings.SplitN(data, "\r\n\r\n", 2)
h := make(map[string][]string, 0)
// FIXME this fails if the message content has no headers - specifically,
// if it doesn't contain \r\n\r\n
if len(x) == 2 {
headers, body := x[0], x[1]
hdrs := strings.Split(headers, "\r\n")
var lastHdr = ""
for _, hdr := range hdrs {
if lastHdr != "" && (strings.HasPrefix(hdr, " ") || strings.HasPrefix(hdr, "\t")) {
h[lastHdr][len(h[lastHdr])-1] = h[lastHdr][len(h[lastHdr])-1] + hdr
} else if strings.Contains(hdr, ": ") {
y := strings.SplitN(hdr, ": ", 2)
key, value := y[0], y[1]
// TODO multiple header fields
h[key] = []string{value}
lastHdr = key
} else if len(hdr) > 0 {
logf("Found invalid header: '%s'", hdr)
}
}
return &Content{
Size: len(data),
Headers: h,
Body: body,
}
}
return &Content{
Size: len(data),
Headers: h,
Body: x[0],
}
}
// extractBoundary extract boundary string in contentType.
// It returns empty string if no valid boundary found
func extractBoundary(contentType string) string {
_, params, err := mime.ParseMediaType(contentType)
if err == nil {
return params["boundary"]
}
return ""
}