mirror of
https://github.com/mjl-/mox.git
synced 2025-07-13 05:34:38 +03:00
webmail: add export functionality
per mailbox, or for all mailboxes, in maildir/mbox format, in tar/tgz/zip archive or without archive format for single mbox, single or recursive. the webaccount already had an option to export all mailboxes, it now looks similar to the webmail version.
This commit is contained in:
@ -3,10 +3,7 @@
|
||||
package webaccount
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"context"
|
||||
cryptorand "crypto/rand"
|
||||
"encoding/base64"
|
||||
@ -39,6 +36,7 @@ import (
|
||||
"github.com/mjl-/mox/webapi"
|
||||
"github.com/mjl-/mox/webauth"
|
||||
"github.com/mjl-/mox/webhook"
|
||||
"github.com/mjl-/mox/webops"
|
||||
)
|
||||
|
||||
var pkglog = mlog.New("webaccount", nil)
|
||||
@ -218,7 +216,7 @@ func handle(apiHandler http.Handler, isForwarded bool, w http.ResponseWriter, r
|
||||
// All other URLs, except the login endpoint require some authentication.
|
||||
if r.URL.Path != "/api/LoginPrep" && r.URL.Path != "/api/Login" {
|
||||
var ok bool
|
||||
isExport := strings.HasPrefix(r.URL.Path, "/export/")
|
||||
isExport := r.URL.Path == "/export"
|
||||
requireCSRF := isAPI || r.URL.Path == "/import" || isExport
|
||||
accName, sessionToken, loginAddress, ok = webauth.Check(ctx, log, webauth.Accounts, "webaccount", isForwarded, w, r, isAPI, requireCSRF, isExport)
|
||||
if !ok {
|
||||
@ -235,47 +233,8 @@ func handle(apiHandler http.Handler, isForwarded bool, w http.ResponseWriter, r
|
||||
}
|
||||
|
||||
switch r.URL.Path {
|
||||
case "/export/mail-export-maildir.tgz", "/export/mail-export-maildir.zip", "/export/mail-export-mbox.tgz", "/export/mail-export-mbox.zip":
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "405 - method not allowed - use post", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
maildir := strings.Contains(r.URL.Path, "maildir")
|
||||
tgz := strings.Contains(r.URL.Path, ".tgz")
|
||||
|
||||
acc, err := store.OpenAccount(log, accName)
|
||||
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")
|
||||
}()
|
||||
|
||||
var archiver store.Archiver
|
||||
if 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)}
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/zip")
|
||||
archiver = store.ZipArchiver{Writer: zip.NewWriter(w)}
|
||||
}
|
||||
defer func() {
|
||||
err := archiver.Close()
|
||||
log.Check(err, "exporting mail close")
|
||||
}()
|
||||
if err := store.ExportMessages(r.Context(), log, acc.DB, acc.Dir, archiver, maildir, ""); err != nil {
|
||||
log.Errorx("exporting mail", err)
|
||||
}
|
||||
case "/export":
|
||||
webops.Export(log, accName, w, r)
|
||||
|
||||
case "/import":
|
||||
if r.Method != "POST" {
|
||||
|
@ -1233,9 +1233,6 @@ const index = async () => {
|
||||
});
|
||||
});
|
||||
};
|
||||
const exportForm = (filename) => {
|
||||
return dom.form(attr.target('_blank'), attr.method('POST'), attr.action('export/' + filename), dom.input(attr.type('hidden'), attr.name('csrf'), attr.value(localStorageGet('webaccountcsrftoken') || '')), dom.submitbutton('Export'));
|
||||
};
|
||||
const authorizationPopup = (dest) => {
|
||||
let username;
|
||||
let password;
|
||||
@ -1474,7 +1471,7 @@ const index = async () => {
|
||||
}), dom.table(dom.thead(dom.tr(dom.th('Address', attr.title('Address that caused this entry to be added to the list. The title (shown on hover) displays an address with a fictional simplified localpart, with lower-cased, dots removed, only first part before "+" or "-" (typicaly catchall separators). When checking if an address is on the suppression list, it is checked against this address.')), dom.th('Manual', attr.title('Whether suppression was added manually, instead of automatically based on bounces.')), dom.th('Reason'), dom.th('Since'), dom.th('Action'))), dom.tbody((suppressions || []).length === 0 ? dom.tr(dom.td(attr.colspan('5'), '(None)')) : [], (suppressions || []).map(s => dom.tr(dom.td(s.OriginalAddress, attr.title(s.BaseAddress)), dom.td(s.Manual ? '✓' : ''), dom.td(s.Reason), dom.td(age(s.Created)), dom.td(dom.clickbutton('Remove', async function click(e) {
|
||||
await check(e.target, client.SuppressionRemove(s.OriginalAddress));
|
||||
window.location.reload(); // todo: reload less
|
||||
}))))), dom.tfoot(dom.tr(dom.td(suppressionAddress = dom.input(attr.type('required'), attr.form('suppressionAdd'))), dom.td(), dom.td(suppressionReason = dom.input(style({ width: '100%' }), attr.form('suppressionAdd'))), dom.td(), dom.td(dom.submitbutton('Add suppression', attr.form('suppressionAdd')))))), dom.br(), dom.h2('Export'), dom.p('Export all messages in all mailboxes. In maildir or mbox format, as .zip or .tgz file.'), dom.table(dom._class('slim'), dom.tr(dom.td('Maildirs in .tgz'), dom.td(exportForm('mail-export-maildir.tgz'))), dom.tr(dom.td('Maildirs in .zip'), dom.td(exportForm('mail-export-maildir.zip'))), dom.tr(dom.td('Mbox files in .tgz'), dom.td(exportForm('mail-export-mbox.tgz'))), dom.tr(dom.td('Mbox files in .zip'), dom.td(exportForm('mail-export-mbox.zip')))), dom.br(), dom.h2('Import'), dom.p('Import messages from a .zip or .tgz file with maildirs and/or mbox files.'), importForm = dom.form(async function submit(e) {
|
||||
}))))), dom.tfoot(dom.tr(dom.td(suppressionAddress = dom.input(attr.type('required'), attr.form('suppressionAdd'))), dom.td(), dom.td(suppressionReason = dom.input(style({ width: '100%' }), attr.form('suppressionAdd'))), dom.td(), dom.td(dom.submitbutton('Add suppression', attr.form('suppressionAdd')))))), dom.br(), dom.h2('Export'), dom.p('Export all messages in all mailboxes.'), dom.form(attr.target('_blank'), attr.method('POST'), attr.action('export'), dom.input(attr.type('hidden'), attr.name('csrf'), attr.value(localStorageGet('webaccountcsrftoken') || '')), dom.input(attr.type('hidden'), attr.name('mailbox'), attr.value('')), dom.input(attr.type('hidden'), attr.name('recursive'), attr.value('on')), dom.div(style({ display: 'flex', flexDirection: 'column', gap: '.5ex' }), dom.div(dom.label(dom.input(attr.type('radio'), attr.name('format'), attr.value('maildir'), attr.checked('')), ' Maildir'), ' ', dom.label(dom.input(attr.type('radio'), attr.name('format'), attr.value('mbox')), ' Mbox')), dom.div(dom.label(dom.input(attr.type('radio'), attr.name('archive'), attr.value('tar')), ' Tar'), ' ', dom.label(dom.input(attr.type('radio'), attr.name('archive'), attr.value('tgz'), attr.checked('')), ' Tgz'), ' ', dom.label(dom.input(attr.type('radio'), attr.name('archive'), attr.value('zip')), ' Zip'), ' '), dom.div(style({ marginTop: '1ex' }), dom.submitbutton('Export')))), dom.br(), dom.h2('Import'), dom.p('Import messages from a .zip or .tgz file with maildirs and/or mbox files.'), importForm = dom.form(async function submit(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
const request = async () => {
|
||||
|
@ -477,14 +477,6 @@ const index = async () => {
|
||||
})
|
||||
}
|
||||
|
||||
const exportForm = (filename: string) => {
|
||||
return dom.form(
|
||||
attr.target('_blank'), attr.method('POST'), attr.action('export/'+filename),
|
||||
dom.input(attr.type('hidden'), attr.name('csrf'), attr.value(localStorageGet('webaccountcsrftoken') || '')),
|
||||
dom.submitbutton('Export'),
|
||||
)
|
||||
}
|
||||
|
||||
const authorizationPopup = (dest: HTMLInputElement) => {
|
||||
let username: HTMLInputElement
|
||||
let password: HTMLInputElement
|
||||
@ -1148,23 +1140,24 @@ const index = async () => {
|
||||
dom.br(),
|
||||
|
||||
dom.h2('Export'),
|
||||
dom.p('Export all messages in all mailboxes. In maildir or mbox format, as .zip or .tgz file.'),
|
||||
dom.table(dom._class('slim'),
|
||||
dom.tr(
|
||||
dom.td('Maildirs in .tgz'),
|
||||
dom.td(exportForm('mail-export-maildir.tgz')),
|
||||
),
|
||||
dom.tr(
|
||||
dom.td('Maildirs in .zip'),
|
||||
dom.td(exportForm('mail-export-maildir.zip')),
|
||||
),
|
||||
dom.tr(
|
||||
dom.td('Mbox files in .tgz'),
|
||||
dom.td(exportForm('mail-export-mbox.tgz')),
|
||||
),
|
||||
dom.tr(
|
||||
dom.td('Mbox files in .zip'),
|
||||
dom.td(exportForm('mail-export-mbox.zip')),
|
||||
dom.p('Export all messages in all mailboxes.'),
|
||||
dom.form(
|
||||
attr.target('_blank'), attr.method('POST'), attr.action('export'),
|
||||
dom.input(attr.type('hidden'), attr.name('csrf'), attr.value(localStorageGet('webaccountcsrftoken') || '')),
|
||||
dom.input(attr.type('hidden'), attr.name('mailbox'), attr.value('')),
|
||||
dom.input(attr.type('hidden'), attr.name('recursive'), attr.value('on')),
|
||||
|
||||
dom.div(style({display: 'flex', flexDirection: 'column', gap: '.5ex'}),
|
||||
dom.div(
|
||||
dom.label(dom.input(attr.type('radio'), attr.name('format'), attr.value('maildir'), attr.checked('')), ' Maildir'), ' ',
|
||||
dom.label(dom.input(attr.type('radio'), attr.name('format'), attr.value('mbox')), ' Mbox'),
|
||||
),
|
||||
dom.div(
|
||||
dom.label(dom.input(attr.type('radio'), attr.name('archive'), attr.value('tar')), ' Tar'), ' ',
|
||||
dom.label(dom.input(attr.type('radio'), attr.name('archive'), attr.value('tgz'), attr.checked('')), ' Tgz'), ' ',
|
||||
dom.label(dom.input(attr.type('radio'), attr.name('archive'), attr.value('zip')), ' Zip'), ' ',
|
||||
),
|
||||
dom.div(style({marginTop: '1ex'}), dom.submitbutton('Export')),
|
||||
),
|
||||
),
|
||||
dom.br(),
|
||||
|
@ -214,12 +214,9 @@ func TestAccount(t *testing.T) {
|
||||
|
||||
testHTTP("POST", "/import", httpHeaders{}, http.StatusForbidden, nil, nil)
|
||||
testHTTP("POST", "/import", httpHeaders{hdrSessionBad}, http.StatusForbidden, nil, nil)
|
||||
testHTTP("GET", "/export/mail-export-maildir.tgz", httpHeaders{}, http.StatusForbidden, nil, nil)
|
||||
testHTTP("GET", "/export/mail-export-maildir.tgz", httpHeaders{hdrSessionBad}, http.StatusForbidden, nil, nil)
|
||||
testHTTP("GET", "/export/mail-export-maildir.tgz", httpHeaders{hdrSessionOK}, http.StatusForbidden, nil, nil)
|
||||
testHTTP("GET", "/export/mail-export-maildir.zip", httpHeaders{}, http.StatusForbidden, nil, nil)
|
||||
testHTTP("GET", "/export/mail-export-mbox.tgz", httpHeaders{}, http.StatusForbidden, nil, nil)
|
||||
testHTTP("GET", "/export/mail-export-mbox.zip", httpHeaders{}, http.StatusForbidden, nil, nil)
|
||||
testHTTP("GET", "/export", httpHeaders{}, http.StatusForbidden, nil, nil)
|
||||
testHTTP("GET", "/export", httpHeaders{hdrSessionBad}, http.StatusForbidden, nil, nil)
|
||||
testHTTP("GET", "/export", httpHeaders{hdrSessionOK}, http.StatusForbidden, nil, nil)
|
||||
|
||||
// SetPassword needs the token.
|
||||
sessionToken := store.SessionToken(strings.SplitN(sessionCookie.Value, " ", 2)[0])
|
||||
@ -336,11 +333,17 @@ func TestAccount(t *testing.T) {
|
||||
return nil
|
||||
})
|
||||
|
||||
testExport := func(httppath string, iszip bool, expectFiles int) {
|
||||
testExport := func(format, archive string, expectFiles int) {
|
||||
t.Helper()
|
||||
|
||||
fields := url.Values{"csrf": []string{string(csrfToken)}}
|
||||
r := httptest.NewRequest("POST", httppath, strings.NewReader(fields.Encode()))
|
||||
fields := url.Values{
|
||||
"csrf": []string{string(csrfToken)},
|
||||
"format": []string{format},
|
||||
"archive": []string{archive},
|
||||
"mailbox": []string{""},
|
||||
"recursive": []string{"on"},
|
||||
}
|
||||
r := httptest.NewRequest("POST", "/export", strings.NewReader(fields.Encode()))
|
||||
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
r.Header.Add("Cookie", cookieOK.String())
|
||||
w := httptest.NewRecorder()
|
||||
@ -349,7 +352,7 @@ func TestAccount(t *testing.T) {
|
||||
t.Fatalf("export, got status code %d, expected 200: %s", w.Code, w.Body.Bytes())
|
||||
}
|
||||
var count int
|
||||
if iszip {
|
||||
if archive == "zip" {
|
||||
buf := w.Body.Bytes()
|
||||
zr, err := zip.NewReader(bytes.NewReader(buf), int64(len(buf)))
|
||||
tcheck(t, err, "reading zip")
|
||||
@ -359,9 +362,13 @@ func TestAccount(t *testing.T) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
gzr, err := gzip.NewReader(w.Body)
|
||||
tcheck(t, err, "gzip reader")
|
||||
tr := tar.NewReader(gzr)
|
||||
var src io.Reader = w.Body
|
||||
if archive == "tgz" {
|
||||
gzr, err := gzip.NewReader(src)
|
||||
tcheck(t, err, "gzip reader")
|
||||
src = gzr
|
||||
}
|
||||
tr := tar.NewReader(src)
|
||||
for {
|
||||
h, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
@ -380,10 +387,10 @@ func TestAccount(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
testExport("/export/mail-export-maildir.tgz", false, 6) // 2 mailboxes, each with 2 messages and a dovecot-keyword file
|
||||
testExport("/export/mail-export-maildir.zip", true, 6)
|
||||
testExport("/export/mail-export-mbox.tgz", false, 2)
|
||||
testExport("/export/mail-export-mbox.zip", true, 2)
|
||||
testExport("maildir", "tgz", 6) // 2 mailboxes, each with 2 messages and a dovecot-keyword file
|
||||
testExport("maildir", "zip", 6)
|
||||
testExport("mbox", "tar", 2+6) // 2 imported plus 6 default mailboxes (Inbox, Draft, etc)
|
||||
testExport("mbox", "zip", 2+6)
|
||||
|
||||
sl := api.SuppressionList(ctx)
|
||||
tcompare(t, len(sl), 0)
|
||||
|
Reference in New Issue
Block a user