From e2d5c456dc27f958d79fdf740c7b0852b2af4160 Mon Sep 17 00:00:00 2001 From: Jeffas Date: Wed, 11 Sep 2019 20:28:14 +0100 Subject: Add signatures This adds the ability for per-account signatures in the accounts.conf config file. The signature is added to emails in the editor at the bottom of the email. This includes when forwarding, replying to, and composing emails. There are two config options: signature-file and signature-cmd. The former allows a signature to be read from a file and the latter allows an arbitrary command to be executed to return the signature. The config options have been documented in aerc-config --- widgets/aerc.go | 2 +- widgets/compose.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) (limited to 'widgets') diff --git a/widgets/aerc.go b/widgets/aerc.go index fe3c1e2..8671c87 100644 --- a/widgets/aerc.go +++ b/widgets/aerc.go @@ -430,7 +430,7 @@ func (aerc *Aerc) Mailto(addr *url.URL) error { defaults[header] = strings.Join(vals, ",") } } - composer := NewComposer(aerc.Config(), + composer := NewComposer(aerc, aerc.Config(), acct.AccountConfig(), acct.Worker(), defaults) composer.FocusSubject() title := "New email" diff --git a/widgets/compose.go b/widgets/compose.go index 0e7f09e..22c58da 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -2,6 +2,8 @@ package widgets import ( "bufio" + "bytes" + "fmt" "io" "io/ioutil" "mime" @@ -17,6 +19,7 @@ import ( "github.com/emersion/go-message/mail" "github.com/gdamore/tcell" "github.com/mattn/go-runewidth" + "github.com/mitchellh/go-homedir" "github.com/pkg/errors" "git.sr.ht/~sircmpwn/aerc/config" @@ -29,6 +32,7 @@ type Composer struct { acct *config.AccountConfig config *config.AercConfig + aerc *Aerc defaults map[string]string editor *Terminal @@ -48,7 +52,7 @@ type Composer struct { width int } -func NewComposer(conf *config.AercConfig, +func NewComposer(aerc *Aerc, conf *config.AercConfig, acct *config.AccountConfig, worker *types.Worker, defaults map[string]string) *Composer { if defaults == nil { @@ -68,6 +72,7 @@ func NewComposer(conf *config.AercConfig, } c := &Composer{ + aerc: aerc, editors: editors, acct: acct, config: conf, @@ -80,6 +85,8 @@ func NewComposer(conf *config.AercConfig, focusable: focusable, } + c.AddSignature() + c.updateGrid() c.ShowTerminal() @@ -140,6 +147,63 @@ func (c *Composer) SetContents(reader io.Reader) *Composer { return c } +func (c *Composer) PrependContents(reader io.Reader) { + buf := bytes.NewBuffer(nil) + c.email.Seek(0, io.SeekStart) + io.Copy(buf, c.email) + c.email.Seek(0, io.SeekStart) + io.Copy(c.email, reader) + io.Copy(c.email, buf) + c.email.Sync() +} + +func (c *Composer) AppendContents(reader io.Reader) { + c.email.Seek(0, io.SeekEnd) + io.Copy(c.email, reader) + c.email.Sync() +} + +func (c *Composer) AddSignature() { + var signature []byte + if c.acct.SignatureCmd != "" { + var err error + signature, err = c.readSignatureFromCmd() + if err != nil { + signature = c.readSignatureFromFile() + } + } else { + signature = c.readSignatureFromFile() + } + c.AppendContents(bytes.NewReader(signature)) +} + +func (c *Composer) readSignatureFromCmd() ([]byte, error) { + sigCmd := c.acct.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.acct.SignatureFile + if sigFile == "" { + return nil + } + sigFile, err := homedir.Expand(sigFile) + if err != nil { + return nil + } + signature, err := ioutil.ReadFile(sigFile) + if err != nil { + c.aerc.PushError(fmt.Sprintf(" Error loading signature from file: %v", sigFile)) + return nil + } + return signature +} + func (c *Composer) FocusTerminal() *Composer { if c.editor == nil { return c -- cgit v1.2.3