aboutsummaryrefslogtreecommitdiff
path: root/worker/maildir/container.go
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-07-11 09:44:53 -0400
committerDrew DeVault <sir@cmpwn.com>2019-07-12 11:26:39 -0400
commitd15ba24a2f0c177593533874ce3f4b70a47d832d (patch)
treef5b8af07e12bc7907c9ea6e4da5d0e8b12f0a1d9 /worker/maildir/container.go
parent840b5bd633c4376f32e345e0a7fb9c45a6ceddfe (diff)
Implement maildir copy
Create a delivery in the destination directory with the content of the source message.
Diffstat (limited to 'worker/maildir/container.go')
-rw-r--r--worker/maildir/container.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/worker/maildir/container.go b/worker/maildir/container.go
index 351afed..aa14575 100644
--- a/worker/maildir/container.go
+++ b/worker/maildir/container.go
@@ -2,6 +2,7 @@ package maildir
import (
"fmt"
+ "io"
"io/ioutil"
"log"
"path/filepath"
@@ -103,3 +104,40 @@ func (c *Container) DeleteAll(d maildir.Dir, uids []uint32) ([]uint32, error) {
}
return success, nil
}
+
+func (c *Container) CopyAll(
+ dest maildir.Dir, src maildir.Dir, uids []uint32) error {
+ for _, uid := range uids {
+ if err := c.copyMessage(dest, src, uid); err != nil {
+ return fmt.Errorf("could not copy message %d: %v", uid, err)
+ }
+ }
+ return nil
+}
+
+func (c *Container) copyMessage(
+ dest maildir.Dir, src maildir.Dir, uid uint32) error {
+ key, ok := c.uids.GetKey(uid)
+ if !ok {
+ return fmt.Errorf("could not find key for message id %d", uid)
+ }
+
+ f, err := src.Open(key)
+ if err != nil {
+ return fmt.Errorf("could not open source message: %v", err)
+ }
+
+ del, err := dest.NewDelivery()
+ if err != nil {
+ return fmt.Errorf("could not initialize delivery: %v")
+ }
+ defer del.Close()
+
+ if _, err = io.Copy(del, f); err != nil {
+ return fmt.Errorf("could not copy message to delivery: %v")
+ }
+
+ // TODO: preserve flags
+
+ return nil
+}