2022-07-11 20:11:20 +02:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-07-19 22:31:51 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2022-07-11 20:11:20 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
|
|
|
mboxer "git.sr.ht/~rjarry/aerc/worker/mbox"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ExportMbox struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(ExportMbox{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ExportMbox) Aliases() []string {
|
|
|
|
return []string{"export-mbox"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ExportMbox) Complete(aerc *widgets.Aerc, args []string) []string {
|
|
|
|
if acct := aerc.SelectedAccount(); acct != nil {
|
|
|
|
if path := acct.SelectedDirectory(); path != "" {
|
|
|
|
if f := filepath.Base(path); f != "" {
|
|
|
|
return []string{f + ".mbox"}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ExportMbox) Execute(aerc *widgets.Aerc, args []string) error {
|
|
|
|
if len(args) != 2 {
|
|
|
|
return exportFolderUsage(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")
|
|
|
|
}
|
|
|
|
|
|
|
|
aerc.PushStatus("Exporting to "+filename, 10*time.Second)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
file, err := os.Create(filename)
|
|
|
|
if err != nil {
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Errorf("failed to create file: %v", err)
|
2022-07-11 20:11:20 +02:00
|
|
|
aerc.PushError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
var mu sync.Mutex
|
|
|
|
var ctr uint32
|
|
|
|
var retries int
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
uids := make([]uint32, len(store.Uids()))
|
|
|
|
copy(uids, store.Uids())
|
|
|
|
t := time.Now()
|
|
|
|
|
|
|
|
for len(uids) > 0 {
|
|
|
|
if retries > 0 {
|
|
|
|
if retries > 10 {
|
2022-07-19 22:31:51 +02:00
|
|
|
errorMsg := fmt.Sprintf("too many retries: %d; stopping export", retries)
|
|
|
|
logging.Errorf(errorMsg)
|
|
|
|
aerc.PushError(args[0] + " " + errorMsg)
|
2022-07-11 20:11:20 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
sleeping := time.Duration(retries * 1e9 * 2)
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Debugf("sleeping for %s before retrying; retries: %d", sleeping, retries)
|
2022-07-11 20:11:20 +02:00
|
|
|
time.Sleep(sleeping)
|
|
|
|
}
|
|
|
|
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Debugf("fetching %d for export", len(uids))
|
2022-07-11 20:11:20 +02:00
|
|
|
acct.Worker().PostAction(&types.FetchFullMessages{
|
|
|
|
Uids: uids,
|
|
|
|
}, func(msg types.WorkerMessage) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *types.Done:
|
|
|
|
done <- true
|
|
|
|
case *types.Error:
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Errorf("failed to fetch message: %v", msg.Error)
|
|
|
|
aerc.PushError(args[0] + " error encountered: " + msg.Error.Error())
|
2022-07-11 20:11:20 +02:00
|
|
|
done <- false
|
|
|
|
case *types.FullMessage:
|
|
|
|
mu.Lock()
|
2022-07-29 22:31:54 +02:00
|
|
|
err := mboxer.Write(file, msg.Content.Reader, "", t)
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to write mbox: %v", err)
|
|
|
|
}
|
2022-07-11 20:11:20 +02:00
|
|
|
for i, uid := range uids {
|
|
|
|
if uid == msg.Content.Uid {
|
|
|
|
uids = append(uids[:i], uids[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctr++
|
|
|
|
mu.Unlock()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if ok := <-done; ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
retries++
|
|
|
|
}
|
|
|
|
statusInfo := fmt.Sprintf("Exported %d of %d messages to %s.", ctr, len(store.Uids()), filename)
|
|
|
|
aerc.PushStatus(statusInfo, 10*time.Second)
|
2022-07-19 22:31:51 +02:00
|
|
|
logging.Infof(statusInfo)
|
2022-07-11 20:11:20 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func exportFolderUsage(cmd string) error {
|
|
|
|
return fmt.Errorf("Usage: %s <filename>", cmd)
|
|
|
|
}
|