| 1 |
package imap |
| 2 |
|
| 3 |
import ( |
| 4 |
"cmp" |
| 5 |
"slices" |
| 6 |
|
| 7 |
sortthread "github.com/emersion/go-imap-sortthread" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 10 |
) |
| 11 |
|
| 12 |
func (imapw *IMAPWorker) handleOpenDirectory(msg *types.OpenDirectory) error { |
| 13 |
imapw.worker.Debugf("Opening %s", msg.Directory) |
| 14 |
|
| 15 |
sel, err := imapw.client.Select(msg.Directory, false) |
| 16 |
if err != nil { |
| 17 |
return err |
| 18 |
} |
| 19 |
select { |
| 20 |
case <-msg.Context().Done(): |
| 21 |
return msg.Context().Err() |
| 22 |
default: |
| 23 |
imapw.selected = sel |
| 24 |
} |
| 25 |
return nil |
| 26 |
} |
| 27 |
|
| 28 |
func (imapw *IMAPWorker) handleFetchDirectoryContents( |
| 29 |
msg *types.FetchDirectoryContents, |
| 30 |
) error { |
| 31 |
if msg.Context().Err() != nil { |
| 32 |
return msg.Context().Err() |
| 33 |
} |
| 34 |
if err := imapw.ensureSelected(msg.Directory); err != nil { |
| 35 |
return err |
| 36 |
} |
| 37 |
imapw.worker.Tracef("Fetching UID list") |
| 38 |
|
| 39 |
// Try Gmail X-GM-EXT-1 filtering first if available |
| 40 |
if imapw.caps.Has("X-GM-EXT-1") && imapw.handleGmailFilter(msg) { |
| 41 |
return nil |
| 42 |
} |
| 43 |
|
| 44 |
searchCriteria := translateSearch(msg.Filter) |
| 45 |
sortCriteria := translateSortCriterions(msg.SortCriteria) |
| 46 |
hasSortCriteria := len(sortCriteria) > 0 |
| 47 |
|
| 48 |
var err error |
| 49 |
var uids []uint32 |
| 50 |
|
| 51 |
// If the server supports the SORT extension, do the sorting server side |
| 52 |
switch { |
| 53 |
case imapw.caps.Sort && hasSortCriteria: |
| 54 |
uids, err = imapw.client.sort.UidSort(sortCriteria, searchCriteria) |
| 55 |
if err != nil { |
| 56 |
return err |
| 57 |
} |
| 58 |
// copy in reverse as msgList displays backwards |
| 59 |
for i, j := 0, len(uids)-1; i < j; i, j = i+1, j-1 { |
| 60 |
uids[i], uids[j] = uids[j], uids[i] |
| 61 |
} |
| 62 |
default: |
| 63 |
if hasSortCriteria { |
| 64 |
imapw.worker.Warnf("SORT is not supported but requested: list messages by UID") |
| 65 |
} |
| 66 |
uids, err = imapw.client.UidSearch(searchCriteria) |
| 67 |
if err != nil { |
| 68 |
return err |
| 69 |
} |
| 70 |
} |
| 71 |
if msg.Context().Err() != nil { |
| 72 |
return msg.Context().Err() |
| 73 |
} |
| 74 |
imapw.worker.Tracef("Found %d UIDs", len(uids)) |
| 75 |
if msg.Filter == nil { |
| 76 |
// Only initialize if we are not filtering |
| 77 |
imapw.seqMap.Initialize(uids) |
| 78 |
} |
| 79 |
|
| 80 |
imapw.worker.PostMessage(&types.DirectoryContents{ |
| 81 |
Message: types.RespondTo(msg), |
| 82 |
Directory: msg.Directory, |
| 83 |
Filter: msg.Filter, |
| 84 |
Uids: imapw.Uint32ToUidList(uids), |
| 85 |
}, nil) |
| 86 |
return nil |
| 87 |
} |
| 88 |
|
| 89 |
type sortFieldMapT map[types.SortField]sortthread.SortField |
| 90 |
|
| 91 |
// caution, incomplete mapping |
| 92 |
var sortFieldMap sortFieldMapT = sortFieldMapT{ |
| 93 |
types.SortArrival: sortthread.SortArrival, |
| 94 |
types.SortCc: sortthread.SortCc, |
| 95 |
types.SortDate: sortthread.SortDate, |
| 96 |
types.SortFrom: sortthread.SortFrom, |
| 97 |
types.SortSize: sortthread.SortSize, |
| 98 |
types.SortSubject: sortthread.SortSubject, |
| 99 |
types.SortTo: sortthread.SortTo, |
| 100 |
} |
| 101 |
|
| 102 |
func translateSortCriterions( |
| 103 |
cs []*types.SortCriterion, |
| 104 |
) []sortthread.SortCriterion { |
| 105 |
result := make([]sortthread.SortCriterion, 0, len(cs)) |
| 106 |
for _, c := range cs { |
| 107 |
if f, ok := sortFieldMap[c.Field]; ok { |
| 108 |
result = append(result, sortthread.SortCriterion{Field: f, Reverse: c.Reverse}) |
| 109 |
} |
| 110 |
} |
| 111 |
return result |
| 112 |
} |
| 113 |
|
| 114 |
func (imapw *IMAPWorker) handleDirectoryThreaded( |
| 115 |
msg *types.FetchDirectoryThreaded, |
| 116 |
) error { |
| 117 |
if msg.Context().Err() != nil { |
| 118 |
return msg.Context().Err() |
| 119 |
} |
| 120 |
if err := imapw.ensureSelected(msg.Directory); err != nil { |
| 121 |
return err |
| 122 |
} |
| 123 |
imapw.worker.Tracef("Fetching threaded UID list") |
| 124 |
|
| 125 |
searchCriteria := translateSearch(msg.Filter) |
| 126 |
threads, err := imapw.client.thread.UidThread(imapw.threadAlgorithm, |
| 127 |
searchCriteria) |
| 128 |
if err != nil { |
| 129 |
return err |
| 130 |
} |
| 131 |
aercThreads, count := imapw.convertThreads(threads, nil) |
| 132 |
slices.SortFunc(aercThreads, func(a, b *types.Thread) int { |
| 133 |
return cmp.Compare(a.MaxUid(), b.MaxUid()) |
| 134 |
}) |
| 135 |
imapw.worker.Tracef("Found %d threaded messages", count) |
| 136 |
if msg.Filter == nil { |
| 137 |
// Only initialize if we are not filtering |
| 138 |
var uids []uint32 |
| 139 |
for i := len(aercThreads) - 1; i >= 0; i-- { |
| 140 |
aercThreads[i].Walk(func(t *types.Thread, level int, currentErr error) error { //nolint:errcheck // error indicates skipped threads |
| 141 |
uids = append(uids, imapw.UidToUint32(t.Uid)) |
| 142 |
return nil |
| 143 |
}) |
| 144 |
} |
| 145 |
imapw.seqMap.Initialize(uids) |
| 146 |
} |
| 147 |
if msg.Context().Err() != nil { |
| 148 |
return msg.Context().Err() |
| 149 |
} |
| 150 |
imapw.worker.PostMessage(&types.DirectoryThreaded{ |
| 151 |
Message: types.RespondTo(msg), |
| 152 |
Directory: msg.Directory, |
| 153 |
Filter: msg.Filter, |
| 154 |
Threads: aercThreads, |
| 155 |
}, nil) |
| 156 |
return nil |
| 157 |
} |
| 158 |
|
| 159 |
func (imapw *IMAPWorker) convertThreads(threads []*sortthread.Thread, parent *types.Thread) ([]*types.Thread, int) { |
| 160 |
if threads == nil { |
| 161 |
return nil, 0 |
| 162 |
} |
| 163 |
conv := make([]*types.Thread, len(threads)) |
| 164 |
count := 0 |
| 165 |
|
| 166 |
for i := range threads { |
| 167 |
t := threads[i] |
| 168 |
conv[i] = &types.Thread{ |
| 169 |
Uid: imapw.Uint32ToUid(t.Id), |
| 170 |
} |
| 171 |
|
| 172 |
// Set the first child node |
| 173 |
children, childCount := imapw.convertThreads(t.Children, conv[i]) |
| 174 |
if len(children) > 0 { |
| 175 |
conv[i].FirstChild = children[0] |
| 176 |
} |
| 177 |
|
| 178 |
// Set the parent node |
| 179 |
if parent != nil { |
| 180 |
conv[i].Parent = parent |
| 181 |
|
| 182 |
// elements of threads are siblings |
| 183 |
if i > 0 { |
| 184 |
conv[i].PrevSibling = conv[i-1] |
| 185 |
conv[i-1].NextSibling = conv[i] |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
count += childCount + 1 |
| 190 |
} |
| 191 |
return conv, count |
| 192 |
} |