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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
package mboxer
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"sort"
"git.sr.ht/~rjarry/aerc/logging"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/handlers"
"git.sr.ht/~rjarry/aerc/worker/lib"
"git.sr.ht/~rjarry/aerc/worker/types"
gomessage "github.com/emersion/go-message"
)
func init() {
handlers.RegisterWorkerFactory("mbox", NewWorker)
}
var errUnsupported = fmt.Errorf("unsupported command")
type mboxWorker struct {
data *mailboxContainer
name string
folder *container
worker *types.Worker
}
func NewWorker(worker *types.Worker) (types.Backend, error) {
return &mboxWorker{
worker: worker,
}, nil
}
func (w *mboxWorker) handleMessage(msg types.WorkerMessage) error {
var reterr error // will be returned at the end, needed to support idle
switch msg := msg.(type) {
case *types.Unsupported:
// No-op
case *types.Configure:
u, err := url.Parse(msg.Config.Source)
if err != nil {
reterr = err
break
}
var dir string
if u.Host == "~" {
home, err := os.UserHomeDir()
if err != nil {
reterr = err
break
}
dir = filepath.Join(home, u.Path)
} else {
dir = filepath.Join(u.Host, u.Path)
}
w.data, err = createMailboxContainer(dir)
if err != nil || w.data == nil {
w.data = &mailboxContainer{
mailboxes: make(map[string]*container),
}
reterr = err
break
} else {
logging.Infof("configured with mbox file %s", dir)
}
case *types.Connect, *types.Reconnect, *types.Disconnect:
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
case *types.ListDirectories:
dirs := w.data.Names()
sort.Strings(dirs)
for _, name := range dirs {
w.worker.PostMessage(&types.Directory{
Message: types.RespondTo(msg),
Dir: &models.Directory{
Name: name,
Attributes: nil,
},
}, nil)
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.data.DirectoryInfo(name),
}, nil)
}
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
case *types.OpenDirectory:
w.name = msg.Directory
var ok bool
w.folder, ok = w.data.Mailbox(w.name)
if !ok {
w.folder = w.data.Create(w.name)
w.worker.PostMessage(&types.Done{
Message: types.RespondTo(&types.CreateDirectory{}),
}, nil)
}
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.data.DirectoryInfo(msg.Directory),
}, nil)
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
logging.Infof("%s opened", msg.Directory)
case *types.FetchDirectoryContents:
var infos []*models.MessageInfo
for _, uid := range w.folder.Uids() {
m, err := w.folder.Message(uid)
if err != nil {
logging.Errorf("could not get message %w", err)
continue
}
info, err := lib.MessageInfo(m)
if err != nil {
logging.Errorf("could not get message info %w", err)
continue
}
infos = append(infos, info)
}
uids, err := lib.Sort(infos, msg.SortCriteria)
if err != nil {
reterr = err
break
}
if len(uids) == 0 {
reterr = fmt.Errorf("mbox: no uids in directory")
break
}
w.worker.PostMessage(&types.DirectoryContents{
Message: types.RespondTo(msg),
Uids: uids,
}, nil)
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
case *types.FetchDirectoryThreaded:
reterr = errUnsupported
case *types.CreateDirectory:
w.data.Create(msg.Directory)
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
case *types.RemoveDirectory:
if err := w.data.Remove(msg.Directory); err != nil {
reterr = err
break
}
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
case *types.FetchMessageHeaders:
for _, uid := range msg.Uids {
m, err := w.folder.Message(uid)
if err != nil {
reterr = err
break
}
msgInfo, err := lib.MessageInfo(m)
if err != nil {
reterr = err
break
} else {
w.worker.PostMessage(&types.MessageInfo{
Message: types.RespondTo(msg),
Info: msgInfo,
}, nil)
}
}
w.worker.PostMessage(
&types.Done{Message: types.RespondTo(msg)}, nil)
case *types.FetchMessageBodyPart:
m, err := w.folder.Message(msg.Uid)
if err != nil {
logging.Errorf("could not get message %d: %w", msg.Uid, err)
reterr = err
break
}
contentReader, err := m.NewReader()
if err != nil {
reterr = fmt.Errorf("could not get message reader: %w", err)
break
}
fullMsg, err := gomessage.Read(contentReader)
if err != nil {
reterr = fmt.Errorf("could not read message: %w", err)
break
}
r, err := lib.FetchEntityPartReader(fullMsg, msg.Part)
if err != nil {
logging.Errorf(
"could not get body part reader for message=%d, parts=%#v: %w",
msg.Uid, msg.Part, err)
reterr = err
break
}
w.worker.PostMessage(&types.MessageBodyPart{
Message: types.RespondTo(msg),
Part: &models.MessageBodyPart{
Reader: r,
Uid: msg.Uid,
},
}, nil)
case *types.FetchFullMessages:
for _, uid := range msg.Uids {
m, err := w.folder.Message(uid)
if err != nil {
logging.Errorf("could not get message for uid %d: %w", uid, err)
continue
}
r, err := m.NewReader()
if err != nil {
logging.Errorf("could not get message reader: %w", err)
continue
}
defer r.Close()
b, err := ioutil.ReadAll(r)
if err != nil {
logging.Errorf("could not get message reader: %w", err)
continue
}
w.worker.PostMessage(&types.FullMessage{
Message: types.RespondTo(msg),
Content: &models.FullMessage{
Uid: uid,
Reader: bytes.NewReader(b),
},
}, nil)
}
w.worker.PostMessage(&types.Done{
Message: types.RespondTo(msg),
}, nil)
case *types.DeleteMessages:
deleted := w.folder.Delete(msg.Uids)
if len(deleted) > 0 {
w.worker.PostMessage(&types.MessagesDeleted{
Message: types.RespondTo(msg),
Uids: deleted,
}, nil)
}
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.data.DirectoryInfo(w.name),
}, nil)
w.worker.PostMessage(
&types.Done{Message: types.RespondTo(msg)}, nil)
case *types.FlagMessages:
for _, uid := range msg.Uids {
m, err := w.folder.Message(uid)
if err != nil {
logging.Errorf("could not get message: %w", err)
continue
}
if err := m.(*message).SetFlag(msg.Flag, msg.Enable); err != nil {
logging.Errorf("could change flag %v to %t on message: %w", msg.Flag, msg.Enable, err)
continue
}
info, err := lib.MessageInfo(m)
if err != nil {
logging.Errorf("could not get message info: %w", err)
continue
}
w.worker.PostMessage(&types.MessageInfo{
Message: types.RespondTo(msg),
Info: info,
}, nil)
}
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.data.DirectoryInfo(w.name),
}, nil)
w.worker.PostMessage(
&types.Done{Message: types.RespondTo(msg)}, nil)
case *types.CopyMessages:
err := w.data.Copy(msg.Destination, w.name, msg.Uids)
if err != nil {
reterr = err
break
}
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.data.DirectoryInfo(w.name),
}, nil)
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.data.DirectoryInfo(msg.Destination),
}, nil)
w.worker.PostMessage(
&types.Done{Message: types.RespondTo(msg)}, nil)
case *types.SearchDirectory:
criteria, err := lib.GetSearchCriteria(msg.Argv)
if err != nil {
reterr = err
break
}
logging.Infof("Searching with parsed criteria: %#v", criteria)
m := make([]lib.RawMessage, 0, len(w.folder.Uids()))
for _, uid := range w.folder.Uids() {
msg, err := w.folder.Message(uid)
if err != nil {
logging.Errorf("failed to get message for uid: %d", uid)
continue
}
m = append(m, msg)
}
uids, err := lib.Search(m, criteria)
if err != nil {
reterr = err
break
}
w.worker.PostMessage(&types.SearchResults{
Message: types.RespondTo(msg),
Uids: uids,
}, nil)
case *types.AppendMessage:
if msg.Destination == "" {
reterr = fmt.Errorf("AppendMessage with empty destination directory")
break
}
folder, ok := w.data.Mailbox(msg.Destination)
if !ok {
folder = w.data.Create(msg.Destination)
w.worker.PostMessage(&types.Done{
Message: types.RespondTo(&types.CreateDirectory{}),
}, nil)
}
if err := folder.Append(msg.Reader, msg.Flags); err != nil {
reterr = err
break
} else {
w.worker.PostMessage(&types.DirectoryInfo{
Info: w.data.DirectoryInfo(msg.Destination),
}, nil)
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
}
case *types.AnsweredMessages:
reterr = errUnsupported
default:
reterr = errUnsupported
}
return reterr
}
func (w *mboxWorker) Run() {
for msg := range w.worker.Actions {
msg = w.worker.ProcessAction(msg)
if err := w.handleMessage(msg); errors.Is(err, errUnsupported) {
w.worker.PostMessage(&types.Unsupported{
Message: types.RespondTo(msg),
}, nil)
} else if err != nil {
w.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
}
}
}
|