2019-05-12 04:06:09 +00:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
2019-08-12 13:15:45 +00:00
|
|
|
"errors"
|
2019-08-08 06:15:59 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-05-12 04:06:09 +00:00
|
|
|
|
2021-11-05 09:19:46 +00:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2019-08-08 06:15:59 +00:00
|
|
|
"git.sr.ht/~sircmpwn/getopt"
|
2019-05-12 04:06:09 +00:00
|
|
|
)
|
|
|
|
|
2019-06-27 17:33:11 +00:00
|
|
|
type Compose struct{}
|
|
|
|
|
2019-05-12 04:06:09 +00:00
|
|
|
func init() {
|
2019-06-27 17:33:11 +00:00
|
|
|
register(Compose{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Compose) Aliases() []string {
|
2019-06-27 17:33:11 +00:00
|
|
|
return []string{"compose"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Compose) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-06-27 17:33:11 +00:00
|
|
|
return nil
|
2019-05-12 04:06:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 19:34:03 +00:00
|
|
|
func (Compose) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-11-03 12:51:14 +00:00
|
|
|
body, template, err := buildBody(args)
|
2019-08-08 06:15:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-12 04:06:09 +00:00
|
|
|
}
|
2019-05-13 20:04:01 +00:00
|
|
|
acct := aerc.SelectedAccount()
|
2019-11-03 12:51:14 +00:00
|
|
|
|
2020-04-24 09:42:22 +00:00
|
|
|
composer, err := widgets.NewComposer(aerc, acct,
|
2020-01-08 20:44:14 +00:00
|
|
|
aerc.Config(), acct.AccountConfig(), acct.Worker(),
|
|
|
|
template, nil, models.OriginalMail{})
|
2019-11-03 12:51:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-14 20:18:21 +00:00
|
|
|
tab := aerc.NewTab(composer, "New email")
|
2019-07-22 23:29:07 +00:00
|
|
|
composer.OnHeaderChange("Subject", func(subject string) {
|
2019-05-14 20:18:21 +00:00
|
|
|
if subject == "" {
|
|
|
|
tab.Name = "New email"
|
|
|
|
} else {
|
|
|
|
tab.Name = subject
|
|
|
|
}
|
|
|
|
tab.Content.Invalidate()
|
|
|
|
})
|
2020-04-23 18:55:18 +00:00
|
|
|
go composer.AppendContents(strings.NewReader(body))
|
2019-05-12 04:06:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-08 06:15:59 +00:00
|
|
|
|
2019-11-03 12:51:14 +00:00
|
|
|
func buildBody(args []string) (string, string, error) {
|
|
|
|
var body, template, headers string
|
|
|
|
opts, optind, err := getopt.Getopts(args, "H:T:")
|
2019-08-08 06:15:59 +00:00
|
|
|
if err != nil {
|
2019-11-03 12:51:14 +00:00
|
|
|
return "", "", err
|
2019-08-08 06:15:59 +00:00
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt.Option {
|
|
|
|
case 'H':
|
2019-09-03 19:34:05 +00:00
|
|
|
if strings.Contains(opt.Value, ":") {
|
2019-08-08 06:15:59 +00: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-11-03 12:51:14 +00:00
|
|
|
case 'T':
|
|
|
|
template = opt.Value
|
2019-08-08 06:15:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-12 13:15:45 +00:00
|
|
|
posargs := args[optind:]
|
|
|
|
if len(posargs) > 1 {
|
2022-01-19 19:57:48 +00:00
|
|
|
return "", template, errors.New("Usage: compose [-H header] [-T template] [body]")
|
2019-08-12 13:15:45 +00:00
|
|
|
}
|
|
|
|
if len(posargs) == 1 {
|
|
|
|
body = posargs[0]
|
|
|
|
}
|
2019-08-08 06:15:59 +00:00
|
|
|
if headers != "" {
|
2019-08-12 13:15:45 +00:00
|
|
|
if len(body) > 0 {
|
|
|
|
body = headers + "\n" + body
|
|
|
|
} else {
|
|
|
|
body = headers + "\n\n"
|
|
|
|
}
|
2019-08-08 06:15:59 +00:00
|
|
|
}
|
2019-11-03 12:51:14 +00:00
|
|
|
return body, template, nil
|
2019-08-08 06:15:59 +00:00
|
|
|
}
|