2022-01-28 22:50:02 +01:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2022-08-17 16:19:45 +02:00
|
|
|
"io"
|
2022-01-28 22:50:02 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-03-06 03:58:07 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
2022-03-22 09:52:27 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2022-01-28 22:50:02 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Recover struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(Recover{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Recover) Aliases() []string {
|
|
|
|
return []string{"recover"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Recover) Complete(aerc *widgets.Aerc, args []string) []string {
|
|
|
|
// file name of temp file is hard-coded in the NewComposer() function
|
|
|
|
files, err := filepath.Glob(
|
|
|
|
filepath.Join(os.TempDir(), "aerc-compose-*.eml"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-03-06 03:58:07 +01:00
|
|
|
return make([]string, 0)
|
|
|
|
}
|
|
|
|
// if nothing is entered yet, return all files
|
|
|
|
if len(args) == 0 {
|
|
|
|
return files
|
2022-01-28 22:50:02 +01:00
|
|
|
}
|
2022-07-31 14:32:48 +02:00
|
|
|
switch args[0] {
|
|
|
|
case "-":
|
2022-03-06 03:58:07 +01:00
|
|
|
return []string{"-f"}
|
2022-07-31 14:32:48 +02:00
|
|
|
case "-f":
|
2022-03-06 03:58:07 +01:00
|
|
|
if len(args) == 1 {
|
|
|
|
for i, file := range files {
|
|
|
|
files[i] = args[0] + " " + file
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
} else {
|
|
|
|
// only accepts one file to recover
|
2022-03-15 02:55:51 +01:00
|
|
|
return commands.FilterList(files, args[1], args[0]+" ",
|
|
|
|
aerc.SelectedAccountUiConfig().FuzzyComplete)
|
2022-01-28 22:50:02 +01:00
|
|
|
}
|
2022-07-31 14:32:48 +02:00
|
|
|
default:
|
2022-03-06 03:58:07 +01:00
|
|
|
// only accepts one file to recover
|
2022-03-15 02:55:51 +01:00
|
|
|
return commands.FilterList(files, args[0], "", aerc.SelectedAccountUiConfig().FuzzyComplete)
|
2022-01-28 22:50:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Recover) Execute(aerc *widgets.Aerc, args []string) error {
|
2022-03-06 03:58:07 +01:00
|
|
|
// Complete() expects to be passed only the arguments, not including the command name
|
|
|
|
if len(Recover{}.Complete(aerc, args[1:])) == 0 {
|
2022-01-28 22:50:02 +01:00
|
|
|
return errors.New("No messages to recover.")
|
|
|
|
}
|
|
|
|
|
|
|
|
force := false
|
|
|
|
|
|
|
|
opts, optind, err := getopt.Getopts(args, "f")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
2022-07-31 14:32:48 +02:00
|
|
|
if opt.Option == 'f' {
|
2022-01-28 22:50:02 +01:00
|
|
|
force = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) <= optind {
|
|
|
|
return errors.New("Usage: recover [-f] <file>")
|
|
|
|
}
|
|
|
|
|
|
|
|
acct := aerc.SelectedAccount()
|
|
|
|
if acct == nil {
|
|
|
|
return errors.New("No account selected")
|
|
|
|
}
|
|
|
|
|
|
|
|
readData := func() ([]byte, error) {
|
|
|
|
recoverFile, err := os.Open(args[optind])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer recoverFile.Close()
|
2022-08-17 16:19:45 +02:00
|
|
|
data, err := io.ReadAll(recoverFile)
|
2022-01-28 22:50:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
data, err := readData()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
composer, err := widgets.NewComposer(aerc, acct,
|
|
|
|
aerc.Config(), acct.AccountConfig(), acct.Worker(),
|
|
|
|
"", nil, models.OriginalMail{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tab := aerc.NewTab(composer, "Recovered")
|
|
|
|
composer.OnHeaderChange("Subject", func(subject string) {
|
|
|
|
tab.Name = subject
|
|
|
|
tab.Content.Invalidate()
|
|
|
|
})
|
2022-03-22 09:52:27 +01:00
|
|
|
go func() {
|
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
|
|
|
composer.AppendContents(bytes.NewReader(data))
|
|
|
|
}()
|
2022-01-28 22:50:02 +01:00
|
|
|
|
|
|
|
// remove file if force flag is set
|
|
|
|
if force {
|
|
|
|
err = os.Remove(args[optind])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|