paul@115 | 1 | # -*- coding: iso-8859-1 -*- |
paul@115 | 2 | """ |
paul@115 | 3 | sendkey - Send key using MoinMessage |
paul@115 | 4 | |
paul@115 | 5 | @copyright: 2013, 2014 by Paul Boddie <paul@boddie.org.uk> |
paul@115 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@115 | 7 | """ |
paul@115 | 8 | |
paul@115 | 9 | from MoinMessage import Message, GPG, sendMessage |
paul@115 | 10 | from email.mime.application import MIMEApplication |
paul@115 | 11 | from email.mime.text import MIMEText |
paul@115 | 12 | import sys |
paul@115 | 13 | |
paul@115 | 14 | def readkey(filename): |
paul@115 | 15 | f = open(filename, "rb") |
paul@115 | 16 | try: |
paul@115 | 17 | return MIMEApplication(f.read(), "pgp-keys") |
paul@115 | 18 | finally: |
paul@115 | 19 | f.close() |
paul@115 | 20 | |
paul@115 | 21 | if __name__ == "__main__": |
paul@115 | 22 | try: |
paul@115 | 23 | recipient = sys.argv[1] |
paul@115 | 24 | url = sys.argv[2] |
paul@115 | 25 | filename = sys.argv[3] |
paul@115 | 26 | secret = sys.argv[4] |
paul@115 | 27 | except IndexError: |
paul@115 | 28 | secret = None |
paul@115 | 29 | |
paul@115 | 30 | if not secret: |
paul@115 | 31 | print >>sys.stderr, """\ |
paul@115 | 32 | Need a recipient key identifier, the destination URL, a key filename, and a |
paul@115 | 33 | secret as arguments to this program. |
paul@115 | 34 | """ |
paul@115 | 35 | sys.exit(1) |
paul@115 | 36 | |
paul@115 | 37 | # Bundle the key and secret into a message. |
paul@115 | 38 | |
paul@115 | 39 | message = Message() |
paul@115 | 40 | |
paul@115 | 41 | part = readkey(filename) |
paul@115 | 42 | message.add_update(part) |
paul@115 | 43 | |
paul@115 | 44 | part = MIMEText(secret) |
paul@115 | 45 | message.add_update(part) |
paul@115 | 46 | |
paul@115 | 47 | # Get the e-mail message itself. |
paul@115 | 48 | |
paul@115 | 49 | email_message = message.get_payload("x-moinmessage-keys") |
paul@115 | 50 | |
paul@115 | 51 | # Sign (for authorship), encrypt (for privacy), sign (for authentication), |
paul@115 | 52 | # and send the message. |
paul@115 | 53 | |
paul@115 | 54 | gpg = GPG() |
paul@115 | 55 | encrypted_message = gpg.encryptMessage(email_message, recipient) |
paul@115 | 56 | |
paul@115 | 57 | resp = sendMessage(encrypted_message, url) |
paul@115 | 58 | |
paul@115 | 59 | print resp |
paul@115 | 60 | |
paul@115 | 61 | # vim: tabstop=4 expandtab shiftwidth=4 |