| 1 |
//go:build notmuch |
| 2 |
// +build notmuch |
| 3 |
|
| 4 |
package notmuch |
| 5 |
|
| 6 |
import ( |
| 7 |
"bufio" |
| 8 |
"bytes" |
| 9 |
"context" |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"net/url" |
| 14 |
"os" |
| 15 |
"os/exec" |
| 16 |
"path/filepath" |
| 17 |
"strconv" |
| 18 |
"strings" |
| 19 |
"time" |
| 20 |
|
| 21 |
"git.sr.ht/~rjarry/aerc/config" |
| 22 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 23 |
"git.sr.ht/~rjarry/aerc/lib/watchers" |
| 24 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 25 |
"git.sr.ht/~rjarry/aerc/models" |
| 26 |
"git.sr.ht/~rjarry/aerc/worker/handlers" |
| 27 |
"git.sr.ht/~rjarry/aerc/worker/lib" |
| 28 |
"git.sr.ht/~rjarry/aerc/worker/middleware" |
| 29 |
notmuch "git.sr.ht/~rjarry/aerc/worker/notmuch/lib" |
| 30 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 31 |
"github.com/emersion/go-maildir" |
| 32 |
) |
| 33 |
|
| 34 |
func init() { |
| 35 |
handlers.RegisterWorkerFactory("notmuch", NewWorker) |
| 36 |
} |
| 37 |
|
| 38 |
type worker struct { |
| 39 |
w *types.Worker |
| 40 |
worker types.WorkerInteractor |
| 41 |
nmStateChange chan bool |
| 42 |
query string |
| 43 |
currentQueryName string |
| 44 |
queryMapOrder []string |
| 45 |
nameQueryMap map[string]string |
| 46 |
dynamicNameQueryMap map[string]string |
| 47 |
store *lib.MaildirStore |
| 48 |
maildirAccountPath string |
| 49 |
db *notmuch.DB |
| 50 |
setupErr error |
| 51 |
watcher watchers.FSWatcher |
| 52 |
watcherDebounce *time.Timer |
| 53 |
capabilities *models.Capabilities |
| 54 |
headers []string |
| 55 |
headersExclude []string |
| 56 |
state uint64 |
| 57 |
mfs types.MultiFileStrategy |
| 58 |
} |
| 59 |
|
| 60 |
// NewWorker creates a new notmuch worker with the provided worker. |
| 61 |
func NewWorker(w *types.Worker) (types.Backend, error) { |
| 62 |
events := make(chan bool, 20) |
| 63 |
watcher, err := watchers.NewWatcher() |
| 64 |
if err != nil { |
| 65 |
return nil, fmt.Errorf("could not create file system watcher: %w", err) |
| 66 |
} |
| 67 |
return &worker{ |
| 68 |
w: w, |
| 69 |
worker: w, |
| 70 |
nmStateChange: events, |
| 71 |
watcher: watcher, |
| 72 |
capabilities: &models.Capabilities{ |
| 73 |
Sort: true, |
| 74 |
Thread: true, |
| 75 |
}, |
| 76 |
dynamicNameQueryMap: make(map[string]string), |
| 77 |
}, nil |
| 78 |
} |
| 79 |
|
| 80 |
// Run starts the worker's message handling loop. |
| 81 |
func (w *worker) Run() { |
| 82 |
defer log.PanicHandler() |
| 83 |
for { |
| 84 |
select { |
| 85 |
case action := <-w.w.Actions(): |
| 86 |
msg := w.worker.ProcessAction(action) |
| 87 |
err := w.handleMessage(msg) |
| 88 |
switch { |
| 89 |
case errors.Is(err, types.ErrNoop): |
| 90 |
// Operation did not have any effect. |
| 91 |
// Do *NOT* send a Done message. |
| 92 |
break |
| 93 |
case errors.Is(err, context.Canceled): |
| 94 |
w.worker.PostMessage(&types.Cancelled{ |
| 95 |
Message: types.RespondTo(msg), |
| 96 |
}, nil) |
| 97 |
case errors.Is(err, types.ErrUnsupported): |
| 98 |
w.worker.PostMessage(&types.Unsupported{ |
| 99 |
Message: types.RespondTo(msg), |
| 100 |
}, nil) |
| 101 |
case err != nil: |
| 102 |
w.worker.PostMessage(&types.Error{ |
| 103 |
Message: types.RespondTo(msg), |
| 104 |
Error: err, |
| 105 |
}, nil) |
| 106 |
default: // err == nil |
| 107 |
// Operation is finished. |
| 108 |
// Send a Done message. |
| 109 |
if _, ok := msg.(*types.CheckMail); ok { |
| 110 |
break |
| 111 |
} |
| 112 |
w.worker.PostMessage(&types.Done{ |
| 113 |
Message: types.RespondTo(msg), |
| 114 |
}, nil) |
| 115 |
} |
| 116 |
case <-w.nmStateChange: |
| 117 |
err := w.handleNotmuchEvent() |
| 118 |
if err != nil { |
| 119 |
w.w.Errorf("notmuch event failure: %v", err) |
| 120 |
} |
| 121 |
case <-w.watcher.Events(): |
| 122 |
if w.watcherDebounce != nil { |
| 123 |
w.watcherDebounce.Stop() |
| 124 |
} |
| 125 |
// Debounce FS changes |
| 126 |
w.watcherDebounce = time.AfterFunc(50*time.Millisecond, func() { |
| 127 |
defer log.PanicHandler() |
| 128 |
w.nmStateChange <- true |
| 129 |
}) |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func (w *worker) Capabilities() *models.Capabilities { |
| 135 |
return w.capabilities |
| 136 |
} |
| 137 |
|
| 138 |
func (w *worker) PathSeparator() string { |
| 139 |
// make it configurable? |
| 140 |
// <rockorager> You can use those in query maps to force a tree |
| 141 |
// <rockorager> Might be nice to be configurable? I see some notmuch people namespace with "::" |
| 142 |
return "/" |
| 143 |
} |
| 144 |
|
| 145 |
func (w *worker) handleMessage(msg types.WorkerMessage) error { |
| 146 |
if w.setupErr != nil { |
| 147 |
// only configure can recover from a config error, bail for everything else |
| 148 |
_, isConfigure := msg.(*types.Configure) |
| 149 |
if !isConfigure { |
| 150 |
return w.setupErr |
| 151 |
} |
| 152 |
} |
| 153 |
if w.db != nil { |
| 154 |
err := w.db.Connect() |
| 155 |
if err != nil { |
| 156 |
return err |
| 157 |
} |
| 158 |
defer w.db.Close() |
| 159 |
} |
| 160 |
|
| 161 |
switch msg := msg.(type) { |
| 162 |
case *types.Unsupported: |
| 163 |
// No-op |
| 164 |
case *types.Configure: |
| 165 |
return w.handleConfigure(msg) |
| 166 |
case *types.Connect: |
| 167 |
return w.handleConnect(msg) |
| 168 |
case *types.ListDirectories: |
| 169 |
return w.handleListDirectories(msg) |
| 170 |
case *types.OpenDirectory: |
| 171 |
return w.handleOpenDirectory(msg) |
| 172 |
case *types.FetchDirectoryContents: |
| 173 |
return w.emitDirectoryContents(msg) |
| 174 |
case *types.FetchDirectoryThreaded: |
| 175 |
return w.emitDirectoryThreaded(msg) |
| 176 |
case *types.FetchMessageHeaders: |
| 177 |
return w.handleFetchMessageHeaders(msg) |
| 178 |
case *types.FetchMessageBodyPart: |
| 179 |
return w.handleFetchMessageBodyPart(msg) |
| 180 |
case *types.FetchFullMessages: |
| 181 |
return w.handleFetchFullMessages(msg) |
| 182 |
case *types.FlagMessages: |
| 183 |
return w.handleFlagMessages(msg) |
| 184 |
case *types.AnsweredMessages: |
| 185 |
return w.handleAnsweredMessages(msg) |
| 186 |
case *types.ForwardedMessages: |
| 187 |
return w.handleForwardedMessages(msg) |
| 188 |
case *types.SearchDirectory: |
| 189 |
return w.handleSearchDirectory(msg) |
| 190 |
case *types.ModifyLabels: |
| 191 |
return w.handleModifyLabels(msg) |
| 192 |
case *types.CheckMail: |
| 193 |
go w.handleCheckMail(msg) |
| 194 |
return nil |
| 195 |
case *types.DeleteMessages: |
| 196 |
return w.handleDeleteMessages(msg) |
| 197 |
case *types.CopyMessages: |
| 198 |
return w.handleCopyMessages(msg) |
| 199 |
case *types.MoveMessages: |
| 200 |
return w.handleMoveMessages(msg) |
| 201 |
case *types.AppendMessage: |
| 202 |
return w.handleAppendMessage(msg) |
| 203 |
case *types.CreateDirectory: |
| 204 |
return w.handleCreateDirectory(msg) |
| 205 |
case *types.RemoveDirectory: |
| 206 |
return w.handleRemoveDirectory(msg) |
| 207 |
} |
| 208 |
return types.ErrUnsupported |
| 209 |
} |
| 210 |
|
| 211 |
func (w *worker) handleConfigure(msg *types.Configure) error { |
| 212 |
var err error |
| 213 |
defer func() { |
| 214 |
if err == nil { |
| 215 |
w.setupErr = nil |
| 216 |
return |
| 217 |
} |
| 218 |
w.setupErr = fmt.Errorf("notmuch: %w", err) |
| 219 |
}() |
| 220 |
|
| 221 |
u, err := url.Parse(msg.Config.Source) |
| 222 |
if err != nil { |
| 223 |
w.w.Errorf("error configuring notmuch worker: %v", err) |
| 224 |
return err |
| 225 |
} |
| 226 |
err = w.loadQueryMap(msg.Config) |
| 227 |
if err != nil { |
| 228 |
return fmt.Errorf("could not load query map configuration: %w", err) |
| 229 |
} |
| 230 |
excludedTags := w.loadExcludeTags(msg.Config) |
| 231 |
w.db = notmuch.NewDB(u.Hostname(), excludedTags) |
| 232 |
|
| 233 |
if msg.Config.Params["enable-maildir"] != "false" { |
| 234 |
if err = w.db.Connect(); err != nil { |
| 235 |
return fmt.Errorf("connect: %w", err) |
| 236 |
} |
| 237 |
path := w.db.MailRoot() |
| 238 |
w.db.Close() |
| 239 |
w.maildirAccountPath = msg.Config.Params["maildir-account-path"] |
| 240 |
|
| 241 |
path = filepath.Join(path, w.maildirAccountPath) |
| 242 |
w.w.Infof("using maildir store %q", path) |
| 243 |
store, err := lib.NewMaildirStore(path, false) |
| 244 |
if err != nil { |
| 245 |
return fmt.Errorf("Cannot initialize maildir store: %w", err) |
| 246 |
} |
| 247 |
w.store = store |
| 248 |
} |
| 249 |
|
| 250 |
if name, ok := msg.Config.Params["folder-map"]; ok { |
| 251 |
file := xdg.ExpandHome(name) |
| 252 |
f, err := os.Open(file) |
| 253 |
if err != nil { |
| 254 |
return err |
| 255 |
} |
| 256 |
defer f.Close() |
| 257 |
fmap, order, err := lib.ParseFolderMap(bufio.NewReader(f)) |
| 258 |
if err != nil { |
| 259 |
return err |
| 260 |
} |
| 261 |
w.worker = middleware.NewFolderMapper(w.worker, fmap, order) |
| 262 |
} |
| 263 |
w.headers = msg.Config.Headers |
| 264 |
w.headersExclude = msg.Config.HeadersExclude |
| 265 |
|
| 266 |
mfs := msg.Config.Params["multi-file-strategy"] |
| 267 |
if mfs != "" { |
| 268 |
var ok bool |
| 269 |
w.mfs, ok = types.StrToStrategy[mfs] |
| 270 |
if !ok { |
| 271 |
return fmt.Errorf("invalid multi-file strategy %s", mfs) |
| 272 |
} |
| 273 |
} else { |
| 274 |
w.mfs = types.Refuse |
| 275 |
} |
| 276 |
|
| 277 |
return nil |
| 278 |
} |
| 279 |
|
| 280 |
func (w *worker) handleConnect(msg *types.Connect) error { |
| 281 |
w.emitLabelList() |
| 282 |
// Get initial db state |
| 283 |
w.state = w.db.State() |
| 284 |
// Watch all the files in the xapian folder for changes. We'll debounce |
| 285 |
// changes, so catching multiple is ok |
| 286 |
var dbPath string |
| 287 |
path := filepath.Join(w.db.Path(), ".notmuch", "xapian") |
| 288 |
_, err := os.Stat(path) |
| 289 |
if err != nil { |
| 290 |
if errors.Is(err, os.ErrNotExist) { |
| 291 |
dbPath = filepath.Join(w.db.Path(), "xapian") |
| 292 |
} else { |
| 293 |
return fmt.Errorf("error locating notmuch db: %w", err) |
| 294 |
} |
| 295 |
} else { |
| 296 |
dbPath = path |
| 297 |
} |
| 298 |
|
| 299 |
err = w.watcher.Configure(dbPath) |
| 300 |
log.Tracef("Configuring watcher for path: %v", dbPath) |
| 301 |
if err != nil { |
| 302 |
return fmt.Errorf("error configuring watcher: %w", err) |
| 303 |
} |
| 304 |
return nil |
| 305 |
} |
| 306 |
|
| 307 |
func (w *worker) handleListDirectories(msg *types.ListDirectories) error { |
| 308 |
for _, name := range w.queryMapOrder { |
| 309 |
w.worker.PostMessage(&types.Directory{ |
| 310 |
Message: types.RespondTo(msg), |
| 311 |
Dir: &models.Directory{ |
| 312 |
Name: name, |
| 313 |
Role: models.QueryRole, |
| 314 |
}, |
| 315 |
}, nil) |
| 316 |
} |
| 317 |
|
| 318 |
for name := range w.dynamicNameQueryMap { |
| 319 |
w.worker.PostMessage(&types.Directory{ |
| 320 |
Message: types.RespondTo(msg), |
| 321 |
Dir: &models.Directory{ |
| 322 |
Name: name, |
| 323 |
Role: models.QueryRole, |
| 324 |
}, |
| 325 |
}, nil) |
| 326 |
} |
| 327 |
|
| 328 |
if w.store != nil { |
| 329 |
folders, err := w.store.FolderMap() |
| 330 |
if err != nil { |
| 331 |
w.w.Errorf("failed listing directories: %v", err) |
| 332 |
return err |
| 333 |
} |
| 334 |
for name := range folders { |
| 335 |
w.worker.PostMessage(&types.Directory{ |
| 336 |
Message: types.RespondTo(msg), |
| 337 |
Dir: &models.Directory{ |
| 338 |
Name: name, |
| 339 |
}, |
| 340 |
}, nil) |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
return w.updateDirCounts() |
| 345 |
} |
| 346 |
|
| 347 |
func (w *worker) getDirectoryInfo(name string, query string) *models.DirectoryInfo { |
| 348 |
dirInfo := &models.DirectoryInfo{ |
| 349 |
Name: name, |
| 350 |
// total messages |
| 351 |
Exists: 0, |
| 352 |
// new messages since mailbox was last opened |
| 353 |
Recent: 0, |
| 354 |
// total unread |
| 355 |
Unseen: 0, |
| 356 |
} |
| 357 |
|
| 358 |
count, err := w.db.QueryCountMessages(query) |
| 359 |
if err != nil { |
| 360 |
return dirInfo |
| 361 |
} |
| 362 |
dirInfo.Exists = count.Exists |
| 363 |
dirInfo.Unseen = count.Unread |
| 364 |
|
| 365 |
return dirInfo |
| 366 |
} |
| 367 |
|
| 368 |
func (w *worker) handleOpenDirectory(msg *types.OpenDirectory) error { |
| 369 |
if msg.Context().Err() != nil { |
| 370 |
return context.Canceled |
| 371 |
} |
| 372 |
w.w.Tracef("opening %s with query %s", msg.Directory, msg.Query) |
| 373 |
|
| 374 |
var exists bool |
| 375 |
q := "" |
| 376 |
if w.store != nil { |
| 377 |
folders, _ := w.store.FolderMap() |
| 378 |
var dir maildir.Dir |
| 379 |
dir, exists = folders[msg.Directory] |
| 380 |
if exists { |
| 381 |
folder := filepath.Join(w.maildirAccountPath, msg.Directory) |
| 382 |
q = fmt.Sprintf("folder:%s", strconv.Quote(folder)) |
| 383 |
if err := w.processNewMaildirFiles(string(dir)); err != nil { |
| 384 |
return err |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
if q == "" { |
| 389 |
q, exists = w.nameQueryMap[msg.Directory] |
| 390 |
if !exists { |
| 391 |
q, exists = w.dynamicNameQueryMap[msg.Directory] |
| 392 |
} |
| 393 |
} |
| 394 |
if !exists || msg.Force { |
| 395 |
q = msg.Query |
| 396 |
if q == "" { |
| 397 |
q = msg.Directory |
| 398 |
} |
| 399 |
w.dynamicNameQueryMap[msg.Directory] = q |
| 400 |
w.worker.PostMessage(&types.Directory{ |
| 401 |
Message: types.RespondTo(msg), |
| 402 |
Dir: &models.Directory{ |
| 403 |
Name: msg.Directory, |
| 404 |
Role: models.QueryRole, |
| 405 |
}, |
| 406 |
}, nil) |
| 407 |
} else if msg.Query != "" && msg.Query != q { |
| 408 |
return errors.New("cannot use existing folder name for new query") |
| 409 |
} |
| 410 |
w.query = q |
| 411 |
w.currentQueryName = msg.Directory |
| 412 |
|
| 413 |
w.worker.PostMessage(&types.DirectoryInfo{ |
| 414 |
Info: w.getDirectoryInfo(msg.Directory, w.query), |
| 415 |
Message: types.RespondTo(msg), |
| 416 |
}, nil) |
| 417 |
return nil |
| 418 |
} |
| 419 |
|
| 420 |
func (w *worker) handleFetchMessageHeaders( |
| 421 |
msg *types.FetchMessageHeaders, |
| 422 |
) error { |
| 423 |
for _, uid := range msg.Uids { |
| 424 |
m, err := w.msgFromUid(uid) |
| 425 |
if err != nil { |
| 426 |
w.w.Errorf("could not get message: %v", err) |
| 427 |
w.emitMessageInfoError(msg, msg.Directory, uid, err) |
| 428 |
continue |
| 429 |
} |
| 430 |
err = w.emitMessageInfo(m, msg.Directory, msg) |
| 431 |
if err != nil { |
| 432 |
w.w.Errorf("could not emit message info: %v", err) |
| 433 |
w.emitMessageInfoError(msg, msg.Directory, uid, err) |
| 434 |
continue |
| 435 |
} |
| 436 |
} |
| 437 |
return nil |
| 438 |
} |
| 439 |
|
| 440 |
func (w *worker) uidsFromQuery(ctx context.Context, query string) ([]models.UID, error) { |
| 441 |
return w.db.MsgIDsFromQuery(ctx, query) |
| 442 |
} |
| 443 |
|
| 444 |
func (w *worker) msgFromUid(uid models.UID) (*Message, error) { |
| 445 |
msg := &Message{ |
| 446 |
key: string(uid), |
| 447 |
uid: uid, |
| 448 |
db: w.db, |
| 449 |
} |
| 450 |
return msg, nil |
| 451 |
} |
| 452 |
|
| 453 |
func (w *worker) handleFetchMessageBodyPart( |
| 454 |
msg *types.FetchMessageBodyPart, |
| 455 |
) error { |
| 456 |
m, err := w.msgFromUid(msg.Uid) |
| 457 |
if err != nil { |
| 458 |
w.w.Errorf("could not get message %d: %v", msg.Uid, err) |
| 459 |
return err |
| 460 |
} |
| 461 |
r, err := m.NewBodyPartReader(msg.Part) |
| 462 |
if err != nil { |
| 463 |
w.w.Errorf( |
| 464 |
"could not get body part reader for message=%d, parts=%#v: %w", |
| 465 |
msg.Uid, msg.Part, err) |
| 466 |
return err |
| 467 |
} |
| 468 |
w.worker.PostMessage(&types.MessageBodyPart{ |
| 469 |
Message: types.RespondTo(msg), |
| 470 |
Part: &models.MessageBodyPart{ |
| 471 |
Reader: r, |
| 472 |
Uid: msg.Uid, |
| 473 |
}, |
| 474 |
}, nil) |
| 475 |
return nil |
| 476 |
} |
| 477 |
|
| 478 |
func (w *worker) handleFetchFullMessages(msg *types.FetchFullMessages) error { |
| 479 |
for _, uid := range msg.Uids { |
| 480 |
m, err := w.msgFromUid(uid) |
| 481 |
if err != nil { |
| 482 |
w.w.Errorf("could not get message %d: %v", uid, err) |
| 483 |
return err |
| 484 |
} |
| 485 |
r, err := m.NewReader() |
| 486 |
if err != nil { |
| 487 |
w.w.Errorf("could not get message reader: %v", err) |
| 488 |
return err |
| 489 |
} |
| 490 |
defer r.Close() |
| 491 |
b, err := io.ReadAll(r) |
| 492 |
if err != nil { |
| 493 |
return err |
| 494 |
} |
| 495 |
w.worker.PostMessage(&types.FullMessage{ |
| 496 |
Message: types.RespondTo(msg), |
| 497 |
Content: &models.FullMessage{ |
| 498 |
Uid: uid, |
| 499 |
Reader: bytes.NewReader(b), |
| 500 |
}, |
| 501 |
}, nil) |
| 502 |
} |
| 503 |
return nil |
| 504 |
} |
| 505 |
|
| 506 |
func (w *worker) handleAnsweredMessages(msg *types.AnsweredMessages) error { |
| 507 |
for _, uid := range msg.Uids { |
| 508 |
m, err := w.msgFromUid(uid) |
| 509 |
if err != nil { |
| 510 |
w.w.Errorf("could not get message: %v", err) |
| 511 |
return err |
| 512 |
} |
| 513 |
if err := m.MarkAnswered(msg.Answered); err != nil { |
| 514 |
w.w.Errorf("could not mark message as answered: %v", err) |
| 515 |
return err |
| 516 |
} |
| 517 |
} |
| 518 |
return nil |
| 519 |
} |
| 520 |
|
| 521 |
func (w *worker) handleForwardedMessages(msg *types.ForwardedMessages) error { |
| 522 |
for _, uid := range msg.Uids { |
| 523 |
m, err := w.msgFromUid(uid) |
| 524 |
if err != nil { |
| 525 |
w.w.Errorf("could not get message: %v", err) |
| 526 |
return err |
| 527 |
} |
| 528 |
if err := m.MarkForwarded(msg.Forwarded); err != nil { |
| 529 |
w.w.Errorf("could not mark message as forwarded: %v", err) |
| 530 |
return err |
| 531 |
} |
| 532 |
} |
| 533 |
return nil |
| 534 |
} |
| 535 |
|
| 536 |
func (w *worker) handleFlagMessages(msg *types.FlagMessages) error { |
| 537 |
for _, uid := range msg.Uids { |
| 538 |
m, err := w.msgFromUid(uid) |
| 539 |
if err != nil { |
| 540 |
w.w.Errorf("could not get message: %v", err) |
| 541 |
return err |
| 542 |
} |
| 543 |
if err := m.SetFlag(msg.Flags, msg.Enable); err != nil { |
| 544 |
w.w.Errorf("could not set flag %v as %t for message: %v", |
| 545 |
msg.Flags, msg.Enable, err) |
| 546 |
return err |
| 547 |
} |
| 548 |
} |
| 549 |
return nil |
| 550 |
} |
| 551 |
|
| 552 |
func (w *worker) handleSearchDirectory(msg *types.SearchDirectory) error { |
| 553 |
search := notmuch.AndQueries(w.query, translate(msg.Criteria)) |
| 554 |
log.Debugf("search query: '%s'", search) |
| 555 |
uids, err := w.uidsFromQuery(msg.Context(), search) |
| 556 |
if err != nil { |
| 557 |
return err |
| 558 |
} |
| 559 |
w.worker.PostMessage(&types.SearchResults{ |
| 560 |
Message: types.RespondTo(msg), |
| 561 |
Directory: w.currentQueryName, |
| 562 |
Criteria: msg.Criteria, |
| 563 |
Uids: uids, |
| 564 |
}, nil) |
| 565 |
return nil |
| 566 |
} |
| 567 |
|
| 568 |
func (w *worker) handleModifyLabels(msg *types.ModifyLabels) error { |
| 569 |
for _, uid := range msg.Uids { |
| 570 |
m, err := w.msgFromUid(uid) |
| 571 |
if err != nil { |
| 572 |
return fmt.Errorf("could not get message from uid %s: %w", uid, err) |
| 573 |
} |
| 574 |
err = m.ModifyTags(msg.Add, msg.Remove, msg.Toggle) |
| 575 |
if err != nil { |
| 576 |
return fmt.Errorf("could not modify message tags: %w", err) |
| 577 |
} |
| 578 |
} |
| 579 |
return nil |
| 580 |
} |
| 581 |
|
| 582 |
func (w *worker) loadQueryMap(acctConfig *config.AccountConfig) error { |
| 583 |
raw, ok := acctConfig.Params["query-map"] |
| 584 |
if !ok { |
| 585 |
// nothing to do |
| 586 |
return nil |
| 587 |
} |
| 588 |
file := xdg.ExpandHome(raw) |
| 589 |
f, err := os.Open(file) |
| 590 |
if err != nil { |
| 591 |
return err |
| 592 |
} |
| 593 |
defer f.Close() |
| 594 |
w.nameQueryMap, w.queryMapOrder, err = lib.ParseFolderMap(f) |
| 595 |
return err |
| 596 |
} |
| 597 |
|
| 598 |
func (w *worker) loadExcludeTags( |
| 599 |
acctConfig *config.AccountConfig, |
| 600 |
) []string { |
| 601 |
raw, ok := acctConfig.Params["exclude-tags"] |
| 602 |
if !ok { |
| 603 |
// nothing to do |
| 604 |
return nil |
| 605 |
} |
| 606 |
excludedTags := strings.Split(raw, ",") |
| 607 |
for idx, tag := range excludedTags { |
| 608 |
excludedTags[idx] = strings.Trim(tag, " ") |
| 609 |
} |
| 610 |
return excludedTags |
| 611 |
} |
| 612 |
|
| 613 |
func (w *worker) emitDirectoryContents(msg *types.FetchDirectoryContents) error { |
| 614 |
query := notmuch.AndQueries(w.query, translate(msg.Filter)) |
| 615 |
log.Debugf("filter query: '%s'", query) |
| 616 |
uids, err := w.uidsFromQuery(msg.Context(), query) |
| 617 |
if err != nil { |
| 618 |
return fmt.Errorf("could not fetch uids: %w", err) |
| 619 |
} |
| 620 |
sortedUids, err := w.sort(uids, msg.SortCriteria, msg.Directory) |
| 621 |
if err != nil { |
| 622 |
w.w.Errorf("error sorting directory: %v", err) |
| 623 |
return err |
| 624 |
} |
| 625 |
w.worker.PostMessage(&types.DirectoryContents{ |
| 626 |
Message: types.RespondTo(msg), |
| 627 |
Directory: msg.Directory, |
| 628 |
Filter: msg.Filter, |
| 629 |
Uids: sortedUids, |
| 630 |
}, nil) |
| 631 |
return nil |
| 632 |
} |
| 633 |
|
| 634 |
func (w *worker) emitDirectoryThreaded(msg *types.FetchDirectoryThreaded) error { |
| 635 |
query := notmuch.AndQueries(w.query, translate(msg.Filter)) |
| 636 |
log.Debugf("filter query: '%s'", query) |
| 637 |
threads, err := w.db.ThreadsFromQuery(msg.Context(), query, msg.ThreadContext) |
| 638 |
if err != nil { |
| 639 |
return err |
| 640 |
} |
| 641 |
w.worker.PostMessage(&types.DirectoryThreaded{ |
| 642 |
Message: types.RespondTo(msg), |
| 643 |
Directory: msg.Directory, |
| 644 |
Filter: msg.Filter, |
| 645 |
Threads: threads, |
| 646 |
}, nil) |
| 647 |
return nil |
| 648 |
} |
| 649 |
|
| 650 |
func (w *worker) emitMessageInfoError(msg types.WorkerMessage, dir string, uid models.UID, err error) { |
| 651 |
w.worker.PostMessage(&types.MessageInfo{ |
| 652 |
Message: types.RespondTo(msg), |
| 653 |
Info: &models.MessageInfo{ |
| 654 |
Envelope: &models.Envelope{}, |
| 655 |
Flags: models.SeenFlag, |
| 656 |
Directory: dir, |
| 657 |
Uid: uid, |
| 658 |
Error: err, |
| 659 |
}, |
| 660 |
}, nil) |
| 661 |
} |
| 662 |
|
| 663 |
func (w *worker) emitMessageInfo(m *Message, dir string, |
| 664 |
parent types.WorkerMessage, |
| 665 |
) error { |
| 666 |
info, err := m.MessageInfo(dir) |
| 667 |
if err != nil { |
| 668 |
return fmt.Errorf("could not get MessageInfo: %w", err) |
| 669 |
} |
| 670 |
switch { |
| 671 |
case len(w.headersExclude) > 0: |
| 672 |
info.RFC822Headers = lib.LimitHeaders(info.RFC822Headers, w.headersExclude, true) |
| 673 |
case len(w.headers) > 0: |
| 674 |
info.RFC822Headers = lib.LimitHeaders(info.RFC822Headers, w.headers, false) |
| 675 |
} |
| 676 |
switch parent { |
| 677 |
case nil: |
| 678 |
w.worker.PostMessage(&types.MessageInfo{ |
| 679 |
Info: info, |
| 680 |
}, nil) |
| 681 |
default: |
| 682 |
w.worker.PostMessage(&types.MessageInfo{ |
| 683 |
Message: types.RespondTo(parent), |
| 684 |
Info: info, |
| 685 |
}, nil) |
| 686 |
} |
| 687 |
return nil |
| 688 |
} |
| 689 |
|
| 690 |
func (w *worker) emitLabelList() { |
| 691 |
tags := w.db.ListTags() |
| 692 |
w.worker.PostMessage(&types.LabelList{Labels: tags}, nil) |
| 693 |
} |
| 694 |
|
| 695 |
func (w *worker) msgHeadersFromUid(uid models.UID, dir string) (*models.MessageInfo, error) { |
| 696 |
m := &Message{ |
| 697 |
key: string(uid), |
| 698 |
uid: uid, |
| 699 |
db: w.db, |
| 700 |
} |
| 701 |
info, err := m.MessageHeaders(dir) |
| 702 |
if err != nil { |
| 703 |
return nil, err |
| 704 |
} |
| 705 |
return info, nil |
| 706 |
} |
| 707 |
|
| 708 |
func (w *worker) sort( |
| 709 |
uids []models.UID, criteria []*types.SortCriterion, dir string, |
| 710 |
) ([]models.UID, error) { |
| 711 |
if len(criteria) == 0 { |
| 712 |
return uids, nil |
| 713 |
} |
| 714 |
if len(criteria) == 1 && criteria[0].Reverse == true && criteria[0].Field == types.SortDate { |
| 715 |
log.Debugf("Sort criteria matches notmuch's output, so skipping sort") |
| 716 |
return uids, nil |
| 717 |
} |
| 718 |
|
| 719 |
// Note: unlike with maildir, fetching message headers isn't easily |
| 720 |
// parallelizable, because notmuch is used to get each message's |
| 721 |
// filename, and notmuch isn't thread safe. |
| 722 |
var msgInfos []*models.MessageInfo |
| 723 |
for _, uid := range uids { |
| 724 |
info, err := w.msgHeadersFromUid(uid, dir) |
| 725 |
if err != nil { |
| 726 |
w.w.Errorf("could not get message info: %v", err) |
| 727 |
continue |
| 728 |
} |
| 729 |
msgInfos = append(msgInfos, info) |
| 730 |
} |
| 731 |
|
| 732 |
sortedUids, err := lib.Sort(msgInfos, criteria) |
| 733 |
if err != nil { |
| 734 |
w.w.Errorf("could not sort the messages: %v", err) |
| 735 |
return nil, err |
| 736 |
} |
| 737 |
return sortedUids, nil |
| 738 |
} |
| 739 |
|
| 740 |
func (w *worker) handleCheckMail(msg *types.CheckMail) { |
| 741 |
defer log.PanicHandler() |
| 742 |
if msg.Command == "" { |
| 743 |
w.worker.PostMessage(&types.Error{ |
| 744 |
Message: types.RespondTo(msg), |
| 745 |
Error: fmt.Errorf("(%s) checkmail: no command specified", msg.Account()), |
| 746 |
}, nil) |
| 747 |
return |
| 748 |
} |
| 749 |
ctx, cancel := context.WithTimeout(context.Background(), msg.Timeout) |
| 750 |
defer cancel() |
| 751 |
cmd := exec.CommandContext(ctx, "sh", "-c", msg.Command) |
| 752 |
err := cmd.Run() |
| 753 |
switch { |
| 754 |
case ctx.Err() != nil: |
| 755 |
w.worker.PostMessage(&types.Error{ |
| 756 |
Message: types.RespondTo(msg), |
| 757 |
Error: fmt.Errorf("(%s) checkmail: timed out", msg.Account()), |
| 758 |
}, nil) |
| 759 |
case err != nil: |
| 760 |
w.worker.PostMessage(&types.Error{ |
| 761 |
Message: types.RespondTo(msg), |
| 762 |
Error: fmt.Errorf("(%s) checkmail: error running command: %w", msg.Account(), err), |
| 763 |
}, nil) |
| 764 |
default: |
| 765 |
w.worker.PostMessage(&types.Done{ |
| 766 |
Message: types.RespondTo(msg), |
| 767 |
}, nil) |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
// folderDir returns the maildir.Dir for the given folder name. If name is |
| 772 |
// empty, returns the currently selected folder. |
| 773 |
func (w *worker) folderDir(folders map[string]maildir.Dir, name string) maildir.Dir { |
| 774 |
if name != "" { |
| 775 |
if dir, ok := folders[name]; ok { |
| 776 |
return dir |
| 777 |
} |
| 778 |
} |
| 779 |
return folders[w.currentQueryName] |
| 780 |
} |
| 781 |
|
| 782 |
func (w *worker) handleDeleteMessages(msg *types.DeleteMessages) error { |
| 783 |
if w.store == nil { |
| 784 |
return types.ErrUnsupported |
| 785 |
} |
| 786 |
|
| 787 |
var deleted []models.UID |
| 788 |
|
| 789 |
folders, _ := w.store.FolderMap() |
| 790 |
curDir := w.folderDir(folders, msg.Directory) |
| 791 |
|
| 792 |
mfs := w.mfs |
| 793 |
if msg.MultiFileStrategy != nil { |
| 794 |
mfs = *msg.MultiFileStrategy |
| 795 |
} |
| 796 |
|
| 797 |
for _, uid := range msg.Uids { |
| 798 |
m, err := w.msgFromUid(uid) |
| 799 |
if err != nil { |
| 800 |
w.w.Errorf("could not get message: %v", err) |
| 801 |
return err |
| 802 |
} |
| 803 |
if err := m.Remove(curDir, mfs); err != nil { |
| 804 |
w.w.Errorf("could not remove message: %v", err) |
| 805 |
return err |
| 806 |
} |
| 807 |
deleted = append(deleted, uid) |
| 808 |
} |
| 809 |
if len(deleted) > 0 { |
| 810 |
w.worker.PostMessage(&types.MessagesDeleted{ |
| 811 |
Message: types.RespondTo(msg), |
| 812 |
Directory: msg.Directory, |
| 813 |
Uids: deleted, |
| 814 |
}, nil) |
| 815 |
} |
| 816 |
return nil |
| 817 |
} |
| 818 |
|
| 819 |
func (w *worker) handleCopyMessages(msg *types.CopyMessages) error { |
| 820 |
if w.store == nil { |
| 821 |
return types.ErrUnsupported |
| 822 |
} |
| 823 |
|
| 824 |
// Only allow file to be copied to a maildir folder |
| 825 |
folders, _ := w.store.FolderMap() |
| 826 |
dest, ok := folders[msg.Destination] |
| 827 |
if !ok { |
| 828 |
return fmt.Errorf("Can only copy file to a maildir folder") |
| 829 |
} |
| 830 |
|
| 831 |
curDir := w.folderDir(folders, msg.Source) |
| 832 |
|
| 833 |
mfs := w.mfs |
| 834 |
if msg.MultiFileStrategy != nil { |
| 835 |
mfs = *msg.MultiFileStrategy |
| 836 |
} |
| 837 |
|
| 838 |
for _, uid := range msg.Uids { |
| 839 |
m, err := w.msgFromUid(uid) |
| 840 |
if err != nil { |
| 841 |
w.w.Errorf("could not get message: %v", err) |
| 842 |
return err |
| 843 |
} |
| 844 |
if err := m.Copy(curDir, dest, mfs); err != nil { |
| 845 |
w.w.Errorf("could not copy message: %v", err) |
| 846 |
return err |
| 847 |
} |
| 848 |
} |
| 849 |
w.worker.PostMessage(&types.MessagesCopied{ |
| 850 |
Message: types.RespondTo(msg), |
| 851 |
Destination: msg.Destination, |
| 852 |
Uids: msg.Uids, |
| 853 |
}, nil) |
| 854 |
return nil |
| 855 |
} |
| 856 |
|
| 857 |
func (w *worker) handleMoveMessages(msg *types.MoveMessages) error { |
| 858 |
if w.store == nil { |
| 859 |
return types.ErrUnsupported |
| 860 |
} |
| 861 |
|
| 862 |
folders, _ := w.store.FolderMap() |
| 863 |
|
| 864 |
// Only allow file to be moved to a maildir folder |
| 865 |
dest, ok := folders[msg.Destination] |
| 866 |
if !ok { |
| 867 |
return fmt.Errorf("Can only move file to a maildir folder") |
| 868 |
} |
| 869 |
|
| 870 |
curDir := w.folderDir(folders, msg.Source) |
| 871 |
|
| 872 |
mfs := w.mfs |
| 873 |
if msg.MultiFileStrategy != nil { |
| 874 |
mfs = *msg.MultiFileStrategy |
| 875 |
} |
| 876 |
|
| 877 |
for _, uid := range msg.Uids { |
| 878 |
m, err := w.msgFromUid(uid) |
| 879 |
if err != nil { |
| 880 |
w.w.Errorf("could not get message: %v", err) |
| 881 |
return err |
| 882 |
} |
| 883 |
if err := m.Move(curDir, dest, mfs); err != nil { |
| 884 |
w.w.Errorf("could not move message: %v", err) |
| 885 |
return err |
| 886 |
} |
| 887 |
} |
| 888 |
w.worker.PostMessage(&types.MessagesDeleted{ |
| 889 |
Message: types.RespondTo(msg), |
| 890 |
Directory: msg.Source, |
| 891 |
Uids: msg.Uids, |
| 892 |
}, nil) |
| 893 |
return nil |
| 894 |
} |
| 895 |
|
| 896 |
func (w *worker) handleAppendMessage(msg *types.AppendMessage) error { |
| 897 |
if w.store == nil { |
| 898 |
return types.ErrUnsupported |
| 899 |
} |
| 900 |
|
| 901 |
// Only allow file to be created in a maildir folder |
| 902 |
// since we are the "master" maildir process, we can modify the maildir directly |
| 903 |
folders, _ := w.store.FolderMap() |
| 904 |
dest, ok := folders[msg.Destination] |
| 905 |
if !ok { |
| 906 |
return fmt.Errorf("Can only create file in a maildir folder") |
| 907 |
} |
| 908 |
newMsg, writer, err := dest.Create(lib.ToMaildirFlags(msg.Flags)) |
| 909 |
if err != nil { |
| 910 |
w.w.Errorf("could not create message at %s: %v", msg.Destination, err) |
| 911 |
return err |
| 912 |
} |
| 913 |
filename := newMsg.Filename() |
| 914 |
if _, err := io.Copy(writer, msg.Reader); err != nil { |
| 915 |
w.w.Errorf("could not write message to destination: %v", err) |
| 916 |
writer.Close() |
| 917 |
os.Remove(filename) |
| 918 |
return err |
| 919 |
} |
| 920 |
writer.Close() |
| 921 |
id, err := w.db.IndexFile(filename) |
| 922 |
if err != nil { |
| 923 |
return err |
| 924 |
} |
| 925 |
|
| 926 |
err = w.addFlags(id, msg.Flags) |
| 927 |
if err != nil { |
| 928 |
return err |
| 929 |
} |
| 930 |
|
| 931 |
w.worker.PostMessage(&types.DirectoryInfo{ |
| 932 |
Info: w.getDirectoryInfo(w.currentQueryName, w.query), |
| 933 |
}, nil) |
| 934 |
return nil |
| 935 |
} |
| 936 |
|
| 937 |
func (w *worker) handleCreateDirectory(msg *types.CreateDirectory) error { |
| 938 |
if w.store == nil { |
| 939 |
return types.ErrUnsupported |
| 940 |
} |
| 941 |
|
| 942 |
dir := w.store.Dir(msg.Directory) |
| 943 |
if err := dir.Init(); err != nil { |
| 944 |
w.w.Errorf("could not create directory %s: %v", |
| 945 |
msg.Directory, err) |
| 946 |
return err |
| 947 |
} |
| 948 |
return nil |
| 949 |
} |
| 950 |
|
| 951 |
func (w *worker) handleRemoveDirectory(msg *types.RemoveDirectory) error { |
| 952 |
_, inQueryMap := w.nameQueryMap[msg.Directory] |
| 953 |
if inQueryMap { |
| 954 |
return types.ErrUnsupported |
| 955 |
} |
| 956 |
|
| 957 |
if _, ok := w.dynamicNameQueryMap[msg.Directory]; ok { |
| 958 |
delete(w.dynamicNameQueryMap, msg.Directory) |
| 959 |
return nil |
| 960 |
} |
| 961 |
|
| 962 |
if w.store == nil { |
| 963 |
return nil |
| 964 |
} |
| 965 |
|
| 966 |
dir := w.store.Dir(msg.Directory) |
| 967 |
if err := os.RemoveAll(string(dir)); err != nil { |
| 968 |
w.w.Errorf("could not remove directory %s: %v", |
| 969 |
msg.Directory, err) |
| 970 |
return err |
| 971 |
} |
| 972 |
return nil |
| 973 |
} |
| 974 |
|
| 975 |
// This is a hack that calls MsgModifyTags with an empty list of tags to |
| 976 |
// apply on new messages causing notmuch to rename files and effectively |
| 977 |
// move them into the cur/ dir. |
| 978 |
func (w *worker) processNewMaildirFiles(dir string) error { |
| 979 |
f, err := os.Open(filepath.Join(dir, "new")) |
| 980 |
if err != nil { |
| 981 |
return err |
| 982 |
} |
| 983 |
defer f.Close() |
| 984 |
names, err := f.Readdirnames(0) |
| 985 |
if err != nil { |
| 986 |
return err |
| 987 |
} |
| 988 |
|
| 989 |
for _, n := range names { |
| 990 |
if n[0] == '.' { |
| 991 |
continue |
| 992 |
} |
| 993 |
|
| 994 |
key, err := w.db.MsgIDFromFilename(filepath.Join(dir, "new", n)) |
| 995 |
if err != nil { |
| 996 |
// Message is not yet indexed, leave it alone |
| 997 |
continue |
| 998 |
} |
| 999 |
// Force message to move from new/ to cur/ |
| 1000 |
err = w.db.MsgModifyTags(key, nil, nil, nil) |
| 1001 |
if err != nil { |
| 1002 |
w.w.Errorf("MsgModifyTags failed: %v", err) |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
return nil |
| 1007 |
} |
| 1008 |
|
| 1009 |
func (w *worker) addFlags(id string, flags models.Flags) error { |
| 1010 |
addTags := []string{} |
| 1011 |
removeTags := []string{} |
| 1012 |
for flag, tag := range flagToTag { |
| 1013 |
if !flags.Has(flag) { |
| 1014 |
continue |
| 1015 |
} |
| 1016 |
|
| 1017 |
if flagToInvert[flag] { |
| 1018 |
removeTags = append(removeTags, tag) |
| 1019 |
} else { |
| 1020 |
addTags = append(addTags, tag) |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
return w.db.MsgModifyTags(id, addTags, removeTags, nil) |
| 1025 |
} |