2020-04-26 11:50:25 +02:00
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
2022-04-05 23:59:33 +02:00
|
|
|
"errors"
|
2020-04-26 11:50:25 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2020-04-26 11:50:25 +02:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Header struct{}
|
|
|
|
|
2022-07-31 22:16:40 +02:00
|
|
|
var headers = []string{
|
|
|
|
"From",
|
|
|
|
"To",
|
|
|
|
"Cc",
|
|
|
|
"Bcc",
|
|
|
|
"Subject",
|
|
|
|
"Comments",
|
|
|
|
"Keywords",
|
|
|
|
}
|
2020-04-26 11:50:25 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(Header{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Header) Aliases() []string {
|
|
|
|
return []string{"header"}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Header) Complete(aerc *widgets.Aerc, args []string) []string {
|
2022-03-06 03:58:07 +01:00
|
|
|
return commands.CompletionFromList(aerc, headers, args)
|
2020-04-26 11:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (Header) Execute(aerc *widgets.Aerc, args []string) error {
|
|
|
|
if len(args) < 2 {
|
|
|
|
return fmt.Errorf("Usage: %s [-f] field [value]", args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, optind, err := getopt.Getopts(args, "f")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-05 23:59:33 +02:00
|
|
|
|
|
|
|
if len(args) < optind+1 {
|
|
|
|
return errors.New("command parsing failed")
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:16:40 +02:00
|
|
|
var force bool = false
|
2020-04-26 11:50:25 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'f':
|
|
|
|
force = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 12:54:55 +02:00
|
|
|
composer, _ := aerc.SelectedTabContent().(*widgets.Composer)
|
2020-04-26 11:50:25 +02:00
|
|
|
|
2022-04-05 23:49:13 +02:00
|
|
|
args[optind] = strings.TrimRight(args[optind], ":")
|
|
|
|
|
2020-11-08 13:39:25 +01:00
|
|
|
value := strings.Join(args[optind+1:], " ")
|
|
|
|
|
2020-04-26 11:50:25 +02:00
|
|
|
if !force {
|
2020-11-10 20:27:30 +01:00
|
|
|
headers, err := composer.PrepareHeader()
|
2020-04-26 11:50:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-08 13:39:25 +01:00
|
|
|
if headers.Has(args[optind]) && value != "" {
|
2020-04-26 11:50:25 +02:00
|
|
|
return fmt.Errorf("Header %s already exists", args[optind])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 13:39:25 +01:00
|
|
|
composer.AddEditor(args[optind], value, false)
|
2020-04-26 11:50:25 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|