2019-07-08 04:43:57 +02:00
|
|
|
package imap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/emersion/go-imap"
|
2019-07-08 04:43:58 +02:00
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-12-07 00:06:29 +01:00
|
|
|
"github.com/emersion/go-message/charset"
|
2020-11-10 19:57:09 +01:00
|
|
|
"github.com/emersion/go-message/mail"
|
2019-07-08 04:43:57 +02:00
|
|
|
)
|
|
|
|
|
2020-03-03 17:55:04 +01:00
|
|
|
func init() {
|
|
|
|
imap.CharsetReader = charset.Reader
|
|
|
|
}
|
|
|
|
|
2019-07-08 04:43:57 +02:00
|
|
|
func toSeqSet(uids []uint32) *imap.SeqSet {
|
|
|
|
var set imap.SeqSet
|
|
|
|
for _, uid := range uids {
|
|
|
|
set.AddNum(uid)
|
|
|
|
}
|
|
|
|
return &set
|
|
|
|
}
|
2019-07-08 04:43:58 +02: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-07 00:06:29 +01:00
|
|
|
|
|
|
|
// TODO: is that all?
|
|
|
|
|
2019-07-08 04:43:58 +02:00
|
|
|
return &models.BodyStructure{
|
|
|
|
MIMEType: bs.MIMEType,
|
|
|
|
MIMESubType: bs.MIMESubType,
|
2020-03-03 17:55:04 +01:00
|
|
|
Params: bs.Params,
|
|
|
|
Description: bs.Description,
|
2019-07-08 04:43:58 +02:00
|
|
|
Encoding: bs.Encoding,
|
|
|
|
Parts: parts,
|
|
|
|
Disposition: bs.Disposition,
|
2020-03-03 17:55:04 +01:00
|
|
|
DispositionParams: bs.DispositionParams,
|
2019-07-08 04:43:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func translateEnvelope(e *imap.Envelope) *models.Envelope {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-07 00:06:29 +01:00
|
|
|
|
2020-11-08 14:18:14 +01:00
|
|
|
// we strip the msgid of "<>" in order to be more compatible with go-message
|
|
|
|
// which wants to handle msgids without the markers
|
|
|
|
// note this is a very naive way of doing it but probably good enough
|
|
|
|
msgID := e.MessageId
|
|
|
|
if len(msgID) > 1 && msgID[0] == '<' && msgID[len(msgID)-1] == '>' {
|
|
|
|
msgID = msgID[1 : len(msgID)-1]
|
|
|
|
}
|
|
|
|
|
2019-07-08 04:43:58 +02:00
|
|
|
return &models.Envelope{
|
|
|
|
Date: e.Date,
|
2020-03-03 17:55:04 +01:00
|
|
|
Subject: e.Subject,
|
2019-07-08 04:43:58 +02:00
|
|
|
From: translateAddresses(e.From),
|
|
|
|
ReplyTo: translateAddresses(e.ReplyTo),
|
|
|
|
To: translateAddresses(e.To),
|
|
|
|
Cc: translateAddresses(e.Cc),
|
|
|
|
Bcc: translateAddresses(e.Bcc),
|
2020-11-08 14:18:14 +01:00
|
|
|
MessageId: msgID,
|
2019-07-08 04:43:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 19:57:09 +01:00
|
|
|
func translateAddresses(addrs []*imap.Address) []*mail.Address {
|
|
|
|
var converted []*mail.Address
|
2019-07-08 04:43:58 +02:00
|
|
|
for _, addr := range addrs {
|
2020-11-10 19:57:09 +01:00
|
|
|
converted = append(converted, &mail.Address{
|
2020-03-03 17:55:04 +01:00
|
|
|
Name: addr.PersonalName,
|
2020-08-19 12:01:45 +02:00
|
|
|
Address: addr.Address(),
|
2020-03-03 17:55:04 +01:00
|
|
|
})
|
2019-07-08 04:43:58 +02:00
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:45:06 +01:00
|
|
|
var imapToFlag = map[string]models.Flag{
|
2019-07-08 04:43:58 +02:00
|
|
|
imap.SeenFlag: models.SeenFlag,
|
|
|
|
imap.RecentFlag: models.RecentFlag,
|
|
|
|
imap.AnsweredFlag: models.AnsweredFlag,
|
|
|
|
imap.DeletedFlag: models.DeletedFlag,
|
|
|
|
imap.FlaggedFlag: models.FlaggedFlag,
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:45:06 +01: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 04:43:58 +02:00
|
|
|
var flags []models.Flag
|
|
|
|
for _, imapFlag := range imapFlags {
|
2020-03-03 14:45:06 +01:00
|
|
|
if flag, ok := imapToFlag[imapFlag]; ok {
|
2019-07-08 04:43:58 +02:00
|
|
|
flags = append(flags, flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
2020-03-03 14:45:06 +01: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
|
|
|
|
}
|