diff options
author | Robin Jarry <robin@jarry.cc> | 2023-07-04 12:18:55 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-07-17 10:23:36 +0200 |
commit | 624fb681a8ceeceecb898627503bf6cf73c2c6e8 (patch) | |
tree | 68cc73e04e0c2100795b9bfb0720bbc8f28f431f /widgets/compose.go | |
parent | 21f14c550a44a4b9207ca27b81976f92a7d26784 (diff) | |
download | aerc-624fb681a8ceeceecb898627503bf6cf73c2c6e8.tar.gz |
compose: only allow setting content at creation
Add a parameter for the initial value of the body to be inserted
*before* the signature when composing a message.
Make AppendContents and SetContents private methods to ensure there is
no other way to change the composer contents from the outside after
creation.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Bence Ferdinandy <bence@ferdinandy.com>
Tested-by: Koni Marti <koni.marti@gmail.com>
Diffstat (limited to 'widgets/compose.go')
-rw-r--r-- | widgets/compose.go | 40 |
1 files changed, 28 insertions, 12 deletions
diff --git a/widgets/compose.go b/widgets/compose.go index 43657a29..c4a93d83 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -71,7 +71,7 @@ type Composer struct { func NewComposer( aerc *Aerc, acct *AccountView, acctConfig *config.AccountConfig, worker *types.Worker, template string, - h *mail.Header, orig *models.OriginalMail, + h *mail.Header, orig *models.OriginalMail, body io.Reader, ) (*Composer, error) { if h == nil { h = new(mail.Header) @@ -100,7 +100,7 @@ func NewComposer( data.SetAccount(acct.acct) data.SetFolder(acct.Directories().SelectedDirectory()) data.SetHeaders(h, orig) - if err := c.AddTemplate(template, data.Data()); err != nil { + if err := c.addTemplate(template, data.Data(), body); err != nil { return nil, err } c.AddSignature() @@ -428,7 +428,7 @@ func (c *Composer) updateCrypto() error { // Note: this does not reload the editor. You must call this before the first // Draw() call. -func (c *Composer) SetContents(reader io.Reader) *Composer { +func (c *Composer) setContents(reader io.Reader) *Composer { _, err := c.email.Seek(0, io.SeekStart) if err != nil { log.Warnf("failed to seek beginning of mail: %v", err) @@ -448,7 +448,7 @@ func (c *Composer) SetContents(reader io.Reader) *Composer { return c } -func (c *Composer) AppendContents(reader io.Reader) { +func (c *Composer) appendContents(reader io.Reader) { _, err := c.email.Seek(0, io.SeekEnd) if err != nil { log.Warnf("failed to seek beginning of mail: %v", err) @@ -496,20 +496,36 @@ func (c *Composer) RemovePart(mimetype string) error { return fmt.Errorf("%s part not found", mimetype) } -func (c *Composer) AddTemplate(template string, data models.TemplateData) error { - if template == "" { +func (c *Composer) addTemplate( + template string, data models.TemplateData, body io.Reader, +) error { + var readers []io.Reader + + if template != "" { + templateText, err := templates.ParseTemplateFromFile( + template, config.Templates.TemplateDirs, data) + if err != nil { + return err + } + readers = append(readers, templateText) + } + if body != nil { + readers = append(readers, body) + } + if len(readers) == 0 { return nil } - templateText, err := templates.ParseTemplateFromFile( - template, config.Templates.TemplateDirs, data) + buf, err := io.ReadAll(io.MultiReader(readers...)) if err != nil { return err } - mr, err := mail.CreateReader(templateText) + mr, err := mail.CreateReader(bytes.NewReader(buf)) if err != nil { - return fmt.Errorf("Template loading failed: %w", err) + // no headers in the template nor body + c.setContents(bytes.NewReader(buf)) + return nil } // copy the headers contained in the template to the compose headers @@ -523,7 +539,7 @@ func (c *Composer) AddTemplate(template string, data models.TemplateData) error return fmt.Errorf("Could not get body of template: %w", err) } - c.AppendContents(part.Body) + c.setContents(part.Body) return nil } @@ -542,7 +558,7 @@ func (c *Composer) AddSignature() { return } signature = ensureSignatureDelimiter(signature) - c.AppendContents(bytes.NewReader(signature)) + c.appendContents(bytes.NewReader(signature)) } func (c *Composer) readSignatureFromCmd() ([]byte, error) { |