2019-07-08 02:43:57 +00:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/emersion/go-imap"
|
2019-07-08 02:43:58 +00:00
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-12-06 23:06:29 +00:00
|
|
|
"github.com/emersion/go-message/charset"
|
2020-11-10 18:57:09 +00:00
|
|
|
"github.com/emersion/go-message/mail"
|
2019-07-08 02:43:57 +00:00
|
|
|
)
|
|
|
|
|
2020-03-03 16:55:04 +00:00
|
|
|
func init() {
|
|
|
|
imap.CharsetReader = charset.Reader
|
|
|
|
}
|
|
|
|
|
2019-07-08 02:43:57 +00:00
|
|
|
func toSeqSet(uids []uint32) *imap.SeqSet {
|
|
|
|
var set imap.SeqSet
|
|
|
|
for _, uid := range uids {
|
|
|
|
set.AddNum(uid)
|
|
|
|
}
|
|
|
|
return &set
|
|
|
|
}
|
2019-07-08 02:43:58 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
2019-12-06 23:06:29 +00:00
|
|
|
|
|
|
|
// TODO: is that all?
|
|
|
|
|
2019-07-08 02:43:58 +00:00
|
|
|
return &models.BodyStructure{
|
|
|
|
MIMEType: bs.MIMEType,
|
|
|
|
MIMESubType: bs.MIMESubType,
|
2020-03-03 16:55:04 +00:00
|
|
|
Params: bs.Params,
|
|
|
|
Description: bs.Description,
|
2019-07-08 02:43:58 +00:00
|
|
|
Encoding: bs.Encoding,
|
|
|
|
Parts: parts,
|
|
|
|
Disposition: bs.Disposition,
|
2020-03-03 16:55:04 +00:00
|
|
|
DispositionParams: bs.DispositionParams,
|
2019-07-08 02:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func translateEnvelope(e *imap.Envelope) *models.Envelope {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-06 23:06:29 +00:00
|
|
|
|
2019-07-08 02:43:58 +00:00
|
|
|
return &models.Envelope{
|
|
|
|
Date: e.Date,
|
2020-03-03 16:55:04 +00:00
|
|
|
Subject: e.Subject,
|
2019-07-08 02:43:58 +00:00
|
|
|
From: translateAddresses(e.From),
|
|
|
|
ReplyTo: translateAddresses(e.ReplyTo),
|
|
|
|
To: translateAddresses(e.To),
|
|
|
|
Cc: translateAddresses(e.Cc),
|
|
|
|
Bcc: translateAddresses(e.Bcc),
|
|
|
|
MessageId: e.MessageId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:57:09 +00:00
|
|
|
func translateAddresses(addrs []*imap.Address) []*mail.Address {
|
|
|
|
var converted []*mail.Address
|
2019-07-08 02:43:58 +00:00
|
|
|
for _, addr := range addrs {
|
2020-11-10 18:57:09 +00:00
|
|
|
converted = append(converted, &mail.Address{
|
2020-03-03 16:55:04 +00:00
|
|
|
Name: addr.PersonalName,
|
2020-08-19 10:01:45 +00:00
|
|
|
Address: addr.Address(),
|
2020-03-03 16:55:04 +00:00
|
|
|
})
|
2019-07-08 02:43:58 +00:00
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-03-03 13:45:06 +00:00
|
|
|
var imapToFlag = map[string]models.Flag{
|
2019-07-08 02:43:58 +00:00
|
|
|
imap.SeenFlag: models.SeenFlag,
|
|
|
|
imap.RecentFlag: models.RecentFlag,
|
|
|
|
imap.AnsweredFlag: models.AnsweredFlag,
|
|
|
|
imap.DeletedFlag: models.DeletedFlag,
|
|
|
|
imap.FlaggedFlag: models.FlaggedFlag,
|
|
|
|
}
|
|
|
|
|
2020-03-03 13:45:06 +00:00
|
|
|
var flagToImap = map[models.Flag]string{
|
|
|
|
models.SeenFlag: imap.SeenFlag,
|
|
|
|
models.RecentFlag: imap.RecentFlag,
|
|
|
|
models.AnsweredFlag: imap.AnsweredFlag,
|
|
|
|
models.DeletedFlag: imap.DeletedFlag,
|
|
|
|
models.FlaggedFlag: imap.FlaggedFlag,
|
|
|
|
}
|
|
|
|
|
|
|
|
func translateImapFlags(imapFlags []string) []models.Flag {
|
2019-07-08 02:43:58 +00:00
|
|
|
var flags []models.Flag
|
|
|
|
for _, imapFlag := range imapFlags {
|
2020-03-03 13:45:06 +00:00
|
|
|
if flag, ok := imapToFlag[imapFlag]; ok {
|
2019-07-08 02:43:58 +00:00
|
|
|
flags = append(flags, flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
2020-03-03 13:45:06 +00:00
|
|
|
|
|
|
|
func translateFlags(flags []models.Flag) []string {
|
|
|
|
var imapFlags []string
|
|
|
|
for _, flag := range flags {
|
|
|
|
if imapFlag, ok := flagToImap[flag]; ok {
|
|
|
|
imapFlags = append(imapFlags, imapFlag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return imapFlags
|
|
|
|
}
|