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