duesee / imap-codec

Rock-solid and complete codec for IMAP
Apache License 2.0
37 stars 14 forks source link

feat: Implement `SORT` and `THREAD=...`. #321

Closed duesee closed 6 months ago

duesee commented 1 year ago

See https://datatracker.ietf.org/doc/html/rfc5256.

(This would potentially be very useful for Himalaya.)

duesee commented 1 year ago

Both

search-criteria = charset 1*(SP search-key)

Sort

Capability (not formally mentioned in RFC)

capability =/ "SORT"
; From RFC 5957
capability =/ "SORT=DISPLAY"

Command

sort            = ["UID" SP] "SORT" SP sort-criteria SP search-criteria

sort-criteria   = "(" sort-criterion *(SP sort-criterion) ")"

sort-criterion  = ["REVERSE" SP] sort-key

sort-key        = "ARRIVAL" / "CC" / "DATE" / "FROM" / "SIZE" / "SUBJECT" / "TO"

; From RFC 5957
sort-key        =/ "DISPLAYFROM" / "DISPLAYTO"

Response

sort-data       = "SORT" *(SP nz-number)

Thread

Capability (not formally mentioned in RFC)

capability =/ "THREAD=" thread-alg

Command

thread          = ["UID" SP] "THREAD" SP thread-alg SP search-criteria

thread-alg      = "ORDEREDSUBJECT" / "REFERENCES" / thread-alg-ext

thread-alg-ext  = atom

Response

thread-data     = "THREAD" [SP 1*thread-list]

thread-list     = "(" (thread-members / thread-nested) ")"

thread-members  = nz-number *(SP nz-number) [SP thread-nested]

thread-nested   = 2*thread-list

Capability (not mentioned)

capability =/ "SORT"
; FROM RFC 5957
capability =/ "SORT=DISPLAY"
sort-key =/ "DISPLAYFROM" / "DISPLAYTO"

capability =/ "THREAD=" thread-alg

Base subject extraction rules (2)-(6):

subject         = *subj-leader [subj-middle] *subj-trailer

subj-leader     = (*subj-blob subj-refwd) / WSP

subj-blob       = "[" *BLOBCHAR "]" *WSP

; Any CHAR8 except '[' and ']'. SHOULD comply with [UTF-8].
BLOBCHAR        = %x01-5a / %x5c / %x5e-ff

subj-refwd      = ("re" / ("fw" ["d"])) *WSP [subj-blob] ":"

; Last subj-blob is subj-base if subj-base would otherwise be empty.
subj-middle     = *subj-blob (subj-base / subj-fwd)

; Can be a subj-blob.
subj-base       = NONWSP *(*WSP NONWSP)

; Any CHAR8 other than WSP. SHOULD comply with [UTF-8].
NONWSP          = %x01-08 / %x0a-1f / %x21-ff

subj-fwd        = subj-fwd-hdr subject subj-fwd-trl

subj-fwd-hdr    = "[fwd:"

subj-fwd-trl    = "]"

subj-trailer    = "(fwd)" / WSP