account: import mbox file to a folder
Append all messages from an mbox file to the selected folder with the import-mbox command. User confirmation is required when the folder already contains messages. A failed append will be retried a few times. If a backend timeout occurs, the entire import is stopped to prevent a hang. Signed-off-by: Koni Marti <koni.marti@gmail.com> Acked-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
parent
845763cb1f
commit
e572087e58
|
@ -0,0 +1,153 @@
|
||||||
|
package account
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.sr.ht/~rjarry/aerc/commands"
|
||||||
|
"git.sr.ht/~rjarry/aerc/models"
|
||||||
|
"git.sr.ht/~rjarry/aerc/widgets"
|
||||||
|
mboxer "git.sr.ht/~rjarry/aerc/worker/mbox"
|
||||||
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ImportMbox struct{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
register(ImportMbox{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ImportMbox) Aliases() []string {
|
||||||
|
return []string{"import-mbox"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ImportMbox) Complete(aerc *widgets.Aerc, args []string) []string {
|
||||||
|
return commands.CompletePath(filepath.Join(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ImportMbox) Execute(aerc *widgets.Aerc, args []string) error {
|
||||||
|
if len(args) != 2 {
|
||||||
|
return importFolderUsage(args[0])
|
||||||
|
}
|
||||||
|
filename := args[1]
|
||||||
|
|
||||||
|
acct := aerc.SelectedAccount()
|
||||||
|
if acct == nil {
|
||||||
|
return errors.New("No account selected")
|
||||||
|
}
|
||||||
|
store := acct.Store()
|
||||||
|
if store == nil {
|
||||||
|
return errors.New("No message store selected")
|
||||||
|
}
|
||||||
|
|
||||||
|
folder := acct.SelectedDirectory()
|
||||||
|
if folder == "" {
|
||||||
|
return errors.New("No directory selected")
|
||||||
|
}
|
||||||
|
|
||||||
|
importFolder := func() {
|
||||||
|
statusInfo := fmt.Sprintln("Importing", filename, "to folder", folder)
|
||||||
|
aerc.PushStatus(statusInfo, 10*time.Second)
|
||||||
|
acct.Logger().Println(args[0], statusInfo)
|
||||||
|
f, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
aerc.PushError(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
messages, err := mboxer.Read(f)
|
||||||
|
if err != nil {
|
||||||
|
aerc.PushError(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
worker := acct.Worker()
|
||||||
|
|
||||||
|
var appended uint32
|
||||||
|
for i, m := range messages {
|
||||||
|
done := make(chan bool)
|
||||||
|
var retries int = 4
|
||||||
|
for retries > 0 {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
r, err := m.NewReader()
|
||||||
|
if err != nil {
|
||||||
|
acct.Logger().Println(fmt.Sprintf("%s: could not get reader for uid %d", args[0], m.UID()))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
nbytes, _ := io.Copy(&buf, r)
|
||||||
|
worker.PostAction(&types.AppendMessage{
|
||||||
|
Destination: folder,
|
||||||
|
Flags: []models.Flag{models.SeenFlag},
|
||||||
|
Date: time.Now(),
|
||||||
|
Reader: &buf,
|
||||||
|
Length: int(nbytes),
|
||||||
|
}, func(msg types.WorkerMessage) {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case *types.Unsupported:
|
||||||
|
errMsg := fmt.Sprintf("%s: AppendMessage is unsupported", args[0])
|
||||||
|
acct.Logger().Println(errMsg)
|
||||||
|
aerc.PushError(errMsg)
|
||||||
|
return
|
||||||
|
case *types.Error:
|
||||||
|
acct.Logger().Println(args[0], msg.Error.Error())
|
||||||
|
done <- false
|
||||||
|
case *types.Done:
|
||||||
|
atomic.AddUint32(&appended, 1)
|
||||||
|
done <- true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
select {
|
||||||
|
case ok := <-done:
|
||||||
|
if ok {
|
||||||
|
retries = 0
|
||||||
|
} else {
|
||||||
|
// error encountered; try to append again after a quick nap
|
||||||
|
retries -= 1
|
||||||
|
sleeping := time.Duration((5 - retries) * 1e9)
|
||||||
|
acct.Logger().Println(args[0], "sleeping for", sleeping, "before append message", i, "again")
|
||||||
|
time.Sleep(sleeping)
|
||||||
|
}
|
||||||
|
case <-time.After(30 * time.Second):
|
||||||
|
acct.Logger().Println(args[0], "timed-out; appended", appended, "of", len(messages))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
infoStr := fmt.Sprintf("%s: imported %d of %d sucessfully.", args[0], appended, len(messages))
|
||||||
|
acct.Logger().Println(infoStr)
|
||||||
|
aerc.SetStatus(infoStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(store.Uids()) > 0 {
|
||||||
|
confirm := widgets.NewSelectorDialog(
|
||||||
|
"Selected directory is not empty",
|
||||||
|
fmt.Sprintf("Import mbox file to %s anyways?", folder),
|
||||||
|
[]string{"No", "Yes"}, 0, aerc.SelectedAccountUiConfig(),
|
||||||
|
func(option string, err error) {
|
||||||
|
aerc.CloseDialog()
|
||||||
|
aerc.Invalidate()
|
||||||
|
switch option {
|
||||||
|
case "Yes":
|
||||||
|
go importFolder()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
},
|
||||||
|
)
|
||||||
|
aerc.AddDialog(confirm)
|
||||||
|
} else {
|
||||||
|
go importFolder()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func importFolderUsage(cmd string) error {
|
||||||
|
return fmt.Errorf("Usage: %s <filename>", cmd)
|
||||||
|
}
|
|
@ -314,6 +314,9 @@ message list, the message in the message viewer, etc).
|
||||||
*export-mbox* <file>
|
*export-mbox* <file>
|
||||||
Exports all messages in the current folder to an mbox file.
|
Exports all messages in the current folder to an mbox file.
|
||||||
|
|
||||||
|
*import-mbox* <file>
|
||||||
|
Imports all messages from an mbox file to the current folder.
|
||||||
|
|
||||||
*next-result*, *prev-result*
|
*next-result*, *prev-result*
|
||||||
Selects the next or previous search result.
|
Selects the next or previous search result.
|
||||||
|
|
||||||
|
|
|
@ -666,7 +666,9 @@ func (w *Worker) handleAppendMessage(msg *types.AppendMessage) error {
|
||||||
w.worker.Logger.Printf("could not write message to destination: %v", err)
|
w.worker.Logger.Printf("could not write message to destination: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
w.worker.PostMessage(&types.Done{
|
||||||
|
Message: types.RespondTo(msg),
|
||||||
|
}, nil)
|
||||||
w.worker.PostMessage(&types.DirectoryInfo{
|
w.worker.PostMessage(&types.DirectoryInfo{
|
||||||
Info: w.getDirectoryInfo(msg.Destination),
|
Info: w.getDirectoryInfo(msg.Destination),
|
||||||
}, nil)
|
}, nil)
|
||||||
|
|
Loading…
Reference in New Issue