aboutsummaryrefslogtreecommitdiff
path: root/worker/imap/open.go
blob: 0f25c5e0e6e0188ebdd26951348ba7bcc7eccdfc (plain)
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
package imap

import (
	"github.com/emersion/go-imap"

	"git.sr.ht/~sircmpwn/aerc2/worker/types"
)

func (imapw *IMAPWorker) handleOpenDirectory(msg *types.OpenDirectory) {
	imapw.worker.Logger.Printf("Opening %s", msg.Directory)
	go func() {
		_, err := imapw.client.Select(msg.Directory, false)
		if err != nil {
			imapw.worker.PostMessage(&types.Error{
				Message: types.RespondTo(msg),
				Error:   err,
			}, nil)
		} else {
			imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
		}
	}()
}

func (imapw *IMAPWorker) handleFetchDirectoryContents(
	msg *types.FetchDirectoryContents) {

	imapw.worker.Logger.Printf("Fetching UID list")

	go func() {
		seqSet := &imap.SeqSet{}
		seqSet.AddRange(1, imapw.selected.Messages)
		uid32, err := imapw.client.UidSearch(&imap.SearchCriteria{
			SeqNum: seqSet,
		})
		if err != nil {
			imapw.worker.PostMessage(&types.Error{
				Message: types.RespondTo(msg),
				Error:   err,
			}, nil)
		} else {
			imapw.worker.Logger.Printf("Found %d UIDs", len(uid32))
			var uids []uint64
			for _, uid := range uid32 {
				uids = append(uids,
					(uint64(imapw.selected.UidValidity)<<32)|uint64(uid))
			}
			imapw.worker.PostMessage(&types.DirectoryContents{
				Message: types.RespondTo(msg),
				Uids:    uids,
			}, nil)
			imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
		}
	}()
}