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
|
package xgmext
import (
"errors"
"fmt"
"git.sr.ht/~rjarry/aerc/log"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"github.com/emersion/go-imap/commands"
"github.com/emersion/go-imap/responses"
)
type handler struct {
client *client.Client
}
func NewHandler(c *client.Client) *handler {
return &handler{client: c}
}
func (h handler) FetchEntireThreads(requested []uint32) ([]uint32, error) {
threadIds, err := h.fetchThreadIds(requested)
if err != nil {
return nil,
fmt.Errorf("faild to fetch thread IDs: %w", err)
}
uids, err := h.searchUids(threadIds)
if err != nil {
return nil,
fmt.Errorf("faild to search for thread IDs: %w", err)
}
return uids, nil
}
func (h handler) fetchThreadIds(uids []uint32) ([]string, error) {
messages := make(chan *imap.Message)
done := make(chan error)
thriditem := imap.FetchItem("X-GM-THRID")
items := []imap.FetchItem{
thriditem,
}
m := make(map[string]struct{}, len(uids))
go func() {
defer log.PanicHandler()
for msg := range messages {
m[msg.Items[thriditem].(string)] = struct{}{}
}
done <- nil
}()
var set imap.SeqSet
set.AddNum(uids...)
err := h.client.UidFetch(&set, items, messages)
<-done
thrid := make([]string, 0, len(m))
for id := range m {
thrid = append(thrid, id)
}
return thrid, err
}
func (h handler) searchUids(thrid []string) ([]uint32, error) {
if len(thrid) == 0 {
return nil, errors.New("no thread IDs provided")
}
if h.client.State() != imap.SelectedState {
return nil, errors.New("no mailbox selected")
}
var cmd imap.Commander = NewThreadIDSearch(thrid)
cmd = &commands.Uid{Cmd: cmd}
res := new(responses.Search)
status, err := h.client.Execute(cmd, res)
if err != nil {
return nil, fmt.Errorf("imap execute failed: %w", err)
}
return res.Ids, status.Err()
}
|