summary refs log tree commit diff stats
path: root/worker/imap
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-07-07 22:43:58 -0400
committerDrew DeVault <sir@cmpwn.com>2019-07-08 16:06:28 -0400
commitc610c3cd9dd47c400e52c1858e987f5f32a7a45b (patch)
tree6e521ba706d87ea4a03ce81d57ff84317506f3df /worker/imap
parent88c379dcbaaf9fd549cd271817e79fe634b1dd84 (diff)
downloadaerc-c610c3cd9dd47c400e52c1858e987f5f32a7a45b.tar.gz
Factor IMAP-specific structs out of UI models
Before, we were using several IMAP-specific concepts to represent
information being displayed in the UI. Factor these structures out of
the IMAP package to make it easier for other backends to provide the
required information.
Diffstat (limited to 'worker/imap')
-rw-r--r--worker/imap/fetch.go10
-rw-r--r--worker/imap/imap.go75
-rw-r--r--worker/imap/worker.go6
3 files changed, 83 insertions, 8 deletions
diff --git a/worker/imap/fetch.go b/worker/imap/fetch.go
index fe25977..1745ead 100644
--- a/worker/imap/fetch.go
+++ b/worker/imap/fetch.go
@@ -82,9 +82,9 @@ func (imapw *IMAPWorker) handleFetchMessages(
 				imapw.worker.PostMessage(&types.MessageInfo{
 					Message: types.RespondTo(msg),
 					Info: &models.MessageInfo{
-						BodyStructure: _msg.BodyStructure,
-						Envelope:      _msg.Envelope,
-						Flags:         _msg.Flags,
+						BodyStructure: translateBodyStructure(_msg.BodyStructure),
+						Envelope:      translateEnvelope(_msg.Envelope),
+						Flags:         translateFlags(_msg.Flags),
 						InternalDate:  _msg.InternalDate,
 						RFC822Headers: header,
 						Uid:           _msg.Uid,
@@ -103,7 +103,7 @@ func (imapw *IMAPWorker) handleFetchMessages(
 				imapw.worker.PostMessage(&types.MessageInfo{
 					Message: types.RespondTo(msg),
 					Info: &models.MessageInfo{
-						Flags: _msg.Flags,
+						Flags: translateFlags(_msg.Flags),
 						Uid:   _msg.Uid,
 					},
 				}, nil)
@@ -120,7 +120,7 @@ func (imapw *IMAPWorker) handleFetchMessages(
 				imapw.worker.PostMessage(&types.MessageInfo{
 					Message: types.RespondTo(msg),
 					Info: &models.MessageInfo{
-						Flags: _msg.Flags,
+						Flags: translateFlags(_msg.Flags),
 						Uid:   _msg.Uid,
 					},
 				}, nil)
diff --git a/worker/imap/imap.go b/worker/imap/imap.go
index 28bac93..06bcd00 100644
--- a/worker/imap/imap.go
+++ b/worker/imap/imap.go
@@ -2,6 +2,8 @@ package imap
 
 import (
 	"github.com/emersion/go-imap"
+
+	"git.sr.ht/~sircmpwn/aerc/models"
 )
 
 func toSeqSet(uids []uint32) *imap.SeqSet {
@@ -11,3 +13,76 @@ func toSeqSet(uids []uint32) *imap.SeqSet {
 	}
 	return &set
 }
+
+func translateBodyStructure(bs *imap.BodyStructure) *models.BodyStructure {
+	if bs == nil {
+		return nil
+	}
+	var parts []*models.BodyStructure
+	for _, part := range bs.Parts {
+		parts = append(parts, translateBodyStructure(part))
+	}
+	return &models.BodyStructure{
+		MIMEType:          bs.MIMEType,
+		MIMESubType:       bs.MIMESubType,
+		Params:            bs.Params,
+		Description:       bs.Description,
+		Encoding:          bs.Encoding,
+		Parts:             parts,
+		Disposition:       bs.Disposition,
+		DispositionParams: bs.DispositionParams,
+	}
+}
+
+func translateEnvelope(e *imap.Envelope) *models.Envelope {
+	if e == nil {
+		return nil
+	}
+	return &models.Envelope{
+		Date:      e.Date,
+		Subject:   e.Subject,
+		From:      translateAddresses(e.From),
+		ReplyTo:   translateAddresses(e.ReplyTo),
+		To:        translateAddresses(e.To),
+		Cc:        translateAddresses(e.Cc),
+		Bcc:       translateAddresses(e.Bcc),
+		MessageId: e.MessageId,
+	}
+}
+
+func translateAddress(a *imap.Address) *models.Address {
+	if a == nil {
+		return nil
+	}
+	return &models.Address{
+		Name:    a.PersonalName,
+		Mailbox: a.MailboxName,
+		Host:    a.HostName,
+	}
+}
+
+func translateAddresses(addrs []*imap.Address) []*models.Address {
+	var converted []*models.Address
+	for _, addr := range addrs {
+		converted = append(converted, translateAddress(addr))
+	}
+	return converted
+}
+
+var flagMap = map[string]models.Flag{
+	imap.SeenFlag:     models.SeenFlag,
+	imap.RecentFlag:   models.RecentFlag,
+	imap.AnsweredFlag: models.AnsweredFlag,
+	imap.DeletedFlag:  models.DeletedFlag,
+	imap.FlaggedFlag:  models.FlaggedFlag,
+}
+
+func translateFlags(imapFlags []string) []models.Flag {
+	var flags []models.Flag
+	for _, imapFlag := range imapFlags {
+		if flag, ok := flagMap[imapFlag]; ok {
+			flags = append(flags, flag)
+		}
+	}
+	return flags
+}
diff --git a/worker/imap/worker.go b/worker/imap/worker.go
index 9ddaa47..88f8b37 100644
--- a/worker/imap/worker.go
+++ b/worker/imap/worker.go
@@ -187,9 +187,9 @@ func (w *IMAPWorker) handleImapUpdate(update client.Update) {
 		}
 		w.worker.PostMessage(&types.MessageInfo{
 			Info: &models.MessageInfo{
-				BodyStructure: msg.BodyStructure,
-				Envelope:      msg.Envelope,
-				Flags:         msg.Flags,
+				BodyStructure: translateBodyStructure(msg.BodyStructure),
+				Envelope:      translateEnvelope(msg.Envelope),
+				Flags:         translateFlags(msg.Flags),
 				InternalDate:  msg.InternalDate,
 				Uid:           msg.Uid,
 			},