From 77ede6eb5a22a5407541ac587736189fcca0037f Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 29 Mar 2019 22:35:53 -0400 Subject: Add body fetching support code --- worker/imap/fetch.go | 61 ++++++++++++++++++++++++++++++++++++------------ worker/imap/worker.go | 2 ++ worker/types/messages.go | 9 +++++-- 3 files changed, 55 insertions(+), 17 deletions(-) (limited to 'worker') diff --git a/worker/imap/fetch.go b/worker/imap/fetch.go index 489dbe4..884ab73 100644 --- a/worker/imap/fetch.go +++ b/worker/imap/fetch.go @@ -2,6 +2,7 @@ package imap import ( "github.com/emersion/go-imap" + "github.com/mohamedattahri/mail" "git.sr.ht/~sircmpwn/aerc2/worker/types" ) @@ -10,28 +11,58 @@ func (imapw *IMAPWorker) handleFetchMessageHeaders( msg *types.FetchMessageHeaders) { imapw.worker.Logger.Printf("Fetching message headers") + items := []imap.FetchItem{ + 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) - items := []imap.FetchItem{ - imap.FetchEnvelope, - imap.FetchInternalDate, - imap.FetchFlags, - imap.FetchUid, - } go func() { - done <- imapw.client.UidFetch(&msg.Uids, items, messages) + done <- imapw.client.UidFetch(uids, items, messages) }() go func() { - for msg := range messages { - imapw.seqMap[msg.SeqNum-1] = msg.Uid - imapw.worker.PostMessage(&types.MessageInfo{ - Envelope: msg.Envelope, - Flags: msg.Flags, - InternalDate: msg.InternalDate, - Uid: msg.Uid, - }, nil) + 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{ + Envelope: _msg.Envelope, + Flags: _msg.Flags, + InternalDate: _msg.InternalDate, + Uid: _msg.Uid, + }, nil) + } } if err := <-done; err != nil { imapw.worker.PostMessage(&types.Error{ diff --git a/worker/imap/worker.go b/worker/imap/worker.go index ea7f317..2f98595 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -158,6 +158,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { w.handleFetchDirectoryContents(msg) case *types.FetchMessageHeaders: w.handleFetchMessageHeaders(msg) + case *types.FetchMessageBodies: + w.handleFetchMessageBodies(msg) case *types.DeleteMessages: w.handleDeleteMessages(msg) default: diff --git a/worker/types/messages.go b/worker/types/messages.go index ff2c36b..f38bb22 100644 --- a/worker/types/messages.go +++ b/worker/types/messages.go @@ -2,10 +2,10 @@ package types import ( "crypto/x509" - "net/mail" "time" "github.com/emersion/go-imap" + "github.com/mohamedattahri/mail" "git.sr.ht/~sircmpwn/aerc2/config" ) @@ -123,11 +123,16 @@ type MessageInfo struct { Envelope *imap.Envelope Flags []string InternalDate time.Time - Mail *mail.Message Size uint32 Uid uint32 } +type MessageBody struct { + Message + Mail *mail.Message + Uid uint32 +} + type MessagesDeleted struct { Message Uids []uint32 -- cgit 1.4.1-2-gfad0