Debian Codegraph @

commands/msg/mark.go

raw
1 package msg
2
3 import (
4 "fmt"
5 "iter"
6 "slices"
7 "strings"
8
9 "git.sr.ht/~rjarry/aerc/commands"
10 "git.sr.ht/~rjarry/aerc/lib"
11 "git.sr.ht/~rjarry/aerc/lib/log"
12 "git.sr.ht/~rjarry/aerc/models"
13 )
14
15 type Mark struct {
16 All bool `opt:"-a" aliases:"mark,unmark" desc:"Mark all messages in current folder."`
17 Toggle bool `opt:"-t" aliases:"mark,unmark" desc:"Toggle the marked state."`
18 Visual bool `opt:"-v" aliases:"mark" desc:"Enter / leave visual mark mode."`
19 VisualClear bool `opt:"-V" aliases:"mark" desc:"Same as -v but does not clear existing selection."`
20 Thread bool `opt:"-T" aliases:"mark,unmark" desc:"Mark messages from the selected thread."`
21 SenderFilter bool `opt:"-s" aliases:"mark,unmark" desc:"Mark messages having the substring in their From: header."`
22 RecipientFilter bool `opt:"-r" aliases:"mark,unmark" desc:"Mark messages having the substring in their To:, Cc:, or Bcc: header."`
23 Unread bool `opt:"-u" aliases:"mark,unmark" desc:"Mark unread messages"`
24 NotUnread bool `opt:"-U" aliases:"mark,unmark" desc:"Mark read messages"`
25 FilterString string `opt:"..." required:"false" desc:"Mark messages matching this string."`
26 }
27
28 func init() {
29 commands.Register(Mark{})
30 }
31
32 func (Mark) Description() string {
33 return "Mark, unmark or remark messages."
34 }
35
36 func (Mark) Context() commands.CommandContext {
37 return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER
38 }
39
40 func (Mark) Aliases() []string {
41 return []string{"mark", "unmark", "remark"}
42 }
43
44 func (m Mark) Execute(args []string) error {
45 h := newHelper()
46
47 store, err := h.store()
48 if err != nil {
49 return err
50 }
51 marker := store.Marker()
52
53 if m.Thread && m.All {
54 return fmt.Errorf("-a and -T are mutually exclusive")
55 }
56 if m.Thread && (m.Visual || m.VisualClear) {
57 return fmt.Errorf("-v and -T are mutually exclusive")
58 }
59 if m.Visual && m.All {
60 return fmt.Errorf("-a and -v are mutually exclusive")
61 }
62 if m.SenderFilter && m.RecipientFilter {
63 return fmt.Errorf("-s and -r are mutually exclusive")
64 }
65 if m.Visual && m.FilterString != "" {
66 return fmt.Errorf("visual mode does not support filtering")
67 }
68 if m.Unread == m.NotUnread && m.Unread {
69 return fmt.Errorf("-u and -U are mutually exclusive")
70 }
71 if (m.SenderFilter || m.RecipientFilter) && m.FilterString == "" {
72 return fmt.Errorf("-s and -r require a filter string")
73 }
74
75 // if filtering and only a single message is provided,
76 m.All = (m.FilterString != "" && !(m.Thread || m.All)) ||
77 // or if filtering by read status,
78 ((m.Unread || m.NotUnread) && !(m.Thread || m.All)) ||
79 // filter all instead
80 m.All
81
82 // fallback: selected message
83 filter := func(yield func(models.UID) bool) {
84 selected, err := h.msgProvider.SelectedMessage()
85 if err != nil {
86 log.Errorf("failed to retrieve selected message: %v", err)
87 return
88 }
89 yield(selected.Uid)
90 }
91
92 switch {
93 case m.All:
94 filter = slices.Values(store.Uids())
95 case m.Thread:
96 threadPtr, err := store.SelectedThread()
97 if err != nil {
98 return err
99 }
100 filter = slices.Values(threadPtr.Root().Uids())
101 }
102
103 switch {
104 case m.SenderFilter:
105 filter = senderFilter(store, m.FilterString)(filter)
106 case m.RecipientFilter:
107 filter = recipientFilter(store, m.FilterString)(filter)
108 case m.FilterString != "":
109 filter = subjectFilter(store, m.FilterString)(filter)
110 }
111 if m.Unread {
112 filter = seenFilter(store, false)(filter)
113 }
114 if m.NotUnread {
115 filter = seenFilter(store, true)(filter)
116 }
117
118 switch args[0] {
119 case "mark":
120 var modFunc func(models.UID)
121 if m.Toggle {
122 modFunc = marker.ToggleMark
123 } else {
124 modFunc = marker.Mark
125 }
126
127 if m.Visual || m.VisualClear {
128 marker.ToggleVisualMark(m.VisualClear)
129 return nil
130 }
131
132 for uid := range filter {
133 modFunc(uid)
134 }
135 return nil
136
137 case "unmark":
138 if m.Visual || m.VisualClear {
139 return fmt.Errorf("visual mode not supported for this command")
140 }
141
142 var modFunc func(models.UID)
143 if m.Toggle {
144 modFunc = marker.ToggleMark
145 } else {
146 modFunc = marker.Unmark
147 }
148
149 for uid := range filter {
150 modFunc(uid)
151 }
152 return nil
153 case "remark":
154 marker.Remark()
155 return nil
156 }
157 return nil // never reached
158 }
159
160 type filterFunc func(iter.Seq[models.UID]) iter.Seq[models.UID]
161
162 func senderFilter(store *lib.MessageStore, senderMatches string) filterFunc {
163 return func(uids iter.Seq[models.UID]) iter.Seq[models.UID] {
164 return func(yield func(models.UID) bool) {
165 for uid := range uids {
166 msg := store.Messages[uid]
167 if msg == nil || msg.Envelope == nil {
168 continue
169 }
170 from := msg.Envelope.From
171 for _, sender := range from {
172 if strings.Contains(sender.String(), senderMatches) {
173 if !yield(uid) {
174 return
175 }
176 break
177 }
178 }
179 }
180 }
181 }
182 }
183
184 func recipientFilter(store *lib.MessageStore, recipientMatches string) filterFunc {
185 return func(uids iter.Seq[models.UID]) iter.Seq[models.UID] {
186 return func(yield func(models.UID) bool) {
187 for uid := range uids {
188 msg := store.Messages[uid]
189 if msg == nil {
190 continue
191 }
192 recipients := slices.Concat(msg.Envelope.To, msg.Envelope.Cc, msg.Envelope.Bcc)
193 for _, recipient := range recipients {
194 if strings.Contains(recipient.String(), recipientMatches) {
195 if !yield(uid) {
196 return
197 }
198 break
199 }
200 }
201 }
202 }
203 }
204 }
205
206 func subjectFilter(store *lib.MessageStore, subjectMatches string) filterFunc {
207 return func(uids iter.Seq[models.UID]) iter.Seq[models.UID] {
208 return func(yield func(models.UID) bool) {
209 for uid := range uids {
210 msg := store.Messages[uid]
211 if msg == nil {
212 continue
213 }
214 subject := msg.Envelope.Subject
215 if strings.Contains(subject, subjectMatches) {
216 if !yield(uid) {
217 return
218 }
219 }
220 }
221 }
222 }
223 }
224
225 func seenFilter(store *lib.MessageStore, isSeen bool) filterFunc {
226 return func(uids iter.Seq[models.UID]) iter.Seq[models.UID] {
227 return func(yield func(models.UID) bool) {
228 for uid := range uids {
229 msg := store.Messages[uid]
230 if msg == nil {
231 continue
232 }
233 if msg.Flags.Has(models.SeenFlag) == isSeen {
234 if !yield(uid) {
235 return
236 }
237 }
238 }
239 }
240 }
241 }