| 1 |
package lib |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"regexp" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"git.sr.ht/~rjarry/aerc/models" |
| 11 |
"github.com/emersion/go-maildir" |
| 12 |
) |
| 13 |
|
| 14 |
type MaildirStore struct { |
| 15 |
root string |
| 16 |
maildirpp bool // whether to use Maildir++ directory layout |
| 17 |
} |
| 18 |
|
| 19 |
func NewMaildirStore(root string, maildirpp bool) (*MaildirStore, error) { |
| 20 |
f, err := os.Open(root) |
| 21 |
if err != nil { |
| 22 |
return nil, err |
| 23 |
} |
| 24 |
defer f.Close() |
| 25 |
s, err := f.Stat() |
| 26 |
if err != nil { |
| 27 |
return nil, err |
| 28 |
} |
| 29 |
if !s.IsDir() { |
| 30 |
return nil, fmt.Errorf("Given maildir '%s' not a directory", root) |
| 31 |
} |
| 32 |
return &MaildirStore{ |
| 33 |
root: root, maildirpp: maildirpp, |
| 34 |
}, nil |
| 35 |
} |
| 36 |
|
| 37 |
func (s *MaildirStore) FolderMap() (map[string]maildir.Dir, error) { |
| 38 |
folders := make(map[string]maildir.Dir) |
| 39 |
if s.maildirpp { |
| 40 |
// In Maildir++ layout, INBOX is the root folder |
| 41 |
folders["INBOX"] = maildir.Dir(s.root) |
| 42 |
} |
| 43 |
err := filepath.Walk(s.root, func(path string, info os.FileInfo, err error) error { |
| 44 |
if err != nil { |
| 45 |
return fmt.Errorf("Invalid path '%s': error: %w", path, err) |
| 46 |
} |
| 47 |
if !info.IsDir() { |
| 48 |
return nil |
| 49 |
} |
| 50 |
|
| 51 |
// Skip maildir's default directories |
| 52 |
n := info.Name() |
| 53 |
if n == "new" || n == "tmp" || n == "cur" { |
| 54 |
return filepath.SkipDir |
| 55 |
} |
| 56 |
|
| 57 |
// Get the relative path from the parent directory |
| 58 |
dirPath, err := filepath.Rel(s.root, path) |
| 59 |
if err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
|
| 63 |
// Skip the parent directory |
| 64 |
if dirPath == "." { |
| 65 |
return nil |
| 66 |
} |
| 67 |
|
| 68 |
// Drop dirs that lack {new,tmp,cur} subdirs |
| 69 |
for _, sub := range []string{"new", "tmp", "cur"} { |
| 70 |
if _, err := os.Stat(filepath.Join(path, sub)); os.IsNotExist(err) { |
| 71 |
return nil |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if s.maildirpp { |
| 76 |
// In Maildir++ layout, mailboxes are stored in a single directory |
| 77 |
// and prefixed with a dot, and subfolders are separated by dots. |
| 78 |
if !strings.HasPrefix(dirPath, ".") { |
| 79 |
return filepath.SkipDir |
| 80 |
} |
| 81 |
dirPath = strings.TrimPrefix(dirPath, ".") |
| 82 |
dirPath = strings.ReplaceAll(dirPath, ".", "/") |
| 83 |
folders[dirPath] = maildir.Dir(path) |
| 84 |
|
| 85 |
// Since all mailboxes are stored in a single directory, don't |
| 86 |
// recurse into subdirectories |
| 87 |
return filepath.SkipDir |
| 88 |
} |
| 89 |
|
| 90 |
folders[dirPath] = maildir.Dir(path) |
| 91 |
return nil |
| 92 |
}) |
| 93 |
return folders, err |
| 94 |
} |
| 95 |
|
| 96 |
// Folder returns a maildir.Dir with the specified name inside the Store |
| 97 |
func (s *MaildirStore) Dir(name string) maildir.Dir { |
| 98 |
if s.maildirpp { |
| 99 |
// Use Maildir++ layout |
| 100 |
if name == "INBOX" { |
| 101 |
return maildir.Dir(s.root) |
| 102 |
} |
| 103 |
return maildir.Dir(filepath.Join(s.root, "."+strings.ReplaceAll(name, "/", "."))) |
| 104 |
} |
| 105 |
return maildir.Dir(filepath.Join(s.root, name)) |
| 106 |
} |
| 107 |
|
| 108 |
// uidReg matches filename encoded UIDs in maildirs synched with mbsync or |
| 109 |
// OfflineIMAP |
| 110 |
var uidReg = regexp.MustCompile(`,U=\d+`) |
| 111 |
|
| 112 |
func StripUIDFromMessageFilename(basename string) string { |
| 113 |
return uidReg.ReplaceAllString(basename, "") |
| 114 |
} |
| 115 |
|
| 116 |
var MaildirToFlag = map[maildir.Flag]models.Flags{ |
| 117 |
maildir.FlagReplied: models.AnsweredFlag, |
| 118 |
maildir.FlagSeen: models.SeenFlag, |
| 119 |
maildir.FlagTrashed: models.DeletedFlag, |
| 120 |
maildir.FlagFlagged: models.FlaggedFlag, |
| 121 |
maildir.FlagDraft: models.DraftFlag, |
| 122 |
maildir.FlagPassed: models.ForwardedFlag, |
| 123 |
} |
| 124 |
|
| 125 |
var FlagToMaildir = map[models.Flags]maildir.Flag{ |
| 126 |
models.AnsweredFlag: maildir.FlagReplied, |
| 127 |
models.SeenFlag: maildir.FlagSeen, |
| 128 |
models.DeletedFlag: maildir.FlagTrashed, |
| 129 |
models.FlaggedFlag: maildir.FlagFlagged, |
| 130 |
models.DraftFlag: maildir.FlagDraft, |
| 131 |
models.ForwardedFlag: maildir.FlagPassed, |
| 132 |
} |
| 133 |
|
| 134 |
func FromMaildirFlags(maildirFlags []maildir.Flag) models.Flags { |
| 135 |
var flags models.Flags |
| 136 |
for _, maildirFlag := range maildirFlags { |
| 137 |
if flag, ok := MaildirToFlag[maildirFlag]; ok { |
| 138 |
flags |= flag |
| 139 |
} |
| 140 |
} |
| 141 |
return flags |
| 142 |
} |
| 143 |
|
| 144 |
func ToMaildirFlags(flags models.Flags) []maildir.Flag { |
| 145 |
var maildirFlags []maildir.Flag |
| 146 |
for flag, maildirFlag := range FlagToMaildir { |
| 147 |
if flags.Has(flag) { |
| 148 |
maildirFlags = append(maildirFlags, maildirFlag) |
| 149 |
} |
| 150 |
} |
| 151 |
return maildirFlags |
| 152 |
} |