diff options
author | Tim Culverhouse <tim@timculverhouse.com> | 2022-05-05 12:53:16 -0500 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-05-06 11:02:55 +0200 |
commit | b57fceaad4bfcbd4ca3022e013b73eff72079c0b (patch) | |
tree | 7e68b206ca5b5dd7d1e2a8793360a80963c0c1df /widgets | |
parent | 32a16dcd8dc488c1f360553d9d9f6d121af1b367 (diff) | |
download | aerc-b57fceaad4bfcbd4ca3022e013b73eff72079c0b.tar.gz |
pgp: add attach key command
Add compose command ("attach-key") to attach the public key associated
with the sending account. Public key is attached in ascii armor format,
with the mimetype set according to RFC 3156 ("application/pgp-keys").
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Tested-by: Koni Marti <koni.marti@gmail.com>
Diffstat (limited to 'widgets')
-rw-r--r-- | widgets/compose.go | 80 |
1 files changed, 78 insertions, 2 deletions
diff --git a/widgets/compose.go b/widgets/compose.go index 49627fca..8830d9d6 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -51,6 +51,7 @@ type Composer struct { crypto *cryptoStatus sign bool encrypt bool + attachKey bool layout HeaderLayout focusable []ui.MouseableDrawableInteractive @@ -183,6 +184,16 @@ func (c *Composer) Sent() bool { return c.sent } +func (c *Composer) SetAttachKey(attach bool) error { + c.attachKey = attach + c.resetReview() + return nil +} + +func (c *Composer) AttachKey() bool { + return c.attachKey +} + func (c *Composer) SetSign(sign bool) error { c.sign = sign err := c.updateCrypto() @@ -581,7 +592,7 @@ func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error { } func writeMsgImpl(c *Composer, header *mail.Header, writer io.Writer) error { - if len(c.attachments) == 0 { + if len(c.attachments) == 0 && !c.attachKey { // no attachements return writeInlineBody(header, c.email, writer) } else { @@ -598,6 +609,12 @@ func writeMsgImpl(c *Composer, header *mail.Header, writer io.Writer) error { return errors.Wrap(err, "writeAttachment") } } + if c.attachKey { + err := c.writeKeyAttachment(w) + if err != nil { + return err + } + } w.Close() } return nil @@ -1060,6 +1077,9 @@ func newReviewMessage(composer *Composer, err error) *reviewMessage { for i := 0; i < len(composer.attachments)-1; i++ { spec = append(spec, ui.GridSpec{Strategy: ui.SIZE_EXACT, Size: ui.Const(1)}) } + if composer.attachKey { + spec = append(spec, ui.GridSpec{Strategy: ui.SIZE_EXACT, Size: ui.Const(1)}) + } // make the last element fill remaining space spec = append(spec, ui.GridSpec{Strategy: ui.SIZE_WEIGHT, Size: ui.Const(1)}) @@ -1085,7 +1105,12 @@ func newReviewMessage(composer *Composer, err error) *reviewMessage { grid.AddChild(ui.NewText("Attachments:", uiConfig.GetStyle(config.STYLE_TITLE))).At(i, 0) i += 1 - if len(composer.attachments) == 0 { + if composer.attachKey { + grid.AddChild(ui.NewText(composer.crypto.signKey+".asc", + uiConfig.GetStyle(config.STYLE_DEFAULT))).At(i, 0) + i += 1 + } + if len(composer.attachments) == 0 && !composer.attachKey { grid.AddChild(ui.NewText("(none)", uiConfig.GetStyle(config.STYLE_DEFAULT))).At(i, 0) } else { @@ -1185,3 +1210,54 @@ func (c *Composer) checkEncryptionKeys(_ string) bool { c.updateCrypto() return true } + +func (c *Composer) writeKeyAttachment(w *mail.Writer) error { + // Verify key exists and get keyid + cp := c.aerc.Crypto + var ( + err error + s string + ) + if c.crypto.signKey == "" { + if c.acctConfig.PgpKeyId != "" { + s = c.acctConfig.PgpKeyId + } else { + s, err = getSenderEmail(c) + if err != nil { + return err + } + } + c.crypto.signKey, err = cp.GetSignerKeyId(s) + if err != nil { + return err + } + } + // Get the key in armor format + r, err := cp.ExportKey(c.crypto.signKey) + if err != nil { + c.aerc.PushError(err.Error()) + return err + } + filename := c.crypto.signKey + ".asc" + mimeType := "application/pgp-keys" + params := map[string]string{ + "charset": "UTF-8", + "name": filename, + } + // set header fields + ah := mail.AttachmentHeader{} + ah.SetContentType(mimeType, params) + // setting the filename auto sets the content disposition + ah.SetFilename(filename) + + aw, err := w.CreateAttachment(ah) + if err != nil { + return errors.Wrap(err, "CreateKeyAttachment") + } + defer aw.Close() + + if _, err := io.Copy(aw, r); err != nil { + return errors.Wrap(err, "io.Copy") + } + return nil +} |