about summary refs log tree commit diff stats
path: root/worker
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
parent84e9853c1613f43f76bd46f0c1eb143d1db16ac2 (diff)
downloadaerc-77ede6eb5a22a5407541ac587736189fcca0037f.tar.gz
Add body fetching support code
Diffstat (limited to 'worker')
-rw-r--r--worker/imap/fetch.go61
-rw-r--r--worker/imap/worker.go2
-rw-r--r--worker/types/messages.go9
3 files changed, 55 insertions, 17 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{
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