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