aboutsummaryrefslogtreecommitdiff
path: root/worker/imap/fetch.go
blob: feae462be48e385a1916ef6a8f042b051d91a9f3 (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
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
87
88
89
90
91
92
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")
	items := []imap.FetchItem{
		imap.FetchBodyStructure,
		imap.FetchEnvelope,
		imap.FetchInternalDate,
		imap.FetchFlags,
		imap.FetchUid,
	}

	imapw.handleFetchMessages(msg, &msg.Uids, items)
}

func (imapw *IMAPWorker) handleFetchMessageBodies(
	msg *types.FetchMessageBodies) {

	imapw.worker.Logger.Printf("Fetching message bodies")
	section := &imap.BodySectionName{}
	items := []imap.FetchItem{section.FetchItem()}
	imapw.handleFetchMessages(msg, &msg.Uids, items)
}

func (imapw *IMAPWorker) handleFetchMessageBodyPart(
	msg *types.FetchMessageBodyPart) {

	imapw.worker.Logger.Printf("Fetching message part")
	section := &imap.BodySectionName{}
	section.Path = []int{msg.Part}
	items := []imap.FetchItem{section.FetchItem()}
	uids := imap.SeqSet{}
	uids.AddNum(msg.Uid)
	imapw.handleFetchMessages(msg, &uids, items)
}

func (imapw *IMAPWorker) handleFetchMessages(
	msg types.WorkerMessage, uids *imap.SeqSet, items []imap.FetchItem) {

	go func() {
		messages := make(chan *imap.Message)
		done := make(chan error, 1)
		go func() {
			done <- imapw.client.UidFetch(uids, items, messages)
		}()
		go func() {
			section := &imap.BodySectionName{}
			for _msg := range messages {
				imapw.seqMap[_msg.SeqNum-1] = _msg.Uid
				switch msg.(type) {
				case *types.FetchMessageHeaders:
					imapw.worker.PostMessage(&types.MessageInfo{
						BodyStructure: _msg.BodyStructure,
						Envelope:      _msg.Envelope,
						Flags:         _msg.Flags,
						InternalDate:  _msg.InternalDate,
						Uid:           _msg.Uid,
					}, nil)
				case *types.FetchMessageBodies:
					reader := _msg.GetBody(section)
					imapw.worker.PostMessage(&types.MessageBody{
						Reader: reader,
						Uid:    _msg.Uid,
					}, nil)
				case *types.FetchMessageBodyPart:
					reader := _msg.GetBody(section)
					imapw.worker.PostMessage(&types.MessageBodyPart{
						Reader: reader,
						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)
			}
		}()
	}()
}