Repository navigation
Expand file tree
/
Copy pathparse.go
More file actions
289 lines (262 loc) · 8.68 KB
/
Copy pathparse.go
File metadata and controls
289 lines (262 loc) · 8.68 KB
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
package main
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/mail"
"net/textproto"
"regexp"
"strings"
"golang.org/x/net/html/charset"
)
// Parsing mirrors mail-service handle_DATA, including its fixes: binary single-part
// mail (Google DMARC zips) becomes an attachment, attached messages are leaves and
// not flattened, and text without a declared charset is read as UTF-8.
type Attachment struct {
ID, Filename, ContentType string
Content []byte
}
type Parsed struct {
FromAddress, FromName string
Subject, Text, HTML string
Attachments []Attachment
AutoReply bool
}
var wordDecoder = &mime.WordDecoder{CharsetReader: charset.NewReaderLabel}
func decodeHeader(s string) string {
if d, err := wordDecoder.DecodeHeader(s); err == nil {
return d
}
return s
}
var spaceRun = regexp.MustCompile(`\s+`)
func collapse(s string) string { return strings.TrimSpace(spaceRun.ReplaceAllString(s, " ")) }
// Intro is the first 200 characters of the plain body with whitespace collapsed.
func (p *Parsed) Intro() string {
r := []rune(collapse(p.Text))
if len(r) > 200 {
r = r[:200]
}
return string(r)
}
func (p *Parsed) Empty() bool {
return len(p.Attachments) == 0 && strings.TrimSpace(p.Subject) == "" &&
strings.TrimSpace(p.Text) == "" && strings.TrimSpace(p.HTML) == ""
}
var autoReplyPrefixes = []string{"自动回复", "auto:", "automatic reply", "auto-reply", "autoreply", "out of office"}
func isAutoReply(h textproto.MIMEHeader, subject string) bool {
if v := strings.ToLower(strings.TrimSpace(h.Get("Auto-Submitted"))); v != "" && v != "no" {
return true
}
if h.Get("X-Autoreply") != "" || h.Get("X-Autorespond") != "" {
return true
}
if strings.ToLower(strings.TrimSpace(h.Get("Precedence"))) == "auto_reply" {
return true
}
s := strings.ToLower(strings.TrimSpace(subject))
for _, prefix := range autoReplyPrefixes {
if strings.HasPrefix(s, prefix) {
return true
}
}
return false
}
func parseMessage(raw []byte) *Parsed { return parseDepth(raw, 0) }
// parseDepth: an attached message is unwrapped one level only, like Python's inner.get_body;
// unwrapping recursively let nested message/rfc822 parts cost quadratic time.
func parseDepth(raw []byte, depth int) *Parsed {
m, err := mail.ReadMessage(bytes.NewReader(raw))
if err != nil {
// Unparseable headers: keep the bytes readable rather than losing the mail.
return &Parsed{Text: validUTF8(raw)}
}
body, _ := io.ReadAll(m.Body)
h := textproto.MIMEHeader(m.Header)
p := &Parsed{Subject: decodeHeader(h.Get("Subject"))}
from := decodeHeader(h.Get("From"))
if i := strings.LastIndex(from, "<"); i >= 0 {
p.FromName = strings.TrimSpace(strings.Trim(strings.TrimSpace(from[:i]), `"`))
p.FromAddress = strings.ToLower(strings.TrimSpace(strings.TrimRight(from[i+1:], ">")))
} else {
p.FromAddress = strings.ToLower(strings.TrimSpace(from))
}
p.AutoReply = isAutoReply(h, p.Subject)
ct, params := mediaType(h)
switch {
case strings.HasPrefix(ct, "multipart/"):
var leaves []leaf
collectLeaves(h, body, 0, &leaves)
for _, lf := range leaves {
lct, lparams := mediaType(lf.h)
filename := partFilename(lf.h, lparams)
isAtt := strings.Contains(strings.ToLower(lf.h.Get("Content-Disposition")), "attachment") || filename != ""
if !isAtt && (lct == "text/plain" || lct == "text/html") {
// The first plain and HTML parts are the body; later unnamed body parts are dropped.
if lct == "text/plain" && p.Text == "" {
p.Text = partText(lparams, decodeCTE(lf.h, lf.body))
} else if lct == "text/html" && p.HTML == "" {
p.HTML = partText(lparams, decodeCTE(lf.h, lf.body))
}
continue
}
// Everything else is kept, attachment markers or not: DMARC reports,
// calendar invites, attached or bounced messages, delivery status.
payload := decodeCTE(lf.h, lf.body)
if len(payload) > 0 || isAtt {
if filename == "" {
filename = defaultAttachmentName(lct, len(p.Attachments)+1)
}
p.Attachments = append(p.Attachments, Attachment{ID: newID(), Filename: filename, ContentType: lct, Content: payload})
}
}
if p.Text == "" && p.HTML == "" && depth == 0 {
// "Forward as attachment" with no note: show the attached message's body.
for _, lf := range leaves {
if lct, _ := mediaType(lf.h); lct == "message/rfc822" {
inner := parseDepth(decodeCTE(lf.h, lf.body), 1)
p.Text, p.HTML = inner.Text, inner.HTML
break
}
}
}
case strings.HasPrefix(ct, "text/"):
content := partText(params, decodeCTE(h, body))
if ct == "text/html" {
p.HTML = content
} else {
p.Text = content
}
default:
if payload := decodeCTE(h, body); len(payload) > 0 {
filename := partFilename(h, params)
if filename == "" {
filename = "attachment-1"
}
p.Attachments = append(p.Attachments, Attachment{ID: newID(), Filename: filename, ContentType: ct, Content: payload})
}
}
return p
}
type leaf struct {
h textproto.MIMEHeader
body []byte // still transfer-encoded
}
// collectLeaves walks multipart/* only; message/rfc822 parts stay whole.
func collectLeaves(h textproto.MIMEHeader, body []byte, depth int, out *[]leaf) {
ct, params := mediaType(h)
if !strings.HasPrefix(ct, "multipart/") || params["boundary"] == "" || depth > 30 {
*out = append(*out, leaf{h, body})
return
}
mr := multipart.NewReader(bytes.NewReader(body), params["boundary"])
for {
part, err := mr.NextRawPart() // raw: NextPart would silently undo quoted-printable
if err != nil {
return
}
b, _ := io.ReadAll(part) // a truncated last part keeps what was read
collectLeaves(part.Header, b, depth+1, out)
}
}
func mediaType(h textproto.MIMEHeader) (string, map[string]string) {
v := h.Get("Content-Type")
if v == "" {
return "text/plain", map[string]string{}
}
mt, params, err := mime.ParseMediaType(v)
if mt == "" {
return "text/plain", map[string]string{}
}
if err != nil || params == nil {
params = map[string]string{}
if b := looseParam(v, "boundary"); b != "" {
params["boundary"] = b
}
if c := looseParam(v, "charset"); c != "" {
params["charset"] = c
}
}
return mt, params
}
// looseParam reads a parameter from a header mime.ParseMediaType rejects,
// e.g. an unquoted encoded-word filename some Chinese mailers send.
func looseParam(v, key string) string {
re := regexp.MustCompile(`(?i)(?:^|;)\s*` + regexp.QuoteMeta(key) + `\s*=\s*("([^"]*)"|[^;\s]+)`)
m := re.FindStringSubmatch(v)
if m == nil {
return ""
}
if strings.HasPrefix(m[1], `"`) {
return m[2]
}
return m[1]
}
func partFilename(h textproto.MIMEHeader, ctParams map[string]string) string {
if cd := h.Get("Content-Disposition"); cd != "" {
_, p, err := mime.ParseMediaType(cd)
name := p["filename"]
if err != nil {
name = looseParam(cd, "filename")
}
if name != "" {
return decodeHeader(name)
}
}
if name := ctParams["name"]; name != "" {
return decodeHeader(name)
}
if name := looseParam(h.Get("Content-Type"), "name"); name != "" {
return decodeHeader(name)
}
return ""
}
func defaultAttachmentName(ct string, index int) string {
ext := map[string]string{"message/rfc822": ".eml", "text/calendar": ".ics"}[ct]
if ext == "" && (strings.HasPrefix(ct, "text/") || strings.HasPrefix(ct, "message/")) {
ext = ".txt"
}
return fmt.Sprintf("attachment-%d%s", index, ext)
}
var notBase64 = regexp.MustCompile(`[^A-Za-z0-9+/=]`)
func decodeCTE(h textproto.MIMEHeader, body []byte) []byte {
switch strings.ToLower(strings.TrimSpace(h.Get("Content-Transfer-Encoding"))) {
case "base64":
// Lenient like Python: drop line breaks, padding and stray characters.
// Some mailers pad every line, so padding ends a chunk rather than the data.
var out []byte
for _, chunk := range bytes.FieldsFunc(notBase64.ReplaceAll(body, nil), func(r rune) bool { return r == '=' }) {
if len(chunk)%4 == 1 {
chunk = chunk[:len(chunk)-1]
}
buf := make([]byte, base64.RawStdEncoding.DecodedLen(len(chunk)))
n, _ := base64.RawStdEncoding.Decode(buf, chunk)
out = append(out, buf[:n]...)
}
return out
case "quoted-printable":
out, err := io.ReadAll(quotedprintable.NewReader(bytes.NewReader(body)))
if err != nil && len(out) == 0 {
return body
}
return out
}
return body
}
func partText(params map[string]string, b []byte) string {
cs := strings.ToLower(strings.TrimSpace(params["charset"]))
if cs == "" || cs == "utf-8" || cs == "utf8" || cs == "us-ascii" {
return validUTF8(b)
}
r, err := charset.NewReaderLabel(cs, bytes.NewReader(b))
if err != nil {
return validUTF8(b) // unknown charset: same fallback as Python, UTF-8 with replacement
}
out, _ := io.ReadAll(r)
return validUTF8(out)
}
func validUTF8(b []byte) string { return strings.ToValidUTF8(string(b), "\uFFFD") }