1package main
2
3import (
4 "context"
5 "log"
6 "path/filepath"
7 "time"
8
9 "github.com/mjl-/bstore"
10
11 "github.com/mjl-/mox/store"
12)
13
14func cmdExportMaildir(c *cmd) {
15 c.params = "[-single] dst-dir account-path [mailbox]"
16 c.help = `Export one or all mailboxes from an account in maildir format.
17
18Export bypasses a running mox instance. It opens the account mailbox/message
19database file directly. This may block if a running mox instance also has the
20database open, e.g. for IMAP connections. To export from a running instance, use
21the accounts web page or webmail.
22`
23 var single bool
24 c.flag.BoolVar(&single, "single", false, "export single mailbox, without any children. disabled if mailbox isn't specified.")
25 args := c.Parse()
26 xcmdExport(false, single, args, c)
27}
28
29func cmdExportMbox(c *cmd) {
30 c.params = "[-single] dst-dir account-path [mailbox]"
31 c.help = `Export messages from one or all mailboxes in an account in mbox format.
32
33Using mbox is not recommended. Maildir is a better format.
34
35Export bypasses a running mox instance. It opens the account mailbox/message
36database file directly. This may block if a running mox instance also has the
37database open, e.g. for IMAP connections. To export from a running instance, use
38the accounts web page or webmail.
39
40For mbox export, "mboxrd" is used where message lines starting with the magic
41"From " string are escaped by prepending a >. All ">*From " are escaped,
42otherwise reconstructing the original could lose a ">".
43`
44 var single bool
45 c.flag.BoolVar(&single, "single", false, "export single mailbox, without any children. disabled if mailbox isn't specified.")
46 args := c.Parse()
47 xcmdExport(true, single, args, c)
48}
49
50func xcmdExport(mbox, single bool, args []string, c *cmd) {
51 if len(args) != 2 && len(args) != 3 {
52 c.Usage()
53 }
54
55 dst := args[0]
56 accountDir := args[1]
57 var mailbox string
58 if len(args) == 3 {
59 mailbox = args[2]
60 } else {
61 single = false
62 }
63
64 dbpath := filepath.Join(accountDir, "index.db")
65 db, err := bstore.Open(context.Background(), dbpath, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, store.DBTypes...)
66 xcheckf(err, "open database %q", dbpath)
67 defer func() {
68 if err := db.Close(); err != nil {
69 log.Printf("closing db after export: %v", err)
70 }
71 }()
72
73 a := store.DirArchiver{Dir: dst}
74 err = store.ExportMessages(context.Background(), c.log, db, accountDir, a, !mbox, mailbox, !single)
75 xcheckf(err, "exporting messages")
76 err = a.Close()
77 xcheckf(err, "closing archiver")
78}
79