mirror of
https://github.com/mjl-/mox.git
synced 2025-06-28 22:18:17 +03:00

to facilitate migrations from/to other mail setups. a domain can be added in "disabled" mode (or can be disabled/enabled later on). you can configure a disabled domain, but incoming/outgoing messages involving the domain are rejected with temporary error codes (as this may occur during a migration, remote servers will try again, hopefully to the correct machine or after this machine has been configured correctly). also, no acme tls certs will be requested for disabled domains (the autoconfig/mta-sts dns records may still point to the current/previous machine). accounts with addresses at disabled domains can still login, unless logins are disabled for their accounts. an account now has an option to disable logins. you can specify an error message to show. this will be shown in smtp, imap and the web interfaces. it could contain a message about migrations, and possibly a URL to a page with information about how to migrate. incoming/outgoing email involving accounts with login disabled are still accepted/delivered as normal (unless the domain involved in the messages is disabled too). account operations by the admin, such as importing/exporting messages still works. in the admin web interface, listings of domains/accounts show if they are disabled. domains & accounts can be enabled/disabled through the config file, cli commands and admin web interface. for issue #175 by RobSlgm
97 lines
2.8 KiB
Go
97 lines
2.8 KiB
Go
package webops
|
|
|
|
import (
|
|
"archive/tar"
|
|
"archive/zip"
|
|
"compress/gzip"
|
|
"fmt"
|
|
"mime"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/mjl-/mox/mlog"
|
|
"github.com/mjl-/mox/store"
|
|
)
|
|
|
|
// Export is used by webmail and webaccount to export messages of one or
|
|
// multiple mailboxes, in maildir or mbox format, in a tar/tgz/zip archive or
|
|
// direct mbox.
|
|
func Export(log mlog.Log, accName string, w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "POST" {
|
|
http.Error(w, "405 - method not allowed - use post", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
mailbox := r.FormValue("mailbox") // Empty means all.
|
|
format := r.FormValue("format")
|
|
archive := r.FormValue("archive")
|
|
recursive := r.FormValue("recursive") != ""
|
|
switch format {
|
|
case "maildir", "mbox":
|
|
default:
|
|
http.Error(w, "400 - bad request - unknown format", http.StatusBadRequest)
|
|
return
|
|
}
|
|
switch archive {
|
|
case "none", "tar", "tgz", "zip":
|
|
default:
|
|
http.Error(w, "400 - bad request - unknown archive", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if archive == "none" && (format != "mbox" || recursive) {
|
|
http.Error(w, "400 - bad request - archive none can only be used with non-recursive mbox", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
acc, err := store.OpenAccount(log, accName, false)
|
|
if err != nil {
|
|
log.Errorx("open account for export", err)
|
|
http.Error(w, "500 - internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer func() {
|
|
err := acc.Close()
|
|
log.Check(err, "closing account")
|
|
}()
|
|
|
|
name := strings.ReplaceAll(mailbox, "/", "-")
|
|
if name == "" {
|
|
name = "all"
|
|
}
|
|
filename := fmt.Sprintf("mailexport-%s-%s", name, time.Now().Format("20060102-150405"))
|
|
filename += "." + format
|
|
var archiver store.Archiver
|
|
if archive == "none" {
|
|
w.Header().Set("Content-Type", "application/mbox")
|
|
archiver = &store.MboxArchiver{Writer: w}
|
|
} else if archive == "tar" {
|
|
// Don't tempt browsers to "helpfully" decompress.
|
|
w.Header().Set("Content-Type", "application/x-tar")
|
|
archiver = store.TarArchiver{Writer: tar.NewWriter(w)}
|
|
filename += ".tar"
|
|
} else if archive == "tgz" {
|
|
// Don't tempt browsers to "helpfully" decompress.
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
gzw := gzip.NewWriter(w)
|
|
defer func() {
|
|
_ = gzw.Close()
|
|
}()
|
|
archiver = store.TarArchiver{Writer: tar.NewWriter(gzw)}
|
|
filename += ".tgz"
|
|
} else {
|
|
w.Header().Set("Content-Type", "application/zip")
|
|
archiver = store.ZipArchiver{Writer: zip.NewWriter(w)}
|
|
filename += ".zip"
|
|
}
|
|
defer func() {
|
|
err := archiver.Close()
|
|
log.Check(err, "exporting mail close")
|
|
}()
|
|
w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filename}))
|
|
if err := store.ExportMessages(r.Context(), log, acc.DB, acc.Dir, archiver, format == "maildir", mailbox, recursive); err != nil {
|
|
log.Errorx("exporting mail", err)
|
|
}
|
|
}
|