File: mark.go

package info (click to toggle)
aerc 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,900 kB
  • sloc: ansic: 1,181; python: 1,000; sh: 553; awk: 360; makefile: 23
file content (246 lines) | stat: -rw-r--r-- 6,483 bytes parent folder | download
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
package msg

import (
	"fmt"
	"iter"
	"slices"
	"strings"

	"git.sr.ht/~rjarry/aerc/commands"
	"git.sr.ht/~rjarry/aerc/lib"
	"git.sr.ht/~rjarry/aerc/lib/log"
	"git.sr.ht/~rjarry/aerc/models"
	"git.sr.ht/~rjarry/aerc/worker/types"
)

type Mark struct {
	All             bool   `opt:"-a" aliases:"mark,unmark" desc:"Mark all messages in current folder."`
	Toggle          bool   `opt:"-t" aliases:"mark,unmark" desc:"Toggle the marked state."`
	Visual          bool   `opt:"-v" aliases:"mark" desc:"Enter / leave visual mark mode."`
	VisualClear     bool   `opt:"-V" aliases:"mark" desc:"Same as -v but does not clear existing selection."`
	Thread          bool   `opt:"-T" aliases:"mark,unmark" desc:"Mark all messages from the selected thread."`
	SenderFilter    bool   `opt:"-s" aliases:"mark,unmark" desc:"Mark all messages having the substring in their From: header."`
	RecipientFilter bool   `opt:"-r" aliases:"mark,unmark" desc:"Mark all messages having the substring in their To:, Cc:, or Bcc: header."`
	FilterString    string `opt:"..." required:"false" desc:"Mark messages matching this string."`
}

func init() {
	commands.Register(Mark{})
}

func (Mark) Description() string {
	return "Mark, unmark or remark messages."
}

func (Mark) Context() commands.CommandContext {
	return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER
}

func (Mark) Aliases() []string {
	return []string{"mark", "unmark", "remark"}
}

func (m Mark) Execute(args []string) error {
	h := newHelper()
	OnSelectedMessage := func(fn func(models.UID)) error {
		if fn == nil {
			return fmt.Errorf("no operation selected")
		}
		selected, err := h.msgProvider.SelectedMessage()
		if err != nil {
			return err
		}
		fn(selected.Uid)
		return nil
	}
	store, err := h.store()
	if err != nil {
		return err
	}
	marker := store.Marker()

	if m.Thread && m.All {
		return fmt.Errorf("-a and -T are mutually exclusive")
	}
	if m.Thread && (m.Visual || m.VisualClear) {
		return fmt.Errorf("-v and -T are mutually exclusive")
	}
	if m.Visual && m.All {
		return fmt.Errorf("-a and -v are mutually exclusive")
	}
	if m.SenderFilter && m.RecipientFilter {
		return fmt.Errorf("-s and -r are mutually exclusive")
	}
	if m.Visual && m.FilterString != "" {
		return fmt.Errorf("visual mode does not support filtering")
	}
	if (m.SenderFilter || m.RecipientFilter) && m.FilterString == "" {
		return fmt.Errorf("-s and -r require a filter string")
	}

	// if filtering and only a single message is provided, filter all
	// instead
	m.All = (m.FilterString != "" && !(m.Thread || m.All)) || m.All

	filter := slices.Values[[]models.UID, models.UID]
	switch {
	case m.SenderFilter:
		filter = senderFilter(store, m.FilterString)
	case m.RecipientFilter:
		filter = recipientFilter(store, m.FilterString)
	case m.FilterString != "":
		filter = subjectFilter(store, m.FilterString)
	}

	switch args[0] {
	case "mark":
		var modFunc func(models.UID)
		if m.Toggle {
			modFunc = marker.ToggleMark
		} else {
			modFunc = marker.Mark
		}
		switch {
		case m.All:
			uids := store.Uids()
			for uid := range filter(uids) {
				modFunc(uid)
			}
			return nil
		case m.Visual || m.VisualClear:
			marker.ToggleVisualMark(m.VisualClear)
			return nil
		default:
			if m.Thread {
				threadPtr, err := store.SelectedThread()
				if err != nil {
					return err
				}
				for uid := range filter(threadPtr.Root().Uids()) {
					modFunc(uid)
				}
			} else {
				return OnSelectedMessage(modFunc)
			}
			return nil
		}

	case "unmark":
		if m.Visual || m.VisualClear {
			return fmt.Errorf("visual mode not supported for this command")
		}

		switch {
		case m.All && m.Toggle:
			uids := store.Uids()
			for uid := range filter(uids) {
				marker.ToggleMark(uid)
			}
			return nil
		case m.All && !m.Toggle:
			marker.ClearVisualMark()
			return nil
		default:
			if m.Thread {
				threadPtr, err := store.SelectedThread()
				if err != nil {
					return err
				}
				for uid := range filter(threadPtr.Root().Uids()) {
					marker.Unmark(uid)
				}
			} else {
				return OnSelectedMessage(marker.Unmark)
			}
			return nil
		}
	case "remark":
		marker.Remark()
		return nil
	}
	return nil // never reached
}

