| 1 |
package imap |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
) |
| 6 |
|
| 7 |
// Provider dependent EXPUNGE handler |
| 8 |
// |
| 9 |
// To delete N items in a single command, aerc does the following: |
| 10 |
// 1. Set the \Deleted flag on those N items (identified by their sequence |
| 11 |
// number) |
| 12 |
// 2. Call the EXPUNGE command |
| 13 |
// It then gets N ExpungeData messages from go-imap, that reference the |
| 14 |
// individual message actually deleted. |
| 15 |
// Unfortunately the IMAP RFC does not specify the order in which those |
| 16 |
// messages should be sent, and different providers have different policies. |
| 17 |
// In particular: |
| 18 |
// - GMail and FastMail delete messages by increasing sequence number, and |
| 19 |
// at each individual delete, decrement the sequence number of all the |
| 20 |
// messages that still need to be deleted. |
| 21 |
// - Office 365 deletes messages by decreasing sequence number. |
| 22 |
// - Dovecot deletes messages in a seemingly random order. |
| 23 |
// The role of ExpungeHandler is to abstract out those differences, and |
| 24 |
// automatically adapt to the IMAP server's behaviour. |
| 25 |
// Since there's a non-zero probability that the automatic detection is wrong |
| 26 |
// if the server deletes items in a random order, it's also possible to |
| 27 |
// statically configure the expunge policy in accounts.conf. |
| 28 |
|
| 29 |
// The IMAP server behaviour when deleting multiple messages. |
| 30 |
const ( |
| 31 |
// Automatically detect behaviour from the first reply. |
| 32 |
ExpungePolicyAuto = iota |
| 33 |
// The server deletes message in increasing sequence number. After each |
| 34 |
// delete, outstanding messages need to have their sequence numbers |
| 35 |
// decremented. |
| 36 |
ExpungePolicyLowToHigh |
| 37 |
// The server deletes messages in any order, but does not change any of the |
| 38 |
// sequence numbers. |
| 39 |
ExpungePolicyStable |
| 40 |
) |
| 41 |
|
| 42 |
type ExpungeHandler struct { |
| 43 |
lock sync.Mutex |
| 44 |
worker *IMAPWorker |
| 45 |
policy int |
| 46 |
items map[uint32]uint32 |
| 47 |
minNum uint32 |
| 48 |
forDelete bool |
| 49 |
gotFirst bool |
| 50 |
} |
| 51 |
|
| 52 |
// Create a new ExpungeHandler for a list of UIDs that are being deleted or |
| 53 |
// moved. |
| 54 |
func NewExpungeHandler(worker *IMAPWorker, uids []uint32, forDelete bool) *ExpungeHandler { |
| 55 |
snapshot, min := worker.seqMap.Snapshot(uids) |
| 56 |
return &ExpungeHandler{ |
| 57 |
worker: worker, |
| 58 |
policy: worker.config.expungePolicy, |
| 59 |
items: snapshot, |
| 60 |
minNum: min, |
| 61 |
forDelete: forDelete, |
| 62 |
gotFirst: false, |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// Translate the sequence number received from the IMAP server into the |
| 67 |
// associated UID, deduce the policy used by the server from the first reply, |
| 68 |
// and update the remaining mappings according to that policy if required. |
| 69 |
func (h *ExpungeHandler) PopSequenceNumber(seqNum uint32) (uint32, bool) { |
| 70 |
h.lock.Lock() |
| 71 |
defer h.lock.Unlock() |
| 72 |
if !h.gotFirst { |
| 73 |
h.gotFirst = true |
| 74 |
logPrefix := "Configured" |
| 75 |
// This is the very first reply we get; use it to infer the IMAP |
| 76 |
// server's policy if the configuration asks us to. |
| 77 |
if h.policy == ExpungePolicyAuto { |
| 78 |
logPrefix = "Deduced" |
| 79 |
if seqNum == h.minNum { |
| 80 |
h.policy = ExpungePolicyLowToHigh |
| 81 |
} else { |
| 82 |
h.policy = ExpungePolicyStable |
| 83 |
} |
| 84 |
} |
| 85 |
switch h.policy { |
| 86 |
case ExpungePolicyLowToHigh: |
| 87 |
h.worker.worker.Debugf("%s expunge policy: low-to-high", logPrefix) |
| 88 |
case ExpungePolicyStable: |
| 89 |
h.worker.worker.Debugf("%s expunge policy: stable", logPrefix) |
| 90 |
} |
| 91 |
} |
| 92 |
// Resolve the UID from the sequence number and pop the expunger entry. |
| 93 |
uid, ok := h.items[seqNum] |
| 94 |
delete(h.items, seqNum) |
| 95 |
|
| 96 |
// If the server uses the "low to high" policy, we need to decrement all |
| 97 |
// the remaining entries since the server is doing the same on its end. |
| 98 |
if ok && h.policy == ExpungePolicyLowToHigh { |
| 99 |
newSeq := make(map[uint32]uint32) |
| 100 |
|
| 101 |
for s, uid := range h.items { |
| 102 |
newSeq[s-1] = uid |
| 103 |
} |
| 104 |
|
| 105 |
h.items = newSeq |
| 106 |
} |
| 107 |
|
| 108 |
if !ok { |
| 109 |
h.worker.worker.Errorf("Unexpected sequence number: %d; consider "+ |
| 110 |
"overriding the expunge-policy IMAP configuration", seqNum) |
| 111 |
} |
| 112 |
|
| 113 |
return uid, ok |
| 114 |
} |
| 115 |
|
| 116 |
func (h *ExpungeHandler) ForDelete() bool { |
| 117 |
return h.forDelete |
| 118 |
} |
| 119 |
|
| 120 |
func (h *ExpungeHandler) IsExpungingForDelete(uid uint32) bool { |
| 121 |
h.lock.Lock() |
| 122 |
defer h.lock.Unlock() |
| 123 |
if !h.forDelete { |
| 124 |
return false |
| 125 |
} |
| 126 |
for _, u := range h.items { |
| 127 |
if u == uid { |
| 128 |
return true |
| 129 |
} |
| 130 |
} |
| 131 |
return false |
| 132 |
} |