| 1 |
package enmime |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/textproto" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/jhillyerd/enmime/mediatype" |
| 8 |
) |
| 9 |
|
| 10 |
// detectMultipartMessage returns true if the message has a recognized multipart Content-Type header |
| 11 |
func detectMultipartMessage(root *Part) bool { |
| 12 |
// Parse top-level multipart |
| 13 |
ctype := root.Header.Get(hnContentType) |
| 14 |
mtype, _, _, err := mediatype.Parse(ctype) |
| 15 |
|
| 16 |
// According to rfc2046#section-5.1.7 all other multipart should |
| 17 |
// be treated as multipart/mixed |
| 18 |
return err == nil && strings.HasPrefix(mtype, ctMultipartPrefix) |
| 19 |
} |
| 20 |
|
| 21 |
// detectAttachmentHeader returns true, if the given header defines an attachment. First it checks |
| 22 |
// if the Content-Disposition header defines either an attachment part or an inline part with at |
| 23 |
// least one parameter. If this test is false, the Content-Type header is checked for attachment, |
| 24 |
// but not inline. Email clients use inline for their text bodies. |
| 25 |
// |
| 26 |
// Valid Attachment-Headers: |
| 27 |
// |
| 28 |
// - Content-Disposition: attachment; filename="frog.jpg" |
| 29 |
// - Content-Disposition: inline; filename="frog.jpg" |
| 30 |
// - Content-Type: attachment; filename="frog.jpg" |
| 31 |
func detectAttachmentHeader(header textproto.MIMEHeader) bool { |
| 32 |
mtype, params, _, _ := mediatype.Parse(header.Get(hnContentDisposition)) |
| 33 |
if strings.ToLower(mtype) == cdAttachment || |
| 34 |
(strings.ToLower(mtype) == cdInline && len(params) > 0) { |
| 35 |
return true |
| 36 |
} |
| 37 |
|
| 38 |
mtype, _, _, _ = mediatype.Parse(header.Get(hnContentType)) |
| 39 |
return strings.ToLower(mtype) == cdAttachment |
| 40 |
} |
| 41 |
|
| 42 |
// detectTextHeader returns true, if the the MIME headers define a valid 'text/plain' or 'text/html' |
| 43 |
// part. If the emptyContentTypeIsPlain argument is set to true, a missing Content-Type header will |
| 44 |
// result in a positive plain part detection. |
| 45 |
func detectTextHeader(header textproto.MIMEHeader, emptyContentTypeIsText bool) bool { |
| 46 |
ctype := header.Get(hnContentType) |
| 47 |
if ctype == "" && emptyContentTypeIsText { |
| 48 |
return true |
| 49 |
} |
| 50 |
|
| 51 |
if mtype, _, _, err := mediatype.Parse(ctype); err == nil { |
| 52 |
switch mtype { |
| 53 |
case ctTextPlain, ctTextHTML: |
| 54 |
return true |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return false |
| 59 |
} |
| 60 |
|
| 61 |
// detectBinaryBody returns true if the mail header defines a binary body. |
| 62 |
func detectBinaryBody(root *Part) bool { |
| 63 |
if detectTextHeader(root.Header, true) { |
| 64 |
// It is text/plain, but an attachment. |
| 65 |
// Content-Type: text/plain; name="test.csv" |
| 66 |
// Content-Disposition: attachment; filename="test.csv" |
| 67 |
// Check for attachment only, or inline body is marked |
| 68 |
// as attachment, too. |
| 69 |
mtype, _, _, _ := mediatype.Parse(root.Header.Get(hnContentDisposition)) |
| 70 |
return strings.ToLower(mtype) == cdAttachment |
| 71 |
} |
| 72 |
|
| 73 |
isBin := detectAttachmentHeader(root.Header) |
| 74 |
if !isBin { |
| 75 |
// This must be an attachment, if the Content-Type is not |
| 76 |
// 'text/plain' or 'text/html'. |
| 77 |
// Example: |
| 78 |
// Content-Type: application/pdf; name="doc.pdf" |
| 79 |
mtype, _, _, _ := mediatype.Parse(root.Header.Get(hnContentType)) |
| 80 |
mtype = strings.ToLower(mtype) |
| 81 |
if mtype != ctTextPlain && mtype != ctTextHTML { |
| 82 |
return true |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return isBin |
| 87 |
} |