compose: parse headers correctly from -H

By using :compose -H <header> a user should be able to add
arbitrary headers to an email.  The existing implementation from
5b523880b4 added the headers as lines
to the beginning of the body.  These lines were not interpreted as
headers anywhere and ended up as plain text in the body of the email.
Fix the code to parse and add the headers correctly.

Signed-off-by: Evan Gates <evan.gates@gmail.com>
Tested-by: Moritz Poldrack <moritz@poldrack.dev>
This commit is contained in:
Evan Gates 2022-04-05 08:54:58 -06:00 committed by Robin Jarry
parent 247c6c7438
commit 1aa32bf377

View file

@ -2,9 +2,14 @@ package account
import ( import (
"errors" "errors"
"fmt"
"io"
gomail "net/mail"
"regexp" "regexp"
"strings" "strings"
"github.com/emersion/go-message/mail"
"git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/logging"
"git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/widgets" "git.sr.ht/~rjarry/aerc/widgets"
@ -38,9 +43,17 @@ func (Compose) Execute(aerc *widgets.Aerc, args []string) error {
template = aerc.Config().Templates.NewMessage template = aerc.Config().Templates.NewMessage
} }
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)
composer, err := widgets.NewComposer(aerc, acct, composer, err := widgets.NewComposer(aerc, acct,
aerc.Config(), acct.AccountConfig(), acct.Worker(), aerc.Config(), acct.AccountConfig(), acct.Worker(),
template, nil, models.OriginalMail{}) template, &headers, models.OriginalMail{})
if err != nil { if err != nil {
return err return err
} }
@ -56,7 +69,7 @@ func (Compose) Execute(aerc *widgets.Aerc, args []string) error {
go func() { go func() {
defer logging.PanicHandler() defer logging.PanicHandler()
composer.AppendContents(strings.NewReader(body)) composer.AppendContents(msg.Body)
}() }()
return nil return nil
} }