summaryrefslogtreecommitdiff
path: root/worker/imap/fetch.go
blob: 383a8a8fcc3c1a6ee4376c0bcc1f930aa6c23967 (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
package imap

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

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

func (imapw *IMAPWorker) handleFetchMessageHeaders(
	msg *types.FetchMessageHeaders) {

	imapw.worker.Logger.Printf("Fetching message headers")

	go func() {
		messages := make(chan *imap.Message)
		done := make(chan error, 1)
		items := []imap.FetchItem{
			imap.FetchEnvelope,
			imap.FetchInternalDate,
			imap.FetchFlags,
			imap.FetchUid,
		}
		go func() {
			done <- imapw.client.UidFetch(&msg.Uids, items, messages)
		}()
		go func() {
			for msg := range messages {
				imapw.worker.PostMessage(&types.MessageInfo{
					Envelope:     msg.Envelope,
					Flags:        msg.Flags,
					InternalDate: msg.InternalDate,
					Uid:          msg.Uid,
				}, nil)
			}
			if err := <-done; err != nil {
				imapw.worker.PostMessage(&types.Error{
					Message: types.RespondTo(msg),
					Error:   err,
				}, nil)
			} else {
				imapw.worker.PostMessage(
					&types.Done{types.RespondTo(msg)}, nil)
			}
		}()
	}()
}