about summary refs log tree commit diff stats
path: root/worker/imap
diff options
context:
space:
mode:
authorLeszek CimaƂa <ernierasta@zori.cz>2019-12-07 00:06:29 +0100
committerDrew DeVault <sir@cmpwn.com>2019-12-07 14:30:33 -0500
commite84e402e489569b2ab1636ae04016464ec6d46c3 (patch)
treec06a894e45edbd00cb7233e9592c118305eb27ff /worker/imap
parent30aa77c1c975e7fa25e202add14971d4077cc9e1 (diff)
downloadaerc-e84e402e489569b2ab1636ae04016464ec6d46c3.tar.gz
decode(RFC 2047) imap headers to fix encoding in subject & filenames
Me again,
this time fixing encoding of subjects and attachments. It was problem in
IMAP backend. While other backends user MessageInfo() function which
generates MessageInfo decoded via go-message methodes, IMAP worker is
creating MessageInfo directly, so all non-utf8 subjects and filenames
were in raw form.

This patch fixes it. Not sure if we should care about errors (if
DecodeHeader fails it returns raw string back).

>From what I see, this should solve all encoding problem (tested only
IMAP). So, now I can focus on features. ;-)

Have a great weekend!
Leszek
Diffstat (limited to 'worker/imap')
-rw-r--r--worker/imap/imap.go30
1 files changed, 25 insertions, 5 deletions
diff --git a/worker/imap/imap.go b/worker/imap/imap.go
index 06bcd00..b0aacf6 100644
--- a/worker/imap/imap.go
+++ b/worker/imap/imap.go
@@ -4,6 +4,7 @@ import (
 	"github.com/emersion/go-imap"
 
 	"git.sr.ht/~sircmpwn/aerc/models"
+	"github.com/emersion/go-message/charset"
 )
 
 func toSeqSet(uids []uint32) *imap.SeqSet {
@@ -22,15 +23,30 @@ func translateBodyStructure(bs *imap.BodyStructure) *models.BodyStructure {
 	for _, part := range bs.Parts {
 		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:            bs.Params,
-		Description:       bs.Description,
+		Params:            params,
+		Description:       desc,
 		Encoding:          bs.Encoding,
 		Parts:             parts,
 		Disposition:       bs.Disposition,
-		DispositionParams: bs.DispositionParams,
+		DispositionParams: dispParams,
 	}
 }
 
@@ -38,9 +54,12 @@ 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:   e.Subject,
+		Subject:   subject,
 		From:      translateAddresses(e.From),
 		ReplyTo:   translateAddresses(e.ReplyTo),
 		To:        translateAddresses(e.To),
@@ -54,8 +73,9 @@ func translateAddress(a *imap.Address) *models.Address {
 	if a == nil {
 		return nil
 	}
+	personalName, _ := charset.DecodeHeader(a.PersonalName)
 	return &models.Address{
-		Name:    a.PersonalName,
+		Name:    personalName,
 		Mailbox: a.MailboxName,
 		Host:    a.HostName,
 	}