aboutsummaryrefslogtreecommitdiffstats
path: root/commands/eml.go
blob: 45dd4f258cf39435509c23fe7f209b7cb350eff6 (plain) (blame)
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
package commands

import (
	"fmt"
	"io"
	"os"
	"strings"

	"git.sr.ht/~rjarry/aerc/lib"
	"git.sr.ht/~rjarry/aerc/widgets"
)

type Eml struct{}

func init() {
	register(Eml{})
}

func (Eml) Aliases() []string {
	return []string{"eml"}
}

func (Eml) Complete(aerc *widgets.Aerc, args []string) []string {
	return CompletePath(strings.Join(args, " "))
}

func (Eml) Execute(aerc *widgets.Aerc, args []string) error {
	acct := aerc.SelectedAccount()
	if acct == nil {
		return fmt.Errorf("no account selected")
	}

	showEml := func(r io.Reader) {
		data, err := io.ReadAll(r)
		if err != nil {
			aerc.PushError(err.Error())
			return
		}
		lib.NewEmlMessageView(data, aerc.Crypto, aerc.DecryptKeys,
			func(view lib.MessageView, err error) {
				if err != nil {
					aerc.PushError(err.Error())
					return
				}
				msgView := widgets.NewMessageViewer(acct,
					aerc.Config(), view)
				aerc.NewTab(msgView,
					view.MessageInfo().Envelope.Subject)
			})
	}

	if len(args) == 1 {
		switch tab := aerc.SelectedTabContent().(type) {
		case *widgets.MessageViewer:
			part := tab.SelectedMessagePart()
			tab.MessageView().FetchBodyPart(part.Index, showEml)
		default:
			return fmt.Errorf("unsupported operation")
		}
	} else {
		path := strings.Join(args[1:], " ")
		if _, err := os.Stat(path); err != nil {
			return err
		}
		f, err := os.Open(path)
		if err != nil {
			return err
		}
		defer f.Close()
		showEml(f)
	}
	return nil
}