aboutsummaryrefslogtreecommitdiff
path: root/worker
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-08-08 15:27:50 -0400
committerBen Burwell <ben@benburwell.com>2019-09-18 09:49:34 -0400
commitfb488dafef33cbefa8d1263c0302041eb12d36cf (patch)
tree7ae8a70aa27addcd9018d840caf56983c5e58683 /worker
parent35dc57ed90f8c26fd2f792b312934acdcaaf5269 (diff)
Add mbox backendmbox
Diffstat (limited to 'worker')
-rw-r--r--worker/mbox/mbox.go367
-rw-r--r--worker/worker_enabled.go1
2 files changed, 368 insertions, 0 deletions
diff --git a/worker/mbox/mbox.go b/worker/mbox/mbox.go
new file mode 100644
index 0000000..8e411a1
--- /dev/null
+++ b/worker/mbox/mbox.go
@@ -0,0 +1,367 @@
+package mbox
+
+import (
+ "fmt"
+ "io"
+ "io/ioutil"
+ "net/url"
+ "os"
+ "path/filepath"
+ "strings"
+
+ mb "github.com/emersion/go-mbox"
+ "github.com/emersion/go-message"
+
+ "git.sr.ht/~sircmpwn/aerc/lib/uidstore"
+ "git.sr.ht/~sircmpwn/aerc/models"
+ "git.sr.ht/~sircmpwn/aerc/worker/handlers"
+ "git.sr.ht/~sircmpwn/aerc/worker/types"
+)
+
+func init() {
+ handlers.RegisterWorkerFactory("mbox", NewWorker)
+}
+
+var errUnsupported = fmt.Errorf("unsupported command")
+
+type Worker struct {
+ worker *types.Worker
+ basedir string
+ selected string
+ uids *uidstore.Store
+}
+
+func NewWorker(worker *types.Worker) (types.Backend, error) {
+ return &Worker{
+ worker: worker,
+ uids: uidstore.NewStore(),
+ }, nil
+}
+
+// Run starts the worker's message handling loop.
+func (w *Worker) Run() {
+ for {
+ action := <-w.worker.Actions
+ w.handleAction(action)
+ }
+}
+
+func (w *Worker) handleAction(action types.WorkerMessage) {
+ msg := w.worker.ProcessAction(action)
+ if err := w.handleMessage(msg); err == errUnsupported {
+ w.worker.PostMessage(&types.Unsupported{
+ Message: types.RespondTo(msg),
+ }, nil)
+ } else if err != nil {
+ w.worker.PostMessage(&types.Error{
+ Message: types.RespondTo(msg),
+ Error: err,
+ }, nil)
+ } else {
+ w.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil)
+ }
+}
+
+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)
+ 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)
+ case *types.ReadMessages:
+ return w.handleReadMessages(msg)
+ case *types.CopyMessages:
+ return w.handleCopyMessages(msg)
+ 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 {
+ w.worker.Logger.Printf("error configuring maildir worker: %v", err)
+ return err
+ }
+ w.basedir = u.Path
+ if u.Host == "~" {
+ home, err := os.UserHomeDir()
+ if err != nil {
+ return fmt.Errorf("could not resolve home directory: %v", err)
+ }
+ w.basedir = filepath.Join(home, u.Path)
+ }
+ w.worker.Logger.Printf("configured base dir: %s", w.basedir)
+ return nil
+}
+
+func (w *Worker) handleConnect(msg *types.Connect) error {
+ return nil
+}
+
+func (w *Worker) handleListDirectories(msg *types.ListDirectories) error {
+ dirs, err := ioutil.ReadDir(w.basedir)
+ if err != nil {
+ w.worker.Logger.Printf("error listing directories: %v", err)
+ return err
+ }
+ for _, dir := range dirs {
+ name := dir.Name()
+ // TODO: more flexibility? better heuristics?
+ if strings.HasPrefix(name, ".") {
+ continue
+ }
+ w.worker.PostMessage(&types.Directory{
+ Message: types.RespondTo(msg),
+ Dir: &models.Directory{Name: name},
+ }, nil)
+ }
+ return nil
+}
+
+func (w *Worker) handleOpenDirectory(msg *types.OpenDirectory) error {
+ w.worker.Logger.Printf("opening %s", msg.Directory)
+ w.selected = msg.Directory
+ // TODO: why does this need to be sent twice??
+ info := &types.DirectoryInfo{
+ Info: &models.DirectoryInfo{
+ Name: msg.Directory,
+ Flags: []string{},
+ ReadOnly: false,
+ // total messages
+ Exists: 0,
+ // new messages since mailbox was last opened
+ Recent: 0,
+ // total unread
+ Unseen: 0,
+ },
+ }
+ w.worker.PostMessage(info, nil)
+ w.worker.PostMessage(info, nil)
+ return nil
+}
+
+func (w *Worker) scanUIDs() ([]uint32, error) {
+ f, err := os.Open(filepath.Join(w.basedir, w.selected))
+ if err != nil {
+ w.worker.Logger.Printf("could not open mbox: %v", err)
+ return nil, err
+ }
+ defer f.Close()
+ br := mb.NewReader(f)
+ uids := []uint32{}
+ for {
+ mr, err := br.NextMessage()
+ if err == io.EOF {
+ return uids, nil
+ } else if err != nil {
+ return nil, err
+ }
+ msg, err := message.Read(mr)
+ if err != nil {
+ return nil, err
+ }
+ mid, err := msg.Header.Text("message-id")
+ if err != nil {
+ return nil, err
+ }
+ uids = append(uids, w.uids.GetOrInsert(mid))
+ }
+}
+
+func (w *Worker) handleFetchDirectoryContents(msg *types.FetchDirectoryContents) error {
+ uids, err := w.scanUIDs()
+ if err != nil {
+ w.worker.Logger.Printf("error scanning uids: %v", err)
+ return err
+ }
+ w.worker.PostMessage(&types.DirectoryContents{
+ Message: types.RespondTo(msg),
+ Uids: uids,
+ }, nil)
+ return nil
+}
+
+func (w *Worker) handleCreateDirectory(msg *types.CreateDirectory) error {
+ return errUnsupported
+}
+
+func (w *Worker) handleFetchMessageHeaders(msg *types.FetchMessageHeaders) error {
+ // for _, uid := range msg.Uids {
+ // m, err := w.c.Message(*w.selected, uid)
+ // if err != nil {
+ // w.worker.Logger.Printf("could not get message: %v", err)
+ // w.err(msg, err)
+ // continue
+ // }
+ // info, err := m.MessageInfo()
+ // if err != nil {
+ // w.worker.Logger.Printf("could not get message info: %v", err)
+ // w.err(msg, err)
+ // continue
+ // }
+ // w.worker.PostMessage(&types.MessageInfo{
+ // Message: types.RespondTo(msg),
+ // Info: info,
+ // }, nil)
+ // }
+ return nil
+}
+
+func (w *Worker) handleFetchMessageBodyPart(msg *types.FetchMessageBodyPart) error {
+
+ // // get reader
+ // m, err := w.c.Message(*w.selected, msg.Uid)
+ // if err != nil {
+ // w.worker.Logger.Printf("could not get message %d: %v", msg.Uid, err)
+ // return err
+ // }
+ // r, err := m.NewBodyPartReader(msg.Part)
+ // if err != nil {
+ // w.worker.Logger.Printf(
+ // "could not get body part reader for message=%d, parts=%#v: %v",
+ // 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)
+
+ // // mark message as read
+ // if err := m.MarkRead(true); err != nil {
+ // w.worker.Logger.Printf("could not mark message as read: %v", err)
+ // return err
+ // }
+
+ // // send updated flags to ui
+ // info, err := m.MessageInfo()
+ // if err != nil {
+ // w.worker.Logger.Printf("could not fetch message info: %v", err)
+ // return err
+ // }
+ // w.worker.PostMessage(&types.MessageInfo{
+ // Message: types.RespondTo(msg),
+ // Info: info,
+ // }, 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 {
+ // w.worker.Logger.Printf("could not get message %d: %v", uid, err)
+ // return err
+ // }
+ // r, err := m.NewReader()
+ // if err != nil {
+ // w.worker.Logger.Printf("could not get message reader: %v", err)
+ // return err
+ // }
+ // w.worker.PostMessage(&types.FullMessage{
+ // Message: types.RespondTo(msg),
+ // Content: &models.FullMessage{
+ // Uid: uid,
+ // Reader: r,
+ // },
+ // }, nil)
+ // }
+ 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 {
+ // w.worker.Logger.Printf("error removing some messages: %v", err)
+ // return err
+ // }
+ return nil
+}
+
+func (w *Worker) handleReadMessages(msg *types.ReadMessages) error {
+ // for _, uid := range msg.Uids {
+ // m, err := w.c.Message(*w.selected, uid)
+ // if err != nil {
+ // w.worker.Logger.Printf("could not get message: %v", err)
+ // w.err(msg, err)
+ // continue
+ // }
+ // if err := m.MarkRead(msg.Read); err != nil {
+ // w.worker.Logger.Printf("could not mark message as read: %v", err)
+ // w.err(msg, err)
+ // continue
+ // }
+ // info, err := m.MessageInfo()
+ // if err != nil {
+ // w.worker.Logger.Printf("could not get message info: %v", err)
+ // w.err(msg, err)
+ // continue
+ // }
+ // w.worker.PostMessage(&types.MessageInfo{
+ // Message: types.RespondTo(msg),
+ // Info: info,
+ // }, nil)
+ // }
+ return nil
+}
+
+func (w *Worker) handleCopyMessages(msg *types.CopyMessages) error {
+ // dest := w.c.Dir(msg.Destination)
+ // err := w.c.CopyAll(dest, *w.selected, msg.Uids)
+ // if err != nil {
+ // return err
+ // }
+ return nil
+}
+
+func (w *Worker) handleAppendMessage(msg *types.AppendMessage) error {
+ // dest := w.c.Dir(msg.Destination)
+ // delivery, err := dest.NewDelivery()
+ // if err != nil {
+ // w.worker.Logger.Printf("could not deliver message to %s: %v",
+ // msg.Destination, err)
+ // return err
+ // }
+ // defer delivery.Close()
+ // if _, err := io.Copy(delivery, msg.Reader); err != nil {
+ // w.worker.Logger.Printf("could not write message to destination: %v", err)
+ // return err
+ // }
+ return nil
+}
+
+func (w *Worker) handleSearchDirectory(msg *types.SearchDirectory) error {
+ return errUnsupported
+}
diff --git a/worker/worker_enabled.go b/worker/worker_enabled.go
index 4fba729..579430b 100644
--- a/worker/worker_enabled.go
+++ b/worker/worker_enabled.go
@@ -3,3 +3,4 @@ package worker
// the following workers are always enabled
import _ "git.sr.ht/~sircmpwn/aerc/worker/imap"
import _ "git.sr.ht/~sircmpwn/aerc/worker/maildir"
+import _ "git.sr.ht/~sircmpwn/aerc/worker/mbox"