Debian Codegraph @

worker/imap/xgmext.go

raw
1 package imap
2
3 import (
4 "strconv"
5 "strings"
6
7 "git.sr.ht/~rjarry/aerc/models"
8 "git.sr.ht/~rjarry/aerc/worker/types"
9 )
10
11 func searchString(criteria *types.SearchCriteria) string {
12 search := ""
13 if criteria.Match != nil {
14 search += strings.Join(criteria.Match.Terms, " ")
15 }
16 if criteria.Exclude != nil {
17 search += " -" + strings.Join(criteria.Match.Terms, " -")
18 }
19 return search
20 }
21
22 // handleGmailFilter handles FetchDirectoryContents with Gmail X-GM-RAW filtering.
23 // Returns true if the request was handled (caller should not proceed with normal filtering).
24 func (w *IMAPWorker) handleGmailFilter(msg *types.FetchDirectoryContents) bool {
25 if msg.Filter == nil {
26 return false
27 }
28 if !msg.Filter.UseExtension {
29 w.worker.Debugf("use regular imap filter instead of X-GM-EXT1: extension flag not set")
30 return false
31 }
32
33 search := searchString(msg.Filter)
34 if search == "" {
35 return false
36 }
37 w.worker.Debugf("X-GM-EXT1 filter term: '%s'", search)
38
39 uids, err := w.client.xgmext.RawSearch(strconv.Quote(search))
40 if err != nil {
41 w.worker.Errorf("X-GM-EXT1 filter failed: %v", err)
42 w.worker.Warnf("falling back to imap filtering")
43 return false
44 }
45
46 w.worker.PostMessage(&types.DirectoryContents{
47 Message: types.RespondTo(msg),
48 Directory: msg.Directory,
49 Filter: msg.Filter,
50 Uids: w.Uint32ToUidList(uids),
51 }, nil)
52
53 return true
54 }
55
56 // handleGmailSearch handles SearchDirectory with Gmail X-GM-RAW searching.
57 // Returns true if the request was handled (caller should not proceed with normal search).
58 func (w *IMAPWorker) handleGmailSearch(msg *types.SearchDirectory) bool {
59 if msg.Criteria == nil {
60 return false
61 }
62 if !msg.Criteria.UseExtension {
63 w.worker.Debugf("use regular imap search instead of X-GM-EXT1: extension flag not set")
64 return false
65 }
66
67 search := searchString(msg.Criteria)
68 if search == "" {
69 return false
70 }
71 w.worker.Debugf("X-GM-EXT1 search term: '%s'", search)
72
73 uids, err := w.client.xgmext.RawSearch(strconv.Quote(search))
74 if err != nil {
75 w.worker.Errorf("X-GM-EXT1 search failed: %v", err)
76 w.worker.Warnf("falling back to regular imap search.")
77 return false
78 }
79
80 w.worker.PostMessage(&types.SearchResults{
81 Message: types.RespondTo(msg),
82 Directory: msg.Directory,
83 Criteria: msg.Criteria,
84 Uids: w.Uint32ToUidList(uids),
85 }, nil)
86
87 return true
88 }
89
90 // fetchEntireThreads fetches all message UIDs that belong to the same threads
91 // as the requested UIDs using the X-GM-THRID extension.
92 func (w *IMAPWorker) fetchEntireThreads(requested []models.UID) ([]models.UID, error) {
93 uids, err := w.client.xgmext.FetchEntireThreads(w.UidToUint32List(requested))
94 if err != nil {
95 return nil, err
96 }
97 return w.Uint32ToUidList(uids), nil
98 }