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
|
package lib
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"git.sr.ht/~rjarry/aerc/models"
"github.com/emersion/go-maildir"
)
type MaildirStore struct {
root string
maildirpp bool // whether to use Maildir++ directory layout
}
func NewMaildirStore(root string, maildirpp bool) (*MaildirStore, error) {
f, err := os.Open(root)
if err != nil {
return nil, err
}
defer f.Close()
s, err := f.Stat()
if err != nil {
return nil, err
}
if !s.IsDir() {
return nil, fmt.Errorf("Given maildir '%s' not a directory", root)
}
return &MaildirStore{
root: root, maildirpp: maildirpp,
}, nil
}
func (s *MaildirStore) FolderMap() (map[string]maildir.Dir, error) {
folders := make(map[string]maildir.Dir)
if s.maildirpp {
// In Maildir++ layout, INBOX is the root folder
folders["INBOX"] = maildir.Dir(s.root)
}
err := filepath.Walk(s.root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("Invalid path '%s': error: %w", path, err)
}
if !info.IsDir() {
return nil
}
// Skip maildir's default directories
n := info.Name()
if n == "new" || n == "tmp" || n == "cur" {
return filepath.SkipDir
}
// Get the relative path from the parent directory
dirPath, err := filepath.Rel(s.root, path)
if err != nil {
return err
}
// Skip the parent directory
if dirPath == "." {
return nil
}
// Drop dirs that lack {new,tmp,cur} subdirs
for _, sub := range []string{"new", "tmp", "cur"} {
if _, err := os.Stat(filepath.Join(path, sub)); os.IsNotExist(err) {
return nil
}
}
if s.maildirpp {
// In Maildir++ layout, mailboxes are stored in a single directory
// and prefixed with a dot, and subfolders are separated by dots.
if !strings.HasPrefix(dirPath, ".") {
return filepath.SkipDir
}
dirPath = strings.TrimPrefix(dirPath, ".")
dirPath = strings.ReplaceAll(dirPath, ".", "/")
folders[dirPath] = maildir.Dir(path)
// Since all mailboxes are stored in a single directory, don't
// recurse into subdirectories
return filepath.SkipDir
}
folders[dirPath] = maildir.Dir(path)
return nil
})
return folders, err
}
// Folder returns a maildir.Dir with the specified name inside the Store
func (s *MaildirStore) Dir(name string) maildir.Dir {
if s.maildirpp {
// Use Maildir++ layout
if name == "INBOX" {
return maildir.Dir(s.root)
}
return maildir.Dir(filepath.Join(s.root, "."+strings.ReplaceAll(name, "/", ".")))
}
return maildir.Dir(filepath.Join(s.root, name))
}
// uidReg matches filename encoded UIDs in maildirs synched with mbsync or
// OfflineIMAP
var uidReg = regexp.MustCompile(`,U=\d+`)
func StripUIDFromMessageFilename(basename string) string {
return uidReg.ReplaceAllString(basename, "")
}
var MaildirToFlag = map[maildir.Flag]models.Flags{
maildir.FlagReplied: models.AnsweredFlag,
maildir.FlagSeen: models.SeenFlag,
maildir.FlagTrashed: models.DeletedFlag,
maildir.FlagFlagged: models.FlaggedFlag,
maildir.FlagDraft: models.DraftFlag,
// maildir.FlagPassed Flag = 'P'
}
var FlagToMaildir = map[models.Flags]maildir.Flag{
models.AnsweredFlag: maildir.FlagReplied,
models.SeenFlag: maildir.FlagSeen,
models.DeletedFlag: maildir.FlagTrashed,
models.FlaggedFlag: maildir.FlagFlagged,
models.DraftFlag: maildir.FlagDraft,
// maildir.FlagPassed Flag = 'P'
}
func FromMaildirFlags(maildirFlags []maildir.Flag) models.Flags {
var flags models.Flags
for _, maildirFlag := range maildirFlags {
if flag, ok := MaildirToFlag[maildirFlag]; ok {
flags |= flag
}
}
return flags
}
func ToMaildirFlags(flags models.Flags) []maildir.Flag {
var maildirFlags []maildir.Flag
for flag, maildirFlag := range FlagToMaildir {
if flags.Has(flag) {
maildirFlags = append(maildirFlags, maildirFlag)
}
}
return maildirFlags
}
|