aerc/worker/imap/fetch.go

96 lines
2.5 KiB
Go
Raw Normal View History

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")
2019-03-30 02:35:53 +00:00
items := []imap.FetchItem{
2019-03-31 15:10:10 +00:00
imap.FetchBodyStructure,
2019-03-30 02:35:53 +00:00
imap.FetchEnvelope,
imap.FetchInternalDate,
imap.FetchFlags,
imap.FetchUid,
}
2019-03-31 16:35:51 +00:00
imapw.handleFetchMessages(msg, &msg.Uids, items, nil)
2019-03-30 02:35:53 +00:00
}
2019-03-31 16:14:37 +00:00
func (imapw *IMAPWorker) handleFetchMessageBodyPart(
msg *types.FetchMessageBodyPart) {
imapw.worker.Logger.Printf("Fetching message part")
section := &imap.BodySectionName{}
2019-03-31 16:35:51 +00:00
section.Path = []int{msg.Part + 1}
2019-03-31 16:14:37 +00:00
items := []imap.FetchItem{section.FetchItem()}
uids := imap.SeqSet{}
uids.AddNum(msg.Uid)
2019-03-31 16:35:51 +00:00
imapw.handleFetchMessages(msg, &uids, items, section)
2019-03-31 16:14:37 +00: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 16:35:51 +00:00
imapw.handleFetchMessages(msg, &msg.Uids, items, section)
}
2019-03-30 02:35:53 +00:00
func (imapw *IMAPWorker) handleFetchMessages(
2019-03-31 16:35:51 +00:00
msg types.WorkerMessage, uids *imap.SeqSet, items []imap.FetchItem,
section *imap.BodySectionName) {
go func() {
messages := make(chan *imap.Message)
done := make(chan error, 1)
go func() {
2019-03-30 02:35:53 +00:00
done <- imapw.client.UidFetch(uids, items, messages)
}()
go func() {
2019-03-30 02:35:53 +00:00
for _msg := range messages {
imapw.seqMap[_msg.SeqNum-1] = _msg.Uid
2019-03-31 16:14:37 +00:00
switch msg.(type) {
case *types.FetchMessageHeaders:
2019-03-30 02:35:53 +00:00
imapw.worker.PostMessage(&types.MessageInfo{
2019-03-31 16:35:51 +00:00
Message: types.RespondTo(msg),
2019-03-31 15:10:10 +00:00
BodyStructure: _msg.BodyStructure,
Envelope: _msg.Envelope,
Flags: _msg.Flags,
InternalDate: _msg.InternalDate,
Uid: _msg.Uid,
2019-03-30 02:35:53 +00:00
}, nil)
case *types.FetchFullMessages:
2019-03-31 16:14:37 +00:00
reader := _msg.GetBody(section)
2019-03-31 16:35:51 +00:00
imapw.worker.PostMessage(&types.FullMessage{
Message: types.RespondTo(msg),
Reader: reader,
Uid: _msg.Uid,
2019-03-31 16:14:37 +00:00
}, nil)
case *types.FetchMessageBodyPart:
reader := _msg.GetBody(section)
imapw.worker.PostMessage(&types.MessageBodyPart{
2019-03-31 16:35:51 +00:00
Message: types.RespondTo(msg),
Reader: reader,
Uid: _msg.Uid,
2019-03-31 16:14:37 +00:00
}, nil)
2019-03-30 02:35:53 +00:00
}
}
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)
}
}()
}()
}