Skip to content

Instantly share code, notes, and snippets.

@meskio
Last active September 18, 2017 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meskio/5e2f5946aaff53a2e79cb4df5651f695 to your computer and use it in GitHub Desktop.
Save meskio/5e2f5946aaff53a2e79cb4df5651f695 to your computer and use it in GitHub Desktop.
alot attach keys
[bindings]
[[envelope]]
G = call hooks.attach_my_key(ui)
'G G' = call hooks.attach_recipient_keys(ui)
import subprocess
from alot.settings.const import settings
from email.utils import parseaddr
from twisted.internet import defer
def attach_my_key(ui):
"""
Attach the senders openpgp public key
"""
frm = ui.current_buffer.envelope.get('From', "")
address = parseaddr(frm)[1]
acc = settings.get_account_by_address(address)
fpr = acc.gpg_key.fpr
return attach_key(ui, fpr, address)
def attach_recipient_keys(ui):
"""
Attach the openpgp public keys of all the recipients of the email
"""
to = ui.current_buffer.envelope.get('To', "")
cc = ui.current_buffer.envelope.get('Cc', "")
deferreds = []
for recipient in to.split(',')+cc.split(','):
address = parseaddr(recipient)[1]
if address:
deferreds.append(attach_key(ui, address))
return defer.gatherResults(deferreds)
def attach_key(ui, id, name=None):
"""
Export and attach openpgp key
"""
if not name:
name = id
path = "/tmp/%s.asc" % name
subprocess.call('gpg -a --export ' + id + ' > ' + path, shell=True)
return ui.apply_commandline('attach ' + path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment