paul@100 | 1 | # -*- coding: iso-8859-1 -*- |
paul@100 | 2 | """ |
paul@100 | 3 | getfiles - Obtain files using MoinMessage |
paul@100 | 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@95 | 8 | |
paul@95 | 9 | from MoinMessage import * |
paul@95 | 10 | from email.mime.application import MIMEApplication |
paul@95 | 11 | from email.mime.text import MIMEText |
paul@95 | 12 | from email.parser import Parser |
paul@95 | 13 | from os.path import join, exists |
paul@95 | 14 | from os import makedirs |
paul@95 | 15 | import sys |
paul@95 | 16 | |
paul@95 | 17 | def writefile(filename, s): |
paul@95 | 18 | f = open(filename, "wb") |
paul@95 | 19 | try: |
paul@95 | 20 | f.write(s) |
paul@95 | 21 | finally: |
paul@95 | 22 | f.close() |
paul@95 | 23 | |
paul@95 | 24 | if __name__ == "__main__": |
paul@95 | 25 | try: |
paul@95 | 26 | service = sys.argv[1] |
paul@95 | 27 | signer = sys.argv[2] |
paul@95 | 28 | url = sys.argv[3] |
paul@95 | 29 | sender = sys.argv[4] |
paul@95 | 30 | datetime = sys.argv[5] |
paul@95 | 31 | target_dir = sys.argv[6] |
paul@95 | 32 | except IndexError: |
paul@95 | 33 | print >>sys.stderr, """\ |
paul@95 | 34 | Need a service key identifier, signing key identifier, the repository URL, |
paul@95 | 35 | sender identifier, the datetime of the last resources retrieved, and the target |
paul@95 | 36 | directory as arguments to this program. |
paul@95 | 37 | """ |
paul@95 | 38 | sys.exit(1) |
paul@95 | 39 | |
paul@95 | 40 | # Bundle files into a message. |
paul@95 | 41 | |
paul@95 | 42 | message = Message() |
paul@95 | 43 | |
paul@95 | 44 | part = MIMEText("\n".join([sender, datetime]), "moinmessage-request") |
paul@95 | 45 | message.add_update(part) |
paul@95 | 46 | |
paul@95 | 47 | # Get the e-mail message itself. |
paul@95 | 48 | |
paul@95 | 49 | 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@95 | 54 | message = gpg.encryptMessage(message, service) |
paul@95 | 55 | message = gpg.signMessage(message, signer) |
paul@95 | 56 | resp = sendMessageForReading(message, url) |
paul@95 | 57 | |
paul@95 | 58 | # Verify, decrypt and unpack the message. |
paul@95 | 59 | |
paul@95 | 60 | try: |
paul@95 | 61 | if not is_signed(message): |
paul@95 | 62 | print >>sys.stderr, "Incoming message was not signed." |
paul@95 | 63 | sys.exit(1) |
paul@95 | 64 | |
paul@95 | 65 | message = Parser().parse(resp) |
paul@95 | 66 | fingerprint, identity, content = gpg.verifyMessage(message) |
paul@95 | 67 | |
paul@95 | 68 | if is_encrypted(content): |
paul@95 | 69 | text = gpg.decryptMessage(content) |
paul@95 | 70 | content = Parser().parsestr(text) |
paul@95 | 71 | |
paul@95 | 72 | except MoinMessageDecodingError: |
paul@95 | 73 | print >>sys.stderr, "Incoming message was improperly encoded." |
paul@95 | 74 | sys.exit(1) |
paul@95 | 75 | |
paul@95 | 76 | except MoinMessageError, exc: |
paul@95 | 77 | print >>sys.stderr, "Incoming message was not verifiable: %s" % exc |
paul@95 | 78 | sys.exit(1) |
paul@95 | 79 | |
paul@95 | 80 | message = Message() |
paul@95 | 81 | message.handle_message(content) |
paul@95 | 82 | |
paul@95 | 83 | for part in message.updates: |
paul@95 | 84 | |
paul@95 | 85 | # Use the "outer" filename to determine a directory for the retrieved |
paul@95 | 86 | # file, even though the eventual filename in the directory may be |
paul@95 | 87 | # different. |
paul@95 | 88 | |
paul@95 | 89 | filename = part["Content-Disposition"] |
paul@95 | 90 | directory, leafname = filename.split("/") |
paul@95 | 91 | |
paul@95 | 92 | # The data may be encrypted. |
paul@95 | 93 | |
paul@95 | 94 | try: |
paul@97 | 95 | data = gpg.decryptMessage(part) |
paul@95 | 96 | except MoinMessageError: |
paul@95 | 97 | print >>sys.stderr, "Message part was not decrypted." |
paul@97 | 98 | data = part.get_payload(decode=True) |
paul@95 | 99 | |
paul@95 | 100 | # Parse the decoded data. |
paul@95 | 101 | |
paul@95 | 102 | content = Parser().parsestr(data) |
paul@95 | 103 | |
paul@95 | 104 | if content.is_multipart(): |
paul@95 | 105 | files = content.get_payload() |
paul@95 | 106 | else: |
paul@95 | 107 | files = [content] |
paul@95 | 108 | |
paul@95 | 109 | # Treat each part of the verified, possibly encrypted message as a file. |
paul@95 | 110 | |
paul@95 | 111 | for file in files: |
paul@95 | 112 | data = file.get_payload(decode=True) |
paul@95 | 113 | |
paul@95 | 114 | realname = file.get("Content-Disposition") |
paul@95 | 115 | filename = join(directory, realname or leafname) |
paul@95 | 116 | |
paul@95 | 117 | dirpath = join(target_dir, directory) |
paul@95 | 118 | if not exists(dirpath): |
paul@95 | 119 | makedirs(dirpath) |
paul@95 | 120 | writefile(join(target_dir, filename), data) |
paul@95 | 121 | |
paul@95 | 122 | # vim: tabstop=4 expandtab shiftwidth=4 |