aboutsummaryrefslogtreecommitdiffstats
path: root/commands/compose/multipart.go
blob: 13fe8ca3f0340a1d69ad216b2d42f4fe60c3327e (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
package compose

import (
	"bytes"
	"fmt"

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

type Multipart struct {
	Remove bool   `opt:"-d"`
	Mime   string `opt:"mime" metavar:"<mime/type>" complete:"CompleteMime"`
}

func init() {
	commands.Register(Multipart{})
}

func (Multipart) Context() commands.CommandContext {
	return commands.COMPOSE
}

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

func (*Multipart) CompleteMime(arg string) []string {
	var completions []string
	for mime := range config.Converters {
		completions = append(completions, mime)
	}
	return commands.FilterList(completions, arg, nil)
}

func (m 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")
	}

	if m.Remove {
		return composer.RemovePart(m.Mime)
	} else {
		_, found := config.Converters[m.Mime]
		if !found {
			return fmt.Errorf("no command defined for MIME type: %s", m.Mime)
		}
		err := composer.AppendPart(
			m.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
}