-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
172 lines (158 loc) · 3.71 KB
/
message.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
package main
import (
"bytes"
"fmt"
"html"
"io"
"log"
"mime"
"mime/multipart"
"net/mail"
"regexp"
"strings"
"github.com/microcosm-cc/bluemonday"
)
func buildPreface() (p string) {
p = config["preface"]
if p != "" {
p = p + "\n"
}
return
}
func buildSubject(m *mail.Message) (s string) {
s = m.Header.Get("Subject")
if s != "" {
s = "Subject: " + s + "\n"
}
return
}
func buildBody(m *mail.Message) string {
messageType, params := getMessageType(m)
if strings.HasPrefix(messageType, "multipart/") {
boundary := params["boundary"]
if boundary != "" {
content, err := parseMultipart(m, messageType, boundary)
if err == nil {
return string(content)
}
}
}
b, err := io.ReadAll(m.Body)
if err != nil {
log.Fatal(err)
}
return string(b)
}
func buildMessage(email io.Reader) (message string) {
m, err := mail.ReadMessage(email)
if err != nil {
log.Fatal(err)
}
message += buildPreface()
message += buildSubject(m)
message += buildBody(m)
return
}
func removeHtmlTags(message string) (s string) {
policy := bluemonday.StrictPolicy()
s = policy.Sanitize(message)
s = html.UnescapeString(s)
s = fixWhitespace(s)
return
}
func fixWhitespace(message string) string {
re := regexp.MustCompile("\n\n+")
return re.ReplaceAllLiteralString(message, "\n\n")
}
func parseMultipart(m *mail.Message, messageType, boundary string) ([]byte, error) {
mr := multipart.NewReader(m.Body, boundary)
if messageType == "multipart/alternative" {
return readAlternativeParts(mr)
}
if messageType == "multipart/mixed" {
return readMixedParts(mr)
}
return nil, fmt.Errorf("Not a recognized multipart message")
}
// Only text/plain and text/html are recognized. In defiance of MIME (RFC2046),
// text/plain is preferred.
func readAlternativeParts(r *multipart.Reader) ([]byte, error) {
parts := map[string][]byte{}
for {
p, err := r.NextPart()
if err != nil {
break
}
if yes, _ := partIsAttachment(p); yes {
// Ignore attachments
continue
}
partType := getPartType(p)
bytes, err := io.ReadAll(p)
if err == nil {
// The last recognized result (accounting for text/plain preference) should
// be returned. Using a map overrides previous parts with the same type.
parts[partType] = bytes
}
}
if bytes, ok := parts["text/plain"]; ok {
return bytes, nil
}
if bytes, ok := parts["text/html"]; ok {
return bytes, nil
}
return nil, fmt.Errorf("Unsupported alternative part")
}
func readMixedParts(r *multipart.Reader) ([]byte, error) {
out := bytes.NewBuffer([]byte{})
for {
p, err := r.NextPart()
if err != nil {
break
}
if yes, _ := partIsAttachment(p); yes {
// Ignore attachments for now
// TODO: Handle attachments
continue
}
partType := getPartType(p)
// Only text parts are recognized.
if strings.HasPrefix(partType, "text/") {
// Add newline between parts
if out.Len() > 0 {
out.Write([]byte("\n"))
}
// Ignore errors
io.Copy(out, p)
}
}
return out.Bytes(), nil
}
// Get the top-level media type and parameters. If not set, use the default
// according to RFC2045 5.2
func getMessageType(m *mail.Message) (contentType string, params map[string]string) {
c := m.Header["Content-Type"]
if len(c) > 0 {
t, p, _ := mime.ParseMediaType(c[0])
return t, p
}
return "text/plain", map[string]string{"charset": "us-ascii"}
}
func getPartType(p *multipart.Part) string {
c := p.Header["Content-Type"]
if len(c) > 0 {
t, _, _ := mime.ParseMediaType(c[0])
return t
}
return ""
}
func partIsAttachment(p *multipart.Part) (bool, map[string]string) {
d := p.Header["Content-Disposition"]
if len(d) > 0 {
t, params, _ := mime.ParseMediaType(d[0])
if t == "attachment" {
return true, params
}
}
return false, map[string]string{}
}