/
sequpdate.go
260 lines (219 loc) · 5.83 KB
/
sequpdate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package mess
import (
"strconv"
"sync"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/backend"
)
type Manager struct {
handlesLock sync.RWMutex
handles map[interface{}]*sharedHandle
sink chan<- Update
ExternalSubscribe func(key interface{})
ExternalUnsubscribe func(key interface{})
}
func NewManager() *Manager {
return &Manager{
handles: make(map[interface{}]*sharedHandle),
}
}
type Mailbox interface {
backend.Mailbox
Conn() backend.Conn
}
// ManagementHandle initializes a new message handle for the mailbox that
// is opened without an active connection, e.g. from an administrative UI or
// CLI utility.
//
// Such handle never sends updates to mbox.Conn(), only to SetExternalSink if
// set. \Recent flag for new messages will never be shown to such connections
// and it will receive no updates for mailbox changes anyway (Idle, Sync are no-op).
func (m *Manager) ManagementHandle(key interface{}, uids []uint32, recents *imap.SeqSet) *MailboxHandle {
return &MailboxHandle{
m: m,
key: key,
recent: recents,
uidMap: uids,
}
}
// Mailbox initializes a new message handle for the mailbox.
//
// key should be a server-global unique identifier for the mailbox.
// uids should contain the list of all message UIDs existing in the mailbox.
//
// recents should contain the list of message UIDs with persistent \Recent flag.
// Note that persistent \Recent should be unset once passed to Mailbox().
// In particular, two subsequent calls should not receive the same value.
func (m *Manager) Mailbox(key interface{}, mbox Mailbox, uids []uint32, recents *imap.SeqSet) (*MailboxHandle, error) {
m.handlesLock.Lock()
defer m.handlesLock.Unlock()
sharedHndl, ok := m.handles[key]
if sharedHndl == nil {
sharedHndl = &sharedHandle{
key: key,
handles: map[*MailboxHandle]struct{}{},
}
}
handle := &MailboxHandle{
m: m,
key: key,
shared: sharedHndl,
conn: mbox.Conn(),
uidMap: uids,
recent: recents,
pendingFlags: make([]flagsUpdate, 0, 1),
}
for _, set := range recents.Set {
for i := set.Start; i <= set.Stop; i++ {
handle.recentCount++
}
}
sharedHndl.handlesLock.Lock()
sharedHndl.handles[handle] = struct{}{}
sharedHndl.handlesLock.Unlock()
if !ok {
m.handles[key] = sharedHndl
if m.ExternalSubscribe != nil {
m.ExternalSubscribe(key)
}
}
return handle, nil
}
// NewMessages performs necessary updates dispatching when
// new messages are added to the mailbox.
//
// Return value indicates whether backend should store
// a persistent \Recent flag in DB for further retrieval
// (see Mailbox)
func (m *Manager) NewMessages(key interface{}, uid imap.SeqSet) (storeRecent bool) {
if m.sink != nil {
m.sink <- Update{
Type: UpdNewMessage,
Key: key,
SeqSet: uid.String(),
}
}
return m.newMessages(key, uid)
}
func (m *Manager) newMessages(key interface{}, uid imap.SeqSet) (storeRecent bool) {
m.handlesLock.RLock()
defer m.handlesLock.RUnlock()
handle := m.handles[key]
if handle == nil {
return false
}
handle.handlesLock.RLock()
defer handle.handlesLock.RUnlock()
addedRecent := false
for hndl := range handle.handles {
hndl.lock.Lock()
hndl.pendingCreated.AddSet(&uid)
if !addedRecent {
hndl.recent.AddSet(&uid)
for _, set := range uid.Set {
for i := set.Start; i <= set.Stop; i++ {
hndl.recentCount++
}
}
hndl.hasNewRecent = true
addedRecent = true
}
hndl.idleUpdate()
hndl.lock.Unlock()
}
return !addedRecent
}
func (m *Manager) NewMessage(key interface{}, uid uint32) (storeRecent bool) {
if m.sink != nil {
m.sink <- Update{
Type: UpdNewMessage,
Key: key,
SeqSet: strconv.FormatUint(uint64(uid), 10),
}
}
m.handlesLock.RLock()
defer m.handlesLock.RUnlock()
handle := m.handles[key]
if handle == nil {
return true
}
handle.handlesLock.RLock()
defer handle.handlesLock.RUnlock()
addedRecent := false
for hndl := range handle.handles {
hndl.lock.Lock()
hndl.pendingCreated.AddNum(uid)
if !addedRecent {
hndl.recent.AddNum(uid)
hndl.hasNewRecent = true
hndl.recentCount++
addedRecent = true
}
hndl.idleUpdate()
hndl.lock.Unlock()
}
return !addedRecent
}
// MailboxDestroyed should be called when the specified key is no longer
// valid for the mailbox e.g. because it was renamed or deleted.
//
// The appropriate place to call the method from is
// DeleteMailbox - MailboxDestroyed should be called
// for all removed mailboxes - and RenameMailbox where
// it should be called for _both_ source and target mailbox.
//
// In all cases it is better to call MailboxDestroyed _after_
// physically deleting the mailbox.
func (m *Manager) MailboxDestroyed(key interface{}) {
if m.sink != nil {
m.sink <- Update{
Type: UpdMboxDestroyed,
Key: key,
}
}
m.mailboxDestroyed(key)
}
func (m *Manager) mailboxDestroyed(key interface{}) {
m.handlesLock.RLock()
defer m.handlesLock.RUnlock()
handle := m.handles[key]
if handle == nil {
return
}
handle.handlesLock.Lock()
handle.handles = nil
handle.handlesLock.Unlock()
delete(m.handles, key)
if m.ExternalUnsubscribe != nil {
m.ExternalUnsubscribe(key)
}
}
func (m *Manager) removedSet(key interface{}, seq imap.SeqSet) {
m.handlesLock.RLock()
defer m.handlesLock.RUnlock()
handle := m.handles[key]
if handle == nil {
return
}
handle.handlesLock.RLock()
defer handle.handlesLock.RUnlock()
for hndl := range handle.handles {
hndl.lock.Lock()
hndl.pendingExpunge.AddSet(&seq)
hndl.idleUpdate()
hndl.lock.Unlock()
}
}
func (m *Manager) flagsChanged(key interface{}, uid uint32, newFlags []string) {
m.handlesLock.RLock()
defer m.handlesLock.RUnlock()
handle := m.handles[key]
if handle == nil {
return
}
handle.handlesLock.RLock()
defer handle.handlesLock.RUnlock()
for hndl := range handle.handles {
hndl.enqueueFlagsUpdate(uid, newFlags)
}
}