forked from jhillyerd/enmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
85 lines (74 loc) · 2.92 KB
/
detect.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
package enmime
import (
"net/textproto"
"strings"
)
// detectMultipartMessage returns true if the message has a recognized multipart Content-Type header
func detectMultipartMessage(root *Part) bool {
// Parse top-level multipart
ctype := root.Header.Get(hnContentType)
mediatype, _, _, err := ParseMediaType(ctype)
// According to rfc2046#section-5.1.7 all other multipart should
// be treated as multipart/mixed
return err == nil && strings.HasPrefix(mediatype, ctMultipartPrefix)
}
// detectAttachmentHeader returns true, if the given header defines an attachment. First it checks
// if the Content-Disposition header defines either an attachment part or an inline part with at
// least one parameter. If this test is false, the Content-Type header is checked for attachment,
// but not inline. Email clients use inline for their text bodies.
//
// Valid Attachment-Headers:
//
// - Content-Disposition: attachment; filename="frog.jpg"
// - Content-Disposition: inline; filename="frog.jpg"
// - Content-Type: attachment; filename="frog.jpg"
func detectAttachmentHeader(header textproto.MIMEHeader) bool {
mediatype, params, _, _ := ParseMediaType(header.Get(hnContentDisposition))
if strings.ToLower(mediatype) == cdAttachment ||
(strings.ToLower(mediatype) == cdInline && len(params) > 0) {
return true
}
mediatype, _, _, _ = ParseMediaType(header.Get(hnContentType))
return strings.ToLower(mediatype) == cdAttachment
}
// detectTextHeader returns true, if the the MIME headers define a valid 'text/plain' or 'text/html'
// part. If the emptyContentTypeIsPlain argument is set to true, a missing Content-Type header will
// result in a positive plain part detection.
func detectTextHeader(header textproto.MIMEHeader, emptyContentTypeIsText bool) bool {
ctype := header.Get(hnContentType)
if ctype == "" && emptyContentTypeIsText {
return true
}
if mediatype, _, _, err := ParseMediaType(ctype); err == nil {
switch mediatype {
case ctTextPlain, ctTextHTML:
return true
}
}
return false
}
// detectBinaryBody returns true if the mail header defines a binary body.
func detectBinaryBody(root *Part) bool {
if detectTextHeader(root.Header, true) {
// It is text/plain, but an attachment.
// Content-Type: text/plain; name="test.csv"
// Content-Disposition: attachment; filename="test.csv"
// Check for attachment only, or inline body is marked
// as attachment, too.
mediatype, _, _, _ := ParseMediaType(root.Header.Get(hnContentDisposition))
return strings.ToLower(mediatype) == cdAttachment
}
isBin := detectAttachmentHeader(root.Header)
if !isBin {
// This must be an attachment, if the Content-Type is not
// 'text/plain' or 'text/html'.
// Example:
// Content-Type: application/pdf; name="doc.pdf"
mediatype, _, _, _ := ParseMediaType(root.Header.Get(hnContentType))
mediatype = strings.ToLower(mediatype)
if mediatype != ctTextPlain && mediatype != ctTextHTML {
return true
}
}
return isBin
}