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 email import message_from_string |
paul@6 | 10 | from email.encoders import encode_noop |
paul@6 | 11 | from email.mime.multipart import MIMEMultipart |
paul@6 | 12 | from email.mime.application import MIMEApplication |
paul@6 | 13 | from email.mime.base import MIMEBase |
paul@6 | 14 | from subprocess import Popen, PIPE |
paul@8 | 15 | from tempfile import mkstemp |
paul@12 | 16 | from urlparse import urlsplit |
paul@6 | 17 | import httplib |
paul@8 | 18 | import os |
paul@6 | 19 | |
paul@15 | 20 | def is_collection(message): |
paul@15 | 21 | return message.get("Update-Type") == "collection" |
paul@15 | 22 | |
paul@6 | 23 | class Message: |
paul@6 | 24 | |
paul@6 | 25 | "An update message." |
paul@6 | 26 | |
paul@15 | 27 | def __init__(self, text=None): |
paul@6 | 28 | self.updates = [] |
paul@15 | 29 | if text: |
paul@15 | 30 | self.parse_text(text) |
paul@15 | 31 | |
paul@15 | 32 | def parse_text(self, text): |
paul@15 | 33 | |
paul@15 | 34 | "Parse the given 'text' as a message." |
paul@15 | 35 | |
paul@15 | 36 | self.handle_message(message_from_string(text)) |
paul@15 | 37 | |
paul@15 | 38 | def handle_message(self, message): |
paul@15 | 39 | |
paul@15 | 40 | "Handle the given 'message', recording the separate updates." |
paul@15 | 41 | |
paul@15 | 42 | # The message either consists of a collection of updates. |
paul@15 | 43 | |
paul@15 | 44 | if message.is_multipart() and is_collection(message): |
paul@15 | 45 | for part in message.get_payload(): |
paul@15 | 46 | self.updates.append(part) |
paul@15 | 47 | |
paul@15 | 48 | # Or the message is a single update. |
paul@15 | 49 | |
paul@15 | 50 | else: |
paul@15 | 51 | self.updates.append(message) |
paul@6 | 52 | |
paul@6 | 53 | def add_update(self, alternatives): |
paul@13 | 54 | |
paul@13 | 55 | """ |
paul@13 | 56 | Add an update fragment to a message, providing alternative forms of the |
paul@13 | 57 | update content in the given 'alternatives': a list of MIME message |
paul@13 | 58 | parts, each encoding the content according to different MIME types. |
paul@13 | 59 | """ |
paul@13 | 60 | |
paul@6 | 61 | if len(alternatives) > 1: |
paul@6 | 62 | part = MIMEMultipart() |
paul@6 | 63 | for alternative in alternatives: |
paul@6 | 64 | part.attach(alternative) |
paul@6 | 65 | self.updates.append(part) |
paul@6 | 66 | else: |
paul@6 | 67 | self.updates.append(alternatives[0]) |
paul@6 | 68 | |
paul@6 | 69 | def get_payload(self): |
paul@13 | 70 | |
paul@13 | 71 | "Get the multipart payload for the message." |
paul@13 | 72 | |
paul@6 | 73 | if len(self.updates) == 1: |
paul@6 | 74 | message = self.updates[0] |
paul@6 | 75 | else: |
paul@6 | 76 | message = MIMEMultipart() |
paul@6 | 77 | message.add_header("Update-Type", "collection") |
paul@6 | 78 | for update in self.updates: |
paul@6 | 79 | message.attach(update) |
paul@6 | 80 | |
paul@6 | 81 | return message |
paul@6 | 82 | |
paul@15 | 83 | class Mailbox: |
paul@15 | 84 | |
paul@15 | 85 | "A collection of messages within a multipart message." |
paul@15 | 86 | |
paul@15 | 87 | def __init__(self, text=None): |
paul@15 | 88 | self.messages = [] |
paul@15 | 89 | if text: |
paul@15 | 90 | self.parse_text(text) |
paul@15 | 91 | |
paul@15 | 92 | def parse_text(self, text): |
paul@15 | 93 | |
paul@15 | 94 | "Parse the given 'text' as a mailbox." |
paul@15 | 95 | |
paul@15 | 96 | message = message_from_string(text) |
paul@15 | 97 | |
paul@15 | 98 | if message.is_multipart(): |
paul@15 | 99 | for part in message.get_payload(): |
paul@15 | 100 | self.messages.append(part) |
paul@15 | 101 | else: |
paul@15 | 102 | self.messages.append(message) |
paul@15 | 103 | |
paul@15 | 104 | def add_message(self, message): |
paul@15 | 105 | |
paul@15 | 106 | "Add the given 'message' to the mailbox." |
paul@15 | 107 | |
paul@15 | 108 | self.messages.append(message) |
paul@15 | 109 | |
paul@15 | 110 | def get_payload(self): |
paul@15 | 111 | |
paul@15 | 112 | "Get the multipart payload for the mailbox." |
paul@15 | 113 | |
paul@15 | 114 | mailbox = MIMEMultipart() |
paul@15 | 115 | for message in self.messages: |
paul@15 | 116 | mailbox.attach(message) |
paul@15 | 117 | |
paul@15 | 118 | return mailbox |
paul@15 | 119 | |
paul@6 | 120 | class MoinMessageError(Exception): |
paul@6 | 121 | pass |
paul@6 | 122 | |
paul@8 | 123 | class GPG: |
paul@8 | 124 | |
paul@8 | 125 | "A wrapper around the gpg command using a particular configuration." |
paul@6 | 126 | |
paul@8 | 127 | def __init__(self, homedir=None): |
paul@8 | 128 | self.conf_args = [] |
paul@6 | 129 | |
paul@8 | 130 | if homedir: |
paul@8 | 131 | self.conf_args += ["--homedir", homedir] |
paul@8 | 132 | |
paul@8 | 133 | self.errors = None |
paul@6 | 134 | |
paul@8 | 135 | def run(self, args, text=None): |
paul@6 | 136 | |
paul@8 | 137 | """ |
paul@8 | 138 | Invoke gpg with the given 'args', supplying the given 'text' to the |
paul@8 | 139 | command directly or, if 'text' is omitted, using a file provided as part |
paul@8 | 140 | of the 'args' if appropriate. |
paul@6 | 141 | |
paul@8 | 142 | Failure to complete the operation will result in a MoinMessageError |
paul@8 | 143 | being raised. |
paul@8 | 144 | """ |
paul@8 | 145 | |
paul@8 | 146 | cmd = Popen(["gpg"] + self.conf_args + list(args), stdin=PIPE, stdout=PIPE, stderr=PIPE) |
paul@6 | 147 | |
paul@11 | 148 | try: |
paul@11 | 149 | # Attempt to write input to the command and to read output from the |
paul@11 | 150 | # command. |
paul@11 | 151 | |
paul@11 | 152 | try: |
paul@11 | 153 | if text: |
paul@11 | 154 | cmd.stdin.write(text) |
paul@11 | 155 | cmd.stdin.close() |
paul@6 | 156 | |
paul@11 | 157 | text = cmd.stdout.read() |
paul@11 | 158 | |
paul@11 | 159 | # I/O errors can indicate the failure of the command. |
paul@8 | 160 | |
paul@11 | 161 | except IOError: |
paul@11 | 162 | pass |
paul@11 | 163 | |
paul@11 | 164 | self.errors = cmd.stderr.read() |
paul@8 | 165 | |
paul@8 | 166 | # Test for a zero result. |
paul@6 | 167 | |
paul@8 | 168 | if not cmd.wait(): |
paul@8 | 169 | return text |
paul@8 | 170 | else: |
paul@10 | 171 | raise MoinMessageError, self.errors |
paul@8 | 172 | |
paul@8 | 173 | finally: |
paul@8 | 174 | cmd.stdout.close() |
paul@8 | 175 | cmd.stderr.close() |
paul@6 | 176 | |
paul@8 | 177 | def verifyMessage(self, signature, content): |
paul@8 | 178 | |
paul@8 | 179 | "Using the given 'signature', verify the given message 'content'." |
paul@6 | 180 | |
paul@8 | 181 | # Write the detached signature and content to files. |
paul@8 | 182 | |
paul@8 | 183 | signature_fd, signature_filename = mkstemp() |
paul@8 | 184 | content_fd, content_filename = mkstemp() |
paul@6 | 185 | |
paul@8 | 186 | try: |
paul@8 | 187 | signature_fp = os.fdopen(signature_fd, "w") |
paul@8 | 188 | content_fp = os.fdopen(content_fd, "w") |
paul@8 | 189 | try: |
paul@8 | 190 | signature_fp.write(signature) |
paul@8 | 191 | content_fp.write(content) |
paul@8 | 192 | finally: |
paul@8 | 193 | signature_fp.close() |
paul@8 | 194 | content_fp.close() |
paul@6 | 195 | |
paul@8 | 196 | # Verify the message text. |
paul@6 | 197 | |
paul@10 | 198 | text = self.run(["--status-fd", "1", "--verify", signature_filename, content_filename]) |
paul@10 | 199 | |
paul@10 | 200 | # Return the details of the signing key. |
paul@10 | 201 | |
paul@11 | 202 | identity = None |
paul@11 | 203 | fingerprint = None |
paul@11 | 204 | |
paul@10 | 205 | for line in text.split("\n"): |
paul@10 | 206 | try: |
paul@11 | 207 | prefix, msgtype, digest, details = line.strip().split(" ", 3) |
paul@10 | 208 | except ValueError: |
paul@10 | 209 | continue |
paul@10 | 210 | |
paul@10 | 211 | # Return the fingerprint and identity details. |
paul@10 | 212 | |
paul@10 | 213 | if msgtype == "GOODSIG": |
paul@11 | 214 | identity = details |
paul@11 | 215 | elif msgtype == "VALIDSIG": |
paul@11 | 216 | fingerprint = digest |
paul@11 | 217 | |
paul@11 | 218 | if identity and fingerprint: |
paul@11 | 219 | return fingerprint, identity |
paul@10 | 220 | |
paul@10 | 221 | return None |
paul@6 | 222 | |
paul@8 | 223 | finally: |
paul@8 | 224 | os.remove(signature_filename) |
paul@8 | 225 | os.remove(content_filename) |
paul@8 | 226 | |
paul@8 | 227 | def signMessage(self, message, keyid): |
paul@6 | 228 | |
paul@8 | 229 | """ |
paul@8 | 230 | Return a signed version of 'message' using the given 'keyid'. |
paul@8 | 231 | """ |
paul@6 | 232 | |
paul@8 | 233 | text = message.as_string() |
paul@8 | 234 | signature = self.run(["--armor", "-u", keyid, "--detach-sig"], text) |
paul@8 | 235 | |
paul@8 | 236 | # Make the container for the message. |
paul@8 | 237 | |
paul@8 | 238 | signed_message = MIMEMultipart("signed", protocol="application/pgp-signature") |
paul@8 | 239 | signed_message.attach(message) |
paul@6 | 240 | |
paul@8 | 241 | signature_part = MIMEBase("application", "pgp-signature") |
paul@8 | 242 | signature_part.set_payload(signature) |
paul@8 | 243 | signed_message.attach(signature_part) |
paul@8 | 244 | |
paul@8 | 245 | return signed_message |
paul@8 | 246 | |
paul@8 | 247 | def decryptMessage(self, message): |
paul@6 | 248 | |
paul@8 | 249 | "Return a decrypted version of 'message'." |
paul@8 | 250 | |
paul@8 | 251 | return self.run(["--decrypt"], message) |
paul@6 | 252 | |
paul@8 | 253 | def encryptMessage(self, message, keyid): |
paul@6 | 254 | |
paul@8 | 255 | """ |
paul@8 | 256 | Return an encrypted version of 'message' using the given 'keyid'. |
paul@8 | 257 | """ |
paul@6 | 258 | |
paul@8 | 259 | text = message.as_string() |
paul@8 | 260 | encrypted = self.run(["--armor", "-r", keyid, "--encrypt", "--trust-model", "always"], text) |
paul@8 | 261 | |
paul@8 | 262 | # Make the container for the message. |
paul@8 | 263 | |
paul@8 | 264 | encrypted_message = MIMEMultipart("encrypted", protocol="application/pgp-encrypted") |
paul@8 | 265 | |
paul@8 | 266 | # For encrypted content, add the declaration and content. |
paul@6 | 267 | |
paul@8 | 268 | declaration = MIMEBase("application", "pgp-encrypted") |
paul@8 | 269 | declaration.set_payload("Version: 1") |
paul@8 | 270 | encrypted_message.attach(declaration) |
paul@6 | 271 | |
paul@8 | 272 | content = MIMEApplication(encrypted, "octet-stream", encode_noop) |
paul@8 | 273 | encrypted_message.attach(content) |
paul@6 | 274 | |
paul@8 | 275 | return encrypted_message |
paul@8 | 276 | |
paul@8 | 277 | # Communications functions. |
paul@6 | 278 | |
paul@12 | 279 | def sendMessage(message, url): |
paul@6 | 280 | |
paul@12 | 281 | "Send 'message' to the given 'url." |
paul@6 | 282 | |
paul@12 | 283 | scheme, host, port, path = parseURL(url) |
paul@6 | 284 | text = message.as_string() |
paul@6 | 285 | |
paul@12 | 286 | if scheme == "http": |
paul@12 | 287 | cls = httplib.HTTPConnection |
paul@12 | 288 | elif scheme == "https": |
paul@12 | 289 | cls = httplib.HTTPSConnection |
paul@12 | 290 | else: |
paul@12 | 291 | raise MoinMessageError, "Communications protocol not supported: %s" % scheme |
paul@12 | 292 | |
paul@12 | 293 | req = cls(host, port) |
paul@12 | 294 | req.request("PUT", path, text) |
paul@6 | 295 | resp = req.getresponse() |
paul@6 | 296 | return resp.read() |
paul@6 | 297 | |
paul@12 | 298 | def parseURL(url): |
paul@12 | 299 | |
paul@12 | 300 | "Return the scheme, host, port and path for the given 'url'." |
paul@12 | 301 | |
paul@12 | 302 | scheme, host_port, path, query, fragment = urlsplit(url) |
paul@12 | 303 | host_port = host_port.split(":") |
paul@12 | 304 | |
paul@12 | 305 | if query: |
paul@12 | 306 | path += "?" + query |
paul@12 | 307 | |
paul@12 | 308 | if len(host_port) > 1: |
paul@12 | 309 | host = host_port[0] |
paul@12 | 310 | port = int(host_port[1]) |
paul@12 | 311 | else: |
paul@12 | 312 | host = host_port[0] |
paul@12 | 313 | port = 80 |
paul@12 | 314 | |
paul@12 | 315 | return scheme, host, port, path |
paul@12 | 316 | |
paul@6 | 317 | # vim: tabstop=4 expandtab shiftwidth=4 |