2019-05-12 06:06:09 +02:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
2019-08-12 15:15:45 +02:00
|
|
|
"errors"
|
2019-08-08 08:15:59 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-05-12 06:06:09 +02:00
|
|
|
|
2019-05-18 02:57:10 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/widgets"
|
2019-08-08 08:15:59 +02:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
2019-05-12 06:06:09 +02:00
|
|
|
)
|
|
|
|
|
2019-06-27 19:33:11 +02:00
|
|
|
type Compose struct{}
|
|
|
|
|
2019-05-12 06:06:09 +02:00
|
|
|
func init() {
|
2019-06-27 19:33:11 +02:00
|
|
|
register(Compose{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Compose) Aliases() []string {
|
2019-06-27 19:33:11 +02:00
|
|
|
return []string{"compose"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Compose) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-06-27 19:33:11 +02:00
|
|
|
return nil
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Compose) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-08-08 08:15:59 +02:00
|
|
|
body, err := buildBody(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
2019-05-13 22:04:01 +02:00
|
|
|
acct := aerc.SelectedAccount()
|
2019-09-11 21:28:14 +02:00
|
|
|
composer := widgets.NewComposer(aerc,
|
2019-07-23 01:29:07 +02:00
|
|
|
aerc.Config(), acct.AccountConfig(), acct.Worker(), nil)
|
2019-05-14 22:18:21 +02:00
|
|
|
tab := aerc.NewTab(composer, "New email")
|
2019-07-23 01:29:07 +02:00
|
|
|
composer.OnHeaderChange("Subject", func(subject string) {
|
2019-05-14 22:18:21 +02:00
|
|
|
if subject == "" {
|
|
|
|
tab.Name = "New email"
|
|
|
|
} else {
|
|
|
|
tab.Name = subject
|
|
|
|
}
|
|
|
|
tab.Content.Invalidate()
|
|
|
|
})
|
2019-09-11 21:28:14 +02:00
|
|
|
go composer.PrependContents(strings.NewReader(body))
|
2019-05-12 06:06:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-08 08:15:59 +02:00
|
|
|
|
|
|
|
func buildBody(args []string) (string, error) {
|
|
|
|
var body, headers string
|
2019-08-12 15:15:45 +02:00
|
|
|
opts, optind, err := getopt.Getopts(args, "H:")
|
2019-08-08 08:15:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'H':
|
2019-09-03 21:34:05 +02:00
|
|
|
if strings.Contains(opt.Value, ":") {
|
2019-08-08 08:15:59 +02:00
|
|
|
// ensure first colon is followed by a single space
|
|
|
|
re := regexp.MustCompile(`^(.*?):\s*(.*)`)
|
|
|
|
headers += re.ReplaceAllString(opt.Value, "$1: $2") + "\n"
|
|
|
|
} else {
|
|
|
|
headers += opt.Value + ":\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-12 15:15:45 +02:00
|
|
|
posargs := args[optind:]
|
|
|
|
if len(posargs) > 1 {
|
|
|
|
return "", errors.New("Usage: compose [-H] [body]")
|
|
|
|
}
|
|
|
|
if len(posargs) == 1 {
|
|
|
|
body = posargs[0]
|
|
|
|
}
|
2019-08-08 08:15:59 +02:00
|
|
|
if headers != "" {
|
2019-08-12 15:15:45 +02:00
|
|
|
if len(body) > 0 {
|
|
|
|
body = headers + "\n" + body
|
|
|
|
} else {
|
|
|
|
body = headers + "\n\n"
|
|
|
|
}
|
2019-08-08 08:15:59 +02:00
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|