Browse Source

handle folders

Signed-off-by: Erik Hollensbe <github@hollensbe.org>
main
Erik Hollensbe 8 months ago
parent
commit
d7041f42ed
  1. 102
      sink.go

102
sink.go

@ -7,6 +7,9 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"sync"
"github.com/emersion/go-imap"
@ -22,8 +25,9 @@ type mailbox struct {
}
type message struct {
mailbox *imap.MailboxInfo
message *imap.Message
mailbox *imap.MailboxInfo
message *imap.Message
messageKey string
}
func main() {
@ -89,6 +93,43 @@ func main() {
}
}
func createMaildir(path, rootDir string) (maildir.Dir, error) {
dir := maildir.Dir(path)
if path != rootDir {
up := filepath.Dir(path)
rel, err := filepath.Rel(rootDir, up)
if err != nil {
return dir, err
}
if strings.Contains(rel, "../") {
return dir, fmt.Errorf("path %q is below root", path)
}
if _, err := os.Stat(up); err != nil {
if d, err := createMaildir(up, rootDir); err != nil {
return d, err
}
}
}
// FIXME rel checks, etc.
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return dir, err
}
if err := dir.Init(); err != nil {
return dir, fmt.Errorf("could not init maikdir: %v", err)
}
if err := dir.Clean(); err != nil {
return dir, err
}
return dir, nil
}
func run(ctx *cli.Context) error {
if ctx.Args().Len() != 1 {
return errors.New("invalid arguments; try --help")
@ -108,15 +149,10 @@ func run(ctx *cli.Context) error {
username := ctx.String("username")
password := ctx.String("password")
var dir maildir.Dir
rootDir := ctx.Args().First()
if os.Getenv("SINK_NO_WRITE") == "" {
dir = maildir.Dir(ctx.Args().First())
if err := dir.Init(); err != nil {
return fmt.Errorf("could not init maikdir: %v", err)
}
if err := dir.Clean(); err != nil {
if createMaildir(rootDir, rootDir); err != nil {
return err
}
}
@ -277,30 +313,36 @@ func run(ctx *cli.Context) error {
}
for msg := range msgs {
dir, err := createMaildir(path.Join(rootDir, m.mailbox.Name), rootDir)
if err != nil {
listdone <- err
}
messageKey := ""
if os.Getenv("SINK_NO_WRITE") == "" {
wg.Add(1)
go func(msg *imap.Message, section *imap.BodySectionName) {
defer wg.Done()
r := msg.GetBody(section)
_, w, err := dir.Create(nil)
if err != nil {
listdone <- fmt.Errorf("%q: error creating entry in maildir: %v", name, err)
return
}
if _, err := io.Copy(w, r); err != nil {
listdone <- fmt.Errorf("%q: failure writing mail entry: %v", name, err)
return
}
w.Close()
}(msg, &section)
r := msg.GetBody(&section)
key, w, err := dir.Create(nil)
if err != nil {
listdone <- fmt.Errorf("%q: error creating entry in maildir: %v", name, err)
return
}
messageKey = key
if _, err := io.Copy(w, r); err != nil {
listdone <- fmt.Errorf("%q: failure writing mail entry: %v", name, err)
return
}
w.Close()
}
messages <- message{
mailbox: m.mailbox,
message: msg,
mailbox: m.mailbox,
message: msg,
messageKey: messageKey,
}
}
}
@ -326,7 +368,7 @@ func run(ctx *cli.Context) error {
byt := make([]byte, 8)
binary.BigEndian.PutUint64(byt, uint64(m.message.Uid))
return bucket.Put(byt, byt)
return bucket.Put(byt, []byte(m.messageKey))
})
if err != nil {
return err

Loading…
Cancel
Save