Let caller pass in custom headers to compose

This commit is contained in:
Drew DeVault 2019-05-16 10:49:50 -04:00
parent b0bf09b98f
commit 2b3e123cb8
3 changed files with 38 additions and 8 deletions

View File

@ -53,7 +53,7 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
return fmt.Errorf("Unknown transfer protocol %s", uri.Scheme) return fmt.Errorf("Unknown transfer protocol %s", uri.Scheme)
} }
header, rcpts, err := composer.Header() header, rcpts, err := composer.PrepareHeader()
if err != nil { if err != nil {
return err return err
} }
@ -188,7 +188,7 @@ func SendMessage(aerc *widgets.Aerc, args []string) error {
composer.Close() composer.Close()
} }
}) })
header, _, _ := composer.Header() header, _, _ := composer.PrepareHeader()
composer.WriteMessage(header, w) composer.WriteMessage(header, w)
w.Close() w.Close()
} else { } else {

View File

@ -40,6 +40,10 @@ func (ti *TextInput) String() string {
return string(ti.text) return string(ti.text)
} }
func (ti *TextInput) Set(value string) {
ti.text = []rune(value)
}
func (ti *TextInput) Invalidate() { func (ti *TextInput) Invalidate() {
ti.DoInvalidate(ti) ti.DoInvalidate(ti)
} }

View File

@ -27,11 +27,12 @@ type Composer struct {
config *config.AccountConfig config *config.AccountConfig
editor *Terminal defaults map[string]string
email *os.File editor *Terminal
grid *ui.Grid email *os.File
review *reviewMessage grid *ui.Grid
worker *types.Worker review *reviewMessage
worker *types.Worker
focusable []ui.DrawableInteractive focusable []ui.DrawableInteractive
focused int focused int
@ -104,6 +105,24 @@ func NewComposer(conf *config.AercConfig,
return c return c
} }
// Sets additional headers to be added to the outgoing email (e.g. In-Reply-To)
func (c *Composer) Defaults(defaults map[string]string) *Composer {
c.defaults = defaults
if to, ok := defaults["To"]; ok {
c.headers.to.input.Set(to)
delete(defaults, "To")
}
if from, ok := defaults["From"]; ok {
c.headers.from.input.Set(from)
delete(defaults, "From")
}
if subject, ok := defaults["Subject"]; ok {
c.headers.subject.input.Set(subject)
delete(defaults, "Subject")
}
return c
}
func (c *Composer) OnSubjectChange(fn func(subject string)) { func (c *Composer) OnSubjectChange(fn func(subject string)) {
c.headers.subject.OnChange(func() { c.headers.subject.OnChange(func() {
fn(c.headers.subject.input.String()) fn(c.headers.subject.input.String())
@ -163,7 +182,7 @@ func (c *Composer) Worker() *types.Worker {
return c.worker return c.worker
} }
func (c *Composer) Header() (*mail.Header, []string, error) { func (c *Composer) PrepareHeader() (*mail.Header, []string, error) {
// Extract headers from the email, if present // Extract headers from the email, if present
c.email.Seek(0, os.SEEK_SET) c.email.Seek(0, os.SEEK_SET)
var ( var (
@ -209,6 +228,13 @@ func (c *Composer) Header() (*mail.Header, []string, error) {
rcpts = append(rcpts, addr.Address) rcpts = append(rcpts, addr.Address)
} }
} }
// Merge in additional headers
txthdr := mhdr.Header
for key, value := range c.defaults {
if !txthdr.Has(key) {
mhdr.SetText(key, value)
}
}
// TODO: Add cc, bcc to rcpts // TODO: Add cc, bcc to rcpts
return &header, rcpts, nil return &header, rcpts, nil
} }