-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
546 lines (448 loc) · 12.2 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
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
package main
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
"log"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/mail"
"os"
"path/filepath"
"regexp"
"strings"
flag "github.com/spf13/pflag"
"golang.org/x/text/encoding/ianaindex"
)
func (lk *LetterKnife) debugf(format string, args ...interface{}) {
if lk.ModeDebug {
log.Printf("debug: "+format, args...)
}
}
func fatalf(format string, args ...interface{}) {
log.Printf("fatal: "+format, args...)
os.Exit(1)
}
var (
ErrHeaderMatchFailed = errors.New("header match failed")
ErrSelectFailed = errors.New("no part selected")
)
var mimeDecoder = new(mime.WordDecoder)
func init() {
mimeDecoder.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
enc, err := ianaindex.MIME.Encoding(charset)
if err != nil {
return nil, err
}
return enc.NewDecoder().Reader(input), nil
}
}
func extensionsByType(typ string) string {
switch typ {
case "text/html":
return ".html"
case "text/plain":
return ".txt"
}
exts, _ := mime.ExtensionsByType(typ)
if len(exts) > 0 {
return exts[0]
}
return ".bin"
}
var delmiter = "\n"
type LetterKnife struct {
Delmiter string
ShortcutFrom string
ShortcutSubject string
ShortcutHTML bool
ShortcutPlain bool
MatchAddress string
MatchHeader string
SelectPart string
SelectAttachment string
PrintContent bool
PrintHeader string
PrintRaw bool
SaveFile bool
ModeDebug bool
}
func (lk *LetterKnife) ParseFlags(args []string) error {
flags := flag.NewFlagSet("letterknife", flag.ExitOnError)
flags.StringVar(&lk.ShortcutFrom, "from", "", "Shortcut for --match-address 'From:`<pattern>`'")
flags.StringVar(&lk.ShortcutSubject, "subject", "", "Shortcut for --match-header 'Subject:`<pattern>`'")
flags.BoolVar(&lk.ShortcutHTML, "html", false, "Shortcut for --select-part text/html")
flags.BoolVar(&lk.ShortcutPlain, "plain", false, "Shortcut for --select-part text/plain")
// TODO: make multiple
flags.StringVar(&lk.MatchAddress, "match-address", "", "Filter: address header `<header>:<pattern>` eg. \"From:*@example.com\"")
flags.StringVar(&lk.MatchHeader, "match-header", "", "Filter: header `<header>:<pattern>` eg. \"Subject:foobar\"")
flags.StringVar(&lk.SelectPart, "select-part", "", "Select: non-attachment parts by `<content-type>`")
flags.StringVar(&lk.SelectAttachment, "select-attachment", "", "Select: attachments by `<content-type>`")
flags.BoolVar(&lk.PrintContent, "print-content", false, "Action: print decoded content")
flags.StringVar(&lk.PrintHeader, "print-header", "", "Action: print `<header>`")
flags.BoolVar(&lk.PrintRaw, "print-raw", false, "Action: print raw input as-is")
flags.BoolVar(&lk.SaveFile, "save-file", false, "Action: save parts as files and print their paths")
flags.BoolVar(&lk.ModeDebug, "debug", false, "enable debug logging")
flags.SortFlags = false
lk.Delmiter = "\n"
return flags.Parse(args)
}
// --from, --subject, --html, --plain
// --match-address From:...
// --match-header Subject:...
// --select-part text/html
// --select-attachment application/pdf
// --print-content
// --print-json // TODO
// --save-file
// --list-parts // ???
// --debug, --quiet // TODO
func main() {
l := &LetterKnife{}
err := l.ParseFlags(os.Args[1:])
if err != nil {
fatalf("%v", err)
}
err = l.Run(os.Stdin, os.Stdout)
if err != nil {
fatalf("%v", err)
}
}
func (lk *LetterKnife) Run(r io.Reader, w io.Writer) error {
// holds whole input
var in bytes.Buffer
msg, err := mail.ReadMessage(io.TeeReader(r, &in))
if err != nil {
return fmt.Errorf("failed to read message: %w", err)
}
pass := true
if lk.ShortcutFrom != "" {
lk.MatchAddress = "From:" + lk.ShortcutFrom
}
if lk.ShortcutSubject != "" {
lk.MatchHeader = "Subject:" + lk.ShortcutSubject
}
if lk.ShortcutHTML {
lk.SelectPart = "text/html"
}
if lk.ShortcutPlain {
lk.SelectPart = "text/plain"
}
if lk.MatchAddress != "" {
ok, err := lk.checkMatch(msg.Header, lk.MatchAddress, true)
if err != nil {
return fmt.Errorf("checkMatch(%s): %w", lk.MatchAddress, err)
}
if !ok {
pass = false
}
}
if lk.MatchHeader != "" {
ok, err := lk.checkMatch(msg.Header, lk.MatchHeader, false)
if err != nil {
return fmt.Errorf("checkMatch(%s): %w", lk.MatchHeader, err)
}
if !ok {
pass = false
}
}
if !pass {
return ErrHeaderMatchFailed
}
wholePart, err := newMessagePartFromHeader(msg.Header)
if err != nil {
return fmt.Errorf("failed to create part: %w", err)
}
// special case: includes headers along with body
// body not set, but r is set
wholePart.r = &in
rootPart, err := buildPartTree(msg.Header, msg.Body)
if err != nil {
return fmt.Errorf("while building tree: %w", err)
}
var selectedParts []*messagePart
if lk.SelectPart != "" {
pp, err := lk.selectParts(rootPart, lk.SelectPart, false)
if err != nil {
return fmt.Errorf("while selecting parts: %w", err)
}
selectedParts = append(selectedParts, pp...)
}
if lk.SelectAttachment != "" {
pp, err := lk.selectParts(rootPart, lk.SelectAttachment, true)
if err != nil {
return fmt.Errorf("while selecting attachments: %w", err)
}
selectedParts = append(selectedParts, pp...)
}
if lk.SelectPart != "" || lk.SelectAttachment != "" {
if len(selectedParts) == 0 {
return ErrSelectFailed
}
}
if lk.PrintHeader == "" && !lk.SaveFile && !lk.PrintRaw {
lk.PrintContent = true
}
// If no part is selected and --print-content is specified,
// then it should be treated as --print-raw.
if len(selectedParts) == 0 && lk.PrintContent {
lk.PrintContent = false
lk.PrintRaw = true
}
if len(selectedParts) == 0 {
selectedParts = []*messagePart{wholePart}
}
if lk.PrintHeader != "" {
s, err := mimeDecoder.DecodeHeader(wholePart.header.Get(lk.PrintHeader))
if err != nil {
return fmt.Errorf("decoding header %q failed: %w", lk.PrintHeader, err)
}
fmt.Fprint(w, s)
fmt.Fprint(w, delmiter)
}
if lk.PrintContent {
for _, mp := range selectedParts {
_, err = io.Copy(w, mp)
if err != nil {
return err
}
fmt.Fprint(w, delmiter)
}
}
if lk.PrintRaw {
_, err = io.Copy(w, &in)
if err != nil {
return err
}
}
if lk.SaveFile {
dir, err := os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("while creating temporary directory: %w", err)
}
for _, mp := range selectedParts {
filename, _ := mp.attachmentFilename()
var f *os.File
if filename != "" {
path := filepath.Join(dir, filename)
f, err = os.Create(path)
if err != nil {
return fmt.Errorf("creating file: %w", err)
}
} else {
ext := extensionsByType(mp.mediaType)
if mp == wholePart {
ext = ".eml"
}
f, err = os.CreateTemp(dir, "*"+ext)
if err != nil {
return fmt.Errorf("creating file: %w", err)
}
}
_, err = io.Copy(f, mp)
if err != nil {
return err
}
f.Close()
fmt.Fprint(w, f.Name())
fmt.Fprint(w, delmiter)
}
}
return nil
}
type messagePart struct {
header mail.Header
mediaType string
mediaTypeParams map[string]string
r io.Reader
// either is defined
body *bytes.Buffer
subparts []*messagePart
disposition string
dispositionParams map[string]string
}
type errWrappedReader struct {
message string
r io.Reader
}
// Read implements io.Reader
func (r *errWrappedReader) Read(p []byte) (int, error) {
n, err := r.r.Read(p)
if err == io.EOF {
return n, err
} else if err != nil {
return n, fmt.Errorf("%s: %w", r.message, err)
}
return n, nil
}
// Read implements io.Reader
func (m *messagePart) Read(p []byte) (n int, err error) {
if m.r == nil {
var r io.Reader = m.body
cte := m.header.Get("Content-Transfer-Encoding")
if strings.EqualFold(cte, "base64") {
r = base64.NewDecoder(base64.StdEncoding, r)
} else if strings.EqualFold(cte, "quoted-printable") {
r = quotedprintable.NewReader(r)
}
if charset := m.mediaTypeParams["charset"]; charset != "" {
enc, err := ianaindex.MIME.Encoding(charset)
if err != nil {
return 0, fmt.Errorf("failed to build charset %q decoder: %v", charset, err)
}
r = enc.NewDecoder().Reader(r)
r = &errWrappedReader{
message: "decoding " + charset,
r: r,
}
}
m.r = r
}
return m.r.Read(p)
}
func (m *messagePart) isMultipart() bool {
return m.body == nil
}
func (m *messagePart) attachmentFilename() (string, bool) {
if m.disposition != "attachment" {
return "", false
}
return m.dispositionParams["filename"], true
}
func newMessagePartFromHeader(header mail.Header) (*messagePart, error) {
ct := header.Get("Content-Type")
mt, params, err := mime.ParseMediaType(ct)
if err != nil {
return nil, fmt.Errorf("parsing content-type %q: %v", ct, err)
}
disposition, dispositionParams, _ := mime.ParseMediaType(header.Get("Content-Disposition"))
return &messagePart{
header: header,
mediaType: mt,
mediaTypeParams: params,
disposition: disposition,
dispositionParams: dispositionParams,
}, nil
}
func buildPartTree(header mail.Header, body io.Reader) (*messagePart, error) {
part, err := newMessagePartFromHeader(header)
if err != nil {
return nil, err
}
if strings.HasPrefix(part.mediaType, "multipart/") && part.mediaTypeParams["boundary"] != "" {
mr := multipart.NewReader(body, part.mediaTypeParams["boundary"])
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
return nil, fmt.Errorf("reading multipart: %v", err)
}
subpart, err := buildPartTree(mail.Header(p.Header), p)
if err != nil {
return nil, err
}
part.subparts = append(part.subparts, subpart)
}
return part, nil
}
part.body = new(bytes.Buffer)
_, err = io.Copy(part.body, body)
return part, err
}
func (lk *LetterKnife) visitParts(mp *messagePart, visit func(*messagePart) error) error {
lk.debugf("visitParts: %v sub=%v", mp.header.Get("Content-Type"), mp.subparts)
if mp.isMultipart() {
for _, p := range mp.subparts {
if err := lk.visitParts(p, visit); err != nil {
return err
}
}
return nil
}
return visit(mp)
}
func (lk *LetterKnife) selectParts(mp *messagePart, mediaTypeSpec string, isAttachmentSpec bool) ([]*messagePart, error) {
parts := []*messagePart{}
err := lk.visitParts(mp, func(mp *messagePart) error {
_, isAttachment := mp.attachmentFilename()
if isAttachment != isAttachmentSpec {
return nil
}
ok, err := testPattern(mp.mediaType, mediaTypeSpec)
if err != nil {
return err
}
if ok {
parts = append(parts, mp)
}
return nil
})
if err != nil {
return nil, err
}
return parts, nil
}
func (lk *LetterKnife) checkMatch(h mail.Header, in string, isAddr bool) (bool, error) {
// TODO: fail if header does not exist
p := strings.IndexByte(in, ':')
if p == -1 {
return false, fmt.Errorf("must be in the form of `header:pattern`: %q", in)
}
header, pattern := in[0:p], in[p+1:]
var values []string
if isAddr {
addrs, err := (&mail.AddressParser{WordDecoder: mimeDecoder}).ParseList(h.Get(header))
if err != nil {
return false, fmt.Errorf("parsing header %s: as addresses: %v", header, err)
}
values = make([]string, len(addrs))
for i, addr := range addrs {
values[i] = addr.Address
}
} else {
values = []string{h.Get(header)}
}
for _, value := range values {
value, err := mimeDecoder.DecodeHeader(value)
if err != nil {
return false, err
}
lk.debugf("test %s: %q against %q", header, value, pattern)
ok, err := testPattern(value, pattern)
if err != nil {
return false, err
}
if ok {
return true, nil
}
}
return false, nil
}
var rxPattern = regexp.MustCompile(`(\*|.+?)`)
func regexpFromPattern(pattern string) (*regexp.Regexp, error) {
if pattern[0] == '/' && pattern[len(pattern)-1] == '/' {
return regexp.Compile(pattern[1 : len(pattern)-1])
}
p := rxPattern.ReplaceAllStringFunc(pattern, func(s string) string {
if s == "*" {
return ".+?"
} else {
return regexp.QuoteMeta(s)
}
})
return regexp.Compile("^" + p + "$")
}
func testPattern(value, pattern string) (bool, error) {
if strings.IndexByte(pattern, '*') == -1 && (pattern[0] != '/' && pattern[len(pattern)-1] != '/') {
return value == pattern, nil
}
rx, err := regexpFromPattern(pattern)
if err != nil {
return false, err
}
return rx.MatchString(value), nil
}