From b76bf1b7eecfc885798385960b04b92cb62d75f0 Mon Sep 17 00:00:00 2001 From: Bence Ferdinandy Date: Thu, 4 Jan 2024 21:02:19 +0100 Subject: 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 Acked-by: Robin Jarry --- lib/state/templates.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'lib/state/templates.go') 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) +} -- cgit