10// Capability is a known string for with the ENABLED and CAPABILITY command.
14 CapIMAP4rev1 Capability = "IMAP4rev1"
15 CapIMAP4rev2 Capability = "IMAP4rev2"
16 CapLoginDisabled Capability = "LOGINDISABLED"
17 CapStarttls Capability = "STARTTLS"
18 CapAuthPlain Capability = "AUTH=PLAIN"
19 CapLiteralPlus Capability = "LITERAL+"
20 CapLiteralMinus Capability = "LITERAL-"
21 CapIdle Capability = "IDLE"
22 CapNamespace Capability = "NAMESPACE"
23 CapBinary Capability = "BINARY"
24 CapUnselect Capability = "UNSELECT"
25 CapUidplus Capability = "UIDPLUS"
26 CapEsearch Capability = "ESEARCH"
27 CapEnable Capability = "ENABLE"
28 CapSave Capability = "SAVE"
29 CapListExtended Capability = "LIST-EXTENDED"
30 CapSpecialUse Capability = "SPECIAL-USE"
31 CapMove Capability = "MOVE"
32 CapUTF8Only Capability = "UTF8=ONLY"
33 CapUTF8Accept Capability = "UTF8=ACCEPT"
42// Status is the tagged final result of a command.
46 BAD Status = "BAD" // Syntax error.
47 NO Status = "NO" // Command failed.
48 OK Status = "OK" // Command succeeded.
51// Result is the final response for a command, indicating success or failure.
57// CodeArg represents a response code with arguments, i.e. the data between [] in the response line.
58type CodeArg interface {
62// CodeOther is a valid but unrecognized response code.
63type CodeOther struct {
68func (c CodeOther) CodeString() string {
69 return c.Code + " " + strings.Join(c.Args, " ")
72// CodeWords is a code with space-separated string parameters. E.g. CAPABILITY.
73type CodeWords struct {
78func (c CodeWords) CodeString() string {
80 for _, w := range c.Args {
86// CodeList is a code with a list with space-separated strings as parameters. E.g. BADCHARSET, PERMANENTFLAGS.
89 Args []string // If nil, no list was present. List can also be empty.
92func (c CodeList) CodeString() string {
97 return s + "(" + strings.Join(c.Args, " ") + ")"
100// CodeUint is a code with a uint32 parameter, e.g. UIDNEXT and UIDVALIDITY.
101type CodeUint struct {
106func (c CodeUint) CodeString() string {
107 return fmt.Sprintf("%s %d", c.Code, c.Num)
110// "APPENDUID" response code.
111type CodeAppendUID struct {
116func (c CodeAppendUID) CodeString() string {
117 return fmt.Sprintf("APPENDUID %d %d", c.UIDValidity, c.UID)
120// "COPYUID" response code.
121type CodeCopyUID struct {
122 DestUIDValidity uint32
127func (c CodeCopyUID) CodeString() string {
128 str := func(l []NumRange) string {
130 for i, e := range l {
134 s += fmt.Sprintf("%d", e.First)
136 s += fmt.Sprintf(":%d", *e.Last)
141 return fmt.Sprintf("COPYUID %d %s %s", c.DestUIDValidity, str(c.From), str(c.To))
145type CodeModified NumSet
147func (c CodeModified) CodeString() string {
148 return fmt.Sprintf("MODIFIED %s", NumSet(c).String())
152type CodeHighestModSeq int64
154func (c CodeHighestModSeq) CodeString() string {
155 return fmt.Sprintf("HIGHESTMODSEQ %d", c)
158// RespText represents a response line minus the leading tag.
159type RespText struct {
160 Code string // The first word between [] after the status.
161 CodeArg CodeArg // Set if code has a parameter.
162 More string // Any remaining text.
166func astring(s string) string {
170 for _, c := range s {
171 if c <= ' ' || c >= 0x7f || c == '(' || c == ')' || c == '{' || c == '%' || c == '*' || c == '"' || c == '\\' {
178// imap "string", i.e. double-quoted string or syncliteral.
179func stringx(s string) string {
181 for _, c := range s {
182 if c == '\x00' || c == '\r' || c == '\n' {
183 return syncliteral(s)
185 if c == '\\' || c == '"' {
194// sync literal, i.e. {<num>}\r\n<num bytes>.
195func syncliteral(s string) string {
196 return fmt.Sprintf("{%d}\r\n", len(s)) + s
199// Untagged is a parsed untagged response. See types starting with Untagged.
200// todo: make an interface that the untagged responses implement?
203type UntaggedBye RespText
204type UntaggedPreauth RespText
205type UntaggedExpunge uint32
206type UntaggedExists uint32
207type UntaggedRecent uint32
208type UntaggedCapability []string
209type UntaggedEnabled []string
210type UntaggedResult Result
211type UntaggedFlags []string
212type UntaggedList struct {
215 Separator byte // 0 for NIL
217 Extended []MboxListExtendedItem
218 OldName string // If present, taken out of Extended.
220type UntaggedFetch struct {
224type UntaggedSearch []uint32
227type UntaggedSearchModSeq struct {
231type UntaggedStatus struct {
233 Attrs map[StatusAttr]int64 // Upper case status attributes.
237type UntaggedMetadataKeys struct {
238 Mailbox string // Empty means not specific to mailbox.
240 // Keys that have changed. To get values (or determine absence), the server must be
245type Annotation struct {
252type UntaggedMetadataAnnotations struct {
253 Mailbox string // Empty means not specific to mailbox.
254 Annotations []Annotation
258type StatusAttr string
261 StatusMessages StatusAttr = "MESSAGES"
262 StatusUIDNext StatusAttr = "UIDNEXT"
263 StatusUIDValidity StatusAttr = "UIDVALIDITY"
264 StatusUnseen StatusAttr = "UNSEEN"
265 StatusDeleted StatusAttr = "DELETED"
266 StatusSize StatusAttr = "SIZE"
267 StatusRecent StatusAttr = "RECENT"
268 StatusAppendLimit StatusAttr = "APPENDLIMIT"
269 StatusHighestModSeq StatusAttr = "HIGHESTMODSEQ"
270 StatusDeletedStorage StatusAttr = "DELETED-STORAGE"
273type UntaggedNamespace struct {
274 Personal, Other, Shared []NamespaceDescr
276type UntaggedLsub struct {
283// Fields are optional and zero if absent.
284type UntaggedEsearch struct {
293 Exts []EsearchDataExt
296// UntaggedVanished is used in QRESYNC to send UIDs that have been removed.
297type UntaggedVanished struct {
302// UntaggedQuotaroot lists the roots for which quota can be present.
303type UntaggedQuotaroot []string
305// UntaggedQuota holds the quota for a quota root.
306type UntaggedQuota struct {
309 // Always has at least one. Any QUOTA=RES-* capability not mentioned has no limit
310 // or this quota root.
311 Resources []QuotaResource
316// QuotaResourceName is the name of a resource type. More can be defined in the
317// future and encountered in the wild. Always in upper case.
318type QuotaResourceName string
321 QuotaResourceStorage = "STORAGE"
322 QuotaResourceMesssage = "MESSAGE"
323 QuotaResourceMailbox = "MAILBOX"
324 QuotaResourceAnnotationStorage = "ANNOTATION-STORAGE"
327type QuotaResource struct {
328 Name QuotaResourceName
329 Usage int64 // Currently in use. Count or disk size in 1024 byte blocks.
330 Limit int64 // Maximum allowed usage.
335type UntaggedID map[string]string
337// Extended data in an ESEARCH response.
338type EsearchDataExt struct {
343type NamespaceDescr struct {
346 Separator byte // If 0 then separator was absent.
347 Exts []NamespaceExtension
350type NamespaceExtension struct {
356// FetchAttr represents a FETCH response attribute.
357type FetchAttr interface {
358 Attr() string // Name of attribute.
362 SearchResult bool // True if "$", in which case Ranges is irrelevant.
366func (ns NumSet) IsZero() bool {
367 return !ns.SearchResult && ns.Ranges == nil
370func (ns NumSet) String() string {
375 for i, x := range ns.Ranges {
384func ParseNumSet(s string) (ns NumSet, rerr error) {
385 c := Conn{br: bufio.NewReader(strings.NewReader(s))}
386 defer c.recover(&rerr)
387 ns = c.xsequenceSet()
391// NumRange is a single number or range.
392type NumRange struct {
393 First uint32 // 0 for "*".
394 Last *uint32 // Nil if absent, 0 for "*".
397func (nr NumRange) String() string {
402 r += fmt.Sprintf("%d", nr.First)
412 r += fmt.Sprintf("%d", v)
417type TaggedExtComp struct {
419 Comps []TaggedExtComp // Used for both space-separated and ().
422type TaggedExtVal struct {
426 Comp *TaggedExtComp // If SimpleNumber and SimpleSeqSet is nil, this is a Comp. But Comp is optional and can also be nil. Not great.
429type MboxListExtendedItem struct {
435// "FLAGS" fetch response.
436type FetchFlags []string
438func (f FetchFlags) Attr() string { return "FLAGS" }
440// "ENVELOPE" fetch response.
441type FetchEnvelope Envelope
443func (f FetchEnvelope) Attr() string { return "ENVELOPE" }
445// Envelope holds the basic email message fields.
446type Envelope struct {
449 From, Sender, ReplyTo, To, CC, BCC []Address
450 InReplyTo, MessageID string
453// Address is an address field in an email message, e.g. To.
455 Name, Adl, Mailbox, Host string
458// "INTERNALDATE" fetch response.
459type FetchInternalDate struct {
463func (f FetchInternalDate) Attr() string { return "INTERNALDATE" }
466type FetchSaveDate struct {
467 SaveDate *time.Time // nil means absent for message.
470func (f FetchSaveDate) Attr() string { return "SAVEDATE" }
472// "RFC822.SIZE" fetch response.
473type FetchRFC822Size int64
475func (f FetchRFC822Size) Attr() string { return "RFC822.SIZE" }
477// "RFC822" fetch response.
478type FetchRFC822 string
480func (f FetchRFC822) Attr() string { return "RFC822" }
482// "RFC822.HEADER" fetch response.
483type FetchRFC822Header string
485func (f FetchRFC822Header) Attr() string { return "RFC822.HEADER" }
487// "RFC82.TEXT" fetch response.
488type FetchRFC822Text string
490func (f FetchRFC822Text) Attr() string { return "RFC822.TEXT" }
492// "BODYSTRUCTURE" fetch response.
493type FetchBodystructure struct {
496 Body any // BodyType*
499func (f FetchBodystructure) Attr() string { return f.RespAttr }
501// "BODY" fetch response.
502type FetchBody struct {
510func (f FetchBody) Attr() string { return f.RespAttr }
512// BodyFields is part of a FETCH BODY[] response.
513type BodyFields struct {
515 ContentID, ContentDescr, CTE string
519// BodyTypeMpart represents the body structure a multipart message, with
520// subparts and the multipart media subtype. Used in a FETCH response.
521type BodyTypeMpart struct {
523 Bodies []any // BodyTypeBasic, BodyTypeMsg, BodyTypeText
525 Ext *BodyExtensionMpart
528// BodyTypeBasic represents basic information about a part, used in a FETCH
530type BodyTypeBasic struct {
532 MediaType, MediaSubtype string
533 BodyFields BodyFields
534 Ext *BodyExtension1Part
537// BodyTypeMsg represents an email message as a body structure, used in a FETCH
539type BodyTypeMsg struct {
541 MediaType, MediaSubtype string
542 BodyFields BodyFields
544 Bodystructure any // One of the BodyType*
546 Ext *BodyExtension1Part
549// BodyTypeText represents a text part as a body structure, used in a FETCH
551type BodyTypeText struct {
553 MediaType, MediaSubtype string
554 BodyFields BodyFields
556 Ext *BodyExtension1Part
559// BodyExtension1Part has the extensible form fields of a BODYSTRUCTURE for
561type BodyExtensionMpart struct {
565 DispositionParams [][2]string
571// BodyExtension1Part has the extensible form fields of a BODYSTRUCTURE for
573type BodyExtension1Part struct {
577 DispositionParams [][2]string
583// BodyExtension has the additional extension fields for future expansion of
585type BodyExtension struct {
591// "BINARY" fetch response.
592type FetchBinary struct {
594 Parts []uint32 // Can be nil.
598func (f FetchBinary) Attr() string { return f.RespAttr }
600// "BINARY.SIZE" fetch response.
601type FetchBinarySize struct {
607func (f FetchBinarySize) Attr() string { return f.RespAttr }
609// "UID" fetch response.
612func (f FetchUID) Attr() string { return "UID" }
614// "MODSEQ" fetch response.
615type FetchModSeq int64
617func (f FetchModSeq) Attr() string { return "MODSEQ" }