init: pristine aerc 0.20.0 source
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
//go:build notmuch
|
||||
// +build notmuch
|
||||
|
||||
package notmuch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"git.sr.ht/~rjarry/aerc/lib/log"
|
||||
"git.sr.ht/~rjarry/aerc/worker/types"
|
||||
)
|
||||
|
||||
func (w *worker) handleNotmuchEvent() error {
|
||||
err := w.db.Connect()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer w.db.Close()
|
||||
err = w.updateDirCounts()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = w.updateChangedMessages()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.emitLabelList()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *worker) updateDirCounts() error {
|
||||
if w.store != nil {
|
||||
folders, err := w.store.FolderMap()
|
||||
if err != nil {
|
||||
w.w.Errorf("failed listing directories: %v", err)
|
||||
return err
|
||||
}
|
||||
for name := range folders {
|
||||
folder := filepath.Join(w.maildirAccountPath, name)
|
||||
query := fmt.Sprintf("folder:%s", strconv.Quote(folder))
|
||||
w.w.PostMessage(&types.DirectoryInfo{
|
||||
Info: w.getDirectoryInfo(name, query),
|
||||
Refetch: w.query == query,
|
||||
}, nil)
|
||||
}
|
||||
}
|
||||
|
||||
for name, query := range w.nameQueryMap {
|
||||
w.w.PostMessage(&types.DirectoryInfo{
|
||||
Info: w.getDirectoryInfo(name, query),
|
||||
Refetch: w.query == query,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
for name, query := range w.dynamicNameQueryMap {
|
||||
w.w.PostMessage(&types.DirectoryInfo{
|
||||
Info: w.getDirectoryInfo(name, query),
|
||||
Refetch: w.query == query,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *worker) updateChangedMessages() error {
|
||||
newState := w.db.State()
|
||||
if newState == w.state {
|
||||
return nil
|
||||
}
|
||||
w.w.Logger.Debugf("State change: %d to %d", w.state, newState)
|
||||
query := fmt.Sprintf("lastmod:%d..%d and (%s)", w.state, newState, w.query)
|
||||
uids, err := w.uidsFromQuery(context.TODO(), query)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Couldn't get updates messages: %w", err)
|
||||
}
|
||||
for _, uid := range uids {
|
||||
m, err := w.msgFromUid(uid)
|
||||
if err != nil {
|
||||
log.Errorf("%s", err)
|
||||
continue
|
||||
}
|
||||
err = w.emitMessageInfo(m, nil)
|
||||
if err != nil {
|
||||
log.Errorf("%s", err)
|
||||
}
|
||||
}
|
||||
w.state = newState
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,372 @@
|
||||
//go:build notmuch
|
||||
// +build notmuch
|
||||
|
||||
package lib
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"git.sr.ht/~rjarry/aerc/lib/log"
|
||||
"git.sr.ht/~rjarry/aerc/lib/notmuch"
|
||||
"git.sr.ht/~rjarry/aerc/models"
|
||||
"git.sr.ht/~rjarry/aerc/worker/types"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
path string
|
||||
excludedTags []string
|
||||
db *notmuch.Database
|
||||
}
|
||||
|
||||
func NewDB(path string, excludedTags []string) *DB {
|
||||
nm := ¬much.Database{
|
||||
Path: path,
|
||||
}
|
||||
db := &DB{
|
||||
path: path,
|
||||
excludedTags: excludedTags,
|
||||
db: nm,
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func (db *DB) Connect() error {
|
||||
return db.db.Open(notmuch.MODE_READ_ONLY)
|
||||
}
|
||||
|
||||
func (db *DB) Close() error {
|
||||
return db.db.Close()
|
||||
}
|
||||
|
||||
// Returns the DB path
|
||||
func (db *DB) Path() string {
|
||||
return db.db.ResolvedPath()
|
||||
}
|
||||
|
||||
// ListTags lists all known tags
|
||||
func (db *DB) ListTags() []string {
|
||||
return db.db.Tags()
|
||||
}
|
||||
|
||||
// State returns the lastmod of the database. This is a uin64 which is
|
||||
// incremented with every modification
|
||||
func (db *DB) State() uint64 {
|
||||
_, lastmod := db.db.Revision()
|
||||
return lastmod
|
||||
}
|
||||
|
||||
// getQuery returns a query based on the provided query string.
|
||||
// It also configures the query as specified on the worker
|
||||
func (db *DB) newQuery(query string) (*notmuch.Query, error) {
|
||||
q, err := db.db.Query(query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
q.Exclude(notmuch.EXCLUDE_ALL)
|
||||
q.Sort(notmuch.SORT_OLDEST_FIRST)
|
||||
for _, t := range db.excludedTags {
|
||||
err := q.ExcludeTag(t)
|
||||
// do not treat STATUS_IGNORED as an error; this allows explicit
|
||||
// searches using tags that are excluded by default
|
||||
if err != nil && !errors.Is(err, notmuch.STATUS_IGNORED) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &q, nil
|
||||
}
|
||||
|
||||
func (db *DB) MsgIDFromFilename(filename string) (string, error) {
|
||||
msg, err := db.db.FindMessageByFilename(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer msg.Close()
|
||||
return msg.ID(), nil
|
||||
}
|
||||
|
||||
func (db *DB) MsgIDsFromQuery(ctx context.Context, q string) ([]string, error) {
|
||||
query, err := db.newQuery(q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer query.Close()
|
||||
messages, err := query.Messages()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer messages.Close()
|
||||
var msgIDs []string
|
||||
for messages.Next() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, context.Canceled
|
||||
default:
|
||||
msg := messages.Message()
|
||||
defer msg.Close()
|
||||
msgIDs = append(msgIDs, msg.ID())
|
||||
}
|
||||
}
|
||||
return msgIDs, err
|
||||
}
|
||||
|
||||
func (db *DB) ThreadsFromQuery(ctx context.Context, q string, entireThread bool) ([]*types.Thread, error) {
|
||||
query, err := db.newQuery(q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer query.Close()
|
||||
// To get proper ordering of threads, we always sort newest first
|
||||
query.Sort(notmuch.SORT_NEWEST_FIRST)
|
||||
threads, err := query.Threads()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
n, err := query.CountMessages()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer threads.Close()
|
||||
res := make([]*types.Thread, 0, n)
|
||||
for threads.Next() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, context.Canceled
|
||||
default:
|
||||
thread := threads.Thread()
|
||||
tlm := thread.TopLevelMessages()
|
||||
root := db.makeThread(nil, &tlm, entireThread)
|
||||
if len(root) > 1 {
|
||||
root[0].Dummy = true
|
||||
fc := &(root[0].FirstChild)
|
||||
for ; *fc != nil; fc = &((*fc).NextSibling) {
|
||||
}
|
||||
*fc = root[0].NextSibling
|
||||
root[0].NextSibling.PrevSibling = nil
|
||||
root[0].NextSibling = nil
|
||||
for i := 1; i < len(root); i++ {
|
||||
root[i].Parent = root[0]
|
||||
}
|
||||
res = append(res, root[0])
|
||||
} else {
|
||||
res = append(res, root...)
|
||||
}
|
||||
tlm.Close()
|
||||
thread.Close()
|
||||
}
|
||||
}
|
||||
// Reverse the slice
|
||||
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
|
||||
res[i], res[j] = res[j], res[i]
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
type MessageCount struct {
|
||||
Exists int
|
||||
Unread int
|
||||
}
|
||||
|
||||
func (db *DB) QueryCountMessages(q string) (MessageCount, error) {
|
||||
count := MessageCount{}
|
||||
query, err := db.newQuery(q)
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
defer query.Close()
|
||||
count.Exists, err = query.CountMessages()
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
|
||||
unreadQuery, err := db.newQuery(AndQueries(q, "tag:unread"))
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
defer unreadQuery.Close()
|
||||
count.Unread, err = unreadQuery.CountMessages()
|
||||
if err != nil {
|
||||
return count, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (db *DB) MsgFilename(key string) (string, error) {
|
||||
msg, err := db.db.FindMessageByID(key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer msg.Close()
|
||||
return msg.Filename(), nil
|
||||
}
|
||||
|
||||
func (db *DB) MsgTags(key string) ([]string, error) {
|
||||
msg, err := db.db.FindMessageByID(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer msg.Close()
|
||||
return msg.Tags(), nil
|
||||
}
|
||||
|
||||
func (db *DB) MsgFilenames(key string) ([]string, error) {
|
||||
msg, err := db.db.FindMessageByID(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer msg.Close()
|
||||
return msg.Filenames(), nil
|
||||
}
|
||||
|
||||
func (db *DB) DeleteMessage(filename string) error {
|
||||
err := db.db.Reopen(notmuch.MODE_READ_WRITE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err := db.db.Reopen(notmuch.MODE_READ_ONLY); err != nil {
|
||||
log.Errorf("couldn't reopen: %s", err)
|
||||
}
|
||||
}()
|
||||
err = db.db.BeginAtomic()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err := db.db.EndAtomic(); err != nil {
|
||||
log.Errorf("couldn't end atomic: %s", err)
|
||||
}
|
||||
}()
|
||||
err = db.db.RemoveFile(filename)
|
||||
if err != nil && !errors.Is(err, notmuch.STATUS_DUPLICATE_MESSAGE_ID) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) IndexFile(filename string) (string, error) {
|
||||
err := db.db.Reopen(notmuch.MODE_READ_WRITE)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer func() {
|
||||
if err := db.db.Reopen(notmuch.MODE_READ_ONLY); err != nil {
|
||||
log.Errorf("couldn't reopen: %s", err)
|
||||
}
|
||||
}()
|
||||
err = db.db.BeginAtomic()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer func() {
|
||||
if err := db.db.EndAtomic(); err != nil {
|
||||
log.Errorf("couldn't end atomic: %s", err)
|
||||
}
|
||||
}()
|
||||
msg, err := db.db.IndexFile(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer msg.Close()
|
||||
return msg.ID(), nil
|
||||
}
|
||||
|
||||
func (db *DB) MsgModifyTags(key string, add, remove []string) error {
|
||||
err := db.db.Reopen(notmuch.MODE_READ_WRITE)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err := db.db.Reopen(notmuch.MODE_READ_ONLY); err != nil {
|
||||
log.Errorf("couldn't reopen: %s", err)
|
||||
}
|
||||
}()
|
||||
err = db.db.BeginAtomic()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err := db.db.EndAtomic(); err != nil {
|
||||
log.Errorf("couldn't end atomic: %s", err)
|
||||
}
|
||||
}()
|
||||
msg, err := db.db.FindMessageByID(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer msg.Close()
|
||||
for _, tag := range add {
|
||||
err := msg.AddTag(tag)
|
||||
if err != nil {
|
||||
log.Warnf("failed to add tag: %v", err)
|
||||
}
|
||||
}
|
||||
for _, tag := range remove {
|
||||
err := msg.RemoveTag(tag)
|
||||
if err != nil {
|
||||
log.Warnf("failed to add tag: %v", err)
|
||||
}
|
||||
}
|
||||
return msg.SyncTagsToMaildirFlags()
|
||||
}
|
||||
|
||||
func (db *DB) makeThread(parent *types.Thread, msgs *notmuch.Messages, threadContext bool) []*types.Thread {
|
||||
var siblings []*types.Thread
|
||||
for msgs.Next() {
|
||||
msg := msgs.Message()
|
||||
defer msg.Close()
|
||||
msgID := msg.ID()
|
||||
match, err := msg.Flag(notmuch.MESSAGE_FLAG_MATCH)
|
||||
if err != nil {
|
||||
log.Errorf("%s", err)
|
||||
continue
|
||||
}
|
||||
replies := msg.Replies()
|
||||
defer replies.Close()
|
||||
if !match && !threadContext {
|
||||
siblings = append(siblings, db.makeThread(parent, &replies, threadContext)...)
|
||||
continue
|
||||
}
|
||||
node := &types.Thread{
|
||||
Uid: models.UID(msgID),
|
||||
Parent: parent,
|
||||
}
|
||||
switch threadContext {
|
||||
case true:
|
||||
node.Context = !match
|
||||
default:
|
||||
if match {
|
||||
node.Hidden = 0
|
||||
} else {
|
||||
node.Hidden = 1
|
||||
}
|
||||
}
|
||||
if parent != nil && parent.FirstChild == nil {
|
||||
parent.FirstChild = node
|
||||
}
|
||||
siblings = append(siblings, node)
|
||||
db.makeThread(node, &replies, threadContext)
|
||||
}
|
||||
for i := 1; i < len(siblings); i++ {
|
||||
siblings[i-1].NextSibling = siblings[i]
|
||||
}
|
||||
return siblings
|
||||
}
|
||||
|
||||
func AndQueries(q1, q2 string) string {
|
||||
if q1 == "" {
|
||||
return q2
|
||||
}
|
||||
if q2 == "" {
|
||||
return q1
|
||||
}
|
||||
if q1 == "*" {
|
||||
return q2
|
||||
}
|
||||
if q2 == "*" {
|
||||
return q1
|
||||
}
|
||||
return fmt.Sprintf("(%s) and (%s)", q1, q2)
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
//go:build notmuch
|
||||
// +build notmuch
|
||||
|
||||
package notmuch
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/emersion/go-maildir"
|
||||
|
||||
"git.sr.ht/~rjarry/aerc/lib/log"
|
||||
"git.sr.ht/~rjarry/aerc/lib/rfc822"
|
||||
"git.sr.ht/~rjarry/aerc/models"
|
||||
"git.sr.ht/~rjarry/aerc/worker/lib"
|
||||
notmuch "git.sr.ht/~rjarry/aerc/worker/notmuch/lib"
|
||||
"git.sr.ht/~rjarry/aerc/worker/types"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
uid models.UID
|
||||
key string
|
||||
db *notmuch.DB
|
||||
}
|
||||
|
||||
// NewReader returns a reader for a message
|
||||
func (m *Message) NewReader() (io.ReadCloser, error) {
|
||||
name, err := m.Filename()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return os.Open(name)
|
||||
}
|
||||
|
||||
// MessageInfo populates a models.MessageInfo struct for the message.
|
||||
func (m *Message) MessageInfo() (*models.MessageInfo, error) {
|
||||
info, err := rfc822.MessageInfo(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if filenames, err := m.db.MsgFilenames(m.key); err != nil {
|
||||
log.Errorf("failed to obtain filenames: %v", err)
|
||||
} else {
|
||||
info.Filenames = filenames
|
||||
// if size retrieval fails, just return info and log error
|
||||
if len(filenames) > 0 {
|
||||
if info.Size, err = lib.FileSize(filenames[0]); err != nil {
|
||||
log.Errorf("failed to obtain file size: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// NewBodyPartReader creates a new io.Reader for the requested body part(s) of
|
||||
// the message.
|
||||
func (m *Message) NewBodyPartReader(requestedParts []int) (io.Reader, error) {
|
||||
name, err := m.Filename()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
msg, err := rfc822.ReadMessage(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not read message: %w", err)
|
||||
}
|
||||
return rfc822.FetchEntityPartReader(msg, requestedParts)
|
||||
}
|
||||
|
||||
// SetFlag adds or removes a flag from the message.
|
||||
// Notmuch doesn't support all the flags, and for those this errors.
|
||||
func (m *Message) SetFlag(flag models.Flags, enable bool) error {
|
||||
// Translate the flag into a notmuch tag, ignoring no-op flags.
|
||||
tag, ok := flagToTag[flag]
|
||||
if !ok {
|
||||
return fmt.Errorf("Notmuch doesn't support flag %v", flag)
|
||||
}
|
||||
|
||||
// Get the current state of the flag.
|
||||
// Note that notmuch handles some flags in an inverted sense
|
||||
oldState := false
|
||||
tags, err := m.Tags()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, t := range tags {
|
||||
if t == tag {
|
||||
oldState = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if flagToInvert[flag] {
|
||||
enable = !enable
|
||||
}
|
||||
|
||||
switch {
|
||||
case oldState == enable:
|
||||
return nil
|
||||
case enable:
|
||||
return m.AddTag(tag)
|
||||
default:
|
||||
return m.RemoveTag(tag)
|
||||
}
|
||||
}
|
||||
|
||||
// MarkAnswered either adds or removes the "replied" tag from the message.
|
||||
func (m *Message) MarkAnswered(answered bool) error {
|
||||
return m.SetFlag(models.AnsweredFlag, answered)
|
||||
}
|
||||
|
||||
// MarkForwarded either adds or removes the "forwarded" tag from the message.
|
||||
func (m *Message) MarkForwarded(forwarded bool) error {
|
||||
return m.SetFlag(models.ForwardedFlag, forwarded)
|
||||
}
|
||||
|
||||
// MarkRead either adds or removes the maildir.FlagSeen flag from the message.
|
||||
func (m *Message) MarkRead(seen bool) error {
|
||||
return m.SetFlag(models.SeenFlag, seen)
|
||||
}
|
||||
|
||||
// tags returns the notmuch tags of a message
|
||||
func (m *Message) Tags() ([]string, error) {
|
||||
return m.db.MsgTags(m.key)
|
||||
}
|
||||
|
||||
func (m *Message) Labels() ([]string, error) {
|
||||
return m.Tags()
|
||||
}
|
||||
|
||||
func (m *Message) ModelFlags() (models.Flags, error) {
|
||||
var flags models.Flags = models.SeenFlag
|
||||
tags, err := m.Tags()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
for _, tag := range tags {
|
||||
flag := tagToFlag[tag]
|
||||
if flagToInvert[flag] {
|
||||
flags &^= flag
|
||||
} else {
|
||||
flags |= flag
|
||||
}
|
||||
}
|
||||
return flags, nil
|
||||
}
|
||||
|
||||
func (m *Message) UID() models.UID {
|
||||
return m.uid
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (m *Message) Remove(curDir maildir.Dir, mfs types.MultiFileStrategy) error {
|
||||
rm, del, err := m.filenamesForStrategy(mfs, curDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rm = append(rm, del...)
|
||||
return m.deleteFiles(rm)
|
||||
}
|
||||
|
||||
func (m *Message) Copy(curDir, destDir maildir.Dir, mfs types.MultiFileStrategy) error {
|
||||
cp, del, err := m.filenamesForStrategy(mfs, curDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, filename := range cp {
|
||||
source, key := parseFilename(filename)
|
||||
if key == "" {
|
||||
return fmt.Errorf("failed to parse message filename: %s", filename)
|
||||
}
|
||||
|
||||
newKey, err := source.Copy(destDir, key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newFilename, err := destDir.Filename(newKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = m.db.IndexFile(newFilename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return m.deleteFiles(del)
|
||||
}
|
||||
|
||||
func (m *Message) Move(curDir, destDir maildir.Dir, mfs types.MultiFileStrategy) error {
|
||||
move, del, err := m.filenamesForStrategy(mfs, curDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, filename := range move {
|
||||
// Remove encoded UID information from the key to prevent sync issues
|
||||
name := lib.StripUIDFromMessageFilename(filepath.Base(filename))
|
||||
dest := filepath.Join(string(destDir), "cur", name)
|
||||
|
||||
if err := os.Rename(filename, dest); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = m.db.IndexFile(dest); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := m.db.DeleteMessage(filename); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return m.deleteFiles(del)
|
||||
}
|
||||
|
||||
func (m *Message) deleteFiles(filenames []string) error {
|
||||
for _, filename := range filenames {
|
||||
if err := os.Remove(filename); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := m.db.DeleteMessage(filename); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Message) filenamesForStrategy(strategy types.MultiFileStrategy,
|
||||
curDir maildir.Dir,
|
||||
) (act, del []string, err error) {
|
||||
filenames, err := m.db.MsgFilenames(m.key)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return filterForStrategy(filenames, strategy, curDir)
|
||||
}
|
||||
|
||||
func filterForStrategy(filenames []string, strategy types.MultiFileStrategy,
|
||||
curDir maildir.Dir,
|
||||
) (act, del []string, err error) {
|
||||
if curDir == "" &&
|
||||
(strategy == types.ActDir || strategy == types.ActDirDelRest) {
|
||||
strategy = types.Refuse
|
||||
}
|
||||
|
||||
if len(filenames) < 2 {
|
||||
return filenames, []string{}, nil
|
||||
}
|
||||
|
||||
act = []string{}
|
||||
rest := []string{}
|
||||
switch strategy {
|
||||
case types.Refuse:
|
||||
return nil, nil, fmt.Errorf("refusing to act on multiple files")
|
||||
case types.ActAll:
|
||||
act = filenames
|
||||
case types.ActOne:
|
||||
fallthrough
|
||||
case types.ActOneDelRest:
|
||||
act = filenames[:1]
|
||||
rest = filenames[1:]
|
||||
case types.ActDir:
|
||||
fallthrough
|
||||
case types.ActDirDelRest:
|
||||
for _, filename := range filenames {
|
||||
if filepath.Dir(filepath.Dir(filename)) == string(curDir) {
|
||||
act = append(act, filename)
|
||||
} else {
|
||||
rest = append(rest, filename)
|
||||
}
|
||||
}
|
||||
default:
|
||||
return nil, nil, fmt.Errorf("invalid multi-file strategy %v", strategy)
|
||||
}
|
||||
|
||||
switch strategy {
|
||||
case types.ActOneDelRest:
|
||||
fallthrough
|
||||
case types.ActDirDelRest:
|
||||
del = rest
|
||||
default:
|
||||
del = []string{}
|
||||
}
|
||||
|
||||
return act, del, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
//go:build notmuch
|
||||
// +build notmuch
|
||||
|
||||
package notmuch
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.sr.ht/~rjarry/aerc/worker/types"
|
||||
"github.com/emersion/go-maildir"
|
||||
)
|
||||
|
||||
func TestFilterForStrategy(t *testing.T) {
|
||||
tests := []struct {
|
||||
filenames []string
|
||||
strategy types.MultiFileStrategy
|
||||
curDir string
|
||||
expectedAct []string
|
||||
expectedDel []string
|
||||
expectedErr bool
|
||||
}{
|
||||
// if there's only one file, always act on it
|
||||
{
|
||||
filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
strategy: types.Refuse,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
expectedDel: []string{},
|
||||
},
|
||||
{
|
||||
filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
strategy: types.ActAll,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
expectedDel: []string{},
|
||||
},
|
||||
{
|
||||
filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
strategy: types.ActOne,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
expectedDel: []string{},
|
||||
},
|
||||
{
|
||||
filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
strategy: types.ActOneDelRest,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
expectedDel: []string{},
|
||||
},
|
||||
{
|
||||
filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
strategy: types.ActDir,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
expectedDel: []string{},
|
||||
},
|
||||
{
|
||||
filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
strategy: types.ActDirDelRest,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
expectedDel: []string{},
|
||||
},
|
||||
|
||||
// follow strategy for multiple files
|
||||
{
|
||||
filenames: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
strategy: types.Refuse,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
filenames: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
strategy: types.ActAll,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedAct: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
expectedDel: []string{},
|
||||
},
|
||||
{
|
||||
filenames: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
strategy: types.ActOne,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
expectedDel: []string{},
|
||||
},
|
||||
{
|
||||
filenames: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
strategy: types.ActOneDelRest,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
expectedDel: []string{
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
},
|
||||
{
|
||||
filenames: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
strategy: types.ActDir,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedAct: []string{
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
},
|
||||
expectedDel: []string{},
|
||||
},
|
||||
{
|
||||
filenames: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
strategy: types.ActDirDelRest,
|
||||
curDir: "/h/j/m/B",
|
||||
expectedAct: []string{
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
},
|
||||
expectedDel: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
},
|
||||
|
||||
// refuse to act on multiple files for ActDir and friends if
|
||||
// no current dir is provided
|
||||
{
|
||||
filenames: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
strategy: types.ActDir,
|
||||
curDir: "",
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
filenames: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
strategy: types.ActDirDelRest,
|
||||
curDir: "",
|
||||
expectedErr: true,
|
||||
},
|
||||
|
||||
// act on multiple files w/o current dir for other strategies
|
||||
{
|
||||
filenames: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
strategy: types.ActAll,
|
||||
curDir: "",
|
||||
expectedAct: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
expectedDel: []string{},
|
||||
},
|
||||
{
|
||||
filenames: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
strategy: types.ActOne,
|
||||
curDir: "",
|
||||
expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
expectedDel: []string{},
|
||||
},
|
||||
{
|
||||
filenames: []string{
|
||||
"/h/j/m/A/cur/a.b.c:2,",
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
strategy: types.ActOneDelRest,
|
||||
curDir: "",
|
||||
expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
|
||||
expectedDel: []string{
|
||||
"/h/j/m/B/new/b.c.d",
|
||||
"/h/j/m/B/cur/c.d.e:2,S",
|
||||
"/h/j/m/C/new/d.e.f",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
act, del, err := filterForStrategy(test.filenames, test.strategy,
|
||||
maildir.Dir(test.curDir))
|
||||
|
||||
if test.expectedErr && err == nil {
|
||||
t.Errorf("[test %d] got nil, expected error", i)
|
||||
}
|
||||
|
||||
if !test.expectedErr && err != nil {
|
||||
t.Errorf("[test %d] got %v, expected nil", i, err)
|
||||
}
|
||||
|
||||
if !arrEq(act, test.expectedAct) {
|
||||
t.Errorf("[test %d] got %v, expected %v", i, act, test.expectedAct)
|
||||
}
|
||||
|
||||
if !arrEq(del, test.expectedDel) {
|
||||
t.Errorf("[test %d] got %v, expected %v", i, del, test.expectedDel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func arrEq(a, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//go:build notmuch
|
||||
// +build notmuch
|
||||
|
||||
package notmuch
|
||||
|
||||
import "git.sr.ht/~rjarry/aerc/models"
|
||||
|
||||
var tagToFlag = map[string]models.Flags{
|
||||
"unread": models.SeenFlag,
|
||||
"replied": models.AnsweredFlag,
|
||||
"passed": models.ForwardedFlag,
|
||||
"draft": models.DraftFlag,
|
||||
"flagged": models.FlaggedFlag,
|
||||
}
|
||||
|
||||
var flagToTag = map[models.Flags]string{
|
||||
models.SeenFlag: "unread",
|
||||
models.AnsweredFlag: "replied",
|
||||
models.ForwardedFlag: "passed",
|
||||
models.DraftFlag: "draft",
|
||||
models.FlaggedFlag: "flagged",
|
||||
}
|
||||
|
||||
var flagToInvert = map[models.Flags]bool{
|
||||
models.SeenFlag: true,
|
||||
models.AnsweredFlag: false,
|
||||
models.ForwardedFlag: false,
|
||||
models.DraftFlag: false,
|
||||
models.FlaggedFlag: false,
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
//go:build notmuch
|
||||
// +build notmuch
|
||||
|
||||
package notmuch
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.sr.ht/~rjarry/aerc/models"
|
||||
"git.sr.ht/~rjarry/aerc/worker/types"
|
||||
"git.sr.ht/~rjarry/go-opt/v2"
|
||||
)
|
||||
|
||||
type queryBuilder struct {
|
||||
s string
|
||||
}
|
||||
|
||||
func (q *queryBuilder) and(s string) {
|
||||
if len(s) == 0 {
|
||||
return
|
||||
}
|
||||
if len(q.s) != 0 {
|
||||
q.s += " and "
|
||||
}
|
||||
q.s += "(" + s + ")"
|
||||
}
|
||||
|
||||
func (q *queryBuilder) or(s string) {
|
||||
if len(s) == 0 {
|
||||
return
|
||||
}
|
||||
if len(q.s) != 0 {
|
||||
q.s += " or "
|
||||
}
|
||||
q.s += "(" + s + ")"
|
||||
}
|
||||
|
||||
func translate(crit *types.SearchCriteria) string {
|
||||
if crit == nil {
|
||||
return ""
|
||||
}
|
||||
var base queryBuilder
|
||||
|
||||
// recipients
|
||||
var from queryBuilder
|
||||
for _, f := range crit.From {
|
||||
from.or("from:" + opt.QuoteArg(f))
|
||||
}
|
||||
if from.s != "" {
|
||||
base.and(from.s)
|
||||
}
|
||||
|
||||
var to queryBuilder
|
||||
for _, t := range crit.To {
|
||||
to.or("to:" + opt.QuoteArg(t))
|
||||
}
|
||||
if to.s != "" {
|
||||
base.and(to.s)
|
||||
}
|
||||
|
||||
var cc queryBuilder
|
||||
for _, c := range crit.Cc {
|
||||
cc.or("cc:" + opt.QuoteArg(c))
|
||||
}
|
||||
if cc.s != "" {
|
||||
base.and(cc.s)
|
||||
}
|
||||
|
||||
// flags
|
||||
for f := range flagToTag {
|
||||
if crit.WithFlags.Has(f) {
|
||||
base.and(getParsedFlag(f, false))
|
||||
}
|
||||
if crit.WithoutFlags.Has(f) {
|
||||
base.and(getParsedFlag(f, true))
|
||||
}
|
||||
}
|
||||
|
||||
// dates
|
||||
switch {
|
||||
case !crit.StartDate.IsZero() && !crit.EndDate.IsZero():
|
||||
base.and(fmt.Sprintf("date:@%d..@%d",
|
||||
crit.StartDate.Unix(), crit.EndDate.Unix()))
|
||||
case !crit.StartDate.IsZero():
|
||||
base.and(fmt.Sprintf("date:@%d..", crit.StartDate.Unix()))
|
||||
case !crit.EndDate.IsZero():
|
||||
base.and(fmt.Sprintf("date:..@%d", crit.EndDate.Unix()))
|
||||
}
|
||||
|
||||
// other terms
|
||||
if len(crit.Terms) > 0 {
|
||||
if crit.SearchBody {
|
||||
base.and("body:" + opt.QuoteArg(strings.Join(crit.Terms, " ")))
|
||||
} else {
|
||||
for _, term := range crit.Terms {
|
||||
base.and(term)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return base.s
|
||||
}
|
||||
|
||||
func getParsedFlag(flag models.Flags, inverse bool) string {
|
||||
name := "tag:" + flagToTag[flag]
|
||||
if flagToInvert[flag] {
|
||||
name = "not " + name
|
||||
}
|
||||
return name
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user