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
|
|
|
|
2020-01-08 21:44:14 +01:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
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-11-03 13:51:14 +01:00
|
|
|
body, template, err := buildBody(args)
|
2019-08-08 08:15:59 +02:00
|
|
|
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-11-03 13:51:14 +01:00
|
|
|
|
2020-04-24 11:42:22 +02:00
|
|
|
composer, err := widgets.NewComposer(aerc, acct,
|
2020-01-08 21:44:14 +01:00
|
|
|
aerc.Config(), acct.AccountConfig(), acct.Worker(),
|
|
|
|
template, nil, models.OriginalMail{})
|
2019-11-03 13:51:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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()
|
|
|
|
})
|
2020-04-23 20:55:18 +02:00
|
|
|
go composer.AppendContents(strings.NewReader(body))
|
2019-05-12 06:06:09 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-08 08:15:59 +02:00
|
|
|
|
2019-11-03 13:51:14 +01:00
|
|
|
func buildBody(args []string) (string, string, error) {
|
|
|
|
var body, template, headers string
|
|
|
|
opts, optind, err := getopt.Getopts(args, "H:T:")
|
2019-08-08 08:15:59 +02:00
|
|
|
if err != nil {
|
2019-11-03 13:51:14 +01:00
|
|
|
return "", "", err
|
2019-08-08 08:15:59 +02:00
|
|
|
}
|
|
|
|
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-11-03 13:51:14 +01:00
|
|
|
case 'T':
|
|
|
|
template = opt.Value
|
2019-08-08 08:15:59 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-12 15:15:45 +02:00
|
|
|
posargs := args[optind:]
|
|
|
|
if len(posargs) > 1 {
|
2019-11-03 13:51:14 +01:00
|
|
|
return "", template, errors.New("Usage: compose [-H] [body]")
|
2019-08-12 15:15:45 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2019-11-03 13:51:14 +01:00
|
|
|
return body, template, nil
|
2019-08-08 08:15:59 +02:00
|
|
|
}
|