2019-05-26 23:37:39 +02:00
|
|
|
package msgview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
2019-06-25 09:23:52 +02:00
|
|
|
"fmt"
|
2019-05-26 23:37:39 +02:00
|
|
|
"io"
|
|
|
|
"mime/quotedprintable"
|
|
|
|
"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"
|
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register("save", Save)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Save(aerc *widgets.Aerc, args []string) error {
|
2019-06-11 20:26:13 +02:00
|
|
|
opts, optind, err := getopt.Getopts(args, "p")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-26 23:37:39 +02:00
|
|
|
}
|
2019-06-11 20:26:13 +02:00
|
|
|
var (
|
|
|
|
mkdirs bool
|
|
|
|
path string
|
|
|
|
)
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'p':
|
|
|
|
mkdirs = true
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 09:23:52 +02:00
|
|
|
if len(args) == optind+1 {
|
|
|
|
path = args[optind]
|
|
|
|
} else if defaultPath := aerc.Config().General.DefaultSavePath; defaultPath != "" {
|
|
|
|
path = defaultPath
|
|
|
|
} else {
|
2019-06-11 20:26:13 +02:00
|
|
|
return errors.New("Usage: :save [-p] <path>")
|
|
|
|
}
|
2019-05-26 23:37:39 +02:00
|
|
|
|
|
|
|
mv := aerc.SelectedTab().(*widgets.MessageViewer)
|
|
|
|
p := mv.CurrentPart()
|
|
|
|
|
|
|
|
p.Store.FetchBodyPart(p.Msg.Uid, p.Index, func(reader io.Reader) {
|
|
|
|
// email parts are encoded as 7bit (plaintext), quoted-printable, or base64
|
2019-06-15 12:28:03 +02:00
|
|
|
|
|
|
|
if strings.EqualFold(p.Part.Encoding, "base64") {
|
2019-05-26 23:37:39 +02:00
|
|
|
reader = base64.NewDecoder(base64.StdEncoding, reader)
|
2019-06-15 12:28:03 +02:00
|
|
|
} else if strings.EqualFold(p.Part.Encoding, "quoted-printable") {
|
2019-05-26 23:37:39 +02:00
|
|
|
reader = quotedprintable.NewReader(reader)
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:26:13 +02:00
|
|
|
var pathIsDir bool
|
|
|
|
if path[len(path)-1:] == "/" {
|
|
|
|
pathIsDir = true
|
|
|
|
}
|
|
|
|
// Note: path expansion has to happen after test for trailing /,
|
|
|
|
// since it is stripped when path is expanded
|
|
|
|
path, err := homedir.Expand(path)
|
2019-05-26 23:37:39 +02:00
|
|
|
if err != nil {
|
|
|
|
aerc.PushError(" " + err.Error())
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:26:13 +02:00
|
|
|
pathinfo, err := os.Stat(path)
|
|
|
|
if err == nil && pathinfo.IsDir() {
|
|
|
|
pathIsDir = true
|
|
|
|
} else if os.IsExist(err) && pathIsDir {
|
|
|
|
aerc.PushError("The given directory is an existing file")
|
|
|
|
}
|
2019-06-25 09:23:52 +02:00
|
|
|
var (
|
|
|
|
save_file string
|
|
|
|
save_dir string
|
|
|
|
)
|
2019-06-11 20:26:13 +02:00
|
|
|
if pathIsDir {
|
|
|
|
save_dir = path
|
|
|
|
if filename, ok := p.Part.DispositionParams["filename"]; ok {
|
|
|
|
save_file = filename
|
2019-06-25 09:23:52 +02:00
|
|
|
} else {
|
|
|
|
timestamp := time.Now().Format("2006-01-02-150405")
|
|
|
|
save_file = fmt.Sprintf("aerc_%v", timestamp)
|
2019-06-11 20:26:13 +02:00
|
|
|
}
|
2019-06-25 09:23:52 +02:00
|
|
|
} else {
|
|
|
|
save_file = filepath.Base(path)
|
|
|
|
save_dir = filepath.Dir(path)
|
2019-06-11 20:26:13 +02:00
|
|
|
}
|
|
|
|
if _, err := os.Stat(save_dir); os.IsNotExist(err) {
|
|
|
|
if mkdirs {
|
|
|
|
os.MkdirAll(save_dir, 0755)
|
|
|
|
} else {
|
|
|
|
aerc.PushError("Target directory does not exist, use " +
|
|
|
|
":save with the -p option to create it")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
target := filepath.Clean(filepath.Join(save_dir, save_file))
|
|
|
|
|
2019-05-26 23:37:39 +02:00
|
|
|
f, err := os.Create(target)
|
|
|
|
if err != nil {
|
|
|
|
aerc.PushError(" " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(f, reader)
|
|
|
|
if err != nil {
|
|
|
|
aerc.PushError(" " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
aerc.PushStatus("Saved to "+target, 10*time.Second)
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|