Enable the :import-mbox command to also import data from an URL.
Changelog-added: Enable `:import-mbox` to import data from an URL.
Suggested-by: Matěj Cepl <mcepl@cepl.eu>
Signed-off-by: Koni Marti <koni.marti@gmail.com>
---
commands/account/import-mbox.go | 53 +++++++++++++++++++++++----------
doc/aerc.1.scd | 12 ++++++--
2 files changed, 47 insertions(+), 18 deletions(-)
diff --git a/commands/account/import-mbox.go b/commands/account/import-mbox.go
index 85101bc2..208dec44 100644
--- a/commands/account/import-mbox.go
+++ b/commands/account/import-mbox.go
@@ -5,7 +5,9 @@ import (
"errors"
"fmt"
"io"
+ "net/http"
"os"
+ "regexp"
"sync/atomic"
"time"
@@ -19,7 +21,7 @@ import (
)
type ImportMbox struct {
- Filename string `opt:"filename" complete:"CompleteFilename" desc:"Input file path."`
+ Path string `opt:"path" complete:"CompleteFilename" desc:"Input file path or URL."`
}
func init() {
@@ -57,21 +59,11 @@ func (i ImportMbox) Execute(args []string) error {
return errors.New("No directory selected")
}
- i.Filename = xdg.ExpandHome(i.Filename)
-
- importFolder := func() {
+ importFolder := func(r io.ReadCloser) {
defer log.PanicHandler()
- statusInfo := fmt.Sprintln("Importing", i.Filename, "to folder", folder)
- app.PushStatus(statusInfo, 10*time.Second)
- log.Debugf(statusInfo)
- f, err := os.Open(i.Filename)
- if err != nil {
- app.PushError(err.Error())
- return
- }
- defer f.Close()
+ defer r.Close()
- messages, err := mboxer.Read(f)
+ messages, err := mboxer.Read(r)
if err != nil {
app.PushError(err.Error())
return
@@ -135,6 +127,33 @@ func (i ImportMbox) Execute(args []string) error {
app.PushSuccess(infoStr)
}
+ var buf []byte
+
+ path := i.Path
+ if ok, err := regexp.MatchString("^(http[s]\\:|www\\.)", path); ok && err == nil {
+ resp, err := http.Get(path)
+ if err != nil {
+ return err
+ }
+ buf, err = io.ReadAll(resp.Body)
+ _ = resp.Body.Close()
+ if err != nil {
+ return err
+ }
+ } else {
+ path = xdg.ExpandHome(path)
+ buf, err = os.ReadFile(path)
+ if err != nil {
+ return err
+ }
+ }
+
+ r := io.NopCloser(bytes.NewReader(buf))
+
+ statusInfo := fmt.Sprintln("Importing", path, "to folder", folder)
+ app.PushStatus(statusInfo, 10*time.Second)
+ log.Debugf(statusInfo)
+
if len(store.Uids()) > 0 {
confirm := app.NewSelectorDialog(
"Selected directory is not empty",
@@ -143,13 +162,15 @@ func (i ImportMbox) Execute(args []string) error {
func(option string, err error) {
app.CloseDialog()
if option == "Yes" {
- go importFolder()
+ go importFolder(r)
+ } else {
+ _ = r.Close()
}
},
)
app.AddDialog(confirm)
} else {
- go importFolder()
+ go importFolder(r)
}
return nil
diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd
index 575e2658..1facb0ea 100644
--- a/doc/aerc.1.scd
+++ b/doc/aerc.1.scd
@@ -656,8 +656,16 @@ message list, the message in the message viewer, etc).
messages in the folder, only the marked ones are exported. Otherwise the
whole folder is exported.
-*:import-mbox* _<file>_
- Imports all messages from an mbox file to the current folder.
+*:import-mbox* _<path>_
+ Imports all messages from an mbox file to the current folder. _<path>_
+ can either be a path to a file or an URL.
+
+ Examples:
+
+ ```
+ :import-mbox ~/messages.mbox
+ :import-mbox https://lists.sr.ht/~rjarry/aerc-devel/patches/55634/mbox
+ ```
*:next-result*++
*:prev-result*
--
2.47.0
Unzip data when importing gzip compressed mbox files.
Suggested-by: diederich
Signed-off-by: Koni Marti <koni.marti@gmail.com>
---
commands/account/import-mbox.go | 16 ++++++++++++++--
doc/aerc.1.scd | 5 +++--
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/commands/account/import-mbox.go b/commands/account/import-mbox.go
index 208dec44..f2d6c3c5 100644
--- a/commands/account/import-mbox.go
+++ b/commands/account/import-mbox.go
@@ -2,6 +2,7 @@ package account
import (
"bytes"
+ "compress/gzip"
"errors"
"fmt"
"io"
@@ -29,7 +30,7 @@ func init() {
}
func (ImportMbox) Description() string {
- return "Import all messages from an mbox file to the current folder."
+ return "Import all messages from an (gzipped) mbox file to the current folder."
}
func (ImportMbox) Context() commands.CommandContext {
@@ -148,7 +149,18 @@ func (i ImportMbox) Execute(args []string) error {
}
}
- r := io.NopCloser(bytes.NewReader(buf))
+ var r io.ReadCloser
+
+ // detect gzip format compressed files as specified in RFC 1952
+ if len(buf) >= 2 && buf[0] == 0x1f && buf[1] == 0x8b {
+ var err error
+ r, err = gzip.NewReader(bytes.NewReader(buf))
+ if err != nil {
+ return err
+ }
+ } else {
+ r = io.NopCloser(bytes.NewReader(buf))
+ }
statusInfo := fmt.Sprintln("Importing", path, "to folder", folder)
app.PushStatus(statusInfo, 10*time.Second)
diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd
index 1facb0ea..6de06175 100644
--- a/doc/aerc.1.scd
+++ b/doc/aerc.1.scd
@@ -657,14 +657,15 @@ message list, the message in the message viewer, etc).
whole folder is exported.
*:import-mbox* _<path>_
- Imports all messages from an mbox file to the current folder. _<path>_
- can either be a path to a file or an URL.
+ Imports all messages from an (gzipped) mbox file to the current folder.
+ _<path>_ can either be a path to a file or an URL.
Examples:
```
:import-mbox ~/messages.mbox
:import-mbox https://lists.sr.ht/~rjarry/aerc-devel/patches/55634/mbox
+ :import-mbox https://lore.kernel.org/all/20190807155524.5112-1-steve.capper@arm.com/t.mbox.gz
```
*:next-result*++
--
2.47.0