2019-05-26 23:37:39 +02:00
|
|
|
package msgview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-06-25 09:23:52 +02:00
|
|
|
"fmt"
|
2019-05-26 23:37:39 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
2019-06-11 20:26:13 +02:00
|
|
|
"path/filepath"
|
2019-06-15 12:28:03 +02:00
|
|
|
"strings"
|
2019-05-26 23:37:39 +02:00
|
|
|
"time"
|
|
|
|
|
2019-06-11 20:26:13 +02:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
2019-05-26 23:37:39 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-09-20 18:16:29 +02:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
2022-03-22 09:52:27 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2019-05-26 23:37:39 +02:00
|
|
|
)
|
|
|
|
|
2019-06-27 19:33:11 +02:00
|
|
|
type Save struct{}
|
|
|
|
|
2019-05-26 23:37:39 +02:00
|
|
|
func init() {
|
2019-06-27 19:33:11 +02:00
|
|
|
register(Save{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Save) Aliases() []string {
|
2019-06-27 19:33:11 +02:00
|
|
|
return []string{"save"}
|
2019-05-26 23:37:39 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Save) Complete(aerc *widgets.Aerc, args []string) []string {
|
2022-03-25 09:31:45 +01:00
|
|
|
_, optind, _ := getopt.Getopts(args, "fpa")
|
|
|
|
if optind < len(args) {
|
|
|
|
args = args[optind:]
|
|
|
|
}
|
2019-09-20 18:16:29 +02:00
|
|
|
path := strings.Join(args, " ")
|
2022-03-25 09:31:45 +01:00
|
|
|
defaultPath := aerc.Config().General.DefaultSavePath
|
|
|
|
if defaultPath != "" && !isAbsPath(path) {
|
|
|
|
path = filepath.Join(defaultPath, path)
|
|
|
|
}
|
|
|
|
path, _ = homedir.Expand(path)
|
2019-09-20 18:16:29 +02:00
|
|
|
return commands.CompletePath(path)
|
2019-06-27 19:33:11 +02:00
|
|
|
}
|
|
|
|
|
2022-03-24 09:26:06 +01:00
|
|
|
type saveParams struct {
|
|
|
|
force bool
|
|
|
|
createDirs bool
|
|
|
|
trailingSlash bool
|
|
|
|
attachments bool
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Save) Execute(aerc *widgets.Aerc, args []string) error {
|
2022-03-24 09:26:06 +01:00
|
|
|
opts, optind, err := getopt.Getopts(args, "fpa")
|
2019-06-11 20:26:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-26 23:37:39 +02:00
|
|
|
}
|
2019-06-27 19:33:11 +02:00
|
|
|
|
2022-03-24 09:26:06 +01:00
|
|
|
var params saveParams
|
2019-06-27 19:33:11 +02:00
|
|
|
|
2019-06-11 20:26:13 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
2020-01-19 20:22:56 +01:00
|
|
|
case 'f':
|
2022-03-24 09:26:06 +01:00
|
|
|
params.force = true
|
2019-06-11 20:26:13 +02:00
|
|
|
case 'p':
|
2022-03-24 09:26:06 +01:00
|
|
|
params.createDirs = true
|
|
|
|
case 'a':
|
|
|
|
params.attachments = true
|
2019-06-11 20:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-19 20:22:56 +01:00
|
|
|
|
|
|
|
defaultPath := aerc.Config().General.DefaultSavePath
|
|
|
|
// we either need a path or a defaultPath
|
|
|
|
if defaultPath == "" && len(args) == optind {
|
2022-03-24 09:26:06 +01:00
|
|
|
return errors.New("Usage: :save [-fpa] <path>")
|
2019-06-11 20:26:13 +02:00
|
|
|
}
|
2019-05-26 23:37:39 +02:00
|
|
|
|
2020-01-19 20:22:56 +01:00
|
|
|
// as a convenience we join with spaces, so that the user doesn't need to
|
|
|
|
// quote filenames containing spaces
|
|
|
|
path := strings.Join(args[optind:], " ")
|
|
|
|
|
|
|
|
// needs to be determined prior to calling filepath.Clean / filepath.Join
|
|
|
|
// it gets stripped by Clean.
|
|
|
|
// we auto generate a name if a directory was given
|
|
|
|
if len(path) > 0 {
|
2022-03-24 09:26:06 +01:00
|
|
|
params.trailingSlash = path[len(path)-1] == '/'
|
2020-01-19 20:22:56 +01:00
|
|
|
} else if len(defaultPath) > 0 && len(path) == 0 {
|
|
|
|
// empty path, so we might have a default that ends in a trailingSlash
|
2022-03-24 09:26:06 +01:00
|
|
|
params.trailingSlash = defaultPath[len(defaultPath)-1] == '/'
|
2020-01-19 20:22:56 +01:00
|
|
|
}
|
2019-05-26 23:37:39 +02:00
|
|
|
|
2020-01-19 20:22:56 +01:00
|
|
|
// Absolute paths are taken as is so that the user can override the default
|
|
|
|
// if they want to
|
|
|
|
if !isAbsPath(path) {
|
|
|
|
path = filepath.Join(defaultPath, path)
|
|
|
|
}
|
2019-06-15 12:28:03 +02:00
|
|
|
|
2020-01-19 20:22:56 +01:00
|
|
|
path, err = homedir.Expand(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-26 23:37:39 +02:00
|
|
|
|
2022-07-18 12:54:55 +02:00
|
|
|
mv, ok := aerc.SelectedTabContent().(*widgets.MessageViewer)
|
2020-01-19 20:22:56 +01:00
|
|
|
if !ok {
|
2022-07-18 12:54:55 +02:00
|
|
|
return fmt.Errorf("SelectedTabContent is not a MessageViewer")
|
2020-01-19 20:22:56 +01:00
|
|
|
}
|
2022-03-24 09:26:06 +01:00
|
|
|
|
|
|
|
if params.attachments {
|
|
|
|
parts := mv.AttachmentParts()
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return fmt.Errorf("This message has no attachments")
|
|
|
|
}
|
|
|
|
params.trailingSlash = true
|
|
|
|
for _, pi := range parts {
|
2022-06-23 18:26:19 +02:00
|
|
|
if err := savePart(pi, path, mv, aerc, ¶ms); err != nil {
|
2022-03-24 09:26:06 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-19 20:22:56 +01:00
|
|
|
pi := mv.SelectedMessagePart()
|
2022-06-23 18:26:19 +02:00
|
|
|
return savePart(pi, path, mv, aerc, ¶ms)
|
2022-03-24 09:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func savePart(
|
|
|
|
pi *widgets.PartInfo,
|
|
|
|
path string,
|
2022-06-23 18:26:19 +02:00
|
|
|
mv *widgets.MessageViewer,
|
2022-03-24 09:26:06 +01:00
|
|
|
aerc *widgets.Aerc,
|
|
|
|
params *saveParams,
|
|
|
|
) error {
|
|
|
|
if params.trailingSlash || isDirExists(path) {
|
2020-01-19 20:22:56 +01:00
|
|
|
filename := generateFilename(pi.Part)
|
|
|
|
path = filepath.Join(path, filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := filepath.Dir(path)
|
2022-03-24 09:26:06 +01:00
|
|
|
if params.createDirs && dir != "" {
|
2022-07-31 22:16:40 +02:00
|
|
|
err := os.MkdirAll(dir, 0o755)
|
2019-05-26 23:37:39 +02:00
|
|
|
if err != nil {
|
2020-01-19 20:22:56 +01:00
|
|
|
return err
|
2019-05-26 23:37:39 +02:00
|
|
|
}
|
2020-01-19 20:22:56 +01:00
|
|
|
}
|
2019-05-26 23:37:39 +02:00
|
|
|
|
2022-03-24 09:26:06 +01:00
|
|
|
if pathExists(path) && !params.force {
|
2020-01-19 20:22:56 +01:00
|
|
|
return fmt.Errorf("%q already exists and -f not given", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan error, 1)
|
2022-06-23 18:26:19 +02:00
|
|
|
mv.MessageView().FetchBodyPart(pi.Index, func(reader io.Reader) {
|
2020-05-17 11:44:38 +02:00
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = io.Copy(f, reader)
|
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- nil
|
|
|
|
})
|
2019-06-11 20:26:13 +02:00
|
|
|
|
2020-01-19 20:22:56 +01:00
|
|
|
// we need to wait for the callback prior to displaying a result
|
|
|
|
go func() {
|
2022-03-22 09:52:27 +01:00
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2020-01-19 20:22:56 +01:00
|
|
|
err := <-ch
|
2019-05-26 23:37:39 +02:00
|
|
|
if err != nil {
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushError(fmt.Sprintf("Save failed: %v", err))
|
2019-05-26 23:37:39 +02:00
|
|
|
return
|
|
|
|
}
|
2020-05-28 16:32:32 +02:00
|
|
|
aerc.PushStatus("Saved to "+path, 10*time.Second)
|
2020-01-19 20:22:56 +01:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-26 23:37:39 +02:00
|
|
|
|
2022-07-31 22:16:40 +02:00
|
|
|
// isDir returns true if path is a directory and exists
|
2020-01-19 20:22:56 +01:00
|
|
|
func isDirExists(path string) bool {
|
|
|
|
pathinfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return false // we don't really care
|
|
|
|
}
|
|
|
|
if pathinfo.IsDir() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-05-26 23:37:39 +02:00
|
|
|
|
2022-07-31 22:16:40 +02:00
|
|
|
// pathExists returns true if path exists
|
2020-01-19 20:22:56 +01:00
|
|
|
func pathExists(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
2022-03-09 22:48:00 +01:00
|
|
|
|
|
|
|
return err == nil
|
2020-01-19 20:22:56 +01:00
|
|
|
}
|
2019-05-26 23:37:39 +02:00
|
|
|
|
2022-07-31 22:16:40 +02:00
|
|
|
// isAbsPath returns true if path given is anchored to / or . or ~
|
2020-01-19 20:22:56 +01:00
|
|
|
func isAbsPath(path string) bool {
|
|
|
|
if len(path) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch path[0] {
|
|
|
|
case '/':
|
|
|
|
return true
|
|
|
|
case '.':
|
|
|
|
return true
|
|
|
|
case '~':
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateFilename tries to get the filename from the given part.
|
|
|
|
// if that fails it will fallback to a generated one based on the date
|
|
|
|
func generateFilename(part *models.BodyStructure) string {
|
|
|
|
var filename string
|
|
|
|
if fn, ok := part.DispositionParams["filename"]; ok {
|
|
|
|
filename = fn
|
|
|
|
} else if fn, ok := part.Params["name"]; ok {
|
|
|
|
filename = fn
|
2021-01-12 21:58:54 +01:00
|
|
|
}
|
|
|
|
// Some MUAs send attachments with names like /some/stupid/idea/happy.jpeg
|
|
|
|
// Assuming non hostile intent it does make sense to use just the last
|
|
|
|
// portion of the pathname as the filename for saving it.
|
|
|
|
filename = filename[strings.LastIndex(filename, "/")+1:]
|
|
|
|
switch filename {
|
|
|
|
case "", ".", "..":
|
2020-01-19 20:22:56 +01:00
|
|
|
timestamp := time.Now().Format("2006-01-02-150405")
|
|
|
|
filename = fmt.Sprintf("aerc_%v", timestamp)
|
2021-01-12 21:58:54 +01:00
|
|
|
default:
|
|
|
|
// already have a valid name
|
2020-01-19 20:22:56 +01:00
|
|
|
}
|
|
|
|
return filename
|
2019-05-26 23:37:39 +02:00
|
|
|
}
|