-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecoder.go
131 lines (120 loc) · 3.97 KB
/
Decoder.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
package emaildecoder
import (
"encoding/base64"
"fmt"
"io"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/mail"
"path/filepath"
"strings"
)
type AttachmentCallback func(attachment Attachment)
type Decoder struct {
attachmentIdx int
attachmentCallback AttachmentCallback
reader io.Reader
plainText []byte
html []byte
}
type Attachment struct {
io.Reader
Filename string
ContentType string
}
type EmailContent struct {
HTML []byte
PlainText []byte
Headers mail.Header
}
func NewDecoder(reader io.Reader, attachmentCallback AttachmentCallback) *Decoder {
return &Decoder{
attachmentCallback: attachmentCallback,
reader: reader,
}
}
func (d *Decoder) Decode() (*EmailContent, error) {
msg, err := mail.ReadMessage(d.reader)
if err != nil {
panic(err)
}
mediaType, mediaParams, err := mime.ParseMediaType(msg.Header.Get("Content-Type"))
if err != nil {
if err.Error() == "mime: no media type" {
d.plainText, _ = io.ReadAll(msg.Body)
} else {
return nil, err
}
}
if strings.HasPrefix(mediaType, "multipart/") {
d.findParts(msg.Body, mediaParams["boundary"])
} else if strings.HasPrefix(msg.Header.Get("Content-Type"), "text/") {
d.decodeText(msg.Body, mediaType, msg.Header.Get("Content-Transfer-Encoding"), mediaParams)
}
return &EmailContent{
HTML: d.html,
PlainText: d.plainText,
Headers: msg.Header,
}, nil
}
func (d *Decoder) findParts(mime_data io.Reader, boundary string) {
// Instantiate a new io.Reader dedicated to MIME multipart parsing
// using multipart.NewReader()
reader := multipart.NewReader(mime_data, boundary)
if reader == nil {
return
}
// Go through each of the MIME part of the message Body with NextPart(),
for {
newPart, err := reader.NextPart()
if err != nil {
break
}
contentType, contentParams, _ := mime.ParseMediaType(newPart.Header.Get("Content-Type"))
mediaType, mediaParams, _ := mime.ParseMediaType(newPart.Header.Get("Content-Disposition"))
if strings.HasPrefix(contentType, "multipart/") {
if contentBoundary, hasBoundary := contentParams["boundary"]; hasBoundary {
d.findParts(newPart, contentBoundary)
}
} else if strings.HasPrefix(contentType, "text/") && (mediaType == "" || mediaType == "inline") {
d.decodeText(newPart, contentType, newPart.Header.Get("Content-Transfer-Encoding"), contentParams)
} else if (mediaType == "attachment" || mediaType == "inline") && d.attachmentCallback != nil {
attachment := Attachment{
Reader: newPart,
ContentType: contentType,
}
if filename, filenameFound := mediaParams["filename"]; !filenameFound || len(filename) == 0 || filepath.Base(filename) == string(filepath.Separator) {
d.attachmentIdx++
attachment.Filename = fmt.Sprintf("attachement-%d.file", d.attachmentIdx)
} else {
attachment.Filename = filepath.Base(filename)
}
attachment.Reader = d.getDecodeReader(attachment.Reader, newPart.Header.Get("Content-Transfer-Encoding"), contentParams)
d.attachmentCallback(attachment)
}
}
}
func (d *Decoder) getDecodeReader(reader io.Reader, transferEncoding string, contentParams map[string]string) io.Reader {
if strings.Contains(transferEncoding, "base64") {
reader = base64.NewDecoder(base64.StdEncoding, reader)
}
if strings.Contains(transferEncoding, "quoted-printable") {
reader = quotedprintable.NewReader(reader)
}
if charset, charsetFound := contentParams["charset"]; charsetFound {
if charsetEncoding, charsetEncodingFound := charMapEncoders[strings.ToLower(charset)]; charsetEncodingFound {
reader = charsetEncoding.NewDecoder().Reader(reader)
}
}
return reader
}
func (d *Decoder) decodeText(reader io.Reader, contentType, transferEncoding string, contentParams map[string]string) {
reader = d.getDecodeReader(reader, transferEncoding, contentParams)
if strings.HasPrefix(contentType, "text/plain") {
d.plainText, _ = io.ReadAll(reader)
}
if strings.HasPrefix(contentType, "text/html") {
d.html, _ = io.ReadAll(reader)
}
}