aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBence Ferdinandy <bence@ferdinandy.com>2024-01-04 21:02:19 +0100
committerRobin Jarry <robin@jarry.cc>2024-01-08 00:02:21 +0100
commitb76bf1b7eecfc885798385960b04b92cb62d75f0 (patch)
tree33f3b20d6e1e686685eb9d62415a06053009ad10
parentf41a39578f0319d255f32fc08cef6b7b73d6bdac (diff)
downloadaerc-b76bf1b7eecfc885798385960b04b92cb62d75f0.tar.gz
templates: move signature from compose to templates
Currently, when composing a new message, everything is read from the template files, except the signature, which is added directly in the compose code. Add a new template variable {{.Signature}}, by moving the logic of reading signature from command or file from compose to templates. Update the various default template files to preserve the original placement of signatures. Users using the default templates should not notice the change. Users with custom compose templates will need to update their templates with {{.Signature}}. Changelog-changed: Signature placement is now controlled via the `{{.Signature}}` template variable and not hardcoded. Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com> Acked-by: Robin Jarry <robin@jarry.cc>
-rw-r--r--app/compose.go91
-rw-r--r--config/templates.go1
-rw-r--r--lib/state/templates.go66
-rw-r--r--models/templates.go1
-rw-r--r--templates/forward_as_body2
-rw-r--r--templates/new_message2
-rw-r--r--templates/quoted_reply2
7 files changed, 74 insertions, 91 deletions
diff --git a/app/compose.go b/app/compose.go
index 35fdd9bf..43ca6610 100644
--- a/app/compose.go
+++ b/app/compose.go
@@ -28,7 +28,6 @@ import (
"git.sr.ht/~rjarry/aerc/lib/state"
"git.sr.ht/~rjarry/aerc/lib/templates"
"git.sr.ht/~rjarry/aerc/lib/ui"
- "git.sr.ht/~rjarry/aerc/lib/xdg"
"git.sr.ht/~rjarry/aerc/log"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/types"
@@ -113,11 +112,6 @@ func NewComposer(
if err := c.addTemplate(template, data.Data(), body); err != nil {
return nil, err
}
- if sig, err := c.HasSignature(); !sig && err == nil {
- c.AddSignature()
- } else if err != nil {
- return nil, err
- }
if err := c.setupFor(acct); err != nil {
return nil, err
}
@@ -548,14 +542,6 @@ func (c *Composer) setContents(reader io.Reader) error {
return c.writeEml(reader)
}
-func (c *Composer) appendContents(reader io.Reader) error {
- _, err := c.email.Seek(0, io.SeekEnd)
- if err != nil {
- return err
- }
- return c.writeEml(reader)
-}
-
func (c *Composer) AppendPart(mimetype string, params map[string]string, body io.Reader) error {
if !strings.HasPrefix(mimetype, "text") {
return fmt.Errorf("can only append text mimetypes")
@@ -637,83 +623,6 @@ func (c *Composer) addTemplate(
return c.setContents(part.Body)
}
-func (c *Composer) HasSignature() (bool, error) {
- buf, err := c.GetBody()
- if err != nil {
- return false, err
- }
- found := false
- scanner := bufio.NewScanner(buf)
- for scanner.Scan() {
- if scanner.Text() == "-- " {
- found = true
- break
- }
- }
- return found, scanner.Err()
-}
-
-func (c *Composer) AddSignature() {
- var signature []byte
- if c.acctConfig.SignatureCmd != "" {
- var err error
- signature, err = c.readSignatureFromCmd()
- if err != nil {
- signature = c.readSignatureFromFile()
- }
- } else {
- signature = c.readSignatureFromFile()
- }
- if len(bytes.TrimSpace(signature)) == 0 {
- return
- }
- signature = ensureSignatureDelimiter(signature)
- err := c.appendContents(bytes.NewReader(signature))
- if err != nil {
- log.Errorf("appendContents: %s", err)
- }
-}
-
-func (c *Composer) readSignatureFromCmd() ([]byte, error) {
- sigCmd := c.acctConfig.SignatureCmd
- cmd := exec.Command("sh", "-c", sigCmd)
- signature, err := cmd.Output()
- if err != nil {
- return nil, err
- }
- return signature, nil
-}
-
-func (c *Composer) readSignatureFromFile() []byte {
- sigFile := c.acctConfig.SignatureFile
- if sigFile == "" {
- return nil
- }
- sigFile = xdg.ExpandHome(sigFile)
- signature, err := os.ReadFile(sigFile)
- if err != nil {
- PushError(
- fmt.Sprintf(" Error loading signature from file: %v", sigFile))
- return nil
- }
- return signature
-}
-
-func ensureSignatureDelimiter(signature []byte) []byte {
- buf := bytes.NewBuffer(signature)
- scanner := bufio.NewScanner(buf)
- for scanner.Scan() {
- line := scanner.Text()
- if line == "-- " {
- // signature contains standard delimiter, we're good
- return signature
- }
- }
- // signature does not contain standard delimiter, prepend one
- sig := "\n\n-- \n" + strings.TrimLeft(string(signature), " \t\r\n")
- return []byte(sig)
-}
-
func (c *Composer) GetBody() (*bytes.Buffer, error) {
_, err := c.email.Seek(0, io.SeekStart)
if err != nil {
diff --git a/config/templates.go b/config/templates.go
index d8c0c336..c5d11478 100644
--- a/config/templates.go
+++ b/config/templates.go
@@ -65,6 +65,7 @@ var (
)
func (d *dummyData) Account() string { return "work" }
+func (d *dummyData) Signature() string { return "" }
func (d *dummyData) Folder() string { return "INBOX" }
func (d *dummyData) To() []*mail.Address { return []*mail.Address{&addr1} }
func (d *dummyData) Cc() []*mail.Address { return nil }
diff --git a/lib/state/templates.go b/lib/state/templates.go
index 22cdbfa6..ac701e9f 100644
--- a/lib/state/templates.go
+++ b/lib/state/templates.go
@@ -1,12 +1,18 @@
package state
import (
+ "bufio"
+ "bytes"
"fmt"
+ "os"
+ "os/exec"
"strings"
"time"
"git.sr.ht/~rjarry/aerc/config"
"git.sr.ht/~rjarry/aerc/lib/parse"
+ "git.sr.ht/~rjarry/aerc/lib/xdg"
+ "git.sr.ht/~rjarry/aerc/log"
"git.sr.ht/~rjarry/aerc/models"
"github.com/danwakefield/fnmatch"
sortthread "github.com/emersion/go-imap-sortthread"
@@ -651,3 +657,63 @@ top:
}
return mapped
}
+
+func (d *templateData) Signature() string {
+ if d.account == nil {
+ return ""
+ }
+ var signature []byte
+ if d.account.SignatureCmd != "" {
+ var err error
+ signature, err = d.readSignatureFromCmd()
+ if err != nil {
+ signature = d.readSignatureFromFile()
+ }
+ } else {
+ signature = d.readSignatureFromFile()
+ }
+ if len(bytes.TrimSpace(signature)) == 0 {
+ return ""
+ }
+ signature = d.ensureSignatureDelimiter(signature)
+ return string(signature)
+}
+
+func (d *templateData) readSignatureFromCmd() ([]byte, error) {
+ sigCmd := d.account.SignatureCmd
+ cmd := exec.Command("sh", "-c", sigCmd)
+ signature, err := cmd.Output()
+ if err != nil {
+ return nil, err
+ }
+ return signature, nil
+}
+
+func (d *templateData) readSignatureFromFile() []byte {
+ sigFile := d.account.SignatureFile
+ if sigFile == "" {
+ return nil
+ }
+ sigFile = xdg.ExpandHome(sigFile)
+ signature, err := os.ReadFile(sigFile)
+ if err != nil {
+ log.Errorf(" Error loading signature from file: %v", sigFile)
+ return nil
+ }
+ return signature
+}
+
+func (d *templateData) ensureSignatureDelimiter(signature []byte) []byte {
+ buf := bytes.NewBuffer(signature)
+ scanner := bufio.NewScanner(buf)
+ for scanner.Scan() {
+ line := scanner.Text()
+ if line == "-- " {
+ // signature contains standard delimiter, we're good
+ return signature
+ }
+ }
+ // signature does not contain standard delimiter, prepend one
+ sig := "\n\n-- \n" + strings.TrimLeft(string(signature), " \t\r\n")
+ return []byte(sig)
+}
diff --git a/models/templates.go b/models/templates.go
index 77f6b787..a6a85371 100644
--- a/models/templates.go
+++ b/models/templates.go
@@ -59,6 +59,7 @@ type TemplateData interface {
Style(string, string) string
StyleSwitch(string, ...Case) string
StyleMap([]string, ...Case) []string
+ Signature() string
}
type Case interface {
diff --git a/templates/forward_as_body b/templates/forward_as_body
index 564f6ed5..b29c3976 100644
--- a/templates/forward_as_body
+++ b/templates/forward_as_body
@@ -2,3 +2,5 @@ X-Mailer: aerc {{version}}
Forwarded message from {{(index .OriginalFrom 0).Name}} on {{dateFormat .OriginalDate "Mon Jan 2, 2006 at 3:04 PM"}}:
{{.OriginalText}}
+
+{{.Signature}}
diff --git a/templates/new_message b/templates/new_message
index f6b3d02a..34f80bc0 100644
--- a/templates/new_message
+++ b/templates/new_message
@@ -1 +1,3 @@
X-Mailer: aerc {{version}}
+
+{{.Signature}}
diff --git a/templates/quoted_reply b/templates/quoted_reply
index 95162172..147a4fa5 100644
--- a/templates/quoted_reply
+++ b/templates/quoted_reply
@@ -2,3 +2,5 @@ X-Mailer: aerc {{version}}
On {{dateFormat (.OriginalDate | toLocal) "Mon Jan 2, 2006 at 3:04 PM MST"}}, {{(index .OriginalFrom 0).Name}} wrote:
{{trimSignature .OriginalText | quote}}
+
+{{.Signature}}