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@16 | 17 | from MoinMoin.util import lock |
paul@6 | 18 | import httplib |
paul@8 | 19 | import os |
paul@6 | 20 | |
paul@15 | 21 | def is_collection(message): |
paul@15 | 22 | return message.get("Update-Type") == "collection" |
paul@15 | 23 | |
paul@6 | 24 | class Message: |
paul@6 | 25 | |
paul@6 | 26 | "An update message." |
paul@6 | 27 | |
paul@15 | 28 | def __init__(self, text=None): |
paul@6 | 29 | self.updates = [] |
paul@15 | 30 | if text: |
paul@15 | 31 | self.parse_text(text) |
paul@15 | 32 | |
paul@15 | 33 | def parse_text(self, text): |
paul@15 | 34 | |
paul@15 | 35 | "Parse the given 'text' as a message." |
paul@15 | 36 | |
paul@15 | 37 | self.handle_message(message_from_string(text)) |
paul@15 | 38 | |
paul@15 | 39 | def handle_message(self, message): |
paul@15 | 40 | |
paul@15 | 41 | "Handle the given 'message', recording the separate updates." |
paul@15 | 42 | |
paul@15 | 43 | # The message either consists of a collection of updates. |
paul@15 | 44 | |
paul@15 | 45 | if message.is_multipart() and is_collection(message): |
paul@15 | 46 | for part in message.get_payload(): |
paul@15 | 47 | self.updates.append(part) |
paul@15 | 48 | |
paul@15 | 49 | # Or the message is a single update. |
paul@15 | 50 | |
paul@15 | 51 | else: |
paul@15 | 52 | self.updates.append(message) |
paul@6 | 53 | |
paul@16 | 54 | def add_updates(self, parts): |
paul@13 | 55 | |
paul@13 | 56 | """ |
paul@16 | 57 | Add the given 'parts' to a message. |
paul@13 | 58 | """ |
paul@13 | 59 | |
paul@16 | 60 | for part in updates: |
paul@16 | 61 | self.add_update(part) |
paul@16 | 62 | |
paul@16 | 63 | def add_update(self, part): |
paul@16 | 64 | |
paul@16 | 65 | """ |
paul@16 | 66 | Add an update 'part' to a message. |
paul@16 | 67 | """ |
paul@16 | 68 | |
paul@16 | 69 | self.updates.append(part) |
paul@16 | 70 | |
paul@16 | 71 | def get_update(self, alternatives): |
paul@16 | 72 | |
paul@16 | 73 | """ |
paul@16 | 74 | Return a suitable multipart object containing the supplied |
paul@16 | 75 | 'alternatives'. |
paul@16 | 76 | """ |
paul@16 | 77 | |
paul@16 | 78 | part = MIMEMultipart() |
paul@16 | 79 | for alternative in alternatives: |
paul@16 | 80 | part.attach(alternative) |
paul@16 | 81 | return part |
paul@6 | 82 | |
paul@6 | 83 | def get_payload(self): |
paul@13 | 84 | |
paul@13 | 85 | "Get the multipart payload for the message." |
paul@13 | 86 | |
paul@6 | 87 | if len(self.updates) == 1: |
paul@6 | 88 | message = self.updates[0] |
paul@6 | 89 | else: |
paul@6 | 90 | message = MIMEMultipart() |
paul@6 | 91 | message.add_header("Update-Type", "collection") |
paul@6 | 92 | for update in self.updates: |
paul@6 | 93 | message.attach(update) |
paul@6 | 94 | |
paul@6 | 95 | return message |
paul@6 | 96 | |
paul@15 | 97 | class Mailbox: |
paul@15 | 98 | |
paul@15 | 99 | "A collection of messages within a multipart message." |
paul@15 | 100 | |
paul@15 | 101 | def __init__(self, text=None): |
paul@15 | 102 | self.messages = [] |
paul@15 | 103 | if text: |
paul@15 | 104 | self.parse_text(text) |
paul@15 | 105 | |
paul@15 | 106 | def parse_text(self, text): |
paul@15 | 107 | |
paul@15 | 108 | "Parse the given 'text' as a mailbox." |
paul@15 | 109 | |
paul@15 | 110 | message = message_from_string(text) |
paul@15 | 111 | |
paul@15 | 112 | if message.is_multipart(): |
paul@15 | 113 | for part in message.get_payload(): |
paul@15 | 114 | self.messages.append(part) |
paul@15 | 115 | else: |
paul@15 | 116 | self.messages.append(message) |
paul@15 | 117 | |
paul@15 | 118 | def add_message(self, message): |
paul@15 | 119 | |
paul@15 | 120 | "Add the given 'message' to the mailbox." |
paul@15 | 121 | |
paul@15 | 122 | self.messages.append(message) |
paul@15 | 123 | |
paul@15 | 124 | def get_payload(self): |
paul@15 | 125 | |
paul@15 | 126 | "Get the multipart payload for the mailbox." |
paul@15 | 127 | |
paul@15 | 128 | mailbox = MIMEMultipart() |
paul@15 | 129 | for message in self.messages: |
paul@15 | 130 | mailbox.attach(message) |
paul@15 | 131 | |
paul@15 | 132 | return mailbox |
paul@15 | 133 | |
paul@16 | 134 | class MessageStore: |
paul@16 | 135 | |
paul@16 | 136 | "A page-specific message store." |
paul@16 | 137 | |
paul@16 | 138 | def __init__(self, page): |
paul@16 | 139 | |
paul@16 | 140 | "Initialise a message store for the given 'page'." |
paul@16 | 141 | |
paul@16 | 142 | self.path = page.getPagePath("messages") |
paul@16 | 143 | self.next_path = os.path.join(self.path, "next") |
paul@16 | 144 | lock_dir = page.getPagePath("message-locks") |
paul@16 | 145 | self.lock = lock.WriteLock(lock_dir) |
paul@16 | 146 | |
paul@16 | 147 | def get_next(self): |
paul@16 | 148 | |
paul@16 | 149 | "Return the next message number." |
paul@16 | 150 | |
paul@16 | 151 | next = self.read_next() |
paul@16 | 152 | if not next: |
paul@16 | 153 | next = self.deduce_next() |
paul@16 | 154 | self.write_next(next) |
paul@16 | 155 | return next |
paul@16 | 156 | |
paul@16 | 157 | def deduce_next(self): |
paul@16 | 158 | |
paul@16 | 159 | "Deduce the next message number from the existing message files." |
paul@16 | 160 | |
paul@16 | 161 | return max([int(filename) for filename in os.listdir(self.path) if filename.isdigit()] or [0]) + 1 |
paul@16 | 162 | |
paul@16 | 163 | def read_next(self): |
paul@16 | 164 | |
paul@16 | 165 | "Read the next message number from a special file." |
paul@16 | 166 | |
paul@16 | 167 | if not os.path.exists(self.next_path): |
paul@16 | 168 | return 0 |
paul@16 | 169 | |
paul@16 | 170 | f = open(self.next_path) |
paul@16 | 171 | try: |
paul@16 | 172 | try: |
paul@16 | 173 | return int(f.read()) |
paul@16 | 174 | except ValueError: |
paul@16 | 175 | return 0 |
paul@16 | 176 | finally: |
paul@16 | 177 | f.close() |
paul@16 | 178 | |
paul@16 | 179 | def write_next(self, next): |
paul@16 | 180 | |
paul@16 | 181 | "Write the 'next' message number to a special file." |
paul@16 | 182 | |
paul@16 | 183 | f = open(self.next_path, "w") |
paul@16 | 184 | try: |
paul@16 | 185 | f.write(str(next)) |
paul@16 | 186 | finally: |
paul@16 | 187 | f.close() |
paul@16 | 188 | |
paul@16 | 189 | def write_message(self, message, next): |
paul@16 | 190 | |
paul@16 | 191 | "Write the given 'message' to a file with the given 'next' message number." |
paul@16 | 192 | |
paul@16 | 193 | f = open(os.path.join(self.path, str(next)), "w") |
paul@16 | 194 | try: |
paul@16 | 195 | f.write(message.as_string()) |
paul@16 | 196 | finally: |
paul@16 | 197 | f.close() |
paul@16 | 198 | |
paul@16 | 199 | def append(self, message): |
paul@16 | 200 | |
paul@16 | 201 | "Append the given 'message' to the store." |
paul@16 | 202 | |
paul@16 | 203 | self.lock.acquire() |
paul@16 | 204 | try: |
paul@16 | 205 | next = self.get_next() |
paul@16 | 206 | self.write_message(message, next) |
paul@16 | 207 | self.write_next(next + 1) |
paul@16 | 208 | finally: |
paul@16 | 209 | self.lock.release() |
paul@16 | 210 | |
paul@6 | 211 | class MoinMessageError(Exception): |
paul@6 | 212 | pass |
paul@6 | 213 | |
paul@8 | 214 | class GPG: |
paul@8 | 215 | |
paul@8 | 216 | "A wrapper around the gpg command using a particular configuration." |
paul@6 | 217 | |
paul@8 | 218 | def __init__(self, homedir=None): |
paul@8 | 219 | self.conf_args = [] |
paul@6 | 220 | |
paul@8 | 221 | if homedir: |
paul@8 | 222 | self.conf_args += ["--homedir", homedir] |
paul@8 | 223 | |
paul@8 | 224 | self.errors = None |
paul@6 | 225 | |
paul@8 | 226 | def run(self, args, text=None): |
paul@6 | 227 | |
paul@8 | 228 | """ |
paul@8 | 229 | Invoke gpg with the given 'args', supplying the given 'text' to the |
paul@8 | 230 | command directly or, if 'text' is omitted, using a file provided as part |
paul@8 | 231 | of the 'args' if appropriate. |
paul@6 | 232 | |
paul@8 | 233 | Failure to complete the operation will result in a MoinMessageError |
paul@8 | 234 | being raised. |
paul@8 | 235 | """ |
paul@8 | 236 | |
paul@8 | 237 | cmd = Popen(["gpg"] + self.conf_args + list(args), stdin=PIPE, stdout=PIPE, stderr=PIPE) |
paul@6 | 238 | |
paul@11 | 239 | try: |
paul@11 | 240 | # Attempt to write input to the command and to read output from the |
paul@11 | 241 | # command. |
paul@11 | 242 | |
paul@11 | 243 | try: |
paul@11 | 244 | if text: |
paul@11 | 245 | cmd.stdin.write(text) |
paul@11 | 246 | cmd.stdin.close() |
paul@6 | 247 | |
paul@11 | 248 | text = cmd.stdout.read() |
paul@11 | 249 | |
paul@11 | 250 | # I/O errors can indicate the failure of the command. |
paul@8 | 251 | |
paul@11 | 252 | except IOError: |
paul@11 | 253 | pass |
paul@11 | 254 | |
paul@11 | 255 | self.errors = cmd.stderr.read() |
paul@8 | 256 | |
paul@8 | 257 | # Test for a zero result. |
paul@6 | 258 | |
paul@8 | 259 | if not cmd.wait(): |
paul@8 | 260 | return text |
paul@8 | 261 | else: |
paul@10 | 262 | raise MoinMessageError, self.errors |
paul@8 | 263 | |
paul@8 | 264 | finally: |
paul@8 | 265 | cmd.stdout.close() |
paul@8 | 266 | cmd.stderr.close() |
paul@6 | 267 | |
paul@8 | 268 | def verifyMessage(self, signature, content): |
paul@8 | 269 | |
paul@8 | 270 | "Using the given 'signature', verify the given message 'content'." |
paul@6 | 271 | |
paul@8 | 272 | # Write the detached signature and content to files. |
paul@8 | 273 | |
paul@8 | 274 | signature_fd, signature_filename = mkstemp() |
paul@8 | 275 | content_fd, content_filename = mkstemp() |
paul@6 | 276 | |
paul@8 | 277 | try: |
paul@8 | 278 | signature_fp = os.fdopen(signature_fd, "w") |
paul@8 | 279 | content_fp = os.fdopen(content_fd, "w") |
paul@8 | 280 | try: |
paul@8 | 281 | signature_fp.write(signature) |
paul@8 | 282 | content_fp.write(content) |
paul@8 | 283 | finally: |
paul@8 | 284 | signature_fp.close() |
paul@8 | 285 | content_fp.close() |
paul@6 | 286 | |
paul@8 | 287 | # Verify the message text. |
paul@6 | 288 | |
paul@10 | 289 | text = self.run(["--status-fd", "1", "--verify", signature_filename, content_filename]) |
paul@10 | 290 | |
paul@10 | 291 | # Return the details of the signing key. |
paul@10 | 292 | |
paul@11 | 293 | identity = None |
paul@11 | 294 | fingerprint = None |
paul@11 | 295 | |
paul@10 | 296 | for line in text.split("\n"): |
paul@10 | 297 | try: |
paul@11 | 298 | prefix, msgtype, digest, details = line.strip().split(" ", 3) |
paul@10 | 299 | except ValueError: |
paul@10 | 300 | continue |
paul@10 | 301 | |
paul@10 | 302 | # Return the fingerprint and identity details. |
paul@10 | 303 | |
paul@10 | 304 | if msgtype == "GOODSIG": |
paul@11 | 305 | identity = details |
paul@11 | 306 | elif msgtype == "VALIDSIG": |
paul@11 | 307 | fingerprint = digest |
paul@11 | 308 | |
paul@11 | 309 | if identity and fingerprint: |
paul@11 | 310 | return fingerprint, identity |
paul@10 | 311 | |
paul@10 | 312 | return None |
paul@6 | 313 | |
paul@8 | 314 | finally: |
paul@8 | 315 | os.remove(signature_filename) |
paul@8 | 316 | os.remove(content_filename) |
paul@8 | 317 | |
paul@8 | 318 | def signMessage(self, message, keyid): |
paul@6 | 319 | |
paul@8 | 320 | """ |
paul@8 | 321 | Return a signed version of 'message' using the given 'keyid'. |
paul@8 | 322 | """ |
paul@6 | 323 | |
paul@8 | 324 | text = message.as_string() |
paul@8 | 325 | signature = self.run(["--armor", "-u", keyid, "--detach-sig"], text) |
paul@8 | 326 | |
paul@8 | 327 | # Make the container for the message. |
paul@8 | 328 | |
paul@8 | 329 | signed_message = MIMEMultipart("signed", protocol="application/pgp-signature") |
paul@8 | 330 | signed_message.attach(message) |
paul@6 | 331 | |
paul@8 | 332 | signature_part = MIMEBase("application", "pgp-signature") |
paul@8 | 333 | signature_part.set_payload(signature) |
paul@8 | 334 | signed_message.attach(signature_part) |
paul@8 | 335 | |
paul@8 | 336 | return signed_message |
paul@8 | 337 | |
paul@8 | 338 | def decryptMessage(self, message): |
paul@6 | 339 | |
paul@8 | 340 | "Return a decrypted version of 'message'." |
paul@8 | 341 | |
paul@8 | 342 | return self.run(["--decrypt"], message) |
paul@6 | 343 | |
paul@8 | 344 | def encryptMessage(self, message, keyid): |
paul@6 | 345 | |
paul@8 | 346 | """ |
paul@8 | 347 | Return an encrypted version of 'message' using the given 'keyid'. |
paul@8 | 348 | """ |
paul@6 | 349 | |
paul@8 | 350 | text = message.as_string() |
paul@8 | 351 | encrypted = self.run(["--armor", "-r", keyid, "--encrypt", "--trust-model", "always"], text) |
paul@8 | 352 | |
paul@8 | 353 | # Make the container for the message. |
paul@8 | 354 | |
paul@8 | 355 | encrypted_message = MIMEMultipart("encrypted", protocol="application/pgp-encrypted") |
paul@8 | 356 | |
paul@8 | 357 | # For encrypted content, add the declaration and content. |
paul@6 | 358 | |
paul@8 | 359 | declaration = MIMEBase("application", "pgp-encrypted") |
paul@8 | 360 | declaration.set_payload("Version: 1") |
paul@8 | 361 | encrypted_message.attach(declaration) |
paul@6 | 362 | |
paul@8 | 363 | content = MIMEApplication(encrypted, "octet-stream", encode_noop) |
paul@8 | 364 | encrypted_message.attach(content) |
paul@6 | 365 | |
paul@8 | 366 | return encrypted_message |
paul@8 | 367 | |
paul@8 | 368 | # Communications functions. |
paul@6 | 369 | |
paul@12 | 370 | def sendMessage(message, url): |
paul@6 | 371 | |
paul@12 | 372 | "Send 'message' to the given 'url." |
paul@6 | 373 | |
paul@12 | 374 | scheme, host, port, path = parseURL(url) |
paul@6 | 375 | text = message.as_string() |
paul@6 | 376 | |
paul@12 | 377 | if scheme == "http": |
paul@12 | 378 | cls = httplib.HTTPConnection |
paul@12 | 379 | elif scheme == "https": |
paul@12 | 380 | cls = httplib.HTTPSConnection |
paul@12 | 381 | else: |
paul@12 | 382 | raise MoinMessageError, "Communications protocol not supported: %s" % scheme |
paul@12 | 383 | |
paul@12 | 384 | req = cls(host, port) |
paul@12 | 385 | req.request("PUT", path, text) |
paul@6 | 386 | resp = req.getresponse() |
paul@6 | 387 | return resp.read() |
paul@6 | 388 | |
paul@12 | 389 | def parseURL(url): |
paul@12 | 390 | |
paul@12 | 391 | "Return the scheme, host, port and path for the given 'url'." |
paul@12 | 392 | |
paul@12 | 393 | scheme, host_port, path, query, fragment = urlsplit(url) |
paul@12 | 394 | host_port = host_port.split(":") |
paul@12 | 395 | |
paul@12 | 396 | if query: |
paul@12 | 397 | path += "?" + query |
paul@12 | 398 | |
paul@12 | 399 | if len(host_port) > 1: |
paul@12 | 400 | host = host_port[0] |
paul@12 | 401 | port = int(host_port[1]) |
paul@12 | 402 | else: |
paul@12 | 403 | host = host_port[0] |
paul@12 | 404 | port = 80 |
paul@12 | 405 | |
paul@12 | 406 | return scheme, host, port, path |
paul@12 | 407 | |
paul@6 | 408 | # vim: tabstop=4 expandtab shiftwidth=4 |