| 1 |
package compose |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"git.sr.ht/~rjarry/aerc/app" |
| 7 |
"git.sr.ht/~rjarry/aerc/commands" |
| 8 |
"git.sr.ht/~rjarry/aerc/config" |
| 9 |
) |
| 10 |
|
| 11 |
type Multipart struct { |
| 12 |
Remove bool `opt:"-d" desc:"Remove the specified mime/type."` |
| 13 |
Mime string `opt:"mime" metavar:"<mime/type>" complete:"CompleteMime" desc:"MIME/type name."` |
| 14 |
} |
| 15 |
|
| 16 |
func init() { |
| 17 |
commands.Register(Multipart{}) |
| 18 |
} |
| 19 |
|
| 20 |
func (Multipart) Description() string { |
| 21 |
return "Convert the message to multipart with the given mime/type part." |
| 22 |
} |
| 23 |
|
| 24 |
func (Multipart) Context() commands.CommandContext { |
| 25 |
return commands.COMPOSE_EDIT | commands.COMPOSE_REVIEW |
| 26 |
} |
| 27 |
|
| 28 |
func (Multipart) Aliases() []string { |
| 29 |
return []string{"multipart"} |
| 30 |
} |
| 31 |
|
| 32 |
func (*Multipart) CompleteMime(arg string) []string { |
| 33 |
var completions []string |
| 34 |
for mime := range config.Converters() { |
| 35 |
completions = append(completions, mime) |
| 36 |
} |
| 37 |
return commands.FilterList(completions, arg, nil) |
| 38 |
} |
| 39 |
|
| 40 |
func (m Multipart) Execute(args []string) error { |
| 41 |
composer, ok := app.SelectedTabContent().(*app.Composer) |
| 42 |
if !ok { |
| 43 |
return fmt.Errorf(":multipart is only available on the compose::review screen") |
| 44 |
} |
| 45 |
|
| 46 |
if m.Remove { |
| 47 |
return composer.RemovePart(m.Mime) |
| 48 |
} else { |
| 49 |
_, found := config.Converters()[m.Mime] |
| 50 |
if !found { |
| 51 |
return fmt.Errorf("no command defined for MIME type: %s", m.Mime) |
| 52 |
} |
| 53 |
err := composer.AppendPart( |
| 54 |
m.Mime, |
| 55 |
map[string]string{"Charset": "UTF-8"}, |
| 56 |
// the actual content of the part will be rendered |
| 57 |
// every time the body of the email is updated |
| 58 |
nil, |
| 59 |
) |
| 60 |
if err != nil { |
| 61 |
return err |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return nil |
| 66 |
} |