aboutsummaryrefslogtreecommitdiff
path: root/commands/compose/multipart.go
blob: 0ad1dc4d0f368c7e17f2cd2a400e1b126a1f05ad (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
package compose

import (
	"bytes"
	"fmt"

	"git.sr.ht/~rjarry/aerc/app"
	"git.sr.ht/~rjarry/aerc/commands"
	"git.sr.ht/~rjarry/aerc/config"
	"git.sr.ht/~sircmpwn/getopt"
)

type Multipart struct{}

func init() {
	register(Multipart{})
}

func (Multipart) Aliases() []string {
	return []string{"multipart"}
}

func (Multipart) Complete(args []string) []string {
	var completions []string
	completions = append(completions, "-d")
	for mime := range config.Converters {
		completions = append(completions, mime)
	}
	return commands.CompletionFromList(completions, args)
}

func (a Multipart) Execute(args []string) error {
	composer, ok := app.SelectedTabContent().(*app.Composer)
	if !ok {
		return fmt.Errorf(":multipart is only available on the compose::review screen")
	}

	opts, optind, err := getopt.Getopts(args, "d")
	if err != nil {
		return fmt.Errorf("Usage: :multipart [-d] <mime/type>")
	}
	var remove bool = false
	for _, opt := range opts {
		if opt.Option == 'd' {
			remove = true
		}
	}
	args = args[optind:]
	if len(args) != 1 {
		return fmt.Errorf("Usage: :multipart [-d] <mime/type>")
	}
	mime := args[0]

	if remove {
		return composer.RemovePart(mime)
	} else {
		_, found := config.Converters[mime]
		if !found {
			return fmt.Errorf("no command defined for MIME type: %s", mime)
		}
		err = composer.AppendPart(
			mime,
			map[string]string{"Charset": "UTF-8"},
			// the actual content of the part will be rendered
			// every time the body of the email is updated
			bytes.NewReader([]byte{}),
		)
		if err != nil {
			return err
		}
	}

	return nil
}