func senderFilter(store *lib.MessageStore, senderMatches string) func([]models.UID) iter.Seq[models.UID] {
	return func(uids []models.UID) iter.Seq[models.UID] {
		store.FetchHeaders(uids, func(types.WorkerMessage) {})

		store.Lock()
		defer store.Unlock()

		var filteredUIDs []models.UID
		for _, uid := range uids {
			log.Debugf("checking for %s in messageStore", uid)
			msg := store.Messages[uid]
			if msg == nil {
				log.Warnf("message not found in messageStore")
				continue
			}
			log.Debugf("message: %#v", msg)
			from := msg.Envelope.From
			for _, sender := range from {
				if strings.Contains(sender.String(), senderMatches) {
					filteredUIDs = append(filteredUIDs, uid)
					break
				}
			}
		}

		return slices.Values(filteredUIDs)
	}
}

func recipientFilter(store *lib.MessageStore, recipientMatches string) func([]models.UID) iter.Seq[models.UID] {
	return func(uids []models.UID) iter.Seq[models.UID] {
		store.FetchHeaders(uids, func(types.WorkerMessage) {})

		store.Lock()
		defer store.Unlock()

		var filteredUIDs []models.UID
		for _, uid := range uids {
			log.Debugf("checking for %s in messageStore", uid)
			msg := store.Messages[uid]
			if msg == nil {
				log.Warnf("message not found in messageStore")
				continue
			}
			log.Debugf("message: %#v", msg)
			recipients := slices.Concat(msg.Envelope.To, msg.Envelope.Cc, msg.Envelope.Bcc)
			for _, recipient := range recipients {
				if strings.Contains(recipient.String(), recipientMatches) {
					filteredUIDs = append(filteredUIDs, uid)
					break
				}
			}
		}

		return slices.Values(filteredUIDs)
	}
}

func subjectFilter(store *lib.MessageStore, subjectMatches string) func([]models.UID) iter.Seq[models.UID] {
	return func(uids []models.UID) iter.Seq[models.UID] {
		store.FetchHeaders(uids, func(types.WorkerMessage) {})

		store.Lock()
		defer store.Unlock()

		var filteredUIDs []models.UID
		for _, uid := range uids {
			log.Debugf("checking for %s in messageStore", uid)
			msg := store.Messages[uid]
			if msg == nil {
				log.Warnf("message not found in messageStore")
				continue
			}
			log.Debugf("message: %#v", msg)
			subject := msg.Envelope.Subject
			if strings.Contains(subject, subjectMatches) {
				filteredUIDs = append(filteredUIDs, uid)
			}
		}

		return slices.Values(filteredUIDs)
	}
}