7 "github.com/mjl-/mox/store"
11 searchResult bool // "$"
17 last *setNumber // if nil, this numRange is just a setNumber in "first" and first.star will be false
20type setNumber struct {
25// containsSeq returns whether seq is in the numSet, given uids and (saved) searchResult.
26// uids and searchResult must be sorted. searchResult can have uids that are no longer in uids.
27func (ss numSet) containsSeq(seq msgseq, uids []store.UID, searchResult []store.UID) bool {
32 uid := uids[int(seq)-1]
33 return uidSearch(searchResult, uid) > 0 && uidSearch(uids, uid) > 0
35 return ss.containsSeqCount(seq, uint32(len(uids)))
38// containsSeqCount returns whether seq is contained in ss, which must not be a
39// searchResult, assuming the message count.
40func (ss numSet) containsSeqCount(seq msgseq, msgCount uint32) bool {
44 for _, r := range ss.ranges {
45 first := r.first.number
46 if r.first.star || first > msgCount {
53 if r.last.star || last > msgCount {
58 first, last = last, first
61 if uint32(seq) >= first && uint32(seq) <= last {
68// containsKnownUID returns whether uid, which is known to exist, matches the numSet.
69// highestUID must return the highest/last UID in the mailbox, or an error. A last UID must
70// exist, otherwise this method wouldn't have been called with a known uid.
71// highestUID is needed for interpreting UID sets like "<num>:*" where num is
72// higher than the uid to check.
73func (ss numSet) xcontainsKnownUID(uid store.UID, searchResult []store.UID, xhighestUID func() store.UID) bool {
75 return uidSearch(searchResult, uid) > 0
78 for _, r := range ss.ranges {
79 a := store.UID(r.first.number)
80 // Num in <num>:* can be larger than last, but it still matches the last...
83 if r.last != nil && uid >= store.UID(r.last.number) {
90 b = store.UID(r.last.number)
101 if uid >= a && uid <= b {
108// xinterpretStar returns a numset that interprets stars in a uid set using
109// xlastUID, returning a new uid set without stars, with increasing first/last, and
110// without unneeded ranges (first.number != last.number).
111// If there are no messages in the mailbox, xlastUID must return zero and the
112// returned numSet will include 0.
113func (s numSet) xinterpretStar(xlastUID func() store.UID) numSet {
116 for _, r := range s.ranges {
117 first := r.first.number
119 first = uint32(xlastUID())
124 last = uint32(xlastUID())
130 first, last = last, first
132 nr := numRange{first: setNumber{number: first}}
134 nr.last = &setNumber{number: last}
136 ns.ranges = append(ns.ranges, nr)
141// contains returns whether the numset contains the number.
142// only allowed on basic, strictly increasing numsets.
143func (ss numSet) contains(v uint32) bool {
144 for _, r := range ss.ranges {
145 if r.first.number == v || r.last != nil && v > r.first.number && v <= r.last.number {
152func (ss numSet) empty() bool {
153 return !ss.searchResult && len(ss.ranges) == 0
156// Strings returns the numset in zero or more strings of maxSize bytes. If
157// maxSize is <= 0, a single string is returned.
158func (ss numSet) Strings(maxSize int) []string {
164 for _, r := range ss.ranges {
169 s += fmt.Sprintf("%d", r.first.number)
173 panic("invalid numSet range first star without last")
180 s += fmt.Sprintf("%d", r.last.number)
184 nsize := len(line) + len(s)
188 if maxSize > 0 && nsize > maxSize {
204func (ss numSet) String() string {
212// whether numSet only has numbers (no star/search), and is strictly increasing.
213func (s *numSet) isBasicIncreasing() bool {
218 for _, r := range s.ranges {
219 if r.first.star || r.first.number <= last || r.last != nil && (r.last.star || r.last.number < r.first.number) {
222 last = r.first.number
236// newIter must only be called on a numSet that is basic (no star/search) and ascending.
237func (s numSet) newIter() *numIter {
238 return &numIter{s: s}
241func (i *numIter) Next() (uint32, bool) {
242 if v, ok := i.r.Next(); ok {
245 if i.i >= len(i.s.ranges) {
248 i.r = i.s.ranges[i.i].newIter()
253type rangeIter struct {
258// newIter must only be called on a range in a numSet that is basic (no star/search) and ascending.
259func (r numRange) newIter() *rangeIter {
260 return &rangeIter{r: r, o: 0}
263func (r *rangeIter) Next() (uint32, bool) {
269 return r.r.first.number, true
271 if r.r.last == nil || r.r.first.number+uint32(r.o) > r.r.last.number {
274 v := r.r.first.number + uint32(r.o)
279// append adds a new number to the set, extending a range, or starting a new one (possibly the first).
280// can only be used on basic numsets, without star/searchResult.
281func (s *numSet) append(v uint32) {
282 if len(s.ranges) == 0 {
283 s.ranges = []numRange{{first: setNumber{number: v}}}
286 ri := len(s.ranges) - 1
288 if v == r.first.number+1 && r.last == nil {
289 s.ranges[ri].last = &setNumber{number: v}
290 } else if r.last != nil && v == r.last.number+1 {
293 s.ranges = append(s.ranges, numRange{first: setNumber{number: v}})
302type sectionPart struct {
307type sectionText struct {
308 mime bool // if "MIME"
309 msgtext *sectionMsgtext
312// a non-nil *sectionSpec with nil msgtext & nil part means there were []'s, but nothing inside. e.g. "BODY[]".
313type sectionSpec struct {
314 msgtext *sectionMsgtext
318type sectionMsgtext struct {
319 s string // "HEADER", "HEADER.FIELDS", "HEADER.FIELDS.NOT", "TEXT"
320 headers []string // for "HEADER.FIELDS"*
323type fetchAtt struct {
324 field string // uppercase, eg "ENVELOPE", "BODY". ".PEEK" is removed.
327 sectionBinary []uint32
329 previewLazy bool // Not regular "PREVIEW", but "PREVIEW (LAZY)".
332type searchKey struct {
333 // Only one of searchKeys, seqSet and op can be non-nil/non-empty.
334 searchKeys []searchKey // In case of nested/multiple keys. Also for the top-level command.
335 seqSet *numSet // In case of bare sequence set. For op UID, field uidSet contains the parameter.
336 op string // Determines which of the fields below are set.
344 searchKey2 *searchKey
349// Whether we need message sequence numbers to evaluate. Sequence numbers are not
350// allowed with UIDONLY. And if we need sequence numbers we cannot optimize
351// searching for MAX with a query in reverse order.
352func (sk *searchKey) hasSequenceNumbers() bool {
353 for _, k := range sk.searchKeys {
354 if k.hasSequenceNumbers() {
358 if sk.searchKey != nil && sk.searchKey.hasSequenceNumbers() || sk.searchKey2 != nil && sk.searchKey2.hasSequenceNumbers() {
361 return sk.seqSet != nil && !sk.seqSet.searchResult
364// hasModseq returns whether there is a modseq filter anywhere in the searchkey.
365func (sk *searchKey) hasModseq() bool {
366 if sk.clientModseq != nil {
369 for _, e := range sk.searchKeys {
374 if sk.searchKey != nil && sk.searchKey.hasModseq() {
377 if sk.searchKey2 != nil && sk.searchKey2.hasModseq() {
383func compactUIDSet(l []store.UID) (r numSet) {
386 for ; e < len(l) && l[e] == l[e-1]+1; e++ {
388 first := setNumber{number: uint32(l[0])}
391 last = &setNumber{number: uint32(l[e-1])}
393 r.ranges = append(r.ranges, numRange{first, last})