2019-07-11 15:44:51 +02:00
|
|
|
package maildir
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/emersion/go-maildir"
|
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/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.
|
2022-01-19 19:10:08 +01:00
|
|
|
func (m Message) NewReader() (io.ReadCloser, error) {
|
|
|
|
return m.dir.Open(m.key)
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2022-10-26 22:29:02 +02:00
|
|
|
return lib.FromMaildirFlags(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)
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
// SetOneFlag enables or disables a single message flag on the message.
|
|
|
|
func (m Message) SetOneFlag(flag maildir.Flag, enable bool) error {
|
2020-05-25 16:59:48 +02:00
|
|
|
flags, err := m.Flags()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not read previous flags: %w", err)
|
2020-05-25 16:59:48 +02:00
|
|
|
}
|
2020-07-05 16:29:52 +02:00
|
|
|
if enable {
|
|
|
|
flags = append(flags, flag)
|
2020-05-25 16:59:48 +02:00
|
|
|
return m.SetFlags(flags)
|
|
|
|
}
|
|
|
|
var newFlags []maildir.Flag
|
2020-07-05 16:29:52 +02:00
|
|
|
for _, oldFlag := range flags {
|
|
|
|
if oldFlag != flag {
|
|
|
|
newFlags = append(newFlags, oldFlag)
|
2020-05-25 16:59:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return m.SetFlags(newFlags)
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
// MarkReplied either adds or removes the maildir.FlagReplied flag from the
|
|
|
|
// message.
|
|
|
|
func (m Message) MarkReplied(answered bool) error {
|
|
|
|
return m.SetOneFlag(maildir.FlagReplied, answered)
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2022-09-21 00:27:58 +02:00
|
|
|
msg, err := lib.ReadMessage(f)
|
2019-07-11 15:44:51 +02:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return nil, fmt.Errorf("could not read message: %w", err)
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
2019-08-05 09:16:07 +02:00
|
|
|
return lib.FetchEntityPartReader(msg, requestedParts)
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|