/
main.go
365 lines (301 loc) · 6.91 KB
/
main.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
package main
import (
"bufio"
"bytes"
"encoding/base64"
"errors"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/mail"
"os"
"path/filepath"
"strings"
)
const index = "email.html"
const html = `<html>
<body>
<head>
<script>
function toggleHeaders() {
var e = document.getElementById("headers-all")
e.style.display = (e.style.display == 'none') ? 'block' : 'none';
}
</script>
</head>
<div>
Date: {{.Date}}<br>
From: {{.From}}<br>
To: {{.To}}<br>
Subject: {{.Subject}}<br>
</div>
<a onclick="toggleHeaders()" href="#">Toggle all headers</a><br><br>
<div id="headers-all" style="display: none">
{{range $key, $val := .Headers}}
{{range $v := $val}}
{{$key}}: {{$v}}<br>
{{end}}
{{end}}
</div>
{{if .Html}}
<div>
<hr>
<div class="html-content"><iframe width="1280" height="720" src="{{.Html.Filename}}"></iframe></div>
</div>
{{end}}
{{if .Text}}
<div>
<hr>
<div class="text-content">{{.Text}}</div>
</div>
{{end}}
{{if .Attachments}}
<div>
<hr>
Attachments:<br><br>
{{range .Attachments}}
<a href="{{.Filename}}">{{.Filename}}</a><br>
{{end}}
</div>
{{end}}
</body>
</html>`
type Email struct {
Date string
From string
To string
Subject string
Headers map[string][]string
Html *Attachment
Text string
Attachments []Attachment
}
type Attachment struct {
Data []byte
Filename string
}
func main() {
outputdir := flag.String("dir", "", "output directory")
flag.Parse()
if *outputdir == "" {
fmt.Println("dir not set")
os.Exit(1)
}
// Read from stdin
err := handleMessage(bufio.NewReader(os.Stdin), *outputdir)
if err != nil {
fmt.Printf("Fatal error: %s\n", err)
os.Exit(1)
}
}
func handleMessage(reader io.Reader, outputdir string) error {
msg, err := mail.ReadMessage(reader)
if err != nil {
return err
}
mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
if err != nil {
return err
}
from, err := decodeRFC2047Word(msg.Header.Get("From"))
if err != nil {
return err
}
to, err := decodeRFC2047Word(msg.Header.Get("To"))
if err != nil {
return err
}
subject, err := decodeRFC2047Word(msg.Header.Get("Subject"))
if err != nil {
return err
}
// decode headers
headers := make(map[string][]string)
for k, values := range msg.Header {
headers[k] = make([]string, len(values))
for i, v := range values {
headers[k][i], err = decodeRFC2047Word(v)
if err != nil {
return err
}
}
}
email := Email{
Date: msg.Header.Get("Date"),
From: from,
To: to,
Subject: subject,
Headers: headers,
}
if strings.HasPrefix(mediaType, "multipart/") {
if err := handleMultipart(&email, msg.Body, params["boundary"]); err != nil {
return err
}
} else {
if err := addContent(&email, msg.Header, msg.Body); err != nil {
return err
}
}
return writeResult(&email, outputdir)
}
type Header interface {
Get(key string) string
}
func isPlainText(contentType string) bool {
return strings.HasPrefix(contentType, "text/plain")
}
func isHtml(contentType string) bool {
return strings.HasPrefix(contentType, "text/html")
}
func isMultipart(contentType string) bool {
return strings.HasPrefix(contentType, "multipart/")
}
func handleMultipart(email *Email, r io.Reader, boundary string) error {
reader := multipart.NewReader(r, boundary)
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
if err != nil {
return err
}
if err := addContent(email, part.Header, part); err != nil {
return err
}
}
return nil
}
type charsetError string
func (e charsetError) Error() string {
return "charset not supported: " + string(e)
}
var rfc2047Decoder = mime.WordDecoder{
CharsetReader: func(charset string, input io.Reader) (io.Reader, error) {
return nil, charsetError(charset)
},
}
// copied from net/mail/message
func decodeRFC2047Word(s string) (string, error) {
dec, err := rfc2047Decoder.DecodeHeader(s)
if err == nil {
return dec, nil
}
if _, ok := err.(charsetError); ok {
return s, err
}
// Ignore invalid RFC 2047 encoded-word errors.
return s, nil
}
func addContent(email *Email, header Header, r io.Reader) error {
switch header.Get("Content-Transfer-Encoding") {
case "quoted-printable":
r = quotedprintable.NewReader(r)
case "base64":
r = base64.NewDecoder(base64.StdEncoding, r)
}
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
mediaType, params, err := mime.ParseMediaType(header.Get("Content-Type"))
if err != nil {
return err
}
if isPlainText(mediaType) && email.Text == "" {
email.Text = string(data)
email.Text = strings.Replace(email.Text, "\n", "<br>\n", -1)
} else if isHtml(mediaType) && email.Html == nil {
html := string(data)
email.Html = &Attachment{Data: []byte(html), Filename: "email-content.html"}
} else if isMultipart(mediaType) {
if err := handleMultipart(email, bytes.NewReader(data), params["boundary"]); err != nil {
return err
}
} else {
// figure out filename
filename := getFilename(header.Get("Content-Disposition"))
if filename == "" {
if isPlainText(mediaType) {
filename = "attachment.txt"
} else if isHtml(mediaType) {
filename = "attachment.html"
} else {
return errors.New("don't know how to generate filename for " + mediaType)
}
}
email.Attachments = append(email.Attachments, Attachment{Data: data, Filename: filename})
}
return nil
}
func getFilename(contentDisposition string) string {
params := strings.Split(contentDisposition, ";")
if len(params) < 2 {
return ""
}
filename := strings.Split(params[1], "=")
if len(filename) < 2 {
return ""
}
return strings.Trim(filename[1], "\"")
}
func writeResult(email *Email, outputdir string) error {
// remove old directory
if err := os.RemoveAll(outputdir); err != nil {
return err
}
// (re)create directory
if err := os.Mkdir(outputdir, 0755); err != nil {
return err
}
// change mode to 0755, mkdir does not set it correctly
f, err := os.Open(outputdir)
if err != nil {
return err
}
defer f.Close()
if err := f.Chmod(0755); err != nil {
return err
}
// write template
t := template.Must(template.New("html").Parse(html))
f, err = os.Create(filepath.Join(outputdir, index))
if err != nil {
return err
}
defer f.Close()
if err := t.Execute(f, email); err != nil {
return err
}
if err := f.Chmod(0660); err != nil {
return err
}
// write attachments
if email.Html != nil {
if err := write(*email.Html, outputdir); err != nil {
return err
}
}
for _, attachment := range email.Attachments {
if err := write(attachment, outputdir); err != nil {
return err
}
}
return nil
}
func write(attachment Attachment, outputdir string) error {
f, err := os.Create(filepath.Join(outputdir, attachment.Filename))
if err != nil {
return err
}
defer f.Close()
if _, err = f.Write(attachment.Data); err != nil {
return err
}
return f.Chmod(0660)
}