forked from luksen/maildir
/
maildir.go
406 lines (375 loc) · 10.2 KB
/
maildir.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// The maildir package provides an interface to mailboxes in the Maildir format.
package maildir
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/hex"
"io"
"net/mail"
"net/textproto"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync/atomic"
"time"
)
// The Separator separates a messages unique key from its flags in the filename.
// This should only be changed on operating systems where the colon isn't
// allowed in filenames.
var Separator rune = ':'
var id int64 = 10000
// CreateMode holds the permissions used when creating a directory.
const CreateMode = 0700
// A KeyError occurs when a key matches more or less than one message.
type KeyError struct {
Key string // the (invalid) key
N int // number of matches (!= 1)
}
func (e *KeyError) Error() string {
return "maildir: key " + e.Key + " matches " + strconv.Itoa(e.N) + " files."
}
// A FlagError occurs when a non-standard info section is encountered.
type FlagError struct {
Info string // the encountered info section
Experimental bool // info section starts with 1
}
func (e *FlagError) Error() string {
if e.Experimental {
return "maildir: experimental info section encountered: " + e.Info[2:]
}
return "maildir: bad info section encountered: " + e.Info
}
// A Dir represents a single directory in a Maildir mailbox.
type Dir string
// Unseen moves messages from new to cur and returns their keys.
// This means the messages are now known to the application. To find out whether
// a user has seen a message, use Flags().
func (d Dir) Unseen() ([]string, error) {
f, err := os.Open(filepath.Join(string(d), "new"))
if err != nil {
return nil, err
}
defer f.Close()
names, err := f.Readdirnames(0)
if err != nil {
return nil, err
}
var keys []string
for _, n := range names {
if n[0] != '.' {
split := strings.FieldsFunc(n, func(r rune) bool {
return r == Separator
})
key := split[0]
info := "2,"
// Messages in new shouldn't have an info section but
// we act as if, in case some other program didn't
// follow the spec.
if len(split) > 1 {
info = split[1]
}
keys = append(keys, key)
err = os.Rename(filepath.Join(string(d), "new", n),
filepath.Join(string(d), "cur", key+string(Separator)+info))
}
}
return keys, err
}
func readDirNames(d string) ([]string, error) {
f, err := os.Open(d)
if err != nil {
return nil, err
}
defer f.Close()
return f.Readdirnames(0)
}
// Keys returns a slice of valid keys to access messages by.
func (d Dir) Keys() ([]string, error) {
names := []string{}
for _, dir := range []string{"cur", "new"} {
files, err := readDirNames(filepath.Join(string(d), dir))
if err != nil {
return nil, err
}
names = append(names, files...)
}
var keys []string
for _, n := range names {
if n[0] != '.' {
split := strings.FieldsFunc(n, func(r rune) bool {
return r == Separator
})
keys = append(keys, split[0])
}
}
return keys, nil
}
// Filename returns the path to the file corresponding to the key.
func (d Dir) Filename(key string) (string, error) {
matches, err := filepath.Glob(filepath.Join(string(d), "/*/", key+"*"))
if err != nil {
return "", err
}
if n := len(matches); n != 1 {
return "", &KeyError{key, n}
}
return matches[0], nil
}
// Header returns the corresponding mail header to a key.
func (d Dir) Header(key string) (header mail.Header, err error) {
filename, err := d.Filename(key)
if err != nil {
return
}
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
tp := textproto.NewReader(bufio.NewReader(file))
hdr, err := tp.ReadMIMEHeader()
if err != nil {
return
}
header = mail.Header(hdr)
return
}
// Message returns a Message by key.
func (d Dir) Message(key string) (*mail.Message, error) {
filename, err := d.Filename(key)
if err != nil {
return &mail.Message{}, err
}
r, err := os.Open(filename)
if err != nil {
return &mail.Message{}, err
}
defer r.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, r)
if err != nil {
return &mail.Message{}, err
}
msg, err := mail.ReadMessage(buf)
if err != nil {
return msg, err
}
return msg, nil
}
type runeSlice []rune
func (s runeSlice) Len() int { return len(s) }
func (s runeSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s runeSlice) Less(i, j int) bool { return s[i] < s[j] }
// Flags returns the flags for a message sorted in ascending order.
// See the documentation of SetFlags for details.
func (d Dir) Flags(key string) (string, error) {
filename, err := d.Filename(key)
if err != nil {
return "", err
}
split := strings.FieldsFunc(filename, func(r rune) bool {
return r == Separator
})
switch {
case len(split[1]) < 2,
split[1][1] != ',':
return "", &FlagError{split[1], false}
case split[1][0] == '1':
return "", &FlagError{split[1], true}
case split[1][0] != '2':
return "", &FlagError{split[1], false}
}
rs := runeSlice(split[1][2:])
sort.Sort(rs)
return string(rs), nil
}
// SetFlags appends an info section to the filename according to the given flags.
// This function removes duplicates and sorts the flags, but doesn't check
// whether they conform with the Maildir specification.
//
// The following flags are listed in the specification
// (http://cr.yp.to/proto/maildir.html):
//
// Flag "P" (passed): the user has resent/forwarded/bounced this message to someone else.
// Flag "R" (replied): the user has replied to this message.
// Flag "S" (seen): the user has viewed this message, though perhaps he didn't read all the way through it.
// Flag "T" (trashed): the user has moved this message to the trash; the trash will be emptied by a later user action.
// Flag "D" (draft): the user considers this message a draft; toggled at user discretion.
// Flag "F" (flagged): user-defined flag; toggled at user discretion.
func (d Dir) SetFlags(key string, flags string) error {
info := "2,"
rs := runeSlice(flags)
sort.Sort(rs)
for _, r := range rs {
if []rune(info)[len(info)-1] != r {
info += string(r)
}
}
return d.SetInfo(key, info)
}
// Set the info part of the filename.
// Only use this if you plan on using a non-standard info part.
func (d Dir) SetInfo(key, info string) error {
filename, err := d.Filename(key)
if err != nil {
return err
}
err = os.Rename(filename, filepath.Join(string(d), "cur", key+
string(Separator)+info))
return err
}
// Key generates a new unique key as described in the Maildir specification.
// For the third part of the key (delivery identifier) it uses an internal
// counter, the process id and a cryptographical random number to ensure
// uniqueness among messages delivered in the same second.
func Key() (string, error) {
var key string
key += strconv.FormatInt(time.Now().Unix(), 10)
key += "."
host, err := os.Hostname()
if err != err {
return "", err
}
host = strings.Replace(host, "/", "\057", -1)
host = strings.Replace(host, string(Separator), "\072", -1)
key += host
key += "."
key += strconv.FormatInt(int64(os.Getpid()), 10)
key += strconv.FormatInt(id, 10)
atomic.AddInt64(&id, 1)
bs := make([]byte, 10)
_, err = io.ReadFull(rand.Reader, bs)
if err != nil {
return "", err
}
key += hex.EncodeToString(bs)
return key, nil
}
// Create creates the directory structure for a Maildir.
// If the main directory already exists, it tries to create the subdirectories
// in there. If an error occurs while creating one of the subdirectories, this
// function may leave a partially created directory structure.
func (d Dir) Create() error {
err := os.Mkdir(string(d), os.ModeDir|CreateMode)
if err != nil {
if !os.IsExist(err) {
return err
}
}
err = os.Mkdir(filepath.Join(string(d), "tmp"), os.ModeDir|CreateMode)
if err != nil {
return err
}
err = os.Mkdir(filepath.Join(string(d), "new"), os.ModeDir|CreateMode)
if err != nil {
return err
}
err = os.Mkdir(filepath.Join(string(d), "cur"), os.ModeDir|CreateMode)
if err != nil {
return err
}
return nil
}
// Delivery represents an ongoing message delivery to the mailbox.
// It implements the WriteCloser interface. On closing the underlying file is
// moved/relinked to new.
type Delivery struct {
file *os.File
d Dir
key string
}
// NewDelivery creates a new Delivery.
func (d Dir) NewDelivery() (*Delivery, error) {
key, err := Key()
if err != nil {
return nil, err
}
del := &Delivery{}
time.AfterFunc(24*time.Hour, func() { del.Abort() })
file, err := os.Create(filepath.Join(string(d), "tmp", key))
if err != nil {
return nil, err
}
del.file = file
del.d = d
del.key = key
return del, nil
}
func (d *Delivery) Write(p []byte) (int, error) {
return d.file.Write(p)
}
// Close closes the underlying file and moves it to new.
func (d *Delivery) Close() error {
err := d.file.Close()
if err != nil {
return err
}
err = os.Link(filepath.Join(string(d.d), "tmp", d.key),
filepath.Join(string(d.d), "new", d.key))
if err != nil {
return err
}
err = os.Remove(filepath.Join(string(d.d), "tmp", d.key))
if err != nil {
return err
}
return nil
}
// Abort closes the underlying file and removes it completely.
func (d *Delivery) Abort() error {
err := d.file.Close()
if err != nil {
return err
}
err = os.Remove(filepath.Join(string(d.d), "tmp", d.key))
if err != nil {
return err
}
return nil
}
// Move moves a message from this Maildir to another.
func (d Dir) Move(target Dir, key string) error {
path, err := d.Filename(key)
if err != nil {
return err
}
return os.Rename(path, filepath.Join(string(target), "cur", filepath.Base(path)))
}
// Purge removes the actual file behind this message.
func (d Dir) Purge(key string) error {
f, err := d.Filename(key)
if err != nil {
return err
}
return os.Remove(f)
}
// Clean removes old files from tmp and should be run periodically.
// This does not use access time but modification time for portability reasons.
func (d Dir) Clean() error {
f, err := os.Open(filepath.Join(string(d), "tmp"))
if err != nil {
return err
}
defer f.Close()
names, err := f.Readdirnames(0)
if err != nil {
return err
}
now := time.Now()
for _, n := range names {
fi, err := os.Stat(filepath.Join(string(d), "tmp", n))
if err != nil {
continue
}
if now.Sub(fi.ModTime()).Hours() > 36 {
err = os.Remove(filepath.Join(string(d), "tmp", n))
if err != nil {
return err
}
}
}
return nil
}