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@95 | 11 | from os.path import split |
paul@95 | 12 | import sys |
paul@95 | 13 | |
paul@95 | 14 | def readfile(filename): |
paul@95 | 15 | f = open(filename, "rb") |
paul@95 | 16 | try: |
paul@95 | 17 | return MIMEApplication(f.read()) |
paul@95 | 18 | finally: |
paul@95 | 19 | f.close() |
paul@95 | 20 | |
paul@95 | 21 | if __name__ == "__main__": |
paul@95 | 22 | try: |
paul@95 | 23 | recipient = sys.argv[1] |
paul@95 | 24 | signer = sys.argv[2] |
paul@95 | 25 | url = sys.argv[3] |
paul@95 | 26 | args = sys.argv[4:] |
paul@95 | 27 | except IndexError: |
paul@95 | 28 | args = None |
paul@95 | 29 | |
paul@95 | 30 | if not args: |
paul@95 | 31 | print >>sys.stderr, """\ |
paul@95 | 32 | Need a recipient key identifier, signing key identifier, the destination URL, |
paul@95 | 33 | and some filenames for transfer as arguments to this program. |
paul@95 | 34 | """ |
paul@95 | 35 | sys.exit(1) |
paul@95 | 36 | |
paul@95 | 37 | # Bundle files into a message. |
paul@95 | 38 | |
paul@95 | 39 | message = Message() |
paul@95 | 40 | |
paul@95 | 41 | for arg in args: |
paul@95 | 42 | part = readfile(arg) |
paul@95 | 43 | part["Content-Disposition"] = split(arg)[-1] |
paul@95 | 44 | part["Update-Action"] = "store" |
paul@95 | 45 | message.add_update(part) |
paul@95 | 46 | |
paul@95 | 47 | # Get the e-mail message itself. |
paul@95 | 48 | |
paul@100 | 49 | email_message = message.get_payload() |
paul@95 | 50 | |
paul@95 | 51 | # Encrypt, sign and send the message. |
paul@95 | 52 | |
paul@95 | 53 | gpg = GPG() |
paul@100 | 54 | encrypted_message = gpg.encryptMessage(email_message, recipient) |
paul@100 | 55 | |
paul@100 | 56 | timestamp(encrypted_message) |
paul@100 | 57 | encrypted_message["Update-Action"] = "store" |
paul@100 | 58 | message_to_send = gpg.signMessage(encrypted_message, signer) |
paul@100 | 59 | |
paul@100 | 60 | resp = sendMessage(message_to_send, url) |
paul@95 | 61 | |
paul@95 | 62 | print resp |
paul@95 | 63 | |
paul@95 | 64 | # vim: tabstop=4 expandtab shiftwidth=4 |