summary refs log tree commit diff stats
path: root/worker/imap
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2020-03-03 16:55:04 +0000
committerDrew DeVault <sir@cmpwn.com>2020-03-03 16:48:00 -0500
commitf3535703b0b92ff88d7a7fb6837078a21967c832 (patch)
tree0cc37b1d5410ece345ac3c9b419c1855a8383086 /worker/imap
parent6ff3c7a1ba680506d77fc1fe8dfbf5b804a3fea7 (diff)
downloadaerc-f3535703b0b92ff88d7a7fb6837078a21967c832.tar.gz
worker/imap: rely on go-imap for charset handling
Set imap.CharsetReader so that go-imap can automatically decode all
encoded fields.
Diffstat (limited to 'worker/imap')
-rw-r--r--worker/imap/imap.go44
1 files changed, 13 insertions, 31 deletions
diff --git a/worker/imap/imap.go b/worker/imap/imap.go
index fdcbc38..7afab02 100644
--- a/worker/imap/imap.go
+++ b/worker/imap/imap.go
@@ -7,6 +7,10 @@ import (
 	"github.com/emersion/go-message/charset"
 )
 
+func init() {
+	imap.CharsetReader = charset.Reader
+}
+
 func toSeqSet(uids []uint32) *imap.SeqSet {
 	var set imap.SeqSet
 	for _, uid := range uids {
@@ -24,29 +28,17 @@ func translateBodyStructure(bs *imap.BodyStructure) *models.BodyStructure {
 		parts = append(parts, translateBodyStructure(part))
 	}
 
-	// we need to decode, because imap store do not use MessageInfo()
-	// which do it via go-message
-	desc, _ := charset.DecodeHeader(bs.Description)
-	params := map[string]string{}
-	for k, v := range bs.Params {
-		params[k], _ = charset.DecodeHeader(v)
-	}
-	dispParams := map[string]string{}
-	for k, v := range bs.DispositionParams {
-		dispParams[k], _ = charset.DecodeHeader(v)
-	}
-
 	// TODO: is that all?
 
 	return &models.BodyStructure{
 		MIMEType:          bs.MIMEType,
 		MIMESubType:       bs.MIMESubType,
-		Params:            params,
-		Description:       desc,
+		Params:            bs.Params,
+		Description:       bs.Description,
 		Encoding:          bs.Encoding,
 		Parts:             parts,
 		Disposition:       bs.Disposition,
-		DispositionParams: dispParams,
+		DispositionParams: bs.DispositionParams,
 	}
 }
 
@@ -54,12 +46,10 @@ func translateEnvelope(e *imap.Envelope) *models.Envelope {
 	if e == nil {
 		return nil
 	}
-	// TODO: where we should send error?
-	subject, _ := charset.DecodeHeader(e.Subject)
 
 	return &models.Envelope{
 		Date:      e.Date,
-		Subject:   subject,
+		Subject:   e.Subject,
 		From:      translateAddresses(e.From),
 		ReplyTo:   translateAddresses(e.ReplyTo),
 		To:        translateAddresses(e.To),
@@ -69,22 +59,14 @@ func translateEnvelope(e *imap.Envelope) *models.Envelope {
 	}
 }
 
-func translateAddress(a *imap.Address) *models.Address {
-	if a == nil {
-		return nil
-	}
-	personalName, _ := charset.DecodeHeader(a.PersonalName)
-	return &models.Address{
-		Name:    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))
+		converted = append(converted, &models.Address{
+			Name:    addr.PersonalName,
+			Mailbox: addr.MailboxName,
+			Host:    addr.HostName,
+		})
 	}
 	return converted
 }