worker/imap: rely on go-imap for charset handling

Set imap.CharsetReader so that go-imap can automatically decode all
encoded fields.
This commit is contained in:
Simon Ser 2020-03-03 16:55:04 +00:00 committed by Drew DeVault
parent 6ff3c7a1ba
commit f3535703b0

View file

@ -7,6 +7,10 @@ import (
"github.com/emersion/go-message/charset" "github.com/emersion/go-message/charset"
) )
func init() {
imap.CharsetReader = charset.Reader
}
func toSeqSet(uids []uint32) *imap.SeqSet { func toSeqSet(uids []uint32) *imap.SeqSet {
var set imap.SeqSet var set imap.SeqSet
for _, uid := range uids { for _, uid := range uids {
@ -24,29 +28,17 @@ func translateBodyStructure(bs *imap.BodyStructure) *models.BodyStructure {
parts = append(parts, translateBodyStructure(part)) 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? // TODO: is that all?
return &models.BodyStructure{ return &models.BodyStructure{
MIMEType: bs.MIMEType, MIMEType: bs.MIMEType,
MIMESubType: bs.MIMESubType, MIMESubType: bs.MIMESubType,
Params: params, Params: bs.Params,
Description: desc, Description: bs.Description,
Encoding: bs.Encoding, Encoding: bs.Encoding,
Parts: parts, Parts: parts,
Disposition: bs.Disposition, Disposition: bs.Disposition,
DispositionParams: dispParams, DispositionParams: bs.DispositionParams,
} }
} }
@ -54,12 +46,10 @@ func translateEnvelope(e *imap.Envelope) *models.Envelope {
if e == nil { if e == nil {
return nil return nil
} }
// TODO: where we should send error?
subject, _ := charset.DecodeHeader(e.Subject)
return &models.Envelope{ return &models.Envelope{
Date: e.Date, Date: e.Date,
Subject: subject, Subject: e.Subject,
From: translateAddresses(e.From), From: translateAddresses(e.From),
ReplyTo: translateAddresses(e.ReplyTo), ReplyTo: translateAddresses(e.ReplyTo),
To: translateAddresses(e.To), 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 { func translateAddresses(addrs []*imap.Address) []*models.Address {
var converted []*models.Address var converted []*models.Address
for _, addr := range addrs { 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 return converted
} }