Implement maildir copy

Create a delivery in the destination directory with the content of the
source message.
This commit is contained in:
Ben Burwell 2019-07-11 09:44:53 -04:00 committed by Drew DeVault
parent 840b5bd633
commit d15ba24a2f
2 changed files with 41 additions and 2 deletions

View file

@ -2,6 +2,7 @@ package maildir
import ( import (
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"path/filepath" "path/filepath"
@ -103,3 +104,40 @@ func (c *Container) DeleteAll(d maildir.Dir, uids []uint32) ([]uint32, error) {
} }
return success, nil 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
}

View file

@ -327,8 +327,9 @@ func (w *Worker) handleReadMessages(msg *types.ReadMessages) error {
} }
func (w *Worker) handleCopyMessages(msg *types.CopyMessages) error { func (w *Worker) handleCopyMessages(msg *types.CopyMessages) error {
// TODO: this. defer w.done(msg)
return nil dest := w.c.Dir(msg.Destination)
return w.c.CopyAll(dest, *w.selected, msg.Uids)
} }
func (w *Worker) handleAppendMessage(msg *types.AppendMessage) error { func (w *Worker) handleAppendMessage(msg *types.AppendMessage) error {