paul@100 | 1 | # -*- coding: iso-8859-1 -*- |
paul@100 | 2 | """ |
paul@100 | 3 | sendfiles - Send files using MoinMessage |
paul@95 | 4 | |
paul@100 | 5 | @copyright: 2013, 2014 by Paul Boddie <paul@boddie.org.uk> |
paul@100 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@100 | 7 | """ |
paul@100 | 8 | |
paul@100 | 9 | from MoinMessage import Message, GPG, sendMessage, timestamp |
paul@95 | 10 | from email.mime.application import MIMEApplication |
paul@106 | 11 | from email.mime.base import MIMEBase |
paul@106 | 12 | from mimetypes import guess_type |
paul@95 | 13 | from os.path import split |
paul@95 | 14 | import sys |
paul@95 | 15 | |
paul@95 | 16 | def readfile(filename): |
paul@95 | 17 | f = open(filename, "rb") |
paul@95 | 18 | try: |
paul@106 | 19 | mimetype, encoding = guess_type(filename) |
paul@106 | 20 | if mimetype: |
paul@106 | 21 | main, sub = mimetype.split("/") |
paul@106 | 22 | part = MIMEBase(main, sub) |
paul@106 | 23 | part.set_payload(f.read()) |
paul@106 | 24 | return part |
paul@106 | 25 | else: |
paul@106 | 26 | return MIMEApplication(f.read()) |
paul@95 | 27 | finally: |
paul@95 | 28 | f.close() |
paul@95 | 29 | |
paul@95 | 30 | if __name__ == "__main__": |
paul@95 | 31 | try: |
paul@95 | 32 | recipient = sys.argv[1] |
paul@95 | 33 | signer = sys.argv[2] |
paul@95 | 34 | url = sys.argv[3] |
paul@95 | 35 | args = sys.argv[4:] |
paul@95 | 36 | except IndexError: |
paul@95 | 37 | args = None |
paul@95 | 38 | |
paul@95 | 39 | if not args: |
paul@95 | 40 | print >>sys.stderr, """\ |
paul@95 | 41 | Need a recipient key identifier, signing key identifier, the destination URL, |
paul@95 | 42 | and some filenames for transfer as arguments to this program. |
paul@95 | 43 | """ |
paul@95 | 44 | sys.exit(1) |
paul@95 | 45 | |
paul@95 | 46 | # Bundle files into a message. |
paul@95 | 47 | |
paul@95 | 48 | message = Message() |
paul@95 | 49 | |
paul@95 | 50 | for arg in args: |
paul@95 | 51 | part = readfile(arg) |
paul@95 | 52 | part["Content-Disposition"] = split(arg)[-1] |
paul@95 | 53 | part["Update-Action"] = "store" |
paul@95 | 54 | message.add_update(part) |
paul@95 | 55 | |
paul@95 | 56 | # Get the e-mail message itself. |
paul@95 | 57 | |
paul@100 | 58 | email_message = message.get_payload() |
paul@95 | 59 | |
paul@104 | 60 | # Sign (for authorship), encrypt (for privacy), sign (for authentication), |
paul@104 | 61 | # and send the message. |
paul@95 | 62 | |
paul@95 | 63 | gpg = GPG() |
paul@101 | 64 | signed_message = gpg.signMessage(email_message, signer) |
paul@101 | 65 | encrypted_message = gpg.encryptMessage(signed_message, recipient) |
paul@100 | 66 | |
paul@100 | 67 | timestamp(encrypted_message) |
paul@100 | 68 | encrypted_message["Update-Action"] = "store" |
paul@100 | 69 | message_to_send = gpg.signMessage(encrypted_message, signer) |
paul@100 | 70 | |
paul@100 | 71 | resp = sendMessage(message_to_send, url) |
paul@95 | 72 | |
paul@95 | 73 | print resp |
paul@95 | 74 | |
paul@95 | 75 | # vim: tabstop=4 expandtab shiftwidth=4 |