paul@6 | 1 | # -*- coding: iso-8859-1 -*- |
paul@6 | 2 | """ |
paul@6 | 3 | MoinMoin - MoinMessage library |
paul@6 | 4 | |
paul@6 | 5 | @copyright: 2012 by Paul Boddie <paul@boddie.org.uk> |
paul@6 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@6 | 7 | """ |
paul@6 | 8 | |
paul@6 | 9 | from MoinMoin.log import getLogger |
paul@6 | 10 | from email import message_from_string |
paul@6 | 11 | from email.encoders import encode_noop |
paul@6 | 12 | from email.mime.multipart import MIMEMultipart |
paul@6 | 13 | from email.mime.application import MIMEApplication |
paul@6 | 14 | from email.mime.base import MIMEBase |
paul@6 | 15 | from email.mime.text import MIMEText |
paul@6 | 16 | from subprocess import Popen, PIPE |
paul@6 | 17 | import httplib |
paul@6 | 18 | |
paul@6 | 19 | class Message: |
paul@6 | 20 | |
paul@6 | 21 | "An update message." |
paul@6 | 22 | |
paul@6 | 23 | def __init__(self): |
paul@6 | 24 | self.updates = [] |
paul@6 | 25 | |
paul@6 | 26 | def add_update(self, alternatives): |
paul@6 | 27 | if len(alternatives) > 1: |
paul@6 | 28 | part = MIMEMultipart() |
paul@6 | 29 | for alternative in alternatives: |
paul@6 | 30 | part.attach(alternative) |
paul@6 | 31 | self.updates.append(part) |
paul@6 | 32 | else: |
paul@6 | 33 | self.updates.append(alternatives[0]) |
paul@6 | 34 | |
paul@6 | 35 | def get_payload(self): |
paul@6 | 36 | if len(self.updates) == 1: |
paul@6 | 37 | message = self.updates[0] |
paul@6 | 38 | else: |
paul@6 | 39 | message = MIMEMultipart() |
paul@6 | 40 | message.add_header("Update-Type", "collection") |
paul@6 | 41 | for update in self.updates: |
paul@6 | 42 | message.attach(update) |
paul@6 | 43 | |
paul@6 | 44 | return message |
paul@6 | 45 | |
paul@6 | 46 | class MoinMessageError(Exception): |
paul@6 | 47 | pass |
paul@6 | 48 | |
paul@6 | 49 | def gpg(args, text): |
paul@6 | 50 | |
paul@6 | 51 | "Invoke gpg with the given 'args', supplying the given 'text'." |
paul@6 | 52 | |
paul@6 | 53 | cmd = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) |
paul@6 | 54 | |
paul@6 | 55 | cmd.stdin.write(text) |
paul@6 | 56 | cmd.stdin.close() |
paul@6 | 57 | |
paul@6 | 58 | errors = cmd.stderr.read() |
paul@6 | 59 | if errors: |
paul@6 | 60 | getLogger(__name__).warning(errors) |
paul@6 | 61 | |
paul@6 | 62 | try: |
paul@6 | 63 | text = cmd.stdout.read() |
paul@6 | 64 | |
paul@6 | 65 | # Test for a zero result. |
paul@6 | 66 | |
paul@6 | 67 | if not cmd.wait(): |
paul@6 | 68 | return text |
paul@6 | 69 | else: |
paul@6 | 70 | raise MoinMessageError, errors |
paul@6 | 71 | |
paul@6 | 72 | finally: |
paul@6 | 73 | cmd.stdout.close() |
paul@6 | 74 | cmd.stderr.close() |
paul@6 | 75 | |
paul@6 | 76 | def signMessage(message, keyid): |
paul@6 | 77 | |
paul@6 | 78 | """ |
paul@6 | 79 | Return a signed 'message' using the given 'keyid'. |
paul@6 | 80 | """ |
paul@6 | 81 | |
paul@6 | 82 | text = message.as_string() |
paul@6 | 83 | signature = gpg(["gpg", "--armor", "-u", keyid, "--detach-sig"], text) |
paul@6 | 84 | |
paul@6 | 85 | # Make the container for the message. |
paul@6 | 86 | |
paul@6 | 87 | signed_message = MIMEMultipart("signed", protocol="application/pgp-signature") |
paul@6 | 88 | signed_message.attach(message) |
paul@6 | 89 | |
paul@6 | 90 | signature_part = MIMEBase("application", "pgp-signature") |
paul@6 | 91 | signature_part.set_payload(signature) |
paul@6 | 92 | signed_message.attach(signature_part) |
paul@6 | 93 | |
paul@6 | 94 | return signed_message |
paul@6 | 95 | |
paul@6 | 96 | def encryptMessage(message, keyid): |
paul@6 | 97 | |
paul@6 | 98 | """ |
paul@6 | 99 | Return an encrypted 'message' using the given 'keyid'. |
paul@6 | 100 | """ |
paul@6 | 101 | |
paul@6 | 102 | text = message.as_string() |
paul@6 | 103 | encrypted = gpg(["gpg", "--armor", "-r", keyid, "--encrypt", "--trust-model", "always"], text) |
paul@6 | 104 | |
paul@6 | 105 | # Make the container for the message. |
paul@6 | 106 | |
paul@6 | 107 | encrypted_message = MIMEMultipart("encrypted", protocol="application/pgp-encrypted") |
paul@6 | 108 | |
paul@6 | 109 | # For encrypted content, add the declaration and content. |
paul@6 | 110 | |
paul@6 | 111 | declaration = MIMEBase("application", "pgp-encrypted") |
paul@6 | 112 | declaration.set_payload("Version: 1") |
paul@6 | 113 | encrypted_message.attach(declaration) |
paul@6 | 114 | |
paul@6 | 115 | content = MIMEApplication(encrypted, "octet-stream", encode_noop) |
paul@6 | 116 | encrypted_message.attach(content) |
paul@6 | 117 | |
paul@6 | 118 | return encrypted_message |
paul@6 | 119 | |
paul@6 | 120 | def sendMessage(message, host, path): |
paul@6 | 121 | |
paul@6 | 122 | "Send 'message' to the given 'host' using the specified URL 'path'." |
paul@6 | 123 | |
paul@6 | 124 | text = message.as_string() |
paul@6 | 125 | |
paul@6 | 126 | req = httplib.HTTPConnection(host) |
paul@6 | 127 | req.request("PUT", path, text) # {"Content-Length" : len(text)} |
paul@6 | 128 | resp = req.getresponse() |
paul@6 | 129 | return resp.read() |
paul@6 | 130 | |
paul@6 | 131 | # vim: tabstop=4 expandtab shiftwidth=4 |