2019-07-11 15:44:51 +02:00
|
|
|
package maildir
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/emersion/go-maildir"
|
|
|
|
"github.com/emersion/go-message"
|
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-08-05 09:16:07 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/worker/lib"
|
2019-07-11 15:44:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Message is an individual email inside of a maildir.Dir.
|
|
|
|
type Message struct {
|
|
|
|
dir maildir.Dir
|
|
|
|
uid uint32
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewReader reads a message into memory and returns an io.Reader for it.
|
|
|
|
func (m Message) NewReader() (io.Reader, error) {
|
|
|
|
f, err := m.dir.Open(m.key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bytes.NewReader(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flags fetches the set of flags currently applied to the message.
|
|
|
|
func (m Message) Flags() ([]maildir.Flag, error) {
|
|
|
|
return m.dir.Flags(m.key)
|
|
|
|
}
|
|
|
|
|
2019-08-05 09:16:07 +02:00
|
|
|
// ModelFlags fetches the set of models.flags currently applied to the message.
|
|
|
|
func (m Message) ModelFlags() ([]models.Flag, error) {
|
|
|
|
flags, err := m.dir.Flags(m.key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-03 14:45:06 +01:00
|
|
|
return translateMaildirFlags(flags), nil
|
2019-08-05 09:16:07 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
// SetFlags replaces the message's flags with a new set.
|
|
|
|
func (m Message) SetFlags(flags []maildir.Flag) error {
|
|
|
|
return m.dir.SetFlags(m.key, flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkRead either adds or removes the maildir.FlagSeen flag from the message.
|
|
|
|
func (m Message) MarkRead(seen bool) error {
|
|
|
|
flags, err := m.Flags()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not read previous flags: %v", err)
|
|
|
|
}
|
|
|
|
if seen {
|
|
|
|
flags = append(flags, maildir.FlagSeen)
|
|
|
|
return m.SetFlags(flags)
|
|
|
|
}
|
|
|
|
var newFlags []maildir.Flag
|
|
|
|
for _, flag := range flags {
|
|
|
|
if flag != maildir.FlagSeen {
|
|
|
|
newFlags = append(newFlags, flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m.SetFlags(newFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes the email immediately.
|
|
|
|
func (m Message) Remove() error {
|
|
|
|
return m.dir.Remove(m.key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MessageInfo populates a models.MessageInfo struct for the message.
|
|
|
|
func (m Message) MessageInfo() (*models.MessageInfo, error) {
|
2019-08-05 09:16:07 +02:00
|
|
|
return lib.MessageInfo(m)
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBodyPartReader creates a new io.Reader for the requested body part(s) of
|
|
|
|
// the message.
|
|
|
|
func (m Message) NewBodyPartReader(requestedParts []int) (io.Reader, error) {
|
|
|
|
f, err := m.dir.Open(m.key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
msg, err := message.Read(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not read message: %v", err)
|
|
|
|
}
|
2019-08-05 09:16:07 +02:00
|
|
|
return lib.FetchEntityPartReader(msg, requestedParts)
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 14:45:06 +01:00
|
|
|
var maildirToFlag = map[maildir.Flag]models.Flag{
|
2019-07-11 15:44:51 +02:00
|
|
|
maildir.FlagReplied: models.AnsweredFlag,
|
|
|
|
maildir.FlagSeen: models.SeenFlag,
|
|
|
|
maildir.FlagTrashed: models.DeletedFlag,
|
|
|
|
maildir.FlagFlagged: models.FlaggedFlag,
|
|
|
|
// maildir.FlagDraft Flag = 'D'
|
|
|
|
// maildir.FlagPassed Flag = 'P'
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:45:06 +01:00
|
|
|
var flagToMaildir = map[models.Flag]maildir.Flag{
|
|
|
|
models.AnsweredFlag: maildir.FlagReplied,
|
|
|
|
models.SeenFlag: maildir.FlagSeen,
|
|
|
|
models.DeletedFlag: maildir.FlagTrashed,
|
|
|
|
models.FlaggedFlag: maildir.FlagFlagged,
|
|
|
|
// maildir.FlagDraft Flag = 'D'
|
|
|
|
// maildir.FlagPassed Flag = 'P'
|
|
|
|
}
|
|
|
|
|
|
|
|
func translateMaildirFlags(maildirFlags []maildir.Flag) []models.Flag {
|
2019-07-11 15:44:51 +02:00
|
|
|
var flags []models.Flag
|
|
|
|
for _, maildirFlag := range maildirFlags {
|
2020-03-03 14:45:06 +01:00
|
|
|
if flag, ok := maildirToFlag[maildirFlag]; ok {
|
2019-07-11 15:44:51 +02:00
|
|
|
flags = append(flags, flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
2019-08-05 09:16:07 +02:00
|
|
|
|
2020-03-03 14:45:06 +01:00
|
|
|
func translateFlags(flags []models.Flag) []maildir.Flag {
|
|
|
|
var maildirFlags []maildir.Flag
|
|
|
|
for _, flag := range flags {
|
|
|
|
if maildirFlag, ok := flagToMaildir[flag]; ok {
|
|
|
|
maildirFlags = append(maildirFlags, maildirFlag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maildirFlags
|
|
|
|
}
|
|
|
|
|
2019-08-05 09:16:07 +02:00
|
|
|
func (m Message) UID() uint32 {
|
|
|
|
return m.uid
|
|
|
|
}
|
2019-12-23 12:51:58 +01:00
|
|
|
|
|
|
|
func (m Message) Labels() ([]string, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|