aboutsummaryrefslogtreecommitdiffstats
path: root/lib/state
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 /lib/state
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>
Diffstat (limited to 'lib/state')
-rw-r--r--lib/state/templates.go66
1 files changed, 66 insertions, 0 deletions
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)
+}