2019-07-11 15:44:51 +02:00
|
|
|
package maildir
|
|
|
|
|
|
|
|
import (
|
2022-01-20 15:40:34 +01:00
|
|
|
"bytes"
|
2022-05-30 14:34:18 +02:00
|
|
|
"context"
|
2020-07-25 10:11:13 +02:00
|
|
|
"errors"
|
2019-07-11 15:44:51 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2022-05-30 14:34:18 +02:00
|
|
|
"os/exec"
|
2019-07-11 15:44:51 +02:00
|
|
|
"path/filepath"
|
2022-03-06 10:06:34 +01:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2019-07-11 15:44:51 +02:00
|
|
|
|
|
|
|
"github.com/emersion/go-maildir"
|
2019-07-16 19:53:33 +02:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2019-07-11 15:44:51 +02:00
|
|
|
|
2022-07-19 22:31:51 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/handlers"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/lib"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-07-11 15:44:51 +02:00
|
|
|
)
|
|
|
|
|
2019-07-18 06:25:42 +02:00
|
|
|
func init() {
|
|
|
|
handlers.RegisterWorkerFactory("maildir", NewWorker)
|
2022-07-04 21:21:48 +02:00
|
|
|
handlers.RegisterWorkerFactory("maildirpp", NewMaildirppWorker)
|
2019-07-18 06:25:42 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
var errUnsupported = fmt.Errorf("unsupported command")
|
|
|
|
|
|
|
|
// A Worker handles interfacing between aerc's UI and a group of maildirs.
|
|
|
|
type Worker struct {
|
2019-09-20 00:37:44 +02:00
|
|
|
c *Container
|
|
|
|
selected *maildir.Dir
|
|
|
|
selectedName string
|
|
|
|
worker *types.Worker
|
|
|
|
watcher *fsnotify.Watcher
|
|
|
|
currentSortCriteria []*types.SortCriterion
|
2022-07-04 21:21:48 +02:00
|
|
|
maildirpp bool // whether to use Maildir++ directory layout
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWorker creates a new maildir worker with the provided worker.
|
2019-07-18 06:25:42 +02:00
|
|
|
func NewWorker(worker *types.Worker) (types.Backend, error) {
|
2019-07-16 19:53:33 +02:00
|
|
|
watch, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return nil, fmt.Errorf("could not create file system watcher: %w", err)
|
2019-07-16 19:53:33 +02:00
|
|
|
}
|
|
|
|
return &Worker{worker: worker, watcher: watch}, nil
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
|
2022-07-04 21:21:48 +02:00
|
|
|
// NewMaildirppWorker creates a new Maildir++ worker with the provided worker.
|
|
|
|
func NewMaildirppWorker(worker *types.Worker) (types.Backend, error) {
|
|
|
|
watch, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return nil, fmt.Errorf("could not create file system watcher: %w", err)
|
2022-07-04 21:21:48 +02:00
|
|
|
}
|
|
|
|
return &Worker{worker: worker, watcher: watch, maildirpp: true}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
// Run starts the worker's message handling loop.
|
|
|
|
func (w *Worker) Run() {
|
|
|
|
for {
|
2019-07-16 19:53:33 +02:00
|
|
|
select {
|
|
|
|
case action := <-w.worker.Actions:
|
|
|
|
w.handleAction(action)
|
|
|
|
case ev := <-w.watcher.Events:
|
|
|
|
w.handleFSEvent(ev)
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 19:53:33 +02:00
|
|
|
func (w *Worker) handleAction(action types.WorkerMessage) {
|
|
|
|
msg := w.worker.ProcessAction(action)
|
2022-05-30 14:34:18 +02:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
// Explicitly handle all asynchronous actions. Async actions are
|
|
|
|
// responsible for posting their own Done message
|
|
|
|
case *types.CheckMail:
|
|
|
|
go w.handleCheckMail(msg)
|
|
|
|
default:
|
|
|
|
// Default handling, will be performed synchronously
|
2022-07-31 14:32:48 +02:00
|
|
|
err := w.handleMessage(msg)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, errUnsupported):
|
2022-05-30 14:34:18 +02:00
|
|
|
w.worker.PostMessage(&types.Unsupported{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
}, nil)
|
2022-07-31 14:32:48 +02:00
|
|
|
case err != nil:
|
2022-05-30 14:34:18 +02:00
|
|
|
w.worker.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
2022-07-31 14:32:48 +02:00
|
|
|
default:
|
2022-05-30 14:34:18 +02:00
|
|
|
w.done(msg)
|
|
|
|
}
|
2019-07-16 19:53:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) handleFSEvent(ev fsnotify.Event) {
|
2021-12-06 23:45:58 +01:00
|
|
|
// we only care about files being created, removed or renamed
|
|
|
|
switch ev.Op {
|
|
|
|
case fsnotify.Create, fsnotify.Remove, fsnotify.Rename:
|
|
|
|
break
|
|
|
|
default:
|
2019-07-16 19:53:33 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// if there's not a selected directory to rescan, ignore
|
|
|
|
if w.selected == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-09-13 10:42:40 +02:00
|
|
|
err := w.c.SyncNewMail(*w.selected)
|
2019-07-16 19:53:33 +02:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not move new to cur : %w", err)
|
2019-07-16 19:53:33 +02:00
|
|
|
return
|
|
|
|
}
|
2020-09-13 10:42:40 +02:00
|
|
|
|
2020-02-28 01:22:35 +01:00
|
|
|
dirInfo := w.getDirectoryInfo(w.selectedName)
|
2019-09-16 20:22:50 +02:00
|
|
|
w.worker.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: dirInfo,
|
|
|
|
}, nil)
|
2019-07-16 19:53:33 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
func (w *Worker) done(msg types.WorkerMessage) {
|
2022-03-18 09:53:02 +01:00
|
|
|
w.worker.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) err(msg types.WorkerMessage, err error) {
|
|
|
|
w.worker.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
|
|
|
}
|
|
|
|
|
2022-03-06 10:06:34 +01:00
|
|
|
func splitMaildirFile(name string) (uniq string, flags []maildir.Flag, err error) {
|
|
|
|
i := strings.LastIndexByte(name, ':')
|
|
|
|
if i < 0 {
|
|
|
|
return "", nil, &maildir.MailfileError{Name: name}
|
|
|
|
}
|
|
|
|
info := name[i+1:]
|
|
|
|
uniq = name[:i]
|
|
|
|
if len(info) < 2 {
|
|
|
|
return "", nil, &maildir.FlagError{Info: info, Experimental: false}
|
|
|
|
}
|
|
|
|
if info[1] != ',' || info[0] != '2' {
|
|
|
|
return "", nil, &maildir.FlagError{Info: info, Experimental: false}
|
|
|
|
}
|
|
|
|
if info[0] == '1' {
|
|
|
|
return "", nil, &maildir.FlagError{Info: info, Experimental: true}
|
|
|
|
}
|
|
|
|
flags = []maildir.Flag(info[2:])
|
|
|
|
sort.Slice(flags, func(i, j int) bool { return info[i] < info[j] })
|
|
|
|
return uniq, flags, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dirFiles(name string) ([]string, error) {
|
|
|
|
dir, err := os.Open(filepath.Join(name, "cur"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
return dir.Readdirnames(-1)
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:22:35 +01:00
|
|
|
func (w *Worker) getDirectoryInfo(name string) *models.DirectoryInfo {
|
2019-09-16 20:22:50 +02:00
|
|
|
dirInfo := &models.DirectoryInfo{
|
2020-02-28 01:22:35 +01:00
|
|
|
Name: name,
|
2019-09-16 20:22:50 +02:00
|
|
|
Flags: []string{},
|
|
|
|
ReadOnly: false,
|
|
|
|
// total messages
|
|
|
|
Exists: 0,
|
|
|
|
// new messages since mailbox was last opened
|
|
|
|
Recent: 0,
|
|
|
|
// total unread
|
|
|
|
Unseen: 0,
|
2020-02-28 01:22:35 +01:00
|
|
|
|
2022-01-28 13:23:25 +01:00
|
|
|
AccurateCounts: false,
|
2022-07-05 21:48:37 +02:00
|
|
|
|
|
|
|
Caps: &models.Capabilities{
|
|
|
|
Sort: true,
|
|
|
|
Thread: false,
|
|
|
|
},
|
2019-09-16 20:22:50 +02:00
|
|
|
}
|
2020-02-28 01:22:35 +01:00
|
|
|
|
|
|
|
dir := w.c.Dir(name)
|
2022-03-06 10:06:34 +01:00
|
|
|
var keyFlags map[string][]maildir.Flag
|
|
|
|
files, err := dirFiles(string(dir))
|
|
|
|
if err == nil {
|
|
|
|
keyFlags = make(map[string][]maildir.Flag, len(files))
|
|
|
|
for _, v := range files {
|
|
|
|
key, flags, err := splitMaildirFile(v)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("%q: error parsing flags (%q): %w", v, key, err)
|
2022-03-06 10:06:34 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
keyFlags[key] = flags
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Infof("disabled flags cache: %q: %w", dir, err)
|
2022-03-06 10:06:34 +01:00
|
|
|
}
|
2020-02-28 01:22:35 +01:00
|
|
|
|
|
|
|
uids, err := w.c.UIDs(dir)
|
2019-09-16 20:22:50 +02:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get uids: %w", err)
|
2019-09-16 20:22:50 +02:00
|
|
|
return dirInfo
|
|
|
|
}
|
2020-02-28 01:22:35 +01:00
|
|
|
|
2022-01-28 13:23:25 +01:00
|
|
|
dirInfo.Exists = len(uids)
|
2022-03-06 10:06:34 +01:00
|
|
|
for _, uid := range uids {
|
|
|
|
message, err := w.c.Message(dir, uid)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get message: %w", err)
|
2022-03-06 10:06:34 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
var flags []maildir.Flag
|
|
|
|
if keyFlags != nil {
|
|
|
|
ok := false
|
|
|
|
flags, ok = keyFlags[message.key]
|
|
|
|
if !ok {
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Debugf("message (key=%q uid=%d) not found in map cache", message.key, message.uid)
|
2022-03-06 10:06:34 +01:00
|
|
|
flags, err = message.Flags()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get flags: %w", err)
|
2022-03-06 10:06:34 +01:00
|
|
|
continue
|
|
|
|
}
|
2022-01-28 13:23:25 +01:00
|
|
|
}
|
2022-03-06 10:06:34 +01:00
|
|
|
} else {
|
|
|
|
flags, err = message.Flags()
|
2022-01-28 13:23:25 +01:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get flags: %w", err)
|
2022-01-28 13:23:25 +01:00
|
|
|
continue
|
|
|
|
}
|
2022-03-06 10:06:34 +01:00
|
|
|
}
|
|
|
|
seen := false
|
|
|
|
for _, flag := range flags {
|
|
|
|
if flag == maildir.FlagSeen {
|
|
|
|
seen = true
|
|
|
|
break
|
2019-09-16 20:22:50 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-06 10:06:34 +01:00
|
|
|
if !seen {
|
|
|
|
dirInfo.Unseen++
|
|
|
|
}
|
|
|
|
if w.c.IsRecent(uid) {
|
|
|
|
dirInfo.Recent++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dirInfo.AccurateCounts = true
|
2019-09-16 20:22:50 +02:00
|
|
|
return dirInfo
|
|
|
|
}
|
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
func (w *Worker) handleMessage(msg types.WorkerMessage) error {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *types.Unsupported:
|
|
|
|
// No-op
|
|
|
|
case *types.Configure:
|
|
|
|
return w.handleConfigure(msg)
|
|
|
|
case *types.Connect:
|
|
|
|
return w.handleConnect(msg)
|
|
|
|
case *types.ListDirectories:
|
|
|
|
return w.handleListDirectories(msg)
|
|
|
|
case *types.OpenDirectory:
|
|
|
|
return w.handleOpenDirectory(msg)
|
|
|
|
case *types.FetchDirectoryContents:
|
|
|
|
return w.handleFetchDirectoryContents(msg)
|
|
|
|
case *types.CreateDirectory:
|
|
|
|
return w.handleCreateDirectory(msg)
|
2020-08-18 22:27:23 +02:00
|
|
|
case *types.RemoveDirectory:
|
|
|
|
return w.handleRemoveDirectory(msg)
|
2019-07-11 15:44:51 +02:00
|
|
|
case *types.FetchMessageHeaders:
|
|
|
|
return w.handleFetchMessageHeaders(msg)
|
|
|
|
case *types.FetchMessageBodyPart:
|
|
|
|
return w.handleFetchMessageBodyPart(msg)
|
|
|
|
case *types.FetchFullMessages:
|
|
|
|
return w.handleFetchFullMessages(msg)
|
|
|
|
case *types.DeleteMessages:
|
|
|
|
return w.handleDeleteMessages(msg)
|
2020-07-05 16:29:52 +02:00
|
|
|
case *types.FlagMessages:
|
|
|
|
return w.handleFlagMessages(msg)
|
2020-05-25 16:59:48 +02:00
|
|
|
case *types.AnsweredMessages:
|
|
|
|
return w.handleAnsweredMessages(msg)
|
2019-07-11 15:44:51 +02:00
|
|
|
case *types.CopyMessages:
|
|
|
|
return w.handleCopyMessages(msg)
|
2022-08-16 23:23:40 +02:00
|
|
|
case *types.MoveMessages:
|
|
|
|
return w.handleMoveMessages(msg)
|
2019-07-11 15:44:51 +02:00
|
|
|
case *types.AppendMessage:
|
|
|
|
return w.handleAppendMessage(msg)
|
|
|
|
case *types.SearchDirectory:
|
|
|
|
return w.handleSearchDirectory(msg)
|
|
|
|
}
|
|
|
|
return errUnsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) handleConfigure(msg *types.Configure) error {
|
|
|
|
u, err := url.Parse(msg.Config.Source)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("error configuring maildir worker: %w", err)
|
2019-07-11 15:44:51 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
dir := u.Path
|
|
|
|
if u.Host == "~" {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not resolve home directory: %w", err)
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
dir = filepath.Join(home, u.Path)
|
|
|
|
}
|
2020-07-25 10:11:13 +02:00
|
|
|
if len(dir) == 0 {
|
|
|
|
return fmt.Errorf("could not resolve maildir from URL '%s'", msg.Config.Source)
|
|
|
|
}
|
2022-07-19 22:31:51 +02:00
|
|
|
c, err := NewContainer(dir, w.maildirpp)
|
2020-07-25 10:11:13 +02:00
|
|
|
if err != nil {
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Errorf("could not configure maildir: %s", dir)
|
2020-07-25 10:11:13 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.c = c
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Infof("configured base maildir: %s", dir)
|
2019-07-11 15:44:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) handleConnect(msg *types.Connect) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) handleListDirectories(msg *types.ListDirectories) error {
|
2020-07-25 10:11:13 +02:00
|
|
|
// TODO If handleConfigure has returned error, w.c is nil.
|
|
|
|
// It could be better if we skip directory listing completely
|
|
|
|
// when configure fails.
|
|
|
|
if w.c == nil {
|
|
|
|
return errors.New("Incorrect maildir directory")
|
|
|
|
}
|
2019-07-11 15:44:51 +02:00
|
|
|
dirs, err := w.c.ListFolders()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("failed listing directories: %w", err)
|
2019-07-11 15:44:51 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, name := range dirs {
|
|
|
|
w.worker.PostMessage(&types.Directory{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Dir: &models.Directory{
|
|
|
|
Name: name,
|
|
|
|
Attributes: []string{},
|
|
|
|
},
|
|
|
|
}, nil)
|
2020-02-28 01:22:35 +01:00
|
|
|
|
|
|
|
w.worker.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: w.getDirectoryInfo(name),
|
|
|
|
}, nil)
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) handleOpenDirectory(msg *types.OpenDirectory) error {
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Infof("opening %s", msg.Directory)
|
2019-07-16 19:53:33 +02:00
|
|
|
|
2020-02-25 22:35:57 +01:00
|
|
|
// open the directory
|
|
|
|
dir, err := w.c.OpenDirectory(msg.Directory)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-06 20:42:07 +01:00
|
|
|
// remove existing watch paths
|
2019-07-16 19:53:33 +02:00
|
|
|
if w.selected != nil {
|
|
|
|
prevDir := filepath.Join(string(*w.selected), "new")
|
|
|
|
if err := w.watcher.Remove(prevDir); err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not unwatch previous directory: %w", err)
|
2019-07-16 19:53:33 +02:00
|
|
|
}
|
2021-12-06 20:42:07 +01:00
|
|
|
prevDir = filepath.Join(string(*w.selected), "cur")
|
|
|
|
if err := w.watcher.Remove(prevDir); err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not unwatch previous directory: %w", err)
|
2021-12-06 20:42:07 +01:00
|
|
|
}
|
2019-07-16 19:53:33 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
w.selected = &dir
|
2019-09-16 20:22:50 +02:00
|
|
|
w.selectedName = msg.Directory
|
2019-07-16 19:53:33 +02:00
|
|
|
|
2021-12-06 20:42:07 +01:00
|
|
|
// add watch paths
|
2019-07-16 19:53:33 +02:00
|
|
|
newDir := filepath.Join(string(*w.selected), "new")
|
|
|
|
if err := w.watcher.Add(newDir); err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not add watch to directory: %w", err)
|
2019-07-16 19:53:33 +02:00
|
|
|
}
|
2021-12-06 20:42:07 +01:00
|
|
|
newDir = filepath.Join(string(*w.selected), "cur")
|
|
|
|
if err := w.watcher.Add(newDir); err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not add watch to directory: %w", err)
|
2021-12-06 20:42:07 +01:00
|
|
|
}
|
2019-07-16 19:53:33 +02:00
|
|
|
|
2019-07-30 21:12:15 +02:00
|
|
|
if err := dir.Clean(); err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not clean directory: %w", err)
|
2019-07-30 21:12:15 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
info := &types.DirectoryInfo{
|
2020-02-28 01:22:35 +01:00
|
|
|
Info: w.getDirectoryInfo(msg.Directory),
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
w.worker.PostMessage(info, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) handleFetchDirectoryContents(
|
2022-07-31 22:16:40 +02:00
|
|
|
msg *types.FetchDirectoryContents,
|
|
|
|
) error {
|
2022-07-05 21:48:40 +02:00
|
|
|
var (
|
|
|
|
uids []uint32
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if len(msg.FilterCriteria) > 0 {
|
|
|
|
filter, err := parseSearch(msg.FilterCriteria)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
uids, err = w.search(filter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uids, err = w.c.UIDs(*w.selected)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("failed scanning uids: %w", err)
|
2022-07-05 21:48:40 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
2019-09-20 00:37:44 +02:00
|
|
|
sortedUids, err := w.sort(uids, msg.SortCriteria)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("failed sorting directory: %w", err)
|
2019-09-20 00:37:44 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.currentSortCriteria = msg.SortCriteria
|
2019-07-11 15:44:51 +02:00
|
|
|
w.worker.PostMessage(&types.DirectoryContents{
|
|
|
|
Message: types.RespondTo(msg),
|
2019-09-20 00:37:44 +02:00
|
|
|
Uids: sortedUids,
|
2019-07-11 15:44:51 +02:00
|
|
|
}, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-20 00:37:44 +02:00
|
|
|
func (w *Worker) sort(uids []uint32, criteria []*types.SortCriterion) ([]uint32, error) {
|
|
|
|
if len(criteria) == 0 {
|
|
|
|
return uids, nil
|
|
|
|
}
|
|
|
|
var msgInfos []*models.MessageInfo
|
|
|
|
for _, uid := range uids {
|
2020-09-13 10:42:40 +02:00
|
|
|
info, err := w.msgInfoFromUid(uid)
|
2019-09-20 00:37:44 +02:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get message info: %w", err)
|
2019-09-20 00:37:44 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
msgInfos = append(msgInfos, info)
|
|
|
|
}
|
|
|
|
sortedUids, err := lib.Sort(msgInfos, criteria)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not sort the messages: %w", err)
|
2019-09-20 00:37:44 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sortedUids, nil
|
|
|
|
}
|
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
func (w *Worker) handleCreateDirectory(msg *types.CreateDirectory) error {
|
|
|
|
dir := w.c.Dir(msg.Directory)
|
2020-03-03 14:45:06 +01:00
|
|
|
if err := dir.Init(); err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not create directory %s: %w",
|
2019-07-11 15:44:51 +02:00
|
|
|
msg.Directory, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-18 22:27:23 +02:00
|
|
|
func (w *Worker) handleRemoveDirectory(msg *types.RemoveDirectory) error {
|
|
|
|
dir := w.c.Dir(msg.Directory)
|
|
|
|
if err := os.RemoveAll(string(dir)); err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not remove directory %s: %w",
|
2020-08-18 22:27:23 +02:00
|
|
|
msg.Directory, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
func (w *Worker) handleFetchMessageHeaders(
|
2022-07-31 22:16:40 +02:00
|
|
|
msg *types.FetchMessageHeaders,
|
|
|
|
) error {
|
2019-07-11 15:44:51 +02:00
|
|
|
for _, uid := range msg.Uids {
|
2020-09-13 10:42:40 +02:00
|
|
|
info, err := w.msgInfoFromUid(uid)
|
2019-07-11 15:44:51 +02:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get message info: %w", err)
|
2022-09-21 18:44:12 +02:00
|
|
|
w.worker.PostMessageInfoError(msg, uid, err)
|
2019-07-11 15:44:51 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
w.worker.PostMessage(&types.MessageInfo{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Info: info,
|
|
|
|
}, nil)
|
2020-09-13 10:42:40 +02:00
|
|
|
w.c.ClearRecentFlag(uid)
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) handleFetchMessageBodyPart(
|
2022-07-31 22:16:40 +02:00
|
|
|
msg *types.FetchMessageBodyPart,
|
|
|
|
) error {
|
2019-07-11 15:44:51 +02:00
|
|
|
// get reader
|
|
|
|
m, err := w.c.Message(*w.selected, msg.Uid)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get message %d: %w", msg.Uid, err)
|
2019-07-11 15:44:51 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
r, err := m.NewBodyPartReader(msg.Part)
|
|
|
|
if err != nil {
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Errorf(
|
2022-07-31 15:15:27 +02:00
|
|
|
"could not get body part reader for message=%d, parts=%#v: %w",
|
2019-07-11 15:44:51 +02:00
|
|
|
msg.Uid, msg.Part, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.worker.PostMessage(&types.MessageBodyPart{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Part: &models.MessageBodyPart{
|
|
|
|
Reader: r,
|
|
|
|
Uid: msg.Uid,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) handleFetchFullMessages(msg *types.FetchFullMessages) error {
|
|
|
|
for _, uid := range msg.Uids {
|
|
|
|
m, err := w.c.Message(*w.selected, uid)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get message %d: %w", uid, err)
|
2019-07-11 15:44:51 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
r, err := m.NewReader()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get message reader: %w", err)
|
2019-07-11 15:44:51 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-01-19 19:10:08 +01:00
|
|
|
defer r.Close()
|
2022-08-17 16:19:45 +02:00
|
|
|
b, err := io.ReadAll(r)
|
2022-01-20 15:40:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-11 15:44:51 +02:00
|
|
|
w.worker.PostMessage(&types.FullMessage{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Content: &models.FullMessage{
|
|
|
|
Uid: uid,
|
2022-01-20 15:40:34 +01:00
|
|
|
Reader: bytes.NewReader(b),
|
2019-07-11 15:44:51 +02:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
}
|
2022-07-11 20:11:20 +02:00
|
|
|
w.worker.PostMessage(&types.Done{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
}, nil)
|
2019-07-11 15:44:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) handleDeleteMessages(msg *types.DeleteMessages) error {
|
|
|
|
deleted, err := w.c.DeleteAll(*w.selected, msg.Uids)
|
|
|
|
if len(deleted) > 0 {
|
|
|
|
w.worker.PostMessage(&types.MessagesDeleted{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Uids: deleted,
|
|
|
|
}, nil)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("failed removing messages: %w", err)
|
2019-07-11 15:44:51 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-25 16:59:48 +02:00
|
|
|
func (w *Worker) handleAnsweredMessages(msg *types.AnsweredMessages) error {
|
|
|
|
for _, uid := range msg.Uids {
|
|
|
|
m, err := w.c.Message(*w.selected, uid)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get message: %w", err)
|
2020-05-25 16:59:48 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := m.MarkReplied(msg.Answered); err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not mark message as answered: %w", err)
|
2020-05-25 16:59:48 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
info, err := m.MessageInfo()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get message info: %w", err)
|
2020-05-25 16:59:48 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
w.worker.PostMessage(&types.MessageInfo{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Info: info,
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
w.worker.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: w.getDirectoryInfo(w.selectedName),
|
|
|
|
}, nil)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
func (w *Worker) handleFlagMessages(msg *types.FlagMessages) error {
|
2019-07-11 15:44:51 +02:00
|
|
|
for _, uid := range msg.Uids {
|
|
|
|
m, err := w.c.Message(*w.selected, uid)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get message: %w", err)
|
2019-07-11 15:44:51 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-07-05 16:29:52 +02:00
|
|
|
flag := flagToMaildir[msg.Flag]
|
|
|
|
if err := m.SetOneFlag(flag, msg.Enable); err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could change flag %v to %v on message: %w", flag, msg.Enable, err)
|
2019-07-11 15:44:51 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
info, err := m.MessageInfo()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not get message info: %w", err)
|
2019-07-11 15:44:51 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-02-28 01:22:35 +01:00
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
w.worker.PostMessage(&types.MessageInfo{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Info: info,
|
|
|
|
}, nil)
|
|
|
|
}
|
2020-07-18 14:12:44 +02:00
|
|
|
|
|
|
|
w.worker.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: w.getDirectoryInfo(w.selectedName),
|
|
|
|
}, nil)
|
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) handleCopyMessages(msg *types.CopyMessages) error {
|
2019-07-11 15:44:53 +02:00
|
|
|
dest := w.c.Dir(msg.Destination)
|
2019-07-19 20:13:42 +02:00
|
|
|
err := w.c.CopyAll(dest, *w.selected, msg.Uids)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-02 02:24:53 +02:00
|
|
|
w.worker.PostMessage(&types.MessagesCopied{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Destination: msg.Destination,
|
|
|
|
Uids: msg.Uids,
|
2020-02-28 01:22:35 +01:00
|
|
|
}, nil)
|
2019-07-19 20:13:42 +02:00
|
|
|
return nil
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
|
|
|
|
2022-08-16 23:23:40 +02:00
|
|
|
func (w *Worker) handleMoveMessages(msg *types.MoveMessages) error {
|
|
|
|
dest := w.c.Dir(msg.Destination)
|
|
|
|
moved, err := w.c.MoveAll(dest, *w.selected, msg.Uids)
|
|
|
|
destInfo := w.getDirectoryInfo(msg.Destination)
|
|
|
|
w.worker.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: destInfo,
|
|
|
|
}, nil)
|
|
|
|
w.worker.PostMessage(&types.MessagesDeleted{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Uids: moved,
|
|
|
|
}, nil)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-11 15:44:51 +02:00
|
|
|
func (w *Worker) handleAppendMessage(msg *types.AppendMessage) error {
|
2020-03-03 14:45:06 +01:00
|
|
|
// since we are the "master" maildir process, we can modify the maildir directly
|
2019-07-11 15:44:51 +02:00
|
|
|
dest := w.c.Dir(msg.Destination)
|
2020-03-03 14:45:06 +01:00
|
|
|
_, writer, err := dest.Create(translateFlags(msg.Flags))
|
2019-07-11 15:44:51 +02:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not create message at %s: %w", msg.Destination, err)
|
2019-07-11 15:44:51 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-03-03 14:45:06 +01:00
|
|
|
defer writer.Close()
|
|
|
|
if _, err := io.Copy(writer, msg.Reader); err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
logging.Errorf("could not write message to destination: %w", err)
|
2019-07-11 15:44:51 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-07-11 20:11:21 +02:00
|
|
|
w.worker.PostMessage(&types.Done{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
}, nil)
|
2020-02-28 01:22:35 +01:00
|
|
|
w.worker.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: w.getDirectoryInfo(msg.Destination),
|
|
|
|
}, nil)
|
2019-07-11 15:44:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) handleSearchDirectory(msg *types.SearchDirectory) error {
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Infof("Searching directory %v with args: %v", *w.selected, msg.Argv)
|
2019-09-14 19:05:20 +02:00
|
|
|
criteria, err := parseSearch(msg.Argv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Infof("Searching with parsed criteria: %#v", criteria)
|
2019-09-14 19:05:20 +02:00
|
|
|
uids, err := w.search(criteria)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.worker.PostMessage(&types.SearchResults{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Uids: uids,
|
|
|
|
}, nil)
|
|
|
|
return nil
|
2019-07-11 15:44:51 +02:00
|
|
|
}
|
2020-09-13 10:42:40 +02:00
|
|
|
|
|
|
|
func (w *Worker) msgInfoFromUid(uid uint32) (*models.MessageInfo, error) {
|
|
|
|
m, err := w.c.Message(*w.selected, uid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info, err := m.MessageInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if w.c.IsRecent(uid) {
|
|
|
|
info.Flags = append(info.Flags, models.RecentFlag)
|
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
2022-05-30 14:34:18 +02:00
|
|
|
|
|
|
|
func (w *Worker) handleCheckMail(msg *types.CheckMail) {
|
2022-07-29 20:30:01 +02:00
|
|
|
if msg.Command == "" {
|
|
|
|
w.err(msg, fmt.Errorf("checkmail: no command specified"))
|
|
|
|
return
|
|
|
|
}
|
2022-05-30 14:34:18 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), msg.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, "sh", "-c", msg.Command)
|
|
|
|
ch := make(chan error)
|
|
|
|
go func() {
|
|
|
|
err := cmd.Run()
|
|
|
|
ch <- err
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
w.err(msg, fmt.Errorf("checkmail: timed out"))
|
|
|
|
case err := <-ch:
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
w.err(msg, fmt.Errorf("checkmail: error running command: %w", err))
|
2022-05-30 14:34:18 +02:00
|
|
|
} else {
|
|
|
|
w.done(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|