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.text import MIMEText |
paul@95 | 11 | from email.parser import Parser |
paul@95 | 12 | from os.path import join, exists |
paul@95 | 13 | from os import makedirs |
paul@112 | 14 | from time import strftime |
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@104 | 24 | def decrypt(gpg, content): |
paul@104 | 25 | if is_encrypted(content): |
paul@104 | 26 | text = gpg.decryptMessage(content) |
paul@104 | 27 | return Parser().parsestr(text) |
paul@104 | 28 | else: |
paul@104 | 29 | return content |
paul@104 | 30 | |
paul@95 | 31 | if __name__ == "__main__": |
paul@95 | 32 | try: |
paul@95 | 33 | service = sys.argv[1] |
paul@95 | 34 | signer = sys.argv[2] |
paul@95 | 35 | url = sys.argv[3] |
paul@95 | 36 | sender = sys.argv[4] |
paul@95 | 37 | datetime = sys.argv[5] |
paul@95 | 38 | target_dir = sys.argv[6] |
paul@95 | 39 | except IndexError: |
paul@95 | 40 | print >>sys.stderr, """\ |
paul@95 | 41 | Need a service key identifier, signing key identifier, the repository URL, |
paul@95 | 42 | sender identifier, the datetime of the last resources retrieved, and the target |
paul@95 | 43 | directory as arguments to this program. |
paul@95 | 44 | """ |
paul@95 | 45 | sys.exit(1) |
paul@95 | 46 | |
paul@95 | 47 | # Bundle files into a message. |
paul@95 | 48 | |
paul@95 | 49 | message = Message() |
paul@95 | 50 | |
paul@112 | 51 | commands = ["MBOX %s" % sender, "LAST %s" % datetime, "RETR"] |
paul@112 | 52 | part = MIMEText("\n".join(commands), "x-moinmessage-fetch") |
paul@95 | 53 | message.add_update(part) |
paul@95 | 54 | |
paul@95 | 55 | # Get the e-mail message itself. |
paul@95 | 56 | |
paul@104 | 57 | email_message = message.get_payload() |
paul@95 | 58 | |
paul@112 | 59 | # Sign, encrypt and send the request. |
paul@95 | 60 | |
paul@95 | 61 | gpg = GPG() |
paul@112 | 62 | signed_message = gpg.signMessage(email_message, signer) |
paul@112 | 63 | encrypted_message = gpg.encryptMessage(signed_message, service) |
paul@112 | 64 | resp = sendMessageOpener(encrypted_message, url) |
paul@95 | 65 | |
paul@104 | 66 | # Verify the response after possible transport encryption. |
paul@95 | 67 | |
paul@95 | 68 | try: |
paul@95 | 69 | message = Parser().parse(resp) |
paul@104 | 70 | message = decrypt(gpg, message) |
paul@95 | 71 | fingerprint, identity, content = gpg.verifyMessage(message) |
paul@95 | 72 | |
paul@95 | 73 | except MoinMessageDecodingError: |
paul@95 | 74 | print >>sys.stderr, "Incoming message was improperly encoded." |
paul@95 | 75 | sys.exit(1) |
paul@95 | 76 | |
paul@95 | 77 | except MoinMessageError, exc: |
paul@95 | 78 | print >>sys.stderr, "Incoming message was not verifiable: %s" % exc |
paul@95 | 79 | sys.exit(1) |
paul@95 | 80 | |
paul@95 | 81 | message = Message() |
paul@95 | 82 | message.handle_message(content) |
paul@95 | 83 | |
paul@95 | 84 | for part in message.updates: |
paul@112 | 85 | if part.get_content_type() == "text/x-moinmessage-fetch-result": |
paul@112 | 86 | if part["Request-Status"] != "OK": |
paul@112 | 87 | print part |
paul@112 | 88 | else: |
paul@112 | 89 | print part["Request-Type"] |
paul@112 | 90 | continue |
paul@95 | 91 | |
paul@95 | 92 | # Use the "outer" filename to determine a directory for the retrieved |
paul@95 | 93 | # file, even though the eventual filename in the directory may be |
paul@95 | 94 | # different. |
paul@95 | 95 | |
paul@112 | 96 | timestamp = strftime("%Y-%m-%d_%H:%M:%S") |
paul@112 | 97 | directory = part.get("Content-Disposition", timestamp) |
paul@95 | 98 | |
paul@104 | 99 | # The retrieved content may be encrypted. |
paul@104 | 100 | |
paul@104 | 101 | part = decrypt(gpg, part) |
paul@95 | 102 | |
paul@104 | 103 | # The original sender may now be verified. |
paul@95 | 104 | |
paul@104 | 105 | if is_signed(part): |
paul@104 | 106 | fingerprint, identity, content = gpg.verifyMessage(part) |
paul@104 | 107 | print >>sys.stderr, "Content signed by %s." % identity |
paul@95 | 108 | |
paul@95 | 109 | if content.is_multipart(): |
paul@95 | 110 | files = content.get_payload() |
paul@95 | 111 | else: |
paul@95 | 112 | files = [content] |
paul@95 | 113 | |
paul@95 | 114 | # Treat each part of the verified, possibly encrypted message as a file. |
paul@95 | 115 | |
paul@95 | 116 | for file in files: |
paul@95 | 117 | data = file.get_payload(decode=True) |
paul@95 | 118 | |
paul@104 | 119 | realname = file["Content-Disposition"] |
paul@104 | 120 | filename = join(directory, realname) |
paul@95 | 121 | |
paul@95 | 122 | dirpath = join(target_dir, directory) |
paul@95 | 123 | if not exists(dirpath): |
paul@95 | 124 | makedirs(dirpath) |
paul@95 | 125 | writefile(join(target_dir, filename), data) |
paul@95 | 126 | |
paul@95 | 127 | # vim: tabstop=4 expandtab shiftwidth=4 |