2021-12-11 23:20:53 +01:00
|
|
|
//go:build notmuch
|
|
|
|
// +build notmuch
|
2019-08-05 09:16:09 +02:00
|
|
|
|
|
|
|
package notmuch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2022-01-20 15:40:34 +01:00
|
|
|
"bytes"
|
2022-05-30 14:34:18 +02:00
|
|
|
"context"
|
2022-07-31 15:15:27 +02:00
|
|
|
"errors"
|
2019-08-05 09:16:09 +02:00
|
|
|
"fmt"
|
2022-08-17 16:19:45 +02:00
|
|
|
"io"
|
2019-08-05 09:16:09 +02:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2022-05-30 14:34:18 +02:00
|
|
|
"os/exec"
|
2019-08-05 09:16:09 +02:00
|
|
|
"path/filepath"
|
2022-10-26 22:29:04 +02:00
|
|
|
"strconv"
|
2019-08-05 09:16:09 +02:00
|
|
|
"strings"
|
2020-02-15 14:14:47 +01:00
|
|
|
"time"
|
2019-08-05 09:16:09 +02:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/config"
|
2022-03-22 09:52:27 +01: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"
|
|
|
|
notmuch "git.sr.ht/~rjarry/aerc/worker/notmuch/lib"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-08-05 09:16:09 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
handlers.RegisterWorkerFactory("notmuch", NewWorker)
|
|
|
|
}
|
|
|
|
|
|
|
|
var errUnsupported = fmt.Errorf("unsupported command")
|
|
|
|
|
2020-02-15 14:14:47 +01:00
|
|
|
const backgroundRefreshDelay = 1 * time.Minute
|
|
|
|
|
2019-08-05 09:16:09 +02:00
|
|
|
type worker struct {
|
2019-09-20 19:11:09 +02:00
|
|
|
w *types.Worker
|
2020-02-15 14:14:46 +01:00
|
|
|
nmEvents chan eventType
|
2019-09-20 19:11:09 +02:00
|
|
|
query string
|
2020-02-29 14:40:51 +01:00
|
|
|
currentQueryName string
|
2021-11-13 09:10:09 +01:00
|
|
|
queryMapOrder []string
|
2019-09-20 19:11:09 +02:00
|
|
|
nameQueryMap map[string]string
|
2022-10-26 22:29:04 +02:00
|
|
|
store *lib.MaildirStore
|
2019-09-20 19:11:09 +02:00
|
|
|
db *notmuch.DB
|
2019-10-28 21:31:01 +01:00
|
|
|
setupErr error
|
2019-09-20 19:11:09 +02:00
|
|
|
currentSortCriteria []*types.SortCriterion
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
|
2020-07-08 01:02:46 +02:00
|
|
|
// NewWorker creates a new notmuch worker with the provided worker.
|
2019-08-05 09:16:09 +02:00
|
|
|
func NewWorker(w *types.Worker) (types.Backend, error) {
|
2020-02-15 14:14:46 +01:00
|
|
|
events := make(chan eventType, 20)
|
2022-07-31 22:16:40 +02:00
|
|
|
return &worker{
|
|
|
|
w: w,
|
|
|
|
nmEvents: events,
|
|
|
|
}, nil
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run starts the worker's message handling loop.
|
|
|
|
func (w *worker) Run() {
|
|
|
|
for {
|
2020-02-15 14:14:46 +01:00
|
|
|
select {
|
|
|
|
case action := <-w.w.Actions:
|
|
|
|
msg := w.w.ProcessAction(action)
|
2022-07-31 15:15:27 +02:00
|
|
|
if err := w.handleMessage(msg); errors.Is(err, errUnsupported) {
|
2020-02-15 14:14:46 +01:00
|
|
|
w.w.PostMessage(&types.Unsupported{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
}, nil)
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("ProcessAction(%T) unsupported: %v", msg, err)
|
2020-02-15 14:14:46 +01:00
|
|
|
} else if err != nil {
|
|
|
|
w.w.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("ProcessAction(%T) failure: %v", msg, err)
|
2020-02-15 14:14:46 +01:00
|
|
|
}
|
|
|
|
case nmEvent := <-w.nmEvents:
|
|
|
|
err := w.handleNotmuchEvent(nmEvent)
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("notmuch event failure: %v", err)
|
2020-02-15 14:14:46 +01:00
|
|
|
}
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) done(msg types.WorkerMessage) {
|
2022-06-25 00:26:26 +02:00
|
|
|
w.w.PostMessage(&types.Done{Message: types.RespondTo(msg)}, nil)
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) err(msg types.WorkerMessage, err error) {
|
|
|
|
w.w.PostMessage(&types.Error{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Error: err,
|
|
|
|
}, nil)
|
|
|
|
}
|
2022-07-31 22:16:40 +02:00
|
|
|
|
2019-08-05 09:16:09 +02:00
|
|
|
func (w *worker) handleMessage(msg types.WorkerMessage) error {
|
2019-10-28 21:31:01 +01:00
|
|
|
if w.setupErr != nil {
|
|
|
|
// only configure can recover from a config error, bail for everything else
|
|
|
|
_, isConfigure := msg.(*types.Configure)
|
|
|
|
if !isConfigure {
|
|
|
|
return w.setupErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 09:16:09 +02:00
|
|
|
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)
|
2021-11-12 18:12:02 +01:00
|
|
|
case *types.FetchDirectoryThreaded:
|
|
|
|
return w.handleFetchDirectoryThreaded(msg)
|
2019-08-05 09:16:09 +02:00
|
|
|
case *types.FetchMessageHeaders:
|
|
|
|
return w.handleFetchMessageHeaders(msg)
|
|
|
|
case *types.FetchMessageBodyPart:
|
|
|
|
return w.handleFetchMessageBodyPart(msg)
|
|
|
|
case *types.FetchFullMessages:
|
|
|
|
return w.handleFetchFullMessages(msg)
|
2020-07-05 16:29:52 +02:00
|
|
|
case *types.FlagMessages:
|
|
|
|
return w.handleFlagMessages(msg)
|
2020-07-05 15:40:12 +02:00
|
|
|
case *types.AnsweredMessages:
|
|
|
|
return w.handleAnsweredMessages(msg)
|
2019-08-28 06:39:08 +02:00
|
|
|
case *types.SearchDirectory:
|
|
|
|
return w.handleSearchDirectory(msg)
|
2019-09-13 08:48:01 +02:00
|
|
|
case *types.ModifyLabels:
|
|
|
|
return w.handleModifyLabels(msg)
|
2022-05-30 14:34:18 +02:00
|
|
|
case *types.CheckMail:
|
|
|
|
go w.handleCheckMail(msg)
|
|
|
|
return nil
|
2022-10-26 22:29:04 +02:00
|
|
|
case *types.DeleteMessages:
|
|
|
|
return w.handleDeleteMessages(msg)
|
|
|
|
case *types.CopyMessages:
|
|
|
|
return w.handleCopyMessages(msg)
|
|
|
|
case *types.MoveMessages:
|
|
|
|
return w.handleMoveMessages(msg)
|
|
|
|
case *types.AppendMessage:
|
|
|
|
return w.handleAppendMessage(msg)
|
|
|
|
case *types.CreateDirectory:
|
|
|
|
return w.handleCreateDirectory(msg)
|
|
|
|
case *types.RemoveDirectory:
|
|
|
|
return w.handleRemoveDirectory(msg)
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
return errUnsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleConfigure(msg *types.Configure) error {
|
2019-10-28 21:31:01 +01:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
w.setupErr = nil
|
|
|
|
return
|
|
|
|
}
|
2022-07-31 15:15:27 +02:00
|
|
|
w.setupErr = fmt.Errorf("notmuch: %w", err)
|
2019-10-28 21:31:01 +01:00
|
|
|
}()
|
|
|
|
|
2019-08-05 09:16:09 +02:00
|
|
|
u, err := url.Parse(msg.Config.Source)
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("error configuring notmuch worker: %v", err)
|
2019-08-05 09:16:09 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
home, err := homedir.Expand(u.Hostname())
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not resolve home directory: %w", err)
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
2019-09-13 08:47:59 +02:00
|
|
|
pathToDB := filepath.Join(home, u.Path)
|
2019-10-28 21:31:01 +01:00
|
|
|
err = w.loadQueryMap(msg.Config)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not load query map configuration: %w", err)
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
2019-09-13 08:47:59 +02:00
|
|
|
excludedTags := w.loadExcludeTags(msg.Config)
|
2022-07-19 22:31:51 +02:00
|
|
|
w.db = notmuch.NewDB(pathToDB, excludedTags)
|
2022-10-26 22:29:05 +02:00
|
|
|
|
|
|
|
val, ok := msg.Config.Params["maildir-store"]
|
|
|
|
if ok {
|
|
|
|
path, err := homedir.Expand(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
store, err := lib.NewMaildirStore(path, false)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot initialize maildir store: %w", err)
|
|
|
|
}
|
|
|
|
w.store = store
|
2022-10-26 22:29:04 +02:00
|
|
|
}
|
|
|
|
|
2019-08-24 11:53:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleConnect(msg *types.Connect) error {
|
2019-09-13 08:47:59 +02:00
|
|
|
err := w.db.Connect()
|
2019-08-24 11:53:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-05 09:16:09 +02:00
|
|
|
w.done(msg)
|
2019-12-21 16:21:26 +01:00
|
|
|
w.emitLabelList()
|
2020-02-15 14:14:47 +01:00
|
|
|
go func() {
|
2022-03-22 09:52:27 +01:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2020-02-15 14:14:47 +01:00
|
|
|
for {
|
|
|
|
w.nmEvents <- &updateDirCounts{}
|
|
|
|
time.Sleep(backgroundRefreshDelay)
|
|
|
|
}
|
|
|
|
}()
|
2019-08-05 09:16:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleListDirectories(msg *types.ListDirectories) error {
|
2022-10-26 22:29:05 +02:00
|
|
|
if w.store != nil {
|
|
|
|
folders, err := w.store.FolderMap()
|
|
|
|
if err != nil {
|
|
|
|
logging.Errorf("failed listing directories: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for name := range folders {
|
|
|
|
w.w.PostMessage(&types.Directory{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Dir: &models.Directory{
|
|
|
|
Name: name,
|
|
|
|
Attributes: []string{},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
}
|
2022-10-26 22:29:04 +02:00
|
|
|
}
|
|
|
|
|
2021-11-13 09:10:09 +01:00
|
|
|
for _, name := range w.queryMapOrder {
|
2019-08-05 09:16:09 +02:00
|
|
|
w.w.PostMessage(&types.Directory{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Dir: &models.Directory{
|
|
|
|
Name: name,
|
|
|
|
Attributes: []string{},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
}
|
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:29:06 +02:00
|
|
|
func (w *worker) getDirectoryInfo(name string, query string) *models.DirectoryInfo {
|
|
|
|
dirInfo := &models.DirectoryInfo{
|
|
|
|
Name: name,
|
|
|
|
Flags: []string{},
|
|
|
|
ReadOnly: false,
|
|
|
|
// total messages
|
|
|
|
Exists: 0,
|
|
|
|
// new messages since mailbox was last opened
|
|
|
|
Recent: 0,
|
|
|
|
// total unread
|
|
|
|
Unseen: 0,
|
|
|
|
AccurateCounts: true,
|
|
|
|
|
|
|
|
Caps: &models.Capabilities{
|
|
|
|
Sort: true,
|
|
|
|
Thread: true,
|
2019-08-05 09:16:09 +02:00
|
|
|
},
|
|
|
|
}
|
2020-02-15 14:14:45 +01:00
|
|
|
|
2022-10-26 22:29:06 +02:00
|
|
|
count, err := w.db.QueryCountMessages(query)
|
2020-02-15 14:14:45 +01:00
|
|
|
if err != nil {
|
2022-10-26 22:29:06 +02:00
|
|
|
return dirInfo
|
2020-02-15 14:14:45 +01:00
|
|
|
}
|
2022-10-26 22:29:06 +02:00
|
|
|
dirInfo.Exists = count.Exists
|
|
|
|
dirInfo.Unseen = count.Unread
|
2020-02-15 14:14:45 +01:00
|
|
|
|
2022-10-26 22:29:06 +02:00
|
|
|
return dirInfo
|
2020-02-15 14:14:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleOpenDirectory(msg *types.OpenDirectory) error {
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Infof("opening %s", msg.Directory)
|
2022-10-26 22:29:06 +02:00
|
|
|
|
|
|
|
var isDynamicFolder bool
|
|
|
|
q := ""
|
|
|
|
if w.store != nil {
|
|
|
|
folders, _ := w.store.FolderMap()
|
2022-10-26 22:29:10 +02:00
|
|
|
dir, ok := folders[msg.Directory]
|
|
|
|
if ok {
|
2022-10-26 22:29:06 +02:00
|
|
|
q = fmt.Sprintf("folder:%s", strconv.Quote(msg.Directory))
|
2022-10-26 22:29:10 +02:00
|
|
|
if err := w.processNewMaildirFiles(string(dir)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-26 22:29:06 +02:00
|
|
|
}
|
2020-02-15 14:14:45 +01:00
|
|
|
}
|
2022-10-26 22:29:06 +02:00
|
|
|
if q == "" {
|
|
|
|
var ok bool
|
|
|
|
q, ok = w.nameQueryMap[msg.Directory]
|
|
|
|
if !ok {
|
|
|
|
q = msg.Directory
|
|
|
|
isDynamicFolder = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.query = q
|
|
|
|
w.currentQueryName = msg.Directory
|
|
|
|
|
|
|
|
w.w.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: w.getDirectoryInfo(msg.Directory, w.query),
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
}, nil)
|
|
|
|
if isDynamicFolder {
|
|
|
|
w.w.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: w.getDirectoryInfo(msg.Directory, w.query),
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
}, nil)
|
2022-09-29 00:24:56 +02:00
|
|
|
}
|
2019-08-05 09:16:09 +02:00
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleFetchDirectoryContents(
|
2022-07-31 22:16:40 +02:00
|
|
|
msg *types.FetchDirectoryContents,
|
|
|
|
) error {
|
2019-09-20 19:11:09 +02:00
|
|
|
w.currentSortCriteria = msg.SortCriteria
|
2019-09-13 08:48:01 +02:00
|
|
|
err := w.emitDirectoryContents(msg)
|
2019-08-05 09:16:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 18:12:02 +01:00
|
|
|
func (w *worker) handleFetchDirectoryThreaded(
|
2022-07-31 22:16:40 +02:00
|
|
|
msg *types.FetchDirectoryThreaded,
|
|
|
|
) error {
|
2021-11-12 18:12:02 +01:00
|
|
|
// w.currentSortCriteria = msg.SortCriteria
|
|
|
|
err := w.emitDirectoryThreaded(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-05 09:16:09 +02:00
|
|
|
func (w *worker) handleFetchMessageHeaders(
|
2022-07-31 22:16:40 +02:00
|
|
|
msg *types.FetchMessageHeaders,
|
|
|
|
) error {
|
2019-08-05 09:16:09 +02:00
|
|
|
for _, uid := range msg.Uids {
|
|
|
|
m, err := w.msgFromUid(uid)
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not get message: %v", err)
|
2022-09-21 18:44:12 +02:00
|
|
|
w.w.PostMessageInfoError(msg, uid, err)
|
2019-08-05 09:16:09 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-09-13 08:48:01 +02:00
|
|
|
err = w.emitMessageInfo(m, msg)
|
2019-08-05 09:16:09 +02:00
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not emit message info: %v", err)
|
2022-09-21 18:44:12 +02:00
|
|
|
w.w.PostMessageInfoError(msg, uid, err)
|
2019-08-05 09:16:09 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 08:47:59 +02:00
|
|
|
func (w *worker) uidsFromQuery(query string) ([]uint32, error) {
|
|
|
|
msgIDs, err := w.db.MsgIDsFromQuery(query)
|
2019-08-05 09:16:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var uids []uint32
|
2019-09-13 08:47:59 +02:00
|
|
|
for _, id := range msgIDs {
|
2021-11-12 18:12:02 +01:00
|
|
|
uid := w.db.UidFromKey(id)
|
2019-08-05 09:16:09 +02:00
|
|
|
uids = append(uids, uid)
|
|
|
|
|
|
|
|
}
|
|
|
|
return uids, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) msgFromUid(uid uint32) (*Message, error) {
|
2021-11-12 18:12:02 +01:00
|
|
|
key, ok := w.db.KeyFromUid(uid)
|
2019-08-05 09:16:09 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Invalid uid: %v", uid)
|
|
|
|
}
|
|
|
|
msg := &Message{
|
2019-09-13 08:47:59 +02:00
|
|
|
key: key,
|
|
|
|
uid: uid,
|
|
|
|
db: w.db,
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleFetchMessageBodyPart(
|
2022-07-31 22:16:40 +02:00
|
|
|
msg *types.FetchMessageBodyPart,
|
|
|
|
) error {
|
2019-08-05 09:16:09 +02:00
|
|
|
m, err := w.msgFromUid(msg.Uid)
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not get message %d: %v", msg.Uid, err)
|
2019-08-05 09:16:09 +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-08-05 09:16:09 +02:00
|
|
|
msg.Uid, msg.Part, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.w.PostMessage(&types.MessageBodyPart{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Part: &models.MessageBodyPart{
|
|
|
|
Reader: r,
|
|
|
|
Uid: msg.Uid,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleFetchFullMessages(msg *types.FetchFullMessages) error {
|
|
|
|
for _, uid := range msg.Uids {
|
|
|
|
m, err := w.msgFromUid(uid)
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not get message %d: %v", uid, err)
|
2019-08-05 09:16:09 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
r, err := m.NewReader()
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not get message reader: %v", err)
|
2019-08-05 09:16:09 +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-08-05 09:16:09 +02:00
|
|
|
w.w.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-08-05 09:16:09 +02:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
}
|
|
|
|
w.done(msg)
|
|
|
|
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.msgFromUid(uid)
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not get message: %v", err)
|
2020-05-25 16:59:48 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := m.MarkAnswered(msg.Answered); err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not mark message as answered: %v", err)
|
2020-05-25 16:59:48 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = w.emitMessageInfo(m, msg)
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not emit message info: %v", err)
|
2020-05-25 16:59:48 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2022-10-26 22:29:06 +02:00
|
|
|
w.w.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: w.getDirectoryInfo(w.currentQueryName, w.query),
|
|
|
|
}, nil)
|
2020-05-25 16:59:48 +02:00
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 16:29:52 +02:00
|
|
|
func (w *worker) handleFlagMessages(msg *types.FlagMessages) error {
|
2019-08-05 09:16:09 +02:00
|
|
|
for _, uid := range msg.Uids {
|
|
|
|
m, err := w.msgFromUid(uid)
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not get message: %v", err)
|
2019-08-05 09:16:09 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-09-27 19:00:58 +02:00
|
|
|
if err := m.SetFlag(msg.Flag, msg.Enable); err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not set flag %v as %t for message: %v", msg.Flag, msg.Enable, err)
|
2019-08-05 09:16:09 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
2019-09-13 08:48:01 +02:00
|
|
|
err = w.emitMessageInfo(m, msg)
|
2019-08-05 09:16:09 +02:00
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not emit message info: %v", err)
|
2019-08-05 09:16:09 +02:00
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2022-10-26 22:29:06 +02:00
|
|
|
w.w.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: w.getDirectoryInfo(w.currentQueryName, w.query),
|
|
|
|
}, nil)
|
2019-08-05 09:16:09 +02:00
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-28 06:39:08 +02:00
|
|
|
func (w *worker) handleSearchDirectory(msg *types.SearchDirectory) error {
|
|
|
|
// the first item is the command (search / filter)
|
|
|
|
s := strings.Join(msg.Argv[1:], " ")
|
|
|
|
// we only want to search in the current query, so merge the two together
|
2021-11-13 07:31:16 +01:00
|
|
|
search := w.query
|
|
|
|
if s != "" {
|
|
|
|
search = fmt.Sprintf("(%v) and (%v)", w.query, s)
|
|
|
|
}
|
2019-09-13 08:47:59 +02:00
|
|
|
uids, err := w.uidsFromQuery(search)
|
2019-08-28 06:39:08 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.w.PostMessage(&types.SearchResults{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Uids: uids,
|
|
|
|
}, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 08:48:01 +02:00
|
|
|
func (w *worker) handleModifyLabels(msg *types.ModifyLabels) error {
|
|
|
|
for _, uid := range msg.Uids {
|
|
|
|
m, err := w.msgFromUid(uid)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not get message from uid %d: %w", uid, err)
|
2019-09-13 08:48:01 +02:00
|
|
|
}
|
|
|
|
err = m.ModifyTags(msg.Add, msg.Remove)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not modify message tags: %w", err)
|
2019-09-13 08:48:01 +02:00
|
|
|
}
|
|
|
|
err = w.emitMessageInfo(m, msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// tags changed, most probably some messages shifted to other folders
|
|
|
|
// so we need to re-enumerate the query content
|
|
|
|
err := w.emitDirectoryContents(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-21 16:21:26 +01:00
|
|
|
// and update the list of possible tags
|
|
|
|
w.emitLabelList()
|
2022-10-26 22:29:06 +02:00
|
|
|
w.w.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: w.getDirectoryInfo(w.currentQueryName, w.query),
|
|
|
|
}, nil)
|
2019-09-13 08:48:01 +02:00
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-05 09:16:09 +02:00
|
|
|
func (w *worker) loadQueryMap(acctConfig *config.AccountConfig) error {
|
|
|
|
raw, ok := acctConfig.Params["query-map"]
|
|
|
|
if !ok {
|
|
|
|
// nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
file, err := homedir.Expand(raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
w.nameQueryMap = make(map[string]string)
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2019-10-27 14:54:22 +01:00
|
|
|
if line == "" || line[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-05 09:16:09 +02:00
|
|
|
split := strings.SplitN(line, "=", 2)
|
|
|
|
if len(split) != 2 {
|
2019-10-28 21:31:01 +01:00
|
|
|
return fmt.Errorf("%v: invalid line %q, want name=query", file, line)
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
w.nameQueryMap[split[0]] = split[1]
|
2021-11-13 09:10:09 +01:00
|
|
|
w.queryMapOrder = append(w.queryMapOrder, split[0])
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 08:47:59 +02:00
|
|
|
func (w *worker) loadExcludeTags(
|
2022-07-31 22:16:40 +02:00
|
|
|
acctConfig *config.AccountConfig,
|
|
|
|
) []string {
|
2019-08-05 09:16:09 +02:00
|
|
|
raw, ok := acctConfig.Params["exclude-tags"]
|
|
|
|
if !ok {
|
|
|
|
// nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-13 08:47:59 +02:00
|
|
|
excludedTags := strings.Split(raw, ",")
|
2021-04-26 21:36:00 +02:00
|
|
|
for idx, tag := range excludedTags {
|
|
|
|
excludedTags[idx] = strings.Trim(tag, " ")
|
|
|
|
}
|
2019-09-13 08:47:59 +02:00
|
|
|
return excludedTags
|
2019-08-05 09:16:09 +02:00
|
|
|
}
|
2019-09-13 08:48:01 +02:00
|
|
|
|
|
|
|
func (w *worker) emitDirectoryContents(parent types.WorkerMessage) error {
|
2022-07-05 21:48:40 +02:00
|
|
|
query := w.query
|
|
|
|
if msg, ok := parent.(*types.FetchDirectoryContents); ok {
|
|
|
|
s := strings.Join(msg.FilterCriteria[1:], " ")
|
|
|
|
if s != "" {
|
|
|
|
query = fmt.Sprintf("(%v) and (%v)", query, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uids, err := w.uidsFromQuery(query)
|
2019-09-13 08:48:01 +02:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not fetch uids: %w", err)
|
2019-09-13 08:48:01 +02:00
|
|
|
}
|
2019-09-20 19:11:09 +02:00
|
|
|
sortedUids, err := w.sort(uids, w.currentSortCriteria)
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("error sorting directory: %v", err)
|
2019-09-20 19:11:09 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-09-13 08:48:01 +02:00
|
|
|
w.w.PostMessage(&types.DirectoryContents{
|
|
|
|
Message: types.RespondTo(parent),
|
2019-09-20 19:11:09 +02:00
|
|
|
Uids: sortedUids,
|
2019-09-13 08:48:01 +02:00
|
|
|
}, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 18:12:02 +01:00
|
|
|
func (w *worker) emitDirectoryThreaded(parent types.WorkerMessage) error {
|
2022-07-05 21:48:40 +02:00
|
|
|
query := w.query
|
|
|
|
if msg, ok := parent.(*types.FetchDirectoryThreaded); ok {
|
|
|
|
s := strings.Join(msg.FilterCriteria[1:], " ")
|
|
|
|
if s != "" {
|
|
|
|
query = fmt.Sprintf("(%v) and (%v)", query, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
threads, err := w.db.ThreadsFromQuery(query)
|
2021-11-12 18:12:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.w.PostMessage(&types.DirectoryThreaded{
|
|
|
|
Threads: threads,
|
|
|
|
}, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 08:48:01 +02:00
|
|
|
func (w *worker) emitMessageInfo(m *Message,
|
2022-07-31 22:16:40 +02:00
|
|
|
parent types.WorkerMessage,
|
|
|
|
) error {
|
2019-09-13 08:48:01 +02:00
|
|
|
info, err := m.MessageInfo()
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("could not get MessageInfo: %w", err)
|
2019-09-13 08:48:01 +02:00
|
|
|
}
|
|
|
|
w.w.PostMessage(&types.MessageInfo{
|
|
|
|
Message: types.RespondTo(parent),
|
|
|
|
Info: info,
|
|
|
|
}, nil)
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-20 19:11:09 +02:00
|
|
|
|
2019-12-21 16:21:26 +01:00
|
|
|
func (w *worker) emitLabelList() {
|
|
|
|
tags, err := w.db.ListTags()
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not load tags: %v", err)
|
2019-12-21 16:21:26 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.w.PostMessage(&types.LabelList{Labels: tags}, nil)
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:11:09 +02:00
|
|
|
func (w *worker) sort(uids []uint32,
|
2022-07-31 22:16:40 +02:00
|
|
|
criteria []*types.SortCriterion,
|
|
|
|
) ([]uint32, error) {
|
2019-09-20 19:11:09 +02:00
|
|
|
if len(criteria) == 0 {
|
|
|
|
return uids, nil
|
|
|
|
}
|
|
|
|
var msgInfos []*models.MessageInfo
|
|
|
|
for _, uid := range uids {
|
|
|
|
m, err := w.msgFromUid(uid)
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not get message: %v", err)
|
2019-09-20 19:11:09 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
info, err := m.MessageInfo()
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not get message info: %v", err)
|
2019-09-20 19:11:09 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
msgInfos = append(msgInfos, info)
|
|
|
|
}
|
|
|
|
sortedUids, err := lib.Sort(msgInfos, criteria)
|
|
|
|
if err != nil {
|
2022-10-02 11:36:05 +02:00
|
|
|
logging.Errorf("could not sort the messages: %v", err)
|
2019-09-20 19:11:09 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sortedUids, 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 {
|
2022-10-26 22:29:07 +02:00
|
|
|
w.w.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: w.getDirectoryInfo(w.currentQueryName, w.query),
|
|
|
|
}, nil)
|
2022-05-30 14:34:18 +02:00
|
|
|
w.done(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-26 22:29:04 +02:00
|
|
|
|
|
|
|
func (w *worker) handleDeleteMessages(msg *types.DeleteMessages) error {
|
2022-10-26 22:29:05 +02:00
|
|
|
if w.store == nil {
|
|
|
|
return errUnsupported
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:29:04 +02:00
|
|
|
var deleted []uint32
|
|
|
|
|
|
|
|
// With notmuch, two identical files can be referenced under
|
|
|
|
// the same index key, even if they exist in two different
|
|
|
|
// folders. So in order to remove the message from the right
|
|
|
|
// maildir folder we need to pass a hint to Remove() so it
|
|
|
|
// can purge the right file.
|
|
|
|
folders, _ := w.store.FolderMap()
|
|
|
|
path, ok := folders[w.currentQueryName]
|
|
|
|
if !ok {
|
|
|
|
w.err(msg, fmt.Errorf("Can only delete file from a maildir folder"))
|
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, uid := range msg.Uids {
|
|
|
|
m, err := w.msgFromUid(uid)
|
|
|
|
if err != nil {
|
|
|
|
logging.Errorf("could not get message: %v", err)
|
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := m.Remove(path); err != nil {
|
|
|
|
logging.Errorf("could not remove message: %v", err)
|
|
|
|
w.err(msg, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
deleted = append(deleted, uid)
|
|
|
|
}
|
|
|
|
if len(deleted) > 0 {
|
|
|
|
w.w.PostMessage(&types.MessagesDeleted{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Uids: deleted,
|
|
|
|
}, nil)
|
|
|
|
}
|
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleCopyMessages(msg *types.CopyMessages) error {
|
2022-10-26 22:29:05 +02:00
|
|
|
if w.store == nil {
|
|
|
|
return errUnsupported
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:29:04 +02:00
|
|
|
// Only allow file to be copied to a maildir folder
|
|
|
|
folders, _ := w.store.FolderMap()
|
|
|
|
dest, ok := folders[msg.Destination]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Can only move file to a maildir folder")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, uid := range msg.Uids {
|
|
|
|
m, err := w.msgFromUid(uid)
|
|
|
|
if err != nil {
|
|
|
|
logging.Errorf("could not get message: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := m.Copy(dest); err != nil {
|
|
|
|
logging.Errorf("could not copy message: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.w.PostMessage(&types.MessagesCopied{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Destination: msg.Destination,
|
|
|
|
Uids: msg.Uids,
|
|
|
|
}, nil)
|
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleMoveMessages(msg *types.MoveMessages) error {
|
2022-10-26 22:29:05 +02:00
|
|
|
if w.store == nil {
|
|
|
|
return errUnsupported
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:29:04 +02:00
|
|
|
var moved []uint32
|
|
|
|
|
|
|
|
// With notmuch, two identical files can be referenced under
|
|
|
|
// the same index key, even if they exist in two different
|
|
|
|
// folders. So in order to remove the message from the right
|
|
|
|
// maildir folder we need to pass a hint to Move() so it
|
|
|
|
// can act on the right file.
|
|
|
|
folders, _ := w.store.FolderMap()
|
|
|
|
source, ok := folders[w.currentQueryName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Can only move file from a maildir folder")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only allow file to be moved to a maildir folder
|
|
|
|
dest, ok := folders[msg.Destination]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Can only move file to a maildir folder")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, uid := range msg.Uids {
|
|
|
|
m, err := w.msgFromUid(uid)
|
|
|
|
if err != nil {
|
|
|
|
logging.Errorf("could not get message: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err := m.Move(source, dest); err != nil {
|
|
|
|
logging.Errorf("could not copy message: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
moved = append(moved, uid)
|
|
|
|
}
|
|
|
|
w.w.PostMessage(&types.MessagesDeleted{
|
|
|
|
Message: types.RespondTo(msg),
|
|
|
|
Uids: moved,
|
|
|
|
}, nil)
|
|
|
|
if err == nil {
|
|
|
|
w.done(msg)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleAppendMessage(msg *types.AppendMessage) error {
|
2022-10-26 22:29:05 +02:00
|
|
|
if w.store == nil {
|
|
|
|
return errUnsupported
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:29:04 +02:00
|
|
|
// Only allow file to be created in a maildir folder
|
|
|
|
// since we are the "master" maildir process, we can modify the maildir directly
|
|
|
|
folders, _ := w.store.FolderMap()
|
|
|
|
dest, ok := folders[msg.Destination]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Can only create file in a maildir folder")
|
|
|
|
}
|
|
|
|
key, writer, err := dest.Create(lib.ToMaildirFlags(msg.Flags))
|
|
|
|
if err != nil {
|
|
|
|
logging.Errorf("could not create message at %s: %v", msg.Destination, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filename, err := dest.Filename(key)
|
|
|
|
if err != nil {
|
|
|
|
writer.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(writer, msg.Reader); err != nil {
|
|
|
|
logging.Errorf("could not write message to destination: %v", err)
|
|
|
|
writer.Close()
|
|
|
|
os.Remove(filename)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
writer.Close()
|
|
|
|
if _, err := w.db.IndexFile(filename); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-26 22:29:06 +02:00
|
|
|
w.w.PostMessage(&types.DirectoryInfo{
|
|
|
|
Info: w.getDirectoryInfo(w.currentQueryName, w.query),
|
|
|
|
}, nil)
|
2022-10-26 22:29:04 +02:00
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleCreateDirectory(msg *types.CreateDirectory) error {
|
2022-10-26 22:29:05 +02:00
|
|
|
if w.store == nil {
|
|
|
|
return errUnsupported
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:29:04 +02:00
|
|
|
dir := w.store.Dir(msg.Directory)
|
|
|
|
if err := dir.Init(); err != nil {
|
|
|
|
logging.Errorf("could not create directory %s: %v",
|
|
|
|
msg.Directory, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) handleRemoveDirectory(msg *types.RemoveDirectory) error {
|
2022-10-26 22:29:05 +02:00
|
|
|
if w.store == nil {
|
|
|
|
return errUnsupported
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:29:04 +02:00
|
|
|
dir := w.store.Dir(msg.Directory)
|
|
|
|
if err := os.RemoveAll(string(dir)); err != nil {
|
|
|
|
logging.Errorf("could not remove directory %s: %v",
|
|
|
|
msg.Directory, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.done(msg)
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-26 22:29:10 +02:00
|
|
|
|
|
|
|
// This is a hack that calls MsgModifyTags with an empty list of tags to
|
|
|
|
// apply on new messages causing notmuch to rename files and effectively
|
|
|
|
// move them into the cur/ dir.
|
|
|
|
func (w *worker) processNewMaildirFiles(dir string) error {
|
|
|
|
f, err := os.Open(filepath.Join(dir, "new"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
names, err := f.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range names {
|
|
|
|
if n[0] == '.' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := w.db.MsgIDFromFilename(filepath.Join(dir, "new", n))
|
|
|
|
if err != nil {
|
|
|
|
// Message is not yet indexed, leave it alone
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Force message to move from new/ to cur/
|
|
|
|
err = w.db.MsgModifyTags(key, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
logging.Errorf("MsgModifyTags failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|