18 "github.com/mjl-/bstore"
20 "github.com/mjl-/mox/mlog"
23// Archiver can archive multiple mailboxes and their messages.
24type Archiver interface {
25 // Add file to archive. If name ends with a slash, it is created as a directory and
26 // the returned io.WriteCloser can be ignored.
27 Create(name string, size int64, mtime time.Time) (io.WriteCloser, error)
31// TarArchiver is an Archiver that writes to a tar ifle.
32type TarArchiver struct {
36// Create adds a file header to the tar file.
37func (a TarArchiver) Create(name string, size int64, mtime time.Time) (io.WriteCloser, error) {
43 Format: tar.FormatPAX,
45 if err := a.WriteHeader(&hdr); err != nil {
48 return nopCloser{a}, nil
51// ZipArchiver is an Archiver that writes to a zip file.
52type ZipArchiver struct {
56// Create adds a file header to the zip file.
57func (a ZipArchiver) Create(name string, size int64, mtime time.Time) (io.WriteCloser, error) {
58 hdr := zip.FileHeader{
62 UncompressedSize64: uint64(size),
64 w, err := a.CreateHeader(&hdr)
68 return nopCloser{w}, nil
71type nopCloser struct {
76func (nopCloser) Close() error {
80// DirArchiver is an Archiver that writes to a directory.
81type DirArchiver struct {
85// Create create name in the file system, in dir.
86// name must always use forwarded slashes.
87func (a DirArchiver) Create(name string, size int64, mtime time.Time) (io.WriteCloser, error) {
88 isdir := strings.HasSuffix(name, "/")
89 name = strings.TrimSuffix(name, "/")
90 p := filepath.Join(a.Dir, filepath.FromSlash(name))
91 os.MkdirAll(filepath.Dir(p), 0770)
93 return nil, os.Mkdir(p, 0770)
95 return os.OpenFile(p, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
98// Close on a dir does nothing.
99func (a DirArchiver) Close() error {
103// ExportMessages writes messages to archiver. Either in maildir format, or otherwise in
104// mbox. If mailboxOpt is empty, all mailboxes are exported, otherwise only the
107// Some errors are not fatal and result in skipped messages. In that happens, a
108// file "errors.txt" is added to the archive describing the errors. The goal is to
109// let users export (hopefully) most messages even in the face of errors.
110func ExportMessages(ctx context.Context, log mlog.Log, db *bstore.DB, accountDir string, archiver Archiver, maildir bool, mailboxOpt string) error {
111 // todo optimize: should prepare next file to add to archive (can be an mbox with many messages) while writing a file to the archive (which typically compresses, which takes time).
113 // Start transaction without closure, we are going to close it early, but don't
114 // want to deal with declaring many variables now to be able to assign them in a
115 // closure and use them afterwards.
116 tx, err := db.Begin(ctx, false)
118 return fmt.Errorf("transaction: %v", err)
123 log.Check(err, "transaction rollback after export error")
129 // Set up mailbox names and ids.
130 id2name := map[int64]string{}
131 name2id := map[string]int64{}
133 mailboxes, err := bstore.QueryTx[Mailbox](tx).List()
135 return fmt.Errorf("query mailboxes: %w", err)
137 for _, mb := range mailboxes {
138 id2name[mb.ID] = mb.Name
139 name2id[mb.Name] = mb.ID
143 if mailboxOpt != "" {
145 mailboxID, ok = name2id[mailboxOpt]
147 return fmt.Errorf("mailbox not found")
152 for _, name := range id2name {
153 if mailboxOpt != "" && name != mailboxOpt {
156 names = append(names, name)
158 // We need to sort the names because maildirs can create subdirs. Ranging over
159 // id2name directly would randomize the directory names, we would create a sub
160 // maildir before the parent, and fail with "dir exists" when creating the parent
162 sort.Slice(names, func(i, j int) bool {
163 return names[i] < names[j]
166 mailboxOrder := map[int64]int{}
167 for i, name := range names {
168 mbID := name2id[name]
169 mailboxOrder[mbID] = i
172 // Fetch all messages. This can take quite a bit of memory if the mailbox is large.
173 q := bstore.QueryTx[Message](tx)
175 q.FilterNonzero(Message{MailboxID: mailboxID})
177 msgs, err := q.List()
179 return fmt.Errorf("listing messages: %v", err)
182 // Close transaction. We don't want to hold it for too long. We are now at risk
183 // that a message is be removed while we export, or flags changed. At least the
184 // size won't change. If we cannot open the message later on, we'll skip it and add
185 // an error message to an errors.txt file in the output archive.
186 if err := tx.Rollback(); err != nil {
187 return fmt.Errorf("closing transaction: %v", err)
191 // Order the messages by mailbox, received time and finally message ID.
192 sort.Slice(msgs, func(i, j int) bool {
193 iid := msgs[i].MailboxID
194 jid := msgs[j].MailboxID
196 return mailboxOrder[iid] < mailboxOrder[jid]
198 if !msgs[i].Received.Equal(msgs[j].Received) {
199 return msgs[i].Received.Before(msgs[j].Received)
201 return msgs[i].ID < msgs[j].ID
204 // We keep track of errors reading message files. We continue exporting and add an
205 // errors.txt file to the archive. In case of errors, the user can get (hopefully)
206 // most of their emails, and see something went wrong. For other errors, like
207 // writing to the archiver (e.g. a browser), we abort, because we don't want to
208 // continue with useless work.
211 var curMailboxID int64 // Used to set curMailbox and finish a previous mbox file.
212 var curMailbox string
215 var mboxwriter *bufio.Writer
218 CloseRemoveTempFile(log, mboxtmp, "mbox")
222 // For dovecot-keyword-style flags not in standard maildir.
223 maildirFlags := map[string]int{}
224 var maildirFlaglist []string
225 maildirFlag := func(flag string) string {
226 i, ok := maildirFlags[flag]
228 if len(maildirFlags) >= 26 {
229 // Max 26 flag characters.
232 i = len(maildirFlags)
233 maildirFlags[flag] = i
234 maildirFlaglist = append(maildirFlaglist, flag)
236 return string(rune('a' + i))
239 finishMailbox := func() error {
241 if len(maildirFlags) == 0 {
246 for i, flag := range maildirFlaglist {
247 if _, err := fmt.Fprintf(&b, "%d %s\n", i, flag); err != nil {
251 w, err := archiver.Create(curMailbox+"/dovecot-keywords", int64(b.Len()), start)
253 return fmt.Errorf("adding dovecot-keywords: %v", err)
255 if _, err := w.Write(b.Bytes()); err != nil {
257 log.Check(xerr, "closing dovecot-keywords file after closing")
258 return fmt.Errorf("writing dovecot-keywords: %v", err)
260 maildirFlags = map[string]int{}
261 maildirFlaglist = nil
269 if err := mboxwriter.Flush(); err != nil {
270 return fmt.Errorf("flush mbox writer: %v", err)
272 fi, err := mboxtmp.Stat()
274 return fmt.Errorf("stat temporary mbox file: %v", err)
276 if _, err := mboxtmp.Seek(0, 0); err != nil {
277 return fmt.Errorf("seek to start of temporary mbox file")
279 w, err := archiver.Create(curMailbox+".mbox", fi.Size(), fi.ModTime())
281 return fmt.Errorf("add mbox to archive: %v", err)
283 if _, err := io.Copy(w, mboxtmp); err != nil {
285 log.Check(xerr, "closing mbox message file after error")
286 return fmt.Errorf("copying temp mbox file to archive: %v", err)
288 if err := w.Close(); err != nil {
289 return fmt.Errorf("closing message file: %v", err)
291 name := mboxtmp.Name()
292 err = mboxtmp.Close()
293 log.Check(err, "closing temporary mbox file")
294 err = os.Remove(name)
295 log.Check(err, "removing temporary mbox file", slog.String("path", name))
301 exportMessage := func(m Message) error {
302 mp := filepath.Join(accountDir, "msg", MessagePath(m.ID))
304 if m.Size == int64(len(m.MsgPrefix)) {
305 mr = io.NopCloser(bytes.NewReader(m.MsgPrefix))
307 mf, err := os.Open(mp)
309 errors += fmt.Sprintf("open message file for id %d, path %s: %v (message skipped)\n", m.ID, mp, err)
314 log.Check(err, "closing message file after export")
318 errors += fmt.Sprintf("stat message file for id %d, path %s: %v (message skipped)\n", m.ID, mp, err)
321 size := st.Size() + int64(len(m.MsgPrefix))
323 errors += fmt.Sprintf("message size mismatch for message id %d, database has %d, size is %d+%d=%d, using calculated size\n", m.ID, m.Size, len(m.MsgPrefix), st.Size(), size)
325 mr = FileMsgReader(m.MsgPrefix, mf)
331 p = filepath.Join(p, "cur")
333 p = filepath.Join(p, "new")
335 name := fmt.Sprintf("%d.%d.mox:2,", m.Received.Unix(), m.ID)
337 // Standard flags. May need to be sorted.
344 if m.Flags.Answered {
354 // Non-standard flag. We set them with a dovecot-keywords file.
355 if m.Flags.Forwarded {
356 name += maildirFlag("$Forwarded")
359 name += maildirFlag("$Junk")
362 name += maildirFlag("$NotJunk")
364 if m.Flags.Phishing {
365 name += maildirFlag("$Phishing")
368 name += maildirFlag("$MDNSent")
371 p = filepath.Join(p, name)
373 // We store messages with \r\n, maildir needs without. But we need to know the
374 // final size. So first convert, then create file with size, and write from buffer.
375 // todo: for large messages, we should go through a temporary file instead of memory.
377 r := bufio.NewReader(mr)
379 line, rerr := r.ReadBytes('\n')
380 if rerr != io.EOF && rerr != nil {
381 errors += fmt.Sprintf("reading from message for id %d: %v (message skipped)\n", m.ID, err)
385 if bytes.HasSuffix(line, []byte("\r\n")) {
386 line = line[:len(line)-1]
387 line[len(line)-1] = '\n'
389 if _, err = dst.Write(line); err != nil {
390 return fmt.Errorf("writing message: %v", err)
397 size := int64(dst.Len())
398 w, err := archiver.Create(p, size, m.Received)
400 return fmt.Errorf("adding message to archive: %v", err)
402 if _, err := io.Copy(w, &dst); err != nil {
404 log.Check(xerr, "closing message")
405 return fmt.Errorf("copying message to archive: %v", err)
411 if m.MailFrom != "" {
412 mailfrom = m.MailFrom
414 if _, err := fmt.Fprintf(mboxwriter, "From %s %s\n", mailfrom, m.Received.Format(time.ANSIC)); err != nil {
415 return fmt.Errorf("write message line to mbox temp file: %v", err)
418 // Write message flags in the three headers that mbox consumers may (or may not) understand.
420 if _, err := fmt.Fprintf(mboxwriter, "Status: R\n"); err != nil {
421 return fmt.Errorf("writing status header: %v", err)
438 if _, err := fmt.Fprintf(mboxwriter, "X-Status: %s\n", xstatus); err != nil {
439 return fmt.Errorf("writing x-status header: %v", err)
442 var xkeywords []string
444 xkeywords = append(xkeywords, "$Forwarded")
446 if m.Junk && !m.Notjunk {
447 xkeywords = append(xkeywords, "$Junk")
449 if m.Notjunk && !m.Junk {
450 xkeywords = append(xkeywords, "$NotJunk")
453 xkeywords = append(xkeywords, "$Phishing")
456 xkeywords = append(xkeywords, "$MDNSent")
458 if len(xkeywords) > 0 {
459 if _, err := fmt.Fprintf(mboxwriter, "X-Keywords: %s\n", strings.Join(xkeywords, ",")); err != nil {
460 return fmt.Errorf("writing x-keywords header: %v", err)
465 r := bufio.NewReader(mr)
467 line, rerr := r.ReadBytes('\n')
468 if rerr != io.EOF && rerr != nil {
469 return fmt.Errorf("reading message: %v", err)
472 if bytes.HasSuffix(line, []byte("\r\n")) {
473 line = line[:len(line)-1]
474 line[len(line)-1] = '\n'
476 if header && len(line) == 1 {
480 // Skip any previously stored flag-holding or now incorrect content-length headers.
481 // This assumes these headers are just a single line.
482 switch strings.ToLower(string(bytes.SplitN(line, []byte(":"), 2)[0])) {
483 case "status", "x-status", "x-keywords", "content-length":
487 if bytes.HasPrefix(bytes.TrimLeft(line, ">"), []byte("From ")) {
488 if _, err := fmt.Fprint(mboxwriter, ">"); err != nil {
489 return fmt.Errorf("writing escaping >: %v", err)
492 if _, err := mboxwriter.Write(line); err != nil {
493 return fmt.Errorf("writing line: %v", err)
500 if _, err := fmt.Fprint(mboxwriter, "\n"); err != nil {
501 return fmt.Errorf("writing end of message newline: %v", err)
506 for _, m := range msgs {
507 if m.MailboxID != curMailboxID {
508 if err := finishMailbox(); err != nil {
512 curMailbox = id2name[m.MailboxID]
513 curMailboxID = m.MailboxID
515 // Create the directories that show this is a maildir.
516 if _, err := archiver.Create(curMailbox+"/new/", 0, start); err != nil {
517 return fmt.Errorf("adding maildir new directory: %v", err)
519 if _, err := archiver.Create(curMailbox+"/cur/", 0, start); err != nil {
520 return fmt.Errorf("adding maildir cur directory: %v", err)
522 if _, err := archiver.Create(curMailbox+"/tmp/", 0, start); err != nil {
523 return fmt.Errorf("adding maildir tmp directory: %v", err)
527 mboxtmp, err = os.CreateTemp("", "mox-mail-export-mbox")
529 return fmt.Errorf("creating temp mbox file: %v", err)
531 mboxwriter = bufio.NewWriter(mboxtmp)
535 if err := exportMessage(m); err != nil {
539 if err := finishMailbox(); err != nil {
544 w, err := archiver.Create("errors.txt", int64(len(errors)), time.Now())
546 log.Errorx("adding errors.txt to archive", err)
549 if _, err := w.Write([]byte(errors)); err != nil {
550 log.Errorx("writing errors.txt to archive", err)
552 log.Check(xerr, "closing errors.txt after error")
555 if err := w.Close(); err != nil {