2021-12-11 23:20:53 +01:00
|
|
|
//go:build notmuch
|
|
|
|
// +build notmuch
|
2019-08-05 09:16:09 +02:00
|
|
|
|
|
|
|
package notmuch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2022-10-26 22:29:04 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/emersion/go-maildir"
|
2019-08-05 09:16:09 +02:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/lib"
|
|
|
|
notmuch "git.sr.ht/~rjarry/aerc/worker/notmuch/lib"
|
2019-08-05 09:16:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Message struct {
|
2019-09-13 08:47:59 +02:00
|
|
|
uid uint32
|
|
|
|
key string
|
|
|
|
db *notmuch.DB
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 08:40:07 +01:00
|
|
|
// NewReader returns a reader for a message
|
2022-01-19 19:10:08 +01:00
|
|
|
func (m *Message) NewReader() (io.ReadCloser, error) {
|
2019-09-13 08:47:59 +02:00
|
|
|
name, err := m.Filename()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-19 19:10:08 +01:00
|
|
|
return os.Open(name)
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MessageInfo populates a models.MessageInfo struct for the message.
|
2019-08-24 11:53:43 +02:00
|
|
|
func (m *Message) MessageInfo() (*models.MessageInfo, error) {
|
2019-08-05 09:16:09 +02:00
|
|
|
return lib.MessageInfo(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBodyPartReader creates a new io.Reader for the requested body part(s) of
|
|
|
|
// the message.
|
2019-08-24 11:53:43 +02:00
|
|
|
func (m *Message) NewBodyPartReader(requestedParts []int) (io.Reader, error) {
|
2019-09-13 08:47:59 +02:00
|
|
|
name, err := m.Filename()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f, err := os.Open(name)
|
2019-08-05 09:16:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2022-09-21 00:27:58 +02:00
|
|
|
msg, err := lib.ReadMessage(f)
|
2019-08-05 09:16:09 +02:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return nil, fmt.Errorf("could not read message: %w", err)
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
return lib.FetchEntityPartReader(msg, requestedParts)
|
|
|
|
}
|
|
|
|
|
2020-09-27 19:00:58 +02:00
|
|
|
// SetFlag adds or removes a flag from the message.
|
2020-07-05 16:29:52 +02:00
|
|
|
// Notmuch doesn't support all the flags, and for those this errors.
|
2020-09-27 19:00:58 +02:00
|
|
|
func (m *Message) SetFlag(flag models.Flag, enable bool) error {
|
2020-07-05 16:29:52 +02:00
|
|
|
// Translate the flag into a notmuch tag, ignoring no-op flags.
|
|
|
|
var tag string
|
|
|
|
switch flag {
|
|
|
|
case models.SeenFlag:
|
|
|
|
// Note: Inverted properly later
|
|
|
|
tag = "unread"
|
|
|
|
case models.AnsweredFlag:
|
|
|
|
tag = "replied"
|
|
|
|
case models.FlaggedFlag:
|
|
|
|
tag = "flagged"
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Notmuch doesn't support flag %v", flag)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current state of the flag.
|
|
|
|
// Note that notmuch handles models.SeenFlag in an inverted sense.
|
|
|
|
oldState := false
|
2020-05-25 16:59:48 +02:00
|
|
|
tags, err := m.Tags()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, t := range tags {
|
2020-07-05 16:29:52 +02:00
|
|
|
if t == tag {
|
|
|
|
oldState = true
|
2020-05-25 16:59:48 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-07-05 16:29:52 +02:00
|
|
|
if flag == models.SeenFlag {
|
|
|
|
oldState = !oldState
|
2020-05-25 16:59:48 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
// Skip if flag already in correct state.
|
|
|
|
if oldState == enable {
|
2020-05-25 16:59:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
if !enable {
|
|
|
|
if flag == models.SeenFlag {
|
|
|
|
return m.AddTag("unread")
|
|
|
|
} else {
|
|
|
|
return m.RemoveTag(tag)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if flag == models.SeenFlag {
|
|
|
|
return m.RemoveTag("unread")
|
|
|
|
} else {
|
|
|
|
return m.AddTag(tag)
|
|
|
|
}
|
2020-05-25 16:59:48 +02:00
|
|
|
}
|
2020-07-05 16:29:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarkAnswered either adds or removes the "replied" tag from the message.
|
|
|
|
func (m *Message) MarkAnswered(answered bool) error {
|
2020-09-27 19:00:58 +02:00
|
|
|
return m.SetFlag(models.AnsweredFlag, answered)
|
2020-05-25 16:59:48 +02:00
|
|
|
}
|
|
|
|
|
2019-08-05 09:16:09 +02:00
|
|
|
// MarkRead either adds or removes the maildir.FlagSeen flag from the message.
|
2019-08-24 11:53:43 +02:00
|
|
|
func (m *Message) MarkRead(seen bool) error {
|
2020-09-27 19:00:58 +02:00
|
|
|
return m.SetFlag(models.SeenFlag, seen)
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// tags returns the notmuch tags of a message
|
2019-09-13 08:47:59 +02:00
|
|
|
func (m *Message) Tags() ([]string, error) {
|
|
|
|
return m.db.MsgTags(m.key)
|
2019-08-24 11:53:43 +02:00
|
|
|
}
|
|
|
|
|
2019-12-23 12:51:58 +01:00
|
|
|
func (m *Message) Labels() ([]string, error) {
|
|
|
|
return m.Tags()
|
|
|
|
}
|
|
|
|
|
2019-08-24 11:53:43 +02:00
|
|
|
func (m *Message) ModelFlags() ([]models.Flag, error) {
|
2019-08-05 09:16:09 +02:00
|
|
|
var flags []models.Flag
|
|
|
|
seen := true
|
2019-09-13 08:47:59 +02:00
|
|
|
tags, err := m.Tags()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, tag := range tags {
|
2019-08-05 09:16:09 +02:00
|
|
|
switch tag {
|
|
|
|
case "replied":
|
|
|
|
flags = append(flags, models.AnsweredFlag)
|
|
|
|
case "flagged":
|
|
|
|
flags = append(flags, models.FlaggedFlag)
|
|
|
|
case "unread":
|
|
|
|
seen = false
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if seen {
|
|
|
|
flags = append(flags, models.SeenFlag)
|
|
|
|
}
|
|
|
|
return flags, nil
|
|
|
|
}
|
|
|
|
|
2019-08-24 11:53:43 +02:00
|
|
|
func (m *Message) UID() uint32 {
|
2019-08-05 09:16:09 +02:00
|
|
|
return m.uid
|
|
|
|
}
|
2019-09-13 08:47:59 +02:00
|
|
|
|
|
|
|
func (m *Message) Filename() (string, error) {
|
|
|
|
return m.db.MsgFilename(m.key)
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:16:40 +02:00
|
|
|
// AddTag adds a single tag.
|
|
|
|
// Consider using *Message.ModifyTags for multiple additions / removals
|
|
|
|
// instead of looping over a tag array
|
2019-09-13 08:47:59 +02:00
|
|
|
func (m *Message) AddTag(tag string) error {
|
|
|
|
return m.ModifyTags([]string{tag}, nil)
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:16:40 +02:00
|
|
|
// RemoveTag removes a single tag.
|
|
|
|
// Consider using *Message.ModifyTags for multiple additions / removals
|
|
|
|
// instead of looping over a tag array
|
2019-09-13 08:47:59 +02:00
|
|
|
func (m *Message) RemoveTag(tag string) error {
|
|
|
|
return m.ModifyTags(nil, []string{tag})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) ModifyTags(add, remove []string) error {
|
|
|
|
return m.db.MsgModifyTags(m.key, add, remove)
|
|
|
|
}
|
2022-10-26 22:29:04 +02:00
|
|
|
|
|
|
|
func (m *Message) Remove(dir maildir.Dir) error {
|
|
|
|
filenames, err := m.db.MsgFilenames(m.key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, filename := range filenames {
|
|
|
|
if dirContains(dir, filename) {
|
|
|
|
err := m.db.DeleteMessage(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Remove(filename); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("no matching message file found in %s", string(dir))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) Copy(target maildir.Dir) error {
|
|
|
|
filename, err := m.Filename()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
source, key := parseFilename(filename)
|
|
|
|
if key == "" {
|
|
|
|
return fmt.Errorf("failed to parse message filename: %s", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
newKey, err := source.Copy(target, key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newFilename, err := target.Filename(newKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = m.db.IndexFile(newFilename)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) Move(srcDir, destDir maildir.Dir) error {
|
|
|
|
var src string
|
|
|
|
|
|
|
|
filenames, err := m.db.MsgFilenames(m.key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, filename := range filenames {
|
|
|
|
if dirContains(srcDir, filename) {
|
|
|
|
src = filename
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if src == "" {
|
|
|
|
return fmt.Errorf("no matching message file found in %s", string(srcDir))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove encoded UID information from the key to prevent sync issues
|
|
|
|
name := lib.StripUIDFromMessageFilename(filepath.Base(src))
|
|
|
|
dest := filepath.Join(string(destDir), "cur", name)
|
|
|
|
|
|
|
|
if err := m.db.DeleteMessage(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(src, dest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.db.IndexFile(dest)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseFilename(filename string) (maildir.Dir, string) {
|
|
|
|
base := filepath.Base(filename)
|
|
|
|
dir := filepath.Dir(filename)
|
|
|
|
dir, curdir := filepath.Split(dir)
|
|
|
|
if curdir != "cur" {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
split := strings.Split(base, ":")
|
|
|
|
if len(split) < 2 {
|
|
|
|
return maildir.Dir(dir), ""
|
|
|
|
}
|
|
|
|
key := split[0]
|
|
|
|
return maildir.Dir(dir), key
|
|
|
|
}
|
|
|
|
|
|
|
|
func dirContains(dir maildir.Dir, filename string) bool {
|
|
|
|
for _, sub := range []string{"cur", "new"} {
|
|
|
|
match, _ := filepath.Match(filepath.Join(string(dir), sub, "*"), filename)
|
|
|
|
if match {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|