Debian Codegraph @

worker/maildir/container.go

raw
1 package maildir
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "slices"
8
9 "github.com/emersion/go-maildir"
10
11 "git.sr.ht/~rjarry/aerc/lib/log"
12 "git.sr.ht/~rjarry/aerc/models"
13 "git.sr.ht/~rjarry/aerc/worker/lib"
14 )
15
16 // A Container is a directory which contains other directories which adhere to
17 // the Maildir spec
18 type Container struct {
19 Store *lib.MaildirStore
20 recentUIDS map[models.UID]struct{} // used to set the recent flag
21 }
22
23 // NewContainer creates a new container at the specified directory
24 func NewContainer(dir string, maildirpp bool) (*Container, error) {
25 store, err := lib.NewMaildirStore(dir, maildirpp)
26 if err != nil {
27 return nil, err
28 }
29 return &Container{
30 Store: store,
31 recentUIDS: make(map[models.UID]struct{}),
32 }, nil
33 }
34
35 // SyncNewMail adds emails from new to cur, tracking them
36 func (c *Container) SyncNewMail(dir maildir.Dir) error {
37 unseen, err := dir.Unseen()
38 if err != nil {
39 return err
40 }
41 for _, msg := range unseen {
42 c.recentUIDS[models.UID(msg.Key())] = struct{}{}
43 }
44 return nil
45 }
46
47 // OpenDirectory opens an existing maildir in the container by name, moves new
48 // messages into cur, and registers the new keys in the UIDStore.
49 func (c *Container) OpenDirectory(name string) (maildir.Dir, error) {
50 dir := c.Store.Dir(name)
51 if err := c.SyncNewMail(dir); err != nil {
52 return dir, err
53 }
54 return dir, nil
55 }
56
57 // IsRecent returns if a uid has the Recent flag set
58 func (c *Container) IsRecent(uid models.UID) bool {
59 _, ok := c.recentUIDS[uid]
60 return ok
61 }
62
63 // ClearRecentFlag removes the Recent flag from the message with the given uid
64 func (c *Container) ClearRecentFlag(uid models.UID) {
65 delete(c.recentUIDS, uid)
66 }
67
68 // UIDs fetches the unique message identifiers for the maildir
69 func (c *Container) UIDs(d maildir.Dir) ([]models.UID, error) {
70 // messages, err := d.Keys()
71 messages, err := d.Messages()
72 if err != nil && len(messages) == 0 {
73 return nil, fmt.Errorf("could not get keys for %s: %w", d, err)
74 }
75 if err != nil {
76 log.Errorf("could not get all keys for %s: %s", d, err.Error())
77 }
78 var keyList []string
79 for _, msg := range messages {
80 keyList = append(keyList, msg.Key())
81 }
82 slices.Sort(keyList)
83 var uids []models.UID
84 for _, key := range keyList {
85 uids = append(uids, models.UID(key))
86 }
87 return uids, err
88 }
89
90 // Message returns a Message struct for the given UID and maildir
91 func (c *Container) Message(d maildir.Dir, uid models.UID) (*Message, error) {
92 return &Message{
93 dir: d,
94 uid: uid,
95 key: string(uid),
96 }, nil
97 }
98
99 // DeleteAll deletes a set of messages by UID and returns the subset of UIDs
100 // which were successfully deleted, stopping upon the first error.
101 func (c *Container) DeleteAll(d maildir.Dir, uids []models.UID) ([]models.UID, error) {
102 var success []models.UID
103 for _, uid := range uids {
104 msg, err := c.Message(d, uid)
105 if err != nil {
106 return success, err
107 }
108 if err := msg.Remove(); err != nil {
109 return success, err
110 }
111 success = append(success, uid)
112 }
113 return success, nil
114 }
115
116 func (c *Container) CopyAll(
117 dest maildir.Dir, src maildir.Dir, uids []models.UID,
118 ) error {
119 for _, uid := range uids {
120 if err := c.copyMessage(dest, src, uid); err != nil {
121 return fmt.Errorf("could not copy message %s: %w", uid, err)
122 }
123 }
124 return nil
125 }
126
127 func (c *Container) copyMessage(
128 dest maildir.Dir, src maildir.Dir, uid models.UID,
129 ) error {
130 msg, err := src.MessageByKey(string(uid))
131 if err != nil {
132 return fmt.Errorf("failed to retrieve message %q: %w", uid, err)
133 }
134 _, err = msg.CopyTo(dest)
135 return err
136 }
137
138 func (c *Container) MoveAll(dest maildir.Dir, src maildir.Dir, uids []models.UID) ([]models.UID, error) {
139 var success []models.UID
140 for _, uid := range uids {
141 if err := c.moveMessage(dest, src, uid); err != nil {
142 return success, fmt.Errorf("could not move message %s: %w", uid, err)
143 }
144 success = append(success, uid)
145 }
146 return success, nil
147 }
148
149 func (c *Container) moveMessage(dest maildir.Dir, src maildir.Dir, uid models.UID) error {
150 msg, err := src.MessageByKey(string(uid))
151 if err != nil {
152 return fmt.Errorf("failed to retrieve message %q: %w", uid, err)
153 }
154 path := msg.Filename()
155 // Remove encoded UID information from the key to prevent sync issues
156 name := lib.StripUIDFromMessageFilename(filepath.Base(path))
157 destPath := filepath.Join(string(dest), "cur", name)
158 return os.Rename(path, destPath)
159 }