aboutsummaryrefslogtreecommitdiff
path: root/commands/msgview/save.go
blob: 75ba56f33e7e09c6760acd191d143477e4fbc62c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package msgview

import (
	"encoding/base64"
	"errors"
	"fmt"
	"io"
	"mime/quotedprintable"
	"os"
	"path/filepath"
	"strings"
	"time"

	"git.sr.ht/~sircmpwn/aerc/widgets"
	"git.sr.ht/~sircmpwn/getopt"
	"github.com/mitchellh/go-homedir"
)

type Save struct{}

func init() {
	register(Save{})
}

func (_ Save) Aliases() []string {
	return []string{"save"}
}

func (_ Save) Complete(aerc *widgets.Aerc, args []string) []string {
	return nil
}

func (_ Save) Execute(aerc *widgets.Aerc, args []string) error {
	opts, optind, err := getopt.Getopts(args, "p")
	if err != nil {
		return err
	}

	var (
		mkdirs bool
		path   string
	)

	for _, opt := range opts {
		switch opt.Option {
		case 'p':
			mkdirs = true
		}
	}
	if len(args) == optind+1 {
		path = args[optind]
	} else if defaultPath := aerc.Config().General.DefaultSavePath; defaultPath != "" {
		path = defaultPath
	} else {
		return errors.New("Usage: :save [-p] <path>")
	}

	mv := aerc.SelectedTab().(*widgets.MessageViewer)
	p := mv.SelectedMessagePart()

	p.Store.FetchBodyPart(p.Msg.Uid, p.Index, func(reader io.Reader) {
		// email parts are encoded as 7bit (plaintext), quoted-printable, or base64

		if strings.EqualFold(p.Part.Encoding, "base64") {
			reader = base64.NewDecoder(base64.StdEncoding, reader)
		} else if strings.EqualFold(p.Part.Encoding, "quoted-printable") {
			reader = quotedprintable.NewReader(reader)
		}

		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)
		if err != nil {
			aerc.PushError(" " + err.Error())
		}

		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")
		}
		var (
			save_file string
			save_dir  string
		)
		if pathIsDir {
			save_dir = path
			if filename, ok := p.Part.DispositionParams["filename"]; ok {
				save_file = filename
			} else {
				timestamp := time.Now().Format("2006-01-02-150405")
				save_file = fmt.Sprintf("aerc_%v", timestamp)
			}
		} else {
			save_file = filepath.Base(path)
			save_dir = filepath.Dir(path)
		}
		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))

		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
}