aerc/worker/imap/fetch.go

81 lines
2 KiB
Go
Raw Normal View History

package imap
import (
"github.com/emersion/go-imap"
2019-03-30 02:35:53 +00:00
"github.com/mohamedattahri/mail"
"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,
}
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) handleFetchMessages(
msg types.WorkerMessage, uids *imap.SeqSet, items []imap.FetchItem) {
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
section := &imap.BodySectionName{}
for _msg := range messages {
imapw.seqMap[_msg.SeqNum-1] = _msg.Uid
if reader := _msg.GetBody(section); reader != nil {
email, err := mail.ReadMessage(reader)
if err != nil {
imapw.worker.PostMessage(&types.Error{
Message: types.RespondTo(msg),
Error: err,
}, nil)
}
imapw.worker.PostMessage(&types.MessageBody{
Mail: email,
Uid: _msg.Uid,
}, nil)
} else {
imapw.worker.PostMessage(&types.MessageInfo{
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)
}
}
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)
}
}()
}()
}