-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
367 lines (320 loc) · 7.92 KB
/
fetch.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package imap
import (
"bytes"
"fmt"
"strconv"
"strings"
"time"
"github.com/paulrosania/go-mail"
)
type FetchAttribute interface {
Name() string
Marshal(*Message) string
}
func parseFetchAttribute(field string) (FetchAttribute, error) {
switch field {
case "BODY":
return BodystructureFetchAttribute(false), nil
case "BODYSTRUCTURE":
return BodystructureFetchAttribute(true), nil
case "FLAGS":
return FlagsFetchAttribute, nil
case "INTERNALDATE":
return InternalDateFetchAttribute, nil
case "RFC822":
return RFC822FetchAttribute, nil
case "RFC822.HEADER":
return RFC822HeaderFetchAttribute, nil
case "RFC822.SIZE":
return RFC822SizeFetchAttribute, nil
case "RFC822.TEXT":
return RFC822TextFetchAttribute, nil
case "ENVELOPE":
return EnvelopeFetchAttribute, nil
case "UID":
return UIDFetchAttribute, nil
}
return nil, ProtocolErrorf("unknown field %q", field)
}
type bodyFetchMode string
const (
FetchAll bodyFetchMode = ""
FetchText = "TEXT"
FetchHeader = "HEADER"
FetchHeaderFields = "HEADER.FIELDS"
FetchHeaderFieldsNot = "HEADER.FIELDS.NOT"
)
type BodyFetchAttribute struct {
peek bool
part string
mode bodyFetchMode
headerList []string
hasPartial bool
partial [2]int
}
func (fa BodyFetchAttribute) Name() string {
partialPart := ""
if fa.hasPartial {
partialPart = fmt.Sprintf("<%d>", fa.partial[0])
}
sectionPart := fa.part
if fa.mode != FetchAll {
if sectionPart != "" {
sectionPart += "."
}
sectionPart += string(fa.mode)
}
if len(fa.headerList) > 0 {
first := true
sectionPart += " ("
for _, h := range fa.headerList {
if !first {
sectionPart += " "
}
sectionPart += quoteString(h)
first = false
}
sectionPart += ")"
}
modePart := fa.mode
if len(modePart) > 0 {
modePart = "." + modePart
}
return fmt.Sprintf("BODY[%s]%s", sectionPart, partialPart)
}
func (fa BodyFetchAttribute) Marshal(msg *Message) string {
result := ""
if fa.part == "" {
switch fa.mode {
case FetchText:
result = msg.AsText(true)
case FetchHeader:
result = msg.Header.AsText(true)
case FetchHeaderFields:
keep := make(map[string]bool)
for _, name := range fa.headerList {
keep[strings.ToLower(name)] = true
}
i := 0
for i < len(msg.Header.Fields) {
f := msg.Header.Fields[i]
if !keep[strings.ToLower(f.Name())] {
msg.Header.Remove(f)
} else {
i++
}
}
result = msg.Header.AsText(true)
case FetchHeaderFieldsNot:
for _, name := range fa.headerList {
msg.Header.RemoveAllNamed(name)
}
result = msg.Header.AsText(true)
default:
result = msg.RFC822(true)
}
} else {
part := msg.BodyPart(fa.part, false)
if part == nil {
// TODO: handle missing part
} else {
switch fa.mode {
case FetchText:
result = part.AsText(true)
case FetchHeader:
result = part.Header.AsText(true)
case FetchHeaderFields:
keep := make(map[string]bool)
for _, name := range fa.headerList {
keep[strings.ToLower(name)] = true
}
i := 0
for i < len(part.Header.Fields) {
f := part.Header.Fields[i]
if !keep[strings.ToLower(f.Name())] {
part.Header.RemoveAllNamed(f.Name())
} else {
i++
}
}
result = part.Header.AsText(true)
case FetchHeaderFieldsNot:
for _, name := range fa.headerList {
part.Header.RemoveAllNamed(name)
}
result = part.Header.AsText(true)
default:
buf := bytes.NewBuffer(make([]byte, 0, 10000))
buf.WriteString(part.Header.AsText(true))
buf.WriteString("\n")
buf.WriteString(part.AsText(true))
result = buf.String()
}
}
}
if fa.hasPartial {
l := fa.partial[0]
if l > len(result) {
l = len(result)
}
r := fa.partial[1]
if r < l {
r = l
}
result = result[l:r]
}
return fmt.Sprintf("{%d}\r\n%s", len([]byte(result)), result)
}
type BodystructureFetchAttribute bool
func (includeExtensions BodystructureFetchAttribute) Name() string {
if includeExtensions {
return "BODYSTRUCTURE"
} else {
return "BODY"
}
}
func (includeExtensions BodystructureFetchAttribute) marshalPart(p *mail.Part) string {
ct := p.Header.ContentType()
buf := bytes.NewBuffer(make([]byte, 0, 512))
buf.WriteRune('(')
if len(p.Parts) == 0 {
contentType := "text"
subtype := "plain"
if ct != nil {
contentType = ct.Type
subtype = ct.Subtype
}
buf.WriteString(contentType)
buf.WriteRune(' ')
buf.WriteString(subtype)
// params
// TODO
buf.WriteString(" (")
buf.WriteRune(')')
// id
// TODO
buf.WriteRune(' ')
buf.WriteString("NIL")
// desc
buf.WriteRune(' ')
contentDescription := p.Header.ContentDescription()
if contentDescription == "" {
buf.WriteString("NIL")
} else {
buf.WriteString(contentDescription)
}
// enc
// TODO
buf.WriteRune(' ')
buf.WriteString(`"8-BIT"`)
// octets
octets := len([]byte(p.Text))
buf.WriteRune(' ')
buf.WriteString(strconv.Itoa(octets))
if contentType == "text" {
// lines
// TODO
lines := strings.Count(p.Text, "\n")
buf.WriteRune(' ')
buf.WriteString(strconv.Itoa(lines))
} else if contentType == "message" {
// envelope
buf.WriteRune(' ')
buf.WriteString("")
// body
buf.WriteRune(' ')
buf.WriteString("")
// lines
lines := strings.Count(p.Text, "\n")
buf.WriteRune(' ')
buf.WriteString(strconv.Itoa(lines))
}
if includeExtensions {
// TODO
buf.WriteRune(' ')
buf.WriteString("NIL")
}
} else {
for _, pt := range p.Parts {
buf.WriteString(includeExtensions.marshalPart(pt))
}
if includeExtensions {
// TODO
buf.WriteRune(' ')
buf.WriteString("NIL")
}
}
buf.WriteRune(')')
return buf.String()
}
func (includeExtensions BodystructureFetchAttribute) Marshal(msg *Message) string {
buf := bytes.NewBuffer(make([]byte, 0, 512))
if len(msg.Parts) == 0 {
// should never happen
} else if len(msg.Parts) == 1 {
buf.WriteString(includeExtensions.marshalPart(msg.Parts[0]))
} else {
buf.WriteRune('(')
for _, p := range msg.Parts {
buf.WriteString(includeExtensions.marshalPart(p))
}
ct := msg.Header.ContentType()
buf.WriteRune(' ')
buf.WriteString(ct.Subtype)
if includeExtensions {
buf.WriteRune(' ')
// TODO
buf.WriteString("NIL")
}
buf.WriteRune(')')
}
return buf.String()
}
type BasicFetchAttribute struct {
name string
marshaler func(*Message) string
}
func (fa BasicFetchAttribute) Name() string {
return fa.name
}
func (fa BasicFetchAttribute) Marshal(m *Message) string {
return fa.marshaler(m)
}
var (
EnvelopeFetchAttribute = BasicFetchAttribute{"ENVELOPE", MarshalEnvelope}
FlagsFetchAttribute = BasicFetchAttribute{"FLAGS", MarshalFlags}
InternalDateFetchAttribute = BasicFetchAttribute{"INTERNALDATE", MarshalInternalDate}
RFC822FetchAttribute = BasicFetchAttribute{"RFC822", MarshalRFC822}
RFC822HeaderFetchAttribute = BasicFetchAttribute{"RFC822.HEADER", MarshalRFC822Header}
RFC822SizeFetchAttribute = BasicFetchAttribute{"RFC822.SIZE", MarshalRFC822Size}
RFC822TextFetchAttribute = BasicFetchAttribute{"RFC822.TEXT", MarshalRFC822Text}
UIDFetchAttribute = BasicFetchAttribute{"UID", MarshalUID}
)
func MarshalEnvelope(m *Message) string {
return headerToEnvelopeString(m.Header)
}
func MarshalFlags(m *Message) string {
flags := []string{}
for _, f := range m.Flags {
flags = append(flags, f.String())
}
return fmt.Sprintf("(%s)", strings.Join(flags, " "))
}
func MarshalInternalDate(m *Message) string {
return quoteString(m.ReceivedAt.Format(time.RFC822))
}
func MarshalRFC822(m *Message) string {
return quoteString(m.RFC822(true))
}
func MarshalRFC822Header(m *Message) string {
return quoteString(m.Header.AsText(true))
}
func MarshalRFC822Size(m *Message) string {
return strconv.Itoa(m.RFC822Size)
}
func MarshalRFC822Text(m *Message) string {
return quoteString(m.Body(true))
}
func MarshalUID(m *Message) string {
return strconv.Itoa(m.UID)
}