forked from emersion/go-imap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_selected.go
303 lines (245 loc) · 5.84 KB
/
cmd_selected.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package server
import (
"errors"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/commands"
"github.com/emersion/go-imap/responses"
)
// imap errors in Selected state.
var (
ErrNoMailboxSelected = errors.New("No mailbox selected")
ErrMailboxReadOnly = errors.New("Mailbox opened in read-only mode")
)
// A command handler that supports UIDs.
type UidHandler interface {
Handler
// Handle this command using UIDs for a given connection.
UidHandle(conn Conn) error
}
type Check struct {
commands.Check
}
func (cmd *Check) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.Mailbox == nil {
return ErrNoMailboxSelected
}
if ctx.MailboxReadOnly {
return ErrMailboxReadOnly
}
return ctx.Mailbox.Check()
}
type Close struct {
commands.Close
}
func (cmd *Close) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.Mailbox == nil {
return ErrNoMailboxSelected
}
mailbox := ctx.Mailbox
ctx.Mailbox = nil
ctx.MailboxReadOnly = false
// No need to send expunge updates here, since the mailbox is already unselected
return mailbox.Expunge()
}
type Expunge struct {
commands.Expunge
}
func (cmd *Expunge) Handle(conn Conn) error {
ctx := conn.Context()
if ctx.Mailbox == nil {
return ErrNoMailboxSelected
}
if ctx.MailboxReadOnly {
return ErrMailboxReadOnly
}
// Get a list of messages that will be deleted
// That will allow us to send expunge updates if the backend doesn't support it
var seqnums []uint32
if conn.Server().Updates == nil {
criteria := &imap.SearchCriteria{
WithFlags: []string{imap.DeletedFlag},
}
var err error
seqnums, err = ctx.Mailbox.SearchMessages(false, criteria)
if err != nil {
return err
}
}
if err := ctx.Mailbox.Expunge(); err != nil {
return err
}
// If the backend doesn't support expunge updates, let's do it ourselves
if conn.Server().Updates == nil {
done := make(chan error)
defer close(done)
ch := make(chan uint32)
res := &responses.Expunge{SeqNums: ch}
go (func() {
done <- conn.WriteResp(res)
})()
// Iterate sequence numbers from the last one to the first one, as deleting
// messages changes their respective numbers
for i := len(seqnums) - 1; i >= 0; i-- {
ch <- seqnums[i]
}
close(ch)
if err := <-done; err != nil {
return err
}
}
return nil
}
type Search struct {
commands.Search
}
func (cmd *Search) handle(uid bool, conn Conn) error {
ctx := conn.Context()
if ctx.Mailbox == nil {
return ErrNoMailboxSelected
}
ids, err := ctx.Mailbox.SearchMessages(uid, cmd.Criteria)
if err != nil {
return err
}
res := &responses.Search{Ids: ids}
return conn.WriteResp(res)
}
func (cmd *Search) Handle(conn Conn) error {
return cmd.handle(false, conn)
}
func (cmd *Search) UidHandle(conn Conn) error {
return cmd.handle(true, conn)
}
type Fetch struct {
commands.Fetch
}
func (cmd *Fetch) handle(uid bool, conn Conn) error {
ctx := conn.Context()
if ctx.Mailbox == nil {
return ErrNoMailboxSelected
}
ch := make(chan *imap.Message)
res := &responses.Fetch{Messages: ch}
done := make(chan error, 1)
go (func() {
done <- conn.WriteResp(res)
})()
err := ctx.Mailbox.ListMessages(uid, cmd.SeqSet, cmd.Items, ch)
if err != nil {
return err
}
return <-done
}
func (cmd *Fetch) Handle(conn Conn) error {
return cmd.handle(false, conn)
}
func (cmd *Fetch) UidHandle(conn Conn) error {
// Append UID to the list of requested items if it isn't already present
hasUid := false
for _, item := range cmd.Items {
if item == "UID" {
hasUid = true
break
}
}
if !hasUid {
cmd.Items = append(cmd.Items, "UID")
}
return cmd.handle(true, conn)
}
type Store struct {
commands.Store
}
func (cmd *Store) handle(uid bool, conn Conn) error {
ctx := conn.Context()
if ctx.Mailbox == nil {
return ErrNoMailboxSelected
}
if ctx.MailboxReadOnly {
return ErrMailboxReadOnly
}
// Only flags operations are supported
op, silent, err := imap.ParseFlagsOp(cmd.Item)
if err != nil {
return err
}
flagsList, ok := cmd.Value.([]interface{})
if !ok {
return errors.New("Flags must be a list")
}
flags, err := imap.ParseStringList(flagsList)
if err != nil {
return err
}
for i, flag := range flags {
flags[i] = imap.CanonicalFlag(flag)
}
// If the backend supports message updates, this will prevent this connection
// from receiving them
// TODO: find a better way to do this, without conn.silent
*conn.silent() = silent
err = ctx.Mailbox.UpdateMessagesFlags(uid, cmd.SeqSet, op, flags)
*conn.silent() = false
if err != nil {
return err
}
// Not silent: send FETCH updates if the backend doesn't support message
// updates
if conn.Server().Updates == nil && !silent {
inner := &Fetch{}
inner.SeqSet = cmd.SeqSet
inner.Items = []imap.FetchItem{imap.FetchFlags}
if uid {
inner.Items = append(inner.Items, "UID")
}
if err := inner.handle(uid, conn); err != nil {
return err
}
}
return nil
}
func (cmd *Store) Handle(conn Conn) error {
return cmd.handle(false, conn)
}
func (cmd *Store) UidHandle(conn Conn) error {
return cmd.handle(true, conn)
}
type Copy struct {
commands.Copy
}
func (cmd *Copy) handle(uid bool, conn Conn) error {
ctx := conn.Context()
if ctx.Mailbox == nil {
return ErrNoMailboxSelected
}
return ctx.Mailbox.CopyMessages(uid, cmd.SeqSet, cmd.Mailbox)
}
func (cmd *Copy) Handle(conn Conn) error {
return cmd.handle(false, conn)
}
func (cmd *Copy) UidHandle(conn Conn) error {
return cmd.handle(true, conn)
}
type Uid struct {
commands.Uid
}
func (cmd *Uid) Handle(conn Conn) error {
inner := cmd.Cmd.Command()
hdlr, err := conn.commandHandler(inner)
if err != nil {
return err
}
uidHdlr, ok := hdlr.(UidHandler)
if !ok {
return errors.New("Command unsupported with UID")
}
if err := uidHdlr.UidHandle(conn); err != nil {
return err
}
return ErrStatusResp(&imap.StatusResp{
Type: imap.StatusRespOk,
Info: "UID " + inner.Name + " completed",
})
}