2019-05-12 06:06:09 +02:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
2019-08-12 15:15:45 +02:00
|
|
|
"errors"
|
2022-04-05 16:54:58 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
gomail "net/mail"
|
2019-08-08 08:15:59 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-05-12 06:06:09 +02:00
|
|
|
|
2022-04-05 16:54:58 +02:00
|
|
|
"github.com/emersion/go-message/mail"
|
|
|
|
|
2022-03-22 09:52:27 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/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()
|
2022-02-25 00:21:06 +01:00
|
|
|
if acct == nil {
|
|
|
|
return errors.New("No account selected")
|
|
|
|
}
|
2022-01-19 21:28:06 +01:00
|
|
|
if template == "" {
|
|
|
|
template = aerc.Config().Templates.NewMessage
|
|
|
|
}
|
2019-11-03 13:51:14 +01:00
|
|
|
|
2022-04-05 16:54:58 +02:00
|
|
|
msg, err := gomail.ReadMessage(strings.NewReader(body))
|
|
|
|
if errors.Is(err, io.EOF) { // completely empty
|
|
|
|
msg = &gomail.Message{Body: strings.NewReader("")}
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("mail.ReadMessage: %w", err)
|
|
|
|
}
|
|
|
|
headers := mail.HeaderFromMap(msg.Header)
|
|
|
|
|
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(),
|
2022-04-05 16:54:58 +02:00
|
|
|
template, &headers, 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()
|
|
|
|
})
|
2022-03-22 09:52:27 +01:00
|
|
|
go func() {
|
|
|
|
defer logging.PanicHandler()
|
|
|
|
|
2022-04-05 16:54:58 +02:00
|
|
|
composer.AppendContents(msg.Body)
|
2022-03-22 09:52:27 +01:00
|
|
|
}()
|
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 {
|
2022-01-19 20:57:48 +01:00
|
|
|
return "", template, errors.New("Usage: compose [-H header] [-T template] [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
|
|
|
}
|