about summary refs log tree commit diff stats
path: root/worker/imap/fetch.go
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-03-29 22:35:53 -0400
committerDrew DeVault <sir@cmpwn.com>2019-03-29 22:36:15 -0400
commit77ede6eb5a22a5407541ac587736189fcca0037f (patch)
tree45627c15f7087f91ed9881e776a566d440bd878e /worker/imap/fetch.go
parent84e9853c1613f43f76bd46f0c1eb143d1db16ac2 (diff)
downloadaerc-77ede6eb5a22a5407541ac587736189fcca0037f.tar.gz
Add body fetching support code
Diffstat (limited to 'worker/imap/fetch.go')
-rw-r--r--worker/imap/fetch.go61
1 files changed, 46 insertions, 15 deletions
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{