Debian Codegraph @

worker/imap/checkmail.go

raw
1 package imap
2
3 import (
4 "fmt"
5
6 "git.sr.ht/~rjarry/aerc/models"
7 "git.sr.ht/~rjarry/aerc/worker/types"
8 "github.com/emersion/go-imap"
9 )
10
11 func (w *IMAPWorker) handleCheckMailMessage(msg *types.CheckMail) error {
12 items := []imap.StatusItem{
13 imap.StatusMessages,
14 imap.StatusRecent,
15 imap.StatusUnseen,
16 imap.StatusUidNext,
17 imap.StatusUidValidity,
18 }
19 var (
20 statuses []*imap.MailboxStatus
21 err error
22 remaining []string
23 )
24 switch {
25 case w.liststatus:
26 w.worker.Tracef("Checking mail with LIST-STATUS")
27 ref := ""
28 if len(msg.Directories) == 1 {
29 // If checking a single directory, restrict the ListStatus to it.
30 ref = msg.Directories[0]
31 }
32 statuses, err = w.client.liststatus.ListStatus(ref, "*", items, nil)
33 if err == nil && len(statuses) == 0 && len(msg.Directories) == 1 {
34 // For providers such as Zoho, we might get an empty list and
35 // no error when ref contains the name of a single directory.
36 // Workaround this bug by retrying with "".
37 statuses, err = w.client.liststatus.ListStatus("", "*", items, nil)
38 }
39 if err != nil {
40 return err
41 }
42 default:
43 for _, dir := range msg.Directories {
44 if len(w.worker.Actions()) > 0 {
45 remaining = append(remaining, dir)
46 continue
47 }
48 w.worker.Tracef("Getting status of directory %s", dir)
49 status, err := w.client.Status(dir, items)
50 if err != nil {
51 return err
52 }
53 statuses = append(statuses, status)
54 }
55 }
56 for _, status := range statuses {
57 refetch := false
58 if status.Name == w.selected.Name {
59 if status.UidNext != w.selected.UidNext {
60 refetch = true
61 } else if status.Messages != w.selected.Messages {
62 // Some providers (e.g. O365 upon new mail) might report the
63 // same UIDNEXT even though the contents of the folder has
64 // changed. So force a refetch if the server reports a
65 // number of messages different from what we currently have.
66 refetch = true
67 }
68 w.selected = status
69 }
70 info := &models.DirectoryInfo{
71 Name: status.Name,
72 Exists: int(status.Messages),
73 Recent: int(status.Recent),
74 Unseen: int(status.Unseen),
75 }
76 if status.UidValidity != 0 {
77 info.Uid = fmt.Sprintf("%d", status.UidValidity)
78 }
79 w.worker.PostMessage(&types.DirectoryInfo{
80 Info: info,
81 Refetch: refetch,
82 }, nil)
83 }
84 if len(remaining) > 0 {
85 w.worker.PostMessage(&types.CheckMailDirectories{
86 Message: types.RespondTo(msg),
87 Directories: remaining,
88 }, nil)
89 }
90 return nil
91 }