aboutsummaryrefslogtreecommitdiffstats
path: root/worker/mbox/create.go
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2022-07-11 20:11:18 +0200
committerRobin Jarry <robin@jarry.cc>2022-07-14 23:14:45 +0200
commita1a276e002b937e38585c1fe547bd0c00bc525c1 (patch)
treec39610446f03464caeb8d8571c90c2624b48e697 /worker/mbox/create.go
parent12dec19109f4ad91f60a2f012f1556bcf78312e9 (diff)
downloadaerc-a1a276e002b937e38585c1fe547bd0c00bc525c1.tar.gz
mbox: implement an mbox backend worker
Implement an mbox backend worker. Worker can be used for testing and development by mocking a backend for the message store. Worker does not modify the actual mbox file on disk; all operations are performed in memory. To use the mbox backend, create an mbox account in the accounts.conf where the source uses the "mbox://" scheme, such as source = mbox://~/mbox/ or source = mbox://~/mbox/file.mbox If the mbox source points to a directory, all files in this directory with the .mbox suffix will be opened as folders. If an outgoing smtp server is defined for the mbox account, replies can be sent to emails that are stored in the mbox file. Signed-off-by: Koni Marti <koni.marti@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'worker/mbox/create.go')
-rw-r--r--worker/mbox/create.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/worker/mbox/create.go b/worker/mbox/create.go
new file mode 100644
index 00000000..7c4d9f7b
--- /dev/null
+++ b/worker/mbox/create.go
@@ -0,0 +1,60 @@
+package mboxer
+
+import (
+ "io"
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+func createMailboxContainer(path string) (*mailboxContainer, error) {
+
+ file, err := os.Open(path)
+ if err != nil {
+ return nil, err
+ }
+
+ defer file.Close()
+
+ fileInfo, err := file.Stat()
+ if err != nil {
+ return nil, err
+ }
+
+ mbdata := &mailboxContainer{mailboxes: make(map[string]*container)}
+
+ openMboxFile := func(path string, r io.Reader) error {
+ // read mbox file
+ messages, err := Read(r)
+ if err != nil {
+ return err
+ }
+ _, name := filepath.Split(path)
+ name = strings.TrimSuffix(name, ".mbox")
+ mbdata.mailboxes[name] = &container{filename: path, messages: messages}
+ return nil
+ }
+
+ if fileInfo.IsDir() {
+ files, err := filepath.Glob(filepath.Join(path, "*.mbox"))
+ if err != nil {
+ return nil, err
+ }
+ for _, file := range files {
+ f, err := os.Open(file)
+ if err != nil {
+ continue
+ }
+ if err := openMboxFile(file, f); err != nil {
+ return nil, err
+ }
+ f.Close()
+ }
+ } else {
+ if err := openMboxFile(path, file); err != nil {
+ return nil, err
+ }
+ }
+
+ return mbdata, nil
+}