diff options
author | witcher <witcher@wiredspace.de> | 2023-03-02 21:50:32 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-03-02 22:09:11 +0100 |
commit | f28235b9f785bf9fee311d6229c5ca70bb7cb264 (patch) | |
tree | 908cea1b512841faf1b715a370b928e7fc9a6f65 /widgets | |
parent | 58ca92bcd872e3114c9261819aae6118ea267d63 (diff) | |
download | aerc-f28235b9f785bf9fee311d6229c5ca70bb7cb264.tar.gz |
pgp: automatically determine signer from sender
This patch automatically determines the signing account from the sender
of the email (i.e. the From email header).
To reflect this in the compose view after changing the From field, the
restriction in updateCrypto() to only run the update if the signing key
is empty has been lifted, so the key always gets updated when calling
updateCrypt().
Additionally, a Signer() method has been added to the Composer to avoid
specifying the same logic twice; once in updateCrypto() and once in
WriteMessage().
If the From header is not populated for any reason the Signer() method
falls back to the from field specified in accounts.conf.
Signed-off-by: witcher <witcher@wiredspace.de>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'widgets')
-rw-r--r-- | widgets/compose.go | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/widgets/compose.go b/widgets/compose.go index 74493ac9..4f36b297 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -388,15 +388,11 @@ func (c *Composer) updateCrypto() error { uiConfig := c.acct.UiConfig() c.crypto = newCryptoStatus(uiConfig) } - var err error - // Check if signKey is empty so we only run this once - if c.sign && c.crypto.signKey == "" { + if c.sign { cp := c.aerc.Crypto - var s string - if c.acctConfig.PgpKeyId != "" { - s = c.acctConfig.PgpKeyId - } else { - s = c.acctConfig.From.Address + s, err := c.Signer() + if err != nil { + return errors.Wrap(err, "Signer") } c.crypto.signKey, err = cp.GetSignerKeyId(s) if err != nil { @@ -769,6 +765,30 @@ func getRecipientsEmail(c *Composer) ([]string, error) { return results, nil } +func (c *Composer) Signer() (string, error) { + signer := "" + + if c.acctConfig.PgpKeyId != "" { + // get key from explicitly set keyid + signer = c.acctConfig.PgpKeyId + } else { + // get signer from `from` header + from, err := c.header.AddressList("from") + if err != nil { + return "", err + } + + if len(from) > 0 { + signer = from[0].Address + } else { + // fall back to address from config + signer = c.acctConfig.From.Address + } + } + + return signer, nil +} + func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error { if err := c.reloadEmail(); err != nil { return err @@ -785,10 +805,9 @@ func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error { signer := "" if c.sign { - if c.acctConfig.PgpKeyId != "" { - signer = c.acctConfig.PgpKeyId - } else { - signer = c.acctConfig.From.Address + signer, err = c.Signer() + if err != nil { + return errors.Wrap(err, "Signer") } } |