2019-08-05 09:16:09 +02:00
|
|
|
//+build notmuch
|
|
|
|
|
|
|
|
package notmuch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/worker/lib"
|
2019-09-13 08:47:59 +02:00
|
|
|
notmuch "git.sr.ht/~sircmpwn/aerc/worker/notmuch/lib"
|
2019-08-05 09:16:09 +02:00
|
|
|
"github.com/emersion/go-message"
|
|
|
|
_ "github.com/emersion/go-message/charset"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// NewReader reads a message into memory and returns an io.Reader for it.
|
2019-08-24 11:53:43 +02:00
|
|
|
func (m *Message) NewReader() (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()
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bytes.NewReader(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
msg, err := message.Read(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not read message: %v", err)
|
|
|
|
}
|
|
|
|
return lib.FetchEntityPartReader(msg, requestedParts)
|
|
|
|
}
|
|
|
|
|
2020-05-25 16:59:48 +02:00
|
|
|
// MarkAnswered either adds or removes the "replied" tag from the message.
|
|
|
|
func (m *Message) MarkAnswered(answered bool) error {
|
|
|
|
haveReplied := false
|
|
|
|
tags, err := m.Tags()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, t := range tags {
|
|
|
|
if t == "replied" {
|
|
|
|
haveReplied = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if haveReplied == answered {
|
|
|
|
// we already have the desired state
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if haveAnswered {
|
|
|
|
err := m.RemoveTag("replied")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.AddTag("replied")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-08-05 09:16:09 +02:00
|
|
|
haveUnread := false
|
2019-09-13 08:47:59 +02:00
|
|
|
tags, err := m.Tags()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, t := range tags {
|
2019-08-05 09:16:09 +02:00
|
|
|
if t == "unread" {
|
|
|
|
haveUnread = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (haveUnread && !seen) || (!haveUnread && seen) {
|
|
|
|
// we already have the desired state
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if haveUnread {
|
2019-08-24 11:53:43 +02:00
|
|
|
err := m.RemoveTag("unread")
|
2019-08-05 09:16:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 08:47:59 +02:00
|
|
|
err = m.AddTag("unread")
|
2019-08-05 09:16:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
//AddTag adds a single tag.
|
|
|
|
//Consider using *Message.ModifyTags for multiple additions / removals
|
|
|
|
//instead of looping over a tag array
|
|
|
|
func (m *Message) AddTag(tag string) error {
|
|
|
|
return m.ModifyTags([]string{tag}, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
//RemoveTag removes a single tag.
|
|
|
|
//Consider using *Message.ModifyTags for multiple additions / removals
|
|
|
|
//instead of looping over a tag array
|
|
|
|
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)
|
|
|
|
}
|