1package imapclient
2
3import (
4 "bufio"
5 "fmt"
6 "strings"
7)
8
9// Capability is a known string for with the ENABLED and CAPABILITY command.
10type Capability string
11
12const (
13 CapIMAP4rev1 Capability = "IMAP4rev1"
14 CapIMAP4rev2 Capability = "IMAP4rev2"
15 CapLoginDisabled Capability = "LOGINDISABLED"
16 CapStarttls Capability = "STARTTLS"
17 CapAuthPlain Capability = "AUTH=PLAIN"
18 CapLiteralPlus Capability = "LITERAL+"
19 CapLiteralMinus Capability = "LITERAL-"
20 CapIdle Capability = "IDLE"
21 CapNamespace Capability = "NAMESPACE"
22 CapBinary Capability = "BINARY"
23 CapUnselect Capability = "UNSELECT"
24 CapUidplus Capability = "UIDPLUS"
25 CapEsearch Capability = "ESEARCH"
26 CapEnable Capability = "ENABLE"
27 CapSave Capability = "SAVE"
28 CapListExtended Capability = "LIST-EXTENDED"
29 CapSpecialUse Capability = "SPECIAL-USE"
30 CapMove Capability = "MOVE"
31 CapUTF8Only Capability = "UTF8=ONLY"
32 CapUTF8Accept Capability = "UTF8=ACCEPT"
33 CapID Capability = "ID" // ../rfc/2971:80
34)
35
36// Status is the tagged final result of a command.
37type Status string
38
39const (
40 BAD Status = "BAD" // Syntax error.
41 NO Status = "NO" // Command failed.
42 OK Status = "OK" // Command succeeded.
43)
44
45// Result is the final response for a command, indicating success or failure.
46type Result struct {
47 Status Status
48 RespText
49}
50
51// CodeArg represents a response code with arguments, i.e. the data between [] in the response line.
52type CodeArg interface {
53 CodeString() string
54}
55
56// CodeOther is a valid but unrecognized response code.
57type CodeOther struct {
58 Code string
59 Args []string
60}
61
62func (c CodeOther) CodeString() string {
63 return c.Code + " " + strings.Join(c.Args, " ")
64}
65
66// CodeWords is a code with space-separated string parameters. E.g. CAPABILITY.
67type CodeWords struct {
68 Code string
69 Args []string
70}
71
72func (c CodeWords) CodeString() string {
73 s := c.Code
74 for _, w := range c.Args {
75 s += " " + w
76 }
77 return s
78}
79
80// CodeList is a code with a list with space-separated strings as parameters. E.g. BADCHARSET, PERMANENTFLAGS.
81type CodeList struct {
82 Code string
83 Args []string // If nil, no list was present. List can also be empty.
84}
85
86func (c CodeList) CodeString() string {
87 s := c.Code
88 if c.Args == nil {
89 return s
90 }
91 return s + "(" + strings.Join(c.Args, " ") + ")"
92}
93
94// CodeUint is a code with a uint32 parameter, e.g. UIDNEXT and UIDVALIDITY.
95type CodeUint struct {
96 Code string
97 Num uint32
98}
99
100func (c CodeUint) CodeString() string {
101 return fmt.Sprintf("%s %d", c.Code, c.Num)
102}
103
104// "APPENDUID" response code.
105type CodeAppendUID struct {
106 UIDValidity uint32
107 UID uint32
108}
109
110func (c CodeAppendUID) CodeString() string {
111 return fmt.Sprintf("APPENDUID %d %d", c.UIDValidity, c.UID)
112}
113
114// "COPYUID" response code.
115type CodeCopyUID struct {
116 DestUIDValidity uint32
117 From []NumRange
118 To []NumRange
119}
120
121func (c CodeCopyUID) CodeString() string {
122 str := func(l []NumRange) string {
123 s := ""
124 for i, e := range l {
125 if i > 0 {
126 s += ","
127 }
128 s += fmt.Sprintf("%d", e.First)
129 if e.Last != nil {
130 s += fmt.Sprintf(":%d", *e.Last)
131 }
132 }
133 return s
134 }
135 return fmt.Sprintf("COPYUID %d %s %s", c.DestUIDValidity, str(c.From), str(c.To))
136}
137
138// For CONDSTORE.
139type CodeModified NumSet
140
141func (c CodeModified) CodeString() string {
142 return fmt.Sprintf("MODIFIED %s", NumSet(c).String())
143}
144
145// For CONDSTORE.
146type CodeHighestModSeq int64
147
148func (c CodeHighestModSeq) CodeString() string {
149 return fmt.Sprintf("HIGHESTMODSEQ %d", c)
150}
151
152// RespText represents a response line minus the leading tag.
153type RespText struct {
154 Code string // The first word between [] after the status.
155 CodeArg CodeArg // Set if code has a parameter.
156 More string // Any remaining text.
157}
158
159// atom or string.
160func astring(s string) string {
161 if len(s) == 0 {
162 return stringx(s)
163 }
164 for _, c := range s {
165 if c <= ' ' || c >= 0x7f || c == '(' || c == ')' || c == '{' || c == '%' || c == '*' || c == '"' || c == '\\' {
166 return stringx(s)
167 }
168 }
169 return s
170}
171
172// imap "string", i.e. double-quoted string or syncliteral.
173func stringx(s string) string {
174 r := `"`
175 for _, c := range s {
176 if c == '\x00' || c == '\r' || c == '\n' {
177 return syncliteral(s)
178 }
179 if c == '\\' || c == '"' {
180 r += `\`
181 }
182 r += string(c)
183 }
184 r += `"`
185 return r
186}
187
188// sync literal, i.e. {<num>}\r\n<num bytes>.
189func syncliteral(s string) string {
190 return fmt.Sprintf("{%d}\r\n", len(s)) + s
191}
192
193// Untagged is a parsed untagged response. See types starting with Untagged.
194// todo: make an interface that the untagged responses implement?
195type Untagged any
196
197type UntaggedBye RespText
198type UntaggedPreauth RespText
199type UntaggedExpunge uint32
200type UntaggedExists uint32
201type UntaggedRecent uint32
202type UntaggedCapability []string
203type UntaggedEnabled []string
204type UntaggedResult Result
205type UntaggedFlags []string
206type UntaggedList struct {
207 // ../rfc/9051:6690
208 Flags []string
209 Separator byte // 0 for NIL
210 Mailbox string
211 Extended []MboxListExtendedItem
212 OldName string // If present, taken out of Extended.
213}
214type UntaggedFetch struct {
215 Seq uint32
216 Attrs []FetchAttr
217}
218type UntaggedSearch []uint32
219
220// ../rfc/7162:1101
221type UntaggedSearchModSeq struct {
222 Nums []uint32
223 ModSeq int64
224}
225type UntaggedStatus struct {
226 Mailbox string
227 Attrs map[StatusAttr]int64 // Upper case status attributes.
228}
229
230// ../rfc/9051:7059 ../9208:712
231type StatusAttr string
232
233const (
234 StatusMessages StatusAttr = "MESSAGES"
235 StatusUIDNext StatusAttr = "UIDNEXT"
236 StatusUIDValidity StatusAttr = "UIDVALIDITY"
237 StatusUnseen StatusAttr = "UNSEEN"
238 StatusDeleted StatusAttr = "DELETED"
239 StatusSize StatusAttr = "SIZE"
240 StatusRecent StatusAttr = "RECENT"
241 StatusAppendLimit StatusAttr = "APPENDLIMIT"
242 StatusHighestModSeq StatusAttr = "HIGHESTMODSEQ"
243 StatusDeletedStorage StatusAttr = "DELETED-STORAGE"
244)
245
246type UntaggedNamespace struct {
247 Personal, Other, Shared []NamespaceDescr
248}
249type UntaggedLsub struct {
250 // ../rfc/3501:4833
251 Flags []string
252 Separator byte
253 Mailbox string
254}
255
256// Fields are optional and zero if absent.
257type UntaggedEsearch struct {
258 // ../rfc/9051:6546
259 Correlator string
260 UID bool
261 Min uint32
262 Max uint32
263 All NumSet
264 Count *uint32
265 ModSeq int64
266 Exts []EsearchDataExt
267}
268
269// UntaggedVanished is used in QRESYNC to send UIDs that have been removed.
270type UntaggedVanished struct {
271 Earlier bool
272 UIDs NumSet
273}
274
275// UntaggedQuotaroot lists the roots for which quota can be present.
276type UntaggedQuotaroot []string
277
278// UntaggedQuota holds the quota for a quota root.
279type UntaggedQuota struct {
280 Root string
281
282 // Always has at least one. Any QUOTA=RES-* capability not mentioned has no limit
283 // or this quota root.
284 Resources []QuotaResource
285}
286
287// Resource types ../rfc/9208:533
288
289// QuotaResourceName is the name of a resource type. More can be defined in the
290// future and encountered in the wild. Always in upper case.
291type QuotaResourceName string
292
293const (
294 QuotaResourceStorage = "STORAGE"
295 QuotaResourceMesssage = "MESSAGE"
296 QuotaResourceMailbox = "MAILBOX"
297 QuotaResourceAnnotationStorage = "ANNOTATION-STORAGE"
298)
299
300type QuotaResource struct {
301 Name QuotaResourceName
302 Usage int64 // Currently in use. Count or disk size in 1024 byte blocks.
303 Limit int64 // Maximum allowed usage.
304}
305
306// ../rfc/2971:184
307
308type UntaggedID map[string]string
309
310// Extended data in an ESEARCH response.
311type EsearchDataExt struct {
312 Tag string
313 Value TaggedExtVal
314}
315
316type NamespaceDescr struct {
317 // ../rfc/9051:6769
318 Prefix string
319 Separator byte // If 0 then separator was absent.
320 Exts []NamespaceExtension
321}
322
323type NamespaceExtension struct {
324 // ../rfc/9051:6773
325 Key string
326 Values []string
327}
328
329// FetchAttr represents a FETCH response attribute.
330type FetchAttr interface {
331 Attr() string // Name of attribute.
332}
333
334type NumSet struct {
335 SearchResult bool // True if "$", in which case Ranges is irrelevant.
336 Ranges []NumRange
337}
338
339func (ns NumSet) IsZero() bool {
340 return !ns.SearchResult && ns.Ranges == nil
341}
342
343func (ns NumSet) String() string {
344 if ns.SearchResult {
345 return "$"
346 }
347 var r string
348 for i, x := range ns.Ranges {
349 if i > 0 {
350 r += ","
351 }
352 r += x.String()
353 }
354 return r
355}
356
357func ParseNumSet(s string) (ns NumSet, rerr error) {
358 c := Conn{r: bufio.NewReader(strings.NewReader(s))}
359 defer c.recover(&rerr)
360 ns = c.xsequenceSet()
361 return
362}
363
364// NumRange is a single number or range.
365type NumRange struct {
366 First uint32 // 0 for "*".
367 Last *uint32 // Nil if absent, 0 for "*".
368}
369
370func (nr NumRange) String() string {
371 var r string
372 if nr.First == 0 {
373 r += "*"
374 } else {
375 r += fmt.Sprintf("%d", nr.First)
376 }
377 if nr.Last == nil {
378 return r
379 }
380 r += ":"
381 v := *nr.Last
382 if v == 0 {
383 r += "*"
384 } else {
385 r += fmt.Sprintf("%d", v)
386 }
387 return r
388}
389
390type TaggedExtComp struct {
391 String string
392 Comps []TaggedExtComp // Used for both space-separated and ().
393}
394
395type TaggedExtVal struct {
396 // ../rfc/9051:7111
397 Number *int64
398 SeqSet *NumSet
399 Comp *TaggedExtComp // If SimpleNumber and SimpleSeqSet is nil, this is a Comp. But Comp is optional and can also be nil. Not great.
400}
401
402type MboxListExtendedItem struct {
403 // ../rfc/9051:6699
404 Tag string
405 Val TaggedExtVal
406}
407
408// "FLAGS" fetch response.
409type FetchFlags []string
410
411func (f FetchFlags) Attr() string { return "FLAGS" }
412
413// "ENVELOPE" fetch response.
414type FetchEnvelope Envelope
415
416func (f FetchEnvelope) Attr() string { return "ENVELOPE" }
417
418// Envelope holds the basic email message fields.
419type Envelope struct {
420 Date string
421 Subject string
422 From, Sender, ReplyTo, To, CC, BCC []Address
423 InReplyTo, MessageID string
424}
425
426// Address is an address field in an email message, e.g. To.
427type Address struct {
428 Name, Adl, Mailbox, Host string
429}
430
431// "INTERNALDATE" fetch response.
432type FetchInternalDate string // todo: parsed time
433func (f FetchInternalDate) Attr() string { return "INTERNALDATE" }
434
435// "RFC822.SIZE" fetch response.
436type FetchRFC822Size int64
437
438func (f FetchRFC822Size) Attr() string { return "RFC822.SIZE" }
439
440// "RFC822" fetch response.
441type FetchRFC822 string
442
443func (f FetchRFC822) Attr() string { return "RFC822" }
444
445// "RFC822.HEADER" fetch response.
446type FetchRFC822Header string
447
448func (f FetchRFC822Header) Attr() string { return "RFC822.HEADER" }
449
450// "RFC82.TEXT" fetch response.
451type FetchRFC822Text string
452
453func (f FetchRFC822Text) Attr() string { return "RFC822.TEXT" }
454
455// "BODYSTRUCTURE" fetch response.
456type FetchBodystructure struct {
457 // ../rfc/9051:6355
458 RespAttr string
459 Body any // BodyType*
460}
461
462func (f FetchBodystructure) Attr() string { return f.RespAttr }
463
464// "BODY" fetch response.
465type FetchBody struct {
466 // ../rfc/9051:6756 ../rfc/9051:6985
467 RespAttr string
468 Section string // todo: parse more ../rfc/9051:6985
469 Offset int32
470 Body string
471}
472
473func (f FetchBody) Attr() string { return f.RespAttr }
474
475// BodyFields is part of a FETCH BODY[] response.
476type BodyFields struct {
477 Params [][2]string
478 ContentID, ContentDescr, CTE string
479 Octets int32
480}
481
482// BodyTypeMpart represents the body structure a multipart message, with
483// subparts and the multipart media subtype. Used in a FETCH response.
484type BodyTypeMpart struct {
485 // ../rfc/9051:6411
486 Bodies []any // BodyTypeBasic, BodyTypeMsg, BodyTypeText
487 MediaSubtype string
488 Ext *BodyExtensionMpart
489}
490
491// BodyTypeBasic represents basic information about a part, used in a FETCH
492// response.
493type BodyTypeBasic struct {
494 // ../rfc/9051:6407
495 MediaType, MediaSubtype string
496 BodyFields BodyFields
497 Ext *BodyExtension1Part
498}
499
500// BodyTypeMsg represents an email message as a body structure, used in a FETCH
501// response.
502type BodyTypeMsg struct {
503 // ../rfc/9051:6415
504 MediaType, MediaSubtype string
505 BodyFields BodyFields
506 Envelope Envelope
507 Bodystructure any // One of the BodyType*
508 Lines int64
509 Ext *BodyExtension1Part
510}
511
512// BodyTypeText represents a text part as a body structure, used in a FETCH
513// response.
514type BodyTypeText struct {
515 // ../rfc/9051:6418
516 MediaType, MediaSubtype string
517 BodyFields BodyFields
518 Lines int64
519 Ext *BodyExtension1Part
520}
521
522// BodyExtension1Part has the extensible form fields of a BODYSTRUCTURE for
523// multiparts.
524type BodyExtensionMpart struct {
525 // ../rfc/9051:5986 ../rfc/3501:4161 ../rfc/9051:6371 ../rfc/3501:4599
526 Params [][2]string
527 Disposition string
528 DispositionParams [][2]string
529 Language []string
530 Location string
531 More []BodyExtension
532}
533
534// BodyExtension1Part has the extensible form fields of a BODYSTRUCTURE for
535// non-multiparts.
536type BodyExtension1Part struct {
537 // ../rfc/9051:6023 ../rfc/3501:4191 ../rfc/9051:6366 ../rfc/3501:4584
538 MD5 string
539 Disposition string
540 DispositionParams [][2]string
541 Language []string
542 Location string
543 More []BodyExtension
544}
545
546// BodyExtension has the additional extension fields for future expansion of
547// extensions.
548type BodyExtension struct {
549 String *string
550 Number *int64
551 More []BodyExtension
552}
553
554// "BINARY" fetch response.
555type FetchBinary struct {
556 RespAttr string
557 Parts []uint32 // Can be nil.
558 Data string
559}
560
561func (f FetchBinary) Attr() string { return f.RespAttr }
562
563// "BINARY.SIZE" fetch response.
564type FetchBinarySize struct {
565 RespAttr string
566 Parts []uint32
567 Size int64
568}
569
570func (f FetchBinarySize) Attr() string { return f.RespAttr }
571
572// "UID" fetch response.
573type FetchUID uint32
574
575func (f FetchUID) Attr() string { return "UID" }
576
577// "MODSEQ" fetch response.
578type FetchModSeq int64
579
580func (f FetchModSeq) Attr() string { return "MODSEQ" }
581