| 1 |
package account |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
|
| 10 |
"git.sr.ht/~rjarry/aerc/app" |
| 11 |
"git.sr.ht/~rjarry/aerc/commands" |
| 12 |
"git.sr.ht/~rjarry/aerc/config" |
| 13 |
"git.sr.ht/~rjarry/aerc/lib/xdg" |
| 14 |
) |
| 15 |
|
| 16 |
type Recover struct { |
| 17 |
Force bool `opt:"-f" desc:"Delete recovered file after opening the composer."` |
| 18 |
Edit bool `opt:"-e" desc:"Force [compose].edit-headers = true."` |
| 19 |
NoEdit bool `opt:"-E" desc:"Force [compose].edit-headers = false."` |
| 20 |
File string `opt:"file" complete:"CompleteFile" desc:"Recover file path."` |
| 21 |
} |
| 22 |
|
| 23 |
func init() { |
| 24 |
commands.Register(Recover{}) |
| 25 |
} |
| 26 |
|
| 27 |
func (Recover) Description() string { |
| 28 |
return "Resume composing a message that was not sent nor postponed." |
| 29 |
} |
| 30 |
|
| 31 |
func (Recover) Context() commands.CommandContext { |
| 32 |
return commands.MESSAGE_LIST |
| 33 |
} |
| 34 |
|
| 35 |
func (Recover) Aliases() []string { |
| 36 |
return []string{"recover"} |
| 37 |
} |
| 38 |
|
| 39 |
func (Recover) Options() string { |
| 40 |
return "feE" |
| 41 |
} |
| 42 |
|
| 43 |
func (*Recover) CompleteFile(arg string) []string { |
| 44 |
// file name of temp file is hard-coded in the NewComposer() function |
| 45 |
composePath := xdg.StatePath("aerc", "compose") |
| 46 |
files, err := filepath.Glob( |
| 47 |
filepath.Join(composePath, "aerc-compose-*.eml"), |
| 48 |
) |
| 49 |
if err != nil { |
| 50 |
return nil |
| 51 |
} |
| 52 |
return commands.FilterList(files, arg, nil) |
| 53 |
} |
| 54 |
|
| 55 |
func (r Recover) Execute(args []string) error { |
| 56 |
file, err := os.Open(r.File) |
| 57 |
if err != nil { |
| 58 |
return err |
| 59 |
} |
| 60 |
defer file.Close() |
| 61 |
data, err := io.ReadAll(file) |
| 62 |
if err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
|
| 66 |
acct := app.SelectedAccount() |
| 67 |
if acct == nil { |
| 68 |
return errors.New("No account selected") |
| 69 |
} |
| 70 |
|
| 71 |
editHeaders := (config.Compose().EditHeaders || r.Edit) && !r.NoEdit |
| 72 |
|
| 73 |
composer, err := app.NewComposer(acct, |
| 74 |
acct.AccountConfig(), acct.Worker(), editHeaders, |
| 75 |
"", nil, nil, bytes.NewReader(data)) |
| 76 |
if err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
composer.Tab = app.NewTab(composer, "Recovered") |
| 80 |
|
| 81 |
// remove file if force flag is set |
| 82 |
if r.Force { |
| 83 |
err = os.Remove(r.File) |
| 84 |
if err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return nil |
| 90 |
} |