| 1 |
package imap |
| 2 |
|
| 3 |
import ( |
| 4 |
"slices" |
| 5 |
"sync" |
| 6 |
|
| 7 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 8 |
) |
| 9 |
|
| 10 |
type SeqMap struct { |
| 11 |
lock sync.Mutex |
| 12 |
// map of IMAP sequence numbers to message UIDs |
| 13 |
m []uint32 |
| 14 |
} |
| 15 |
|
| 16 |
// Initialize sets the initial seqmap of the mailbox |
| 17 |
func (s *SeqMap) Initialize(uids []uint32) { |
| 18 |
s.lock.Lock() |
| 19 |
s.m = make([]uint32, len(uids)) |
| 20 |
copy(s.m, uids) |
| 21 |
s.sort() |
| 22 |
s.lock.Unlock() |
| 23 |
} |
| 24 |
|
| 25 |
func (s *SeqMap) Size() int { |
| 26 |
s.lock.Lock() |
| 27 |
size := len(s.m) |
| 28 |
s.lock.Unlock() |
| 29 |
return size |
| 30 |
} |
| 31 |
|
| 32 |
// Get returns the UID of the given seqnum |
| 33 |
func (s *SeqMap) Get(seqnum uint32) (uint32, bool) { |
| 34 |
if int(seqnum) > s.Size() || seqnum < 1 { |
| 35 |
return 0, false |
| 36 |
} |
| 37 |
s.lock.Lock() |
| 38 |
uid := s.m[seqnum-1] |
| 39 |
s.lock.Unlock() |
| 40 |
return uid, true |
| 41 |
} |
| 42 |
|
| 43 |
// Put adds a UID to the slice. Put should only be used to add new messages |
| 44 |
// into the slice |
| 45 |
func (s *SeqMap) Put(uid uint32) { |
| 46 |
s.lock.Lock() |
| 47 |
if slices.Contains(s.m, uid) { |
| 48 |
// We already have this UID, don't insert it. |
| 49 |
s.lock.Unlock() |
| 50 |
return |
| 51 |
} |
| 52 |
s.m = append(s.m, uid) |
| 53 |
s.sort() |
| 54 |
s.lock.Unlock() |
| 55 |
} |
| 56 |
|
| 57 |
// Take a snapshot of the SequenceNumber=>UID mappings for the given UIDs, |
| 58 |
// remove those UIDs from the SeqMap, and return the snapshot it to the caller, |
| 59 |
// as well as the loweest sequence number it contains. |
| 60 |
func (s *SeqMap) Snapshot(uids []uint32) (map[uint32]uint32, uint32) { |
| 61 |
// Take the snapshot. |
| 62 |
snapshot := make(map[uint32]uint32) |
| 63 |
var minSequenceNum uint32 = 0 |
| 64 |
var snapshotSeqNums []uint32 |
| 65 |
s.lock.Lock() |
| 66 |
for num, uid := range s.m { |
| 67 |
if slices.Contains(uids, uid) { |
| 68 |
// IMAP sequence numbers start at 1 |
| 69 |
seqNum := uint32(num) + 1 |
| 70 |
snapshotSeqNums = append(snapshotSeqNums, seqNum) |
| 71 |
if minSequenceNum == 0 { |
| 72 |
minSequenceNum = seqNum |
| 73 |
} |
| 74 |
snapshot[seqNum] = uid |
| 75 |
} |
| 76 |
} |
| 77 |
s.lock.Unlock() |
| 78 |
|
| 79 |
// Remove the snapshotted mappings from the sequence; we need to do it from |
| 80 |
// the highest to the lowest key since a SeqMap.Pop moves all the items on |
| 81 |
// the right of the popped sequence number by one position to the left. |
| 82 |
for i := len(snapshotSeqNums) - 1; i >= 0; i-- { |
| 83 |
_, ok := s.Pop(snapshotSeqNums[i]) |
| 84 |
if !ok { |
| 85 |
log.Errorf("Unable to pop %d from SeqMap", snapshotSeqNums[i]) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return snapshot, minSequenceNum |
| 90 |
} |
| 91 |
|
| 92 |
// Pop removes seqnum from the SeqMap. seqnum must be a valid seqnum, ie |
| 93 |
// [1:size of mailbox] |
| 94 |
func (s *SeqMap) Pop(seqnum uint32) (uint32, bool) { |
| 95 |
s.lock.Lock() |
| 96 |
defer s.lock.Unlock() |
| 97 |
if int(seqnum) > len(s.m) || seqnum < 1 { |
| 98 |
return 0, false |
| 99 |
} |
| 100 |
uid := s.m[seqnum-1] |
| 101 |
s.m = append(s.m[:seqnum-1], s.m[seqnum:]...) |
| 102 |
return uid, true |
| 103 |
} |
| 104 |
|
| 105 |
// sort sorts the slice in ascending UID order. See: |
| 106 |
// https://datatracker.ietf.org/doc/html/rfc3501#section-2.3.1.2 |
| 107 |
func (s *SeqMap) sort() { |
| 108 |
// Always be sure the SeqMap is sorted |
| 109 |
slices.Sort(s.m) |
| 110 |
} |