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
|
//go:build notmuch
// +build notmuch
package notmuch
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/emersion/go-maildir"
"git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/rfc822"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/lib"
notmuch "git.sr.ht/~rjarry/aerc/worker/notmuch/lib"
"git.sr.ht/~rjarry/aerc/worker/types"
)
type Message struct {
uid uint32
key string
db *notmuch.DB
}
// NewReader returns a reader for a message
func (m *Message) NewReader() (io.ReadCloser, error) {
name, err := m.Filename()
if err != nil {
return nil, err
}
return os.Open(name)
}
// MessageInfo populates a models.MessageInfo struct for the message.
func (m *Message) MessageInfo() (*models.MessageInfo, error) {
info, err := rfc822.MessageInfo(m)
if err != nil {
return nil, err
}
if filenames, err := m.db.MsgFilenames(m.key); err != nil {
log.Errorf("failed to obtain filenames: %v", err)
} else {
info.Filenames = filenames
// if size retrieval fails, just return info and log error
if len(filenames) > 0 {
if info.Size, err = lib.FileSize(filenames[0]); err != nil {
log.Errorf("failed to obtain file size: %v", err)
}
}
}
return info, nil
}
// NewBodyPartReader creates a new io.Reader for the requested body part(s) of
// the message.
func (m *Message) NewBodyPartReader(requestedParts []int) (io.Reader, error) {
name, err := m.Filename()
if err != nil {
return nil, err
}
f, err := os.Open(name)
if err != nil {
return nil, err
}
defer f.Close()
msg, err := rfc822.ReadMessage(f)
if err != nil {
return nil, fmt.Errorf("could not read message: %w", err)
}
return rfc822.FetchEntityPartReader(msg, requestedParts)
}
// SetFlag adds or removes a flag from the message.
// Notmuch doesn't support all the flags, and for those this errors.
func (m *Message) SetFlag(flag models.Flags, enable bool) error {
// Translate the flag into a notmuch tag, ignoring no-op flags.
tag, ok := flagToTag[flag]
if !ok {
return fmt.Errorf("Notmuch doesn't support flag %v", flag)
}
// Get the current state of the flag.
// Note that notmuch handles some flags in an inverted sense
oldState := false
tags, err := m.Tags()
if err != nil {
return err
}
for _, t := range tags {
if t == tag {
oldState = true
break
}
}
if flagToInvert[flag] {
enable = !enable
}
switch {
case oldState == enable:
return nil
case enable:
return m.AddTag(tag)
default:
return m.RemoveTag(tag)
}
}
// MarkAnswered either adds or removes the "replied" tag from the message.
func (m *Message) MarkAnswered(answered bool) error {
return m.SetFlag(models.AnsweredFlag, answered)
}
// MarkForwarded either adds or removes the "forwarded" tag from the message.
func (m *Message) MarkForwarded(forwarded bool) error {
return m.SetFlag(models.ForwardedFlag, forwarded)
}
// MarkRead either adds or removes the maildir.FlagSeen flag from the message.
func (m *Message) MarkRead(seen bool) error {
return m.SetFlag(models.SeenFlag, seen)
}
// tags returns the notmuch tags of a message
func (m *Message) Tags() ([]string, error) {
return m.db.MsgTags(m.key)
}
func (m *Message) Labels() ([]string, error) {
return m.Tags()
}
func (m *Message) ModelFlags() (models.Flags, error) {
var flags models.Flags = models.SeenFlag
tags, err := m.Tags()
if err != nil {
return 0, err
}
for _, tag := range tags {
flag := tagToFlag[tag]
if flagToInvert[flag] {
flags &^= flag
} else {
flags |= flag
}
}
return flags, nil
}
func (m *Message) UID() uint32 {
return m.uid
}
func (m *Message) Filename() (string, error) {
return m.db.MsgFilename(m.key)
}
// AddTag adds a single tag.
// Consider using *Message.ModifyTags for multiple additions / removals
// instead of looping over a tag array
func (m *Message) AddTag(tag string) error {
return m.ModifyTags([]string{tag}, nil)
}
// RemoveTag removes a single tag.
// Consider using *Message.ModifyTags for multiple additions / removals
// instead of looping over a tag array
func (m *Message) RemoveTag(tag string) error {
return m.ModifyTags(nil, []string{tag})
}
func (m *Message) ModifyTags(add, remove []string) error {
return m.db.MsgModifyTags(m.key, add, remove)
}
func (m *Message) Remove(curDir maildir.Dir, mfs types.MultiFileStrategy) error {
rm, del, err := m.filenamesForStrategy(mfs, curDir)
if err != nil {
return err
}
rm = append(rm, del...)
return m.deleteFiles(rm)
}
func (m *Message) Copy(curDir, destDir maildir.Dir, mfs types.MultiFileStrategy) error {
cp, del, err := m.filenamesForStrategy(mfs, curDir)
if err != nil {
return err
}
for _, filename := range cp {
source, key := parseFilename(filename)
if key == "" {
return fmt.Errorf("failed to parse message filename: %s", filename)
}
newKey, err := source.Copy(destDir, key)
if err != nil {
return err
}
newFilename, err := destDir.Filename(newKey)
if err != nil {
return err
}
_, err = m.db.IndexFile(newFilename)
if err != nil {
return err
}
}
return m.deleteFiles(del)
}
func (m *Message) Move(curDir, destDir maildir.Dir, mfs types.MultiFileStrategy) error {
move, del, err := m.filenamesForStrategy(mfs, curDir)
if err != nil {
return err
}
for _, filename := range move {
// Remove encoded UID information from the key to prevent sync issues
name := lib.StripUIDFromMessageFilename(filepath.Base(filename))
dest := filepath.Join(string(destDir), "cur", name)
if err := os.Rename(filename, dest); err != nil {
return err
}
if _, err = m.db.IndexFile(dest); err != nil {
return err
}
if err := m.db.DeleteMessage(filename); err != nil {
return err
}
}
return m.deleteFiles(del)
}
func (m *Message) deleteFiles(filenames []string) error {
for _, filename := range filenames {
if err := os.Remove(filename); err != nil {
return err
}
if err := m.db.DeleteMessage(filename); err != nil {
return err
}
}
return nil
}
func (m *Message) filenamesForStrategy(strategy types.MultiFileStrategy,
curDir maildir.Dir,
) (act, del []string, err error) {
filenames, err := m.db.MsgFilenames(m.key)
if err != nil {
return nil, nil, err
}
return filterForStrategy(filenames, strategy, curDir)
}
func filterForStrategy(filenames []string, strategy types.MultiFileStrategy,
curDir maildir.Dir,
) (act, del []string, err error) {
if curDir == "" &&
(strategy == types.ActDir || strategy == types.ActDirDelRest) {
strategy = types.Refuse
}
if len(filenames) < 2 {
return filenames, []string{}, nil
}
act = []string{}
rest := []string{}
switch strategy {
case types.Refuse:
return nil, nil, fmt.Errorf("refusing to act on multiple files")
case types.ActAll:
act = filenames
case types.ActOne:
fallthrough
case types.ActOneDelRest:
act = filenames[:1]
rest = filenames[1:]
case types.ActDir:
fallthrough
case types.ActDirDelRest:
for _, filename := range filenames {
if filepath.Dir(filepath.Dir(filename)) == string(curDir) {
act = append(act, filename)
} else {
rest = append(rest, filename)
}
}
default:
return nil, nil, fmt.Errorf("invalid multi-file strategy %v", strategy)
}
switch strategy {
case types.ActOneDelRest:
fallthrough
case types.ActDirDelRest:
del = rest
default:
del = []string{}
}
return act, del, nil
}
func parseFilename(filename string) (maildir.Dir, string) {
base := filepath.Base(filename)
dir := filepath.Dir(filename)
dir, curdir := filepath.Split(dir)
if curdir != "cur" {
return "", ""
}
split := strings.Split(base, ":")
if len(split) < 2 {
return maildir.Dir(dir), ""
}
key := split[0]
return maildir.Dir(dir), key
}
|