1package webops
2
3import (
4 "archive/tar"
5 "archive/zip"
6 "compress/gzip"
7 "fmt"
8 "mime"
9 "net/http"
10 "strconv"
11 "strings"
12 "time"
13
14 "github.com/mjl-/mox/mlog"
15 "github.com/mjl-/mox/store"
16)
17
18// Export is used by webmail and webaccount to export messages of one or
19// multiple mailboxes, in maildir or mbox format, in a tar/tgz/zip archive or
20// direct mbox.
21func Export(log mlog.Log, accName string, w http.ResponseWriter, r *http.Request) {
22 if r.Method != "POST" {
23 http.Error(w, "405 - method not allowed - use post", http.StatusMethodNotAllowed)
24 return
25 }
26
27 // We
28 mailbox := r.FormValue("mailbox") // Empty means all.
29 messageIDstr := r.FormValue("messageids")
30 var messageIDs []int64
31 if messageIDstr != "" {
32 for _, s := range strings.Split(messageIDstr, ",") {
33 id, err := strconv.ParseInt(s, 10, 64)
34 if err != nil {
35 http.Error(w, fmt.Sprintf("400 - bad request - bad message id %q: %v", s, err), http.StatusBadRequest)
36 return
37 }
38 messageIDs = append(messageIDs, id)
39 }
40 }
41 if mailbox != "" && len(messageIDs) > 0 {
42 http.Error(w, "400 - bad request - cannot specify both mailbox and message ids", http.StatusBadRequest)
43 return
44 }
45
46 format := r.FormValue("format")
47 archive := r.FormValue("archive")
48 recursive := r.FormValue("recursive") != ""
49 switch format {
50 case "maildir", "mbox":
51 default:
52 http.Error(w, "400 - bad request - unknown format", http.StatusBadRequest)
53 return
54 }
55 switch archive {
56 case "none", "tar", "tgz", "zip":
57 default:
58 http.Error(w, "400 - bad request - unknown archive", http.StatusBadRequest)
59 return
60 }
61 if archive == "none" && (format != "mbox" || recursive) {
62 http.Error(w, "400 - bad request - archive none can only be used with non-recursive mbox", http.StatusBadRequest)
63 return
64 }
65 if len(messageIDs) > 0 && recursive {
66 http.Error(w, "400 - bad request - cannot export message ids recursively", http.StatusBadRequest)
67 return
68 }
69
70 acc, err := store.OpenAccount(log, accName, false)
71 if err != nil {
72 log.Errorx("open account for export", err)
73 http.Error(w, "500 - internal server error", http.StatusInternalServerError)
74 return
75 }
76 defer func() {
77 err := acc.Close()
78 log.Check(err, "closing account")
79 }()
80
81 var name string
82 if mailbox != "" {
83 name = "-" + strings.ReplaceAll(mailbox, "/", "-")
84 } else if len(messageIDs) > 1 {
85 name = "-selection"
86 } else if len(messageIDs) == 0 {
87 name = "-all"
88 }
89 filename := fmt.Sprintf("mailexport%s-%s", name, time.Now().Format("20060102-150405"))
90 filename += "." + format
91 var archiver store.Archiver
92 if archive == "none" {
93 w.Header().Set("Content-Type", "application/mbox")
94 archiver = &store.MboxArchiver{Writer: w}
95 } else if archive == "tar" {
96 // Don't tempt browsers to "helpfully" decompress.
97 w.Header().Set("Content-Type", "application/x-tar")
98 archiver = store.TarArchiver{Writer: tar.NewWriter(w)}
99 filename += ".tar"
100 } else if archive == "tgz" {
101 // Don't tempt browsers to "helpfully" decompress.
102 w.Header().Set("Content-Type", "application/octet-stream")
103
104 gzw := gzip.NewWriter(w)
105 defer func() {
106 _ = gzw.Close()
107 }()
108 archiver = store.TarArchiver{Writer: tar.NewWriter(gzw)}
109 filename += ".tgz"
110 } else {
111 w.Header().Set("Content-Type", "application/zip")
112 archiver = store.ZipArchiver{Writer: zip.NewWriter(w)}
113 filename += ".zip"
114 }
115 defer func() {
116 err := archiver.Close()
117 log.Check(err, "exporting mail close")
118 }()
119 w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filename}))
120 if err := store.ExportMessages(r.Context(), log, acc.DB, acc.Dir, archiver, format == "maildir", mailbox, messageIDs, recursive); err != nil {
121 log.Errorx("exporting mail", err)
122 }
123}
124