aerc/worker/imap/fetch.go

92 lines
2.3 KiB
Go
Raw Normal View History

package imap
import (
"github.com/emersion/go-imap"
2019-05-18 02:57:10 +02:00
"git.sr.ht/~sircmpwn/aerc/worker/types"
)
func (imapw *IMAPWorker) handleFetchMessageHeaders(
msg *types.FetchMessageHeaders) {
imapw.worker.Logger.Printf("Fetching message headers")
2019-03-30 03:35:53 +01:00
items := []imap.FetchItem{
2019-03-31 17:10:10 +02:00
imap.FetchBodyStructure,
2019-03-30 03:35:53 +01:00
imap.FetchEnvelope,
imap.FetchInternalDate,
imap.FetchFlags,
imap.FetchUid,
}
2019-03-31 18:35:51 +02:00
imapw.handleFetchMessages(msg, &msg.Uids, items, nil)
2019-03-30 03:35:53 +01:00
}
2019-03-31 18:14:37 +02:00
func (imapw *IMAPWorker) handleFetchMessageBodyPart(
msg *types.FetchMessageBodyPart) {
imapw.worker.Logger.Printf("Fetching message part")
section := &imap.BodySectionName{}
2019-05-20 22:42:44 +02:00
section.Path = msg.Part
2019-03-31 18:14:37 +02:00
items := []imap.FetchItem{section.FetchItem()}
uids := imap.SeqSet{}
uids.AddNum(msg.Uid)
2019-03-31 18:35:51 +02:00
imapw.handleFetchMessages(msg, &uids, items, section)
2019-03-31 18:14:37 +02:00
}
func (imapw *IMAPWorker) handleFetchFullMessages(
msg *types.FetchFullMessages) {
imapw.worker.Logger.Printf("Fetching full messages")
section := &imap.BodySectionName{}
items := []imap.FetchItem{section.FetchItem()}
2019-03-31 18:35:51 +02:00
imapw.handleFetchMessages(msg, &msg.Uids, items, section)
}
2019-03-30 03:35:53 +01:00
func (imapw *IMAPWorker) handleFetchMessages(
2019-03-31 18:35:51 +02:00
msg types.WorkerMessage, uids *imap.SeqSet, items []imap.FetchItem,
section *imap.BodySectionName) {
messages := make(chan *imap.Message)
go func() {
for _msg := range messages {
imapw.seqMap[_msg.SeqNum-1] = _msg.Uid
switch msg.(type) {
case *types.FetchMessageHeaders:
imapw.worker.PostMessage(&types.MessageInfo{
Message: types.RespondTo(msg),
BodyStructure: _msg.BodyStructure,
Envelope: _msg.Envelope,
Flags: _msg.Flags,
InternalDate: _msg.InternalDate,
Uid: _msg.Uid,
}, nil)
case *types.FetchFullMessages:
reader := _msg.GetBody(section)
imapw.worker.PostMessage(&types.FullMessage{
Message: types.RespondTo(msg),
Reader: reader,
Uid: _msg.Uid,
}, nil)
case *types.FetchMessageBodyPart:
reader := _msg.GetBody(section)
imapw.worker.PostMessage(&types.MessageBodyPart{
Message: types.RespondTo(msg),
Reader: reader,
Uid: _msg.Uid,
}, nil)
}
}
}()
if err := imapw.client.UidFetch(uids, items, messages); err != nil {
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
} else {
imapw.worker.PostMessage(
&types.Done{types.RespondTo(msg)}, nil)
}
}