paul@6 | 1 | # -*- coding: iso-8859-1 -*- |
paul@6 | 2 | """ |
paul@6 | 3 | MoinMoin - MoinMessage library |
paul@6 | 4 | |
paul@98 | 5 | @copyright: 2012, 2013, 2014 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@105 | 11 | from email.generator import Generator |
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@88 | 15 | from email.parser import Parser |
paul@103 | 16 | from email.utils import formatdate |
paul@6 | 17 | from subprocess import Popen, PIPE |
paul@8 | 18 | from tempfile import mkstemp |
paul@12 | 19 | from urlparse import urlsplit |
paul@103 | 20 | from DateSupport import getDateTimeFromRFC2822 |
paul@6 | 21 | import httplib |
paul@8 | 22 | import os |
paul@6 | 23 | |
paul@105 | 24 | try: |
paul@105 | 25 | from cStringIO import StringIO |
paul@105 | 26 | except ImportError: |
paul@105 | 27 | from StringIO import StringIO |
paul@105 | 28 | |
paul@15 | 29 | def is_collection(message): |
paul@15 | 30 | return message.get("Update-Type") == "collection" |
paul@15 | 31 | |
paul@29 | 32 | def to_replace(message): |
paul@29 | 33 | return message.get("Update-Action") == "replace" |
paul@29 | 34 | |
paul@29 | 35 | def to_store(message): |
paul@29 | 36 | return message.get("Update-Action") == "store" |
paul@29 | 37 | |
paul@85 | 38 | def get_update_action(message): |
paul@85 | 39 | return message.get("Update-Action", "update") |
paul@85 | 40 | |
paul@6 | 41 | class Message: |
paul@6 | 42 | |
paul@6 | 43 | "An update message." |
paul@6 | 44 | |
paul@15 | 45 | def __init__(self, text=None): |
paul@20 | 46 | self.date = None |
paul@6 | 47 | self.updates = [] |
paul@15 | 48 | if text: |
paul@15 | 49 | self.parse_text(text) |
paul@15 | 50 | |
paul@20 | 51 | def init_date(self, message): |
paul@20 | 52 | |
paul@20 | 53 | "Obtain the date of the given 'message'." |
paul@20 | 54 | |
paul@20 | 55 | if message.has_key("Date"): |
paul@103 | 56 | self.date = getDateTimeFromRFC2822(message["Date"]) |
paul@20 | 57 | else: |
paul@20 | 58 | self.date = None |
paul@20 | 59 | |
paul@15 | 60 | def parse_text(self, text): |
paul@15 | 61 | |
paul@15 | 62 | "Parse the given 'text' as a message." |
paul@15 | 63 | |
paul@15 | 64 | self.handle_message(message_from_string(text)) |
paul@15 | 65 | |
paul@15 | 66 | def handle_message(self, message): |
paul@15 | 67 | |
paul@15 | 68 | "Handle the given 'message', recording the separate updates." |
paul@15 | 69 | |
paul@20 | 70 | self.init_date(message) |
paul@20 | 71 | |
paul@15 | 72 | # The message either consists of a collection of updates. |
paul@15 | 73 | |
paul@15 | 74 | if message.is_multipart() and is_collection(message): |
paul@15 | 75 | for part in message.get_payload(): |
paul@15 | 76 | self.updates.append(part) |
paul@15 | 77 | |
paul@15 | 78 | # Or the message is a single update. |
paul@15 | 79 | |
paul@15 | 80 | else: |
paul@15 | 81 | self.updates.append(message) |
paul@6 | 82 | |
paul@16 | 83 | def add_updates(self, parts): |
paul@13 | 84 | |
paul@13 | 85 | """ |
paul@16 | 86 | Add the given 'parts' to a message. |
paul@13 | 87 | """ |
paul@13 | 88 | |
paul@16 | 89 | for part in updates: |
paul@16 | 90 | self.add_update(part) |
paul@16 | 91 | |
paul@16 | 92 | def add_update(self, part): |
paul@16 | 93 | |
paul@16 | 94 | """ |
paul@16 | 95 | Add an update 'part' to a message. |
paul@16 | 96 | """ |
paul@16 | 97 | |
paul@16 | 98 | self.updates.append(part) |
paul@16 | 99 | |
paul@16 | 100 | def get_update(self, alternatives): |
paul@16 | 101 | |
paul@16 | 102 | """ |
paul@16 | 103 | Return a suitable multipart object containing the supplied |
paul@16 | 104 | 'alternatives'. |
paul@16 | 105 | """ |
paul@16 | 106 | |
paul@16 | 107 | part = MIMEMultipart() |
paul@16 | 108 | for alternative in alternatives: |
paul@16 | 109 | part.attach(alternative) |
paul@16 | 110 | return part |
paul@6 | 111 | |
paul@20 | 112 | def get_payload(self, timestamped=True): |
paul@13 | 113 | |
paul@20 | 114 | """ |
paul@20 | 115 | Get the multipart payload for the message. If the 'timestamped' |
paul@20 | 116 | parameter is omitted or set to a true value, the payload will be given a |
paul@20 | 117 | date header set to the current date and time that can be used to assess |
paul@20 | 118 | the validity of a message and to determine whether it has already been |
paul@20 | 119 | received by a recipient. |
paul@20 | 120 | """ |
paul@13 | 121 | |
paul@6 | 122 | if len(self.updates) == 1: |
paul@6 | 123 | message = self.updates[0] |
paul@6 | 124 | else: |
paul@6 | 125 | message = MIMEMultipart() |
paul@6 | 126 | message.add_header("Update-Type", "collection") |
paul@6 | 127 | for update in self.updates: |
paul@6 | 128 | message.attach(update) |
paul@6 | 129 | |
paul@20 | 130 | if timestamped: |
paul@20 | 131 | timestamp(message) |
paul@20 | 132 | self.init_date(message) |
paul@20 | 133 | |
paul@6 | 134 | return message |
paul@6 | 135 | |
paul@15 | 136 | class Mailbox: |
paul@15 | 137 | |
paul@15 | 138 | "A collection of messages within a multipart message." |
paul@15 | 139 | |
paul@15 | 140 | def __init__(self, text=None): |
paul@15 | 141 | self.messages = [] |
paul@15 | 142 | if text: |
paul@15 | 143 | self.parse_text(text) |
paul@15 | 144 | |
paul@15 | 145 | def parse_text(self, text): |
paul@15 | 146 | |
paul@15 | 147 | "Parse the given 'text' as a mailbox." |
paul@15 | 148 | |
paul@15 | 149 | message = message_from_string(text) |
paul@15 | 150 | |
paul@15 | 151 | if message.is_multipart(): |
paul@15 | 152 | for part in message.get_payload(): |
paul@15 | 153 | self.messages.append(part) |
paul@15 | 154 | else: |
paul@15 | 155 | self.messages.append(message) |
paul@15 | 156 | |
paul@15 | 157 | def add_message(self, message): |
paul@15 | 158 | |
paul@15 | 159 | "Add the given 'message' to the mailbox." |
paul@15 | 160 | |
paul@15 | 161 | self.messages.append(message) |
paul@15 | 162 | |
paul@15 | 163 | def get_payload(self): |
paul@15 | 164 | |
paul@15 | 165 | "Get the multipart payload for the mailbox." |
paul@15 | 166 | |
paul@15 | 167 | mailbox = MIMEMultipart() |
paul@15 | 168 | for message in self.messages: |
paul@15 | 169 | mailbox.attach(message) |
paul@15 | 170 | |
paul@15 | 171 | return mailbox |
paul@15 | 172 | |
paul@6 | 173 | class MoinMessageError(Exception): |
paul@6 | 174 | pass |
paul@6 | 175 | |
paul@33 | 176 | class MoinMessageDecodingError(Exception): |
paul@33 | 177 | pass |
paul@33 | 178 | |
paul@33 | 179 | class MoinMessageMissingPart(MoinMessageDecodingError): |
paul@33 | 180 | pass |
paul@33 | 181 | |
paul@33 | 182 | class MoinMessageBadContent(MoinMessageDecodingError): |
paul@33 | 183 | pass |
paul@33 | 184 | |
paul@8 | 185 | class GPG: |
paul@8 | 186 | |
paul@8 | 187 | "A wrapper around the gpg command using a particular configuration." |
paul@6 | 188 | |
paul@8 | 189 | def __init__(self, homedir=None): |
paul@8 | 190 | self.conf_args = [] |
paul@6 | 191 | |
paul@8 | 192 | if homedir: |
paul@8 | 193 | self.conf_args += ["--homedir", homedir] |
paul@8 | 194 | |
paul@8 | 195 | self.errors = None |
paul@6 | 196 | |
paul@8 | 197 | def run(self, args, text=None): |
paul@6 | 198 | |
paul@8 | 199 | """ |
paul@8 | 200 | Invoke gpg with the given 'args', supplying the given 'text' to the |
paul@8 | 201 | command directly or, if 'text' is omitted, using a file provided as part |
paul@8 | 202 | of the 'args' if appropriate. |
paul@6 | 203 | |
paul@8 | 204 | Failure to complete the operation will result in a MoinMessageError |
paul@8 | 205 | being raised. |
paul@8 | 206 | """ |
paul@8 | 207 | |
paul@8 | 208 | cmd = Popen(["gpg"] + self.conf_args + list(args), stdin=PIPE, stdout=PIPE, stderr=PIPE) |
paul@6 | 209 | |
paul@58 | 210 | # Attempt to write input to the command and to read output from the |
paul@58 | 211 | # command. |
paul@11 | 212 | |
paul@58 | 213 | text, self.errors = cmd.communicate(text) |
paul@8 | 214 | |
paul@58 | 215 | # Test for a zero result. |
paul@6 | 216 | |
paul@58 | 217 | if not cmd.returncode: |
paul@58 | 218 | return text |
paul@58 | 219 | else: |
paul@58 | 220 | raise MoinMessageError, self.errors |
paul@6 | 221 | |
paul@33 | 222 | def verifyMessageText(self, signature, content): |
paul@8 | 223 | |
paul@8 | 224 | "Using the given 'signature', verify the given message 'content'." |
paul@6 | 225 | |
paul@8 | 226 | # Write the detached signature and content to files. |
paul@8 | 227 | |
paul@8 | 228 | signature_fd, signature_filename = mkstemp() |
paul@8 | 229 | content_fd, content_filename = mkstemp() |
paul@6 | 230 | |
paul@8 | 231 | try: |
paul@8 | 232 | signature_fp = os.fdopen(signature_fd, "w") |
paul@8 | 233 | content_fp = os.fdopen(content_fd, "w") |
paul@8 | 234 | try: |
paul@8 | 235 | signature_fp.write(signature) |
paul@8 | 236 | content_fp.write(content) |
paul@8 | 237 | finally: |
paul@8 | 238 | signature_fp.close() |
paul@8 | 239 | content_fp.close() |
paul@6 | 240 | |
paul@8 | 241 | # Verify the message text. |
paul@6 | 242 | |
paul@10 | 243 | text = self.run(["--status-fd", "1", "--verify", signature_filename, content_filename]) |
paul@10 | 244 | |
paul@10 | 245 | # Return the details of the signing key. |
paul@10 | 246 | |
paul@11 | 247 | identity = None |
paul@11 | 248 | fingerprint = None |
paul@11 | 249 | |
paul@10 | 250 | for line in text.split("\n"): |
paul@10 | 251 | try: |
paul@11 | 252 | prefix, msgtype, digest, details = line.strip().split(" ", 3) |
paul@10 | 253 | except ValueError: |
paul@10 | 254 | continue |
paul@10 | 255 | |
paul@10 | 256 | # Return the fingerprint and identity details. |
paul@10 | 257 | |
paul@10 | 258 | if msgtype == "GOODSIG": |
paul@11 | 259 | identity = details |
paul@11 | 260 | elif msgtype == "VALIDSIG": |
paul@11 | 261 | fingerprint = digest |
paul@11 | 262 | |
paul@11 | 263 | if identity and fingerprint: |
paul@11 | 264 | return fingerprint, identity |
paul@10 | 265 | |
paul@10 | 266 | return None |
paul@6 | 267 | |
paul@8 | 268 | finally: |
paul@8 | 269 | os.remove(signature_filename) |
paul@8 | 270 | os.remove(content_filename) |
paul@8 | 271 | |
paul@33 | 272 | def verifyMessage(self, message): |
paul@33 | 273 | |
paul@33 | 274 | """ |
paul@33 | 275 | Verify the given RFC 3156 'message', returning a tuple of the form |
paul@33 | 276 | (fingerprint, identity, content). |
paul@33 | 277 | """ |
paul@33 | 278 | |
paul@36 | 279 | content, signature = getContentAndSignature(message) |
paul@33 | 280 | |
paul@33 | 281 | # Verify the message format. |
paul@33 | 282 | |
paul@33 | 283 | if signature.get_content_type() != "application/pgp-signature": |
paul@33 | 284 | raise MoinMessageBadContent |
paul@33 | 285 | |
paul@33 | 286 | # Verify the message. |
paul@33 | 287 | |
paul@105 | 288 | fingerprint, identity = self.verifyMessageText(signature.get_payload(decode=True), as_string(content)) |
paul@105 | 289 | return fingerprint, identity, content |
paul@33 | 290 | |
paul@8 | 291 | def signMessage(self, message, keyid): |
paul@6 | 292 | |
paul@8 | 293 | """ |
paul@8 | 294 | Return a signed version of 'message' using the given 'keyid'. |
paul@8 | 295 | """ |
paul@6 | 296 | |
paul@88 | 297 | # Sign the container's representation. |
paul@88 | 298 | |
paul@105 | 299 | signature = self.run(["--armor", "-u", keyid, "--detach-sig"], as_string(message)) |
paul@8 | 300 | |
paul@8 | 301 | # Make the container for the message. |
paul@8 | 302 | |
paul@8 | 303 | signed_message = MIMEMultipart("signed", protocol="application/pgp-signature") |
paul@105 | 304 | signed_message.attach(message) |
paul@6 | 305 | |
paul@8 | 306 | signature_part = MIMEBase("application", "pgp-signature") |
paul@8 | 307 | signature_part.set_payload(signature) |
paul@8 | 308 | signed_message.attach(signature_part) |
paul@8 | 309 | |
paul@8 | 310 | return signed_message |
paul@8 | 311 | |
paul@33 | 312 | def decryptMessageText(self, message): |
paul@6 | 313 | |
paul@8 | 314 | "Return a decrypted version of 'message'." |
paul@8 | 315 | |
paul@8 | 316 | return self.run(["--decrypt"], message) |
paul@6 | 317 | |
paul@33 | 318 | def decryptMessage(self, message): |
paul@33 | 319 | |
paul@33 | 320 | """ |
paul@33 | 321 | Decrypt the given RFC 3156 'message', returning the message text. |
paul@33 | 322 | """ |
paul@33 | 323 | |
paul@33 | 324 | try: |
paul@33 | 325 | declaration, content = message.get_payload() |
paul@33 | 326 | except ValueError: |
paul@33 | 327 | raise MoinMessageMissingPart |
paul@33 | 328 | |
paul@33 | 329 | # Verify the message format. |
paul@33 | 330 | |
paul@33 | 331 | if content.get_content_type() != "application/octet-stream": |
paul@33 | 332 | raise MoinMessageBadContent |
paul@33 | 333 | |
paul@33 | 334 | # Return the decrypted message text. |
paul@33 | 335 | |
paul@92 | 336 | return self.decryptMessageText(content.get_payload(decode=True)) |
paul@33 | 337 | |
paul@8 | 338 | def encryptMessage(self, message, keyid): |
paul@6 | 339 | |
paul@8 | 340 | """ |
paul@8 | 341 | Return an encrypted version of 'message' using the given 'keyid'. |
paul@8 | 342 | """ |
paul@6 | 343 | |
paul@105 | 344 | text = as_string(message) |
paul@8 | 345 | encrypted = self.run(["--armor", "-r", keyid, "--encrypt", "--trust-model", "always"], text) |
paul@8 | 346 | |
paul@8 | 347 | # Make the container for the message. |
paul@8 | 348 | |
paul@8 | 349 | encrypted_message = MIMEMultipart("encrypted", protocol="application/pgp-encrypted") |
paul@8 | 350 | |
paul@8 | 351 | # For encrypted content, add the declaration and content. |
paul@6 | 352 | |
paul@8 | 353 | declaration = MIMEBase("application", "pgp-encrypted") |
paul@8 | 354 | declaration.set_payload("Version: 1") |
paul@8 | 355 | encrypted_message.attach(declaration) |
paul@6 | 356 | |
paul@8 | 357 | content = MIMEApplication(encrypted, "octet-stream", encode_noop) |
paul@8 | 358 | encrypted_message.attach(content) |
paul@6 | 359 | |
paul@8 | 360 | return encrypted_message |
paul@8 | 361 | |
paul@75 | 362 | def exportKey(self, keyid): |
paul@75 | 363 | |
paul@75 | 364 | """ |
paul@75 | 365 | Return the "armoured" public key text for 'keyid' as a message part with |
paul@75 | 366 | a suitable media type. |
paul@75 | 367 | See: https://tools.ietf.org/html/rfc3156#section-7 |
paul@75 | 368 | """ |
paul@75 | 369 | |
paul@75 | 370 | text = self.run(["--armor", "--export", keyid]) |
paul@75 | 371 | return MIMEApplication(text, "pgp-keys", encode_noop) |
paul@75 | 372 | |
paul@78 | 373 | def listKeys(self, keyid=None): |
paul@78 | 374 | |
paul@78 | 375 | """ |
paul@78 | 376 | Return a list of key details for keys on the keychain, selecting only |
paul@78 | 377 | one specific key if 'keyid' is specified. |
paul@78 | 378 | """ |
paul@78 | 379 | |
paul@78 | 380 | text = self.run(["--list-keys", "--with-colons", "--with-fingerprint"] + |
paul@78 | 381 | (keyid and ["0x%s" % keyid] or [])) |
paul@78 | 382 | return self._getKeysFromResult(text) |
paul@78 | 383 | |
paul@78 | 384 | def listSignatures(self, keyid=None): |
paul@78 | 385 | |
paul@78 | 386 | """ |
paul@78 | 387 | Return a list of key and signature details for keys on the keychain, |
paul@78 | 388 | selecting only one specific key if 'keyid' is specified. |
paul@78 | 389 | """ |
paul@78 | 390 | |
paul@78 | 391 | text = self.run(["--list-sigs", "--with-colons", "--with-fingerprint"] + |
paul@78 | 392 | (keyid and ["0x%s" % keyid] or [])) |
paul@78 | 393 | return self._getKeysFromResult(text) |
paul@78 | 394 | |
paul@77 | 395 | def getKeysFromMessagePart(self, part): |
paul@77 | 396 | |
paul@77 | 397 | """ |
paul@77 | 398 | Process an application/pgp-keys message 'part', returning a list of |
paul@77 | 399 | key details. |
paul@77 | 400 | """ |
paul@77 | 401 | |
paul@93 | 402 | return self.getKeysFromString(part.get_payload(decode=True)) |
paul@77 | 403 | |
paul@77 | 404 | def getKeysFromString(self, s): |
paul@77 | 405 | |
paul@78 | 406 | """ |
paul@78 | 407 | Return a list of key details extracted from the given key block string |
paul@79 | 408 | 's'. Signature information is also included through the use of the gpg |
paul@79 | 409 | verbose option. |
paul@78 | 410 | """ |
paul@77 | 411 | |
paul@79 | 412 | text = self.run(["--with-colons", "--with-fingerprint", "-v"], s) |
paul@78 | 413 | return self._getKeysFromResult(text) |
paul@78 | 414 | |
paul@78 | 415 | def _getKeysFromResult(self, text): |
paul@78 | 416 | |
paul@78 | 417 | """ |
paul@78 | 418 | Return a list of key details extracted from the given command result |
paul@78 | 419 | 'text'. |
paul@78 | 420 | """ |
paul@78 | 421 | |
paul@77 | 422 | keys = [] |
paul@77 | 423 | for line in text.split("\n"): |
paul@77 | 424 | try: |
paul@77 | 425 | recordtype, trust, keylength, algorithm, keyid, cdate, expdate, serial, ownertrust, _rest = line.split(":", 9) |
paul@77 | 426 | except ValueError: |
paul@78 | 427 | continue |
paul@77 | 428 | |
paul@77 | 429 | if recordtype == "pub": |
paul@77 | 430 | userid, _rest = _rest.split(":", 1) |
paul@77 | 431 | keys.append({ |
paul@77 | 432 | "type" : recordtype, "trust" : trust, "keylength" : keylength, |
paul@77 | 433 | "algorithm" : algorithm, "keyid" : keyid, "cdate" : cdate, |
paul@77 | 434 | "expdate" : expdate, "userid" : userid, "ownertrust" : ownertrust, |
paul@78 | 435 | "fingerprint" : None, "subkeys" : [], "signatures" : [] |
paul@77 | 436 | }) |
paul@77 | 437 | elif recordtype == "sub" and keys: |
paul@77 | 438 | keys[-1]["subkeys"].append({ |
paul@77 | 439 | "trust" : trust, "keylength" : keylength, "algorithm" : algorithm, |
paul@77 | 440 | "keyid" : keyid, "cdate" : cdate, "expdate" : expdate, |
paul@77 | 441 | "ownertrust" : ownertrust |
paul@77 | 442 | }) |
paul@77 | 443 | elif recordtype == "fpr" and keys: |
paul@77 | 444 | fingerprint, _rest = _rest.split(":", 1) |
paul@77 | 445 | keys[-1]["fingerprint"] = fingerprint |
paul@78 | 446 | elif recordtype == "sig" and keys: |
paul@78 | 447 | userid, _rest = _rest.split(":", 1) |
paul@78 | 448 | keys[-1]["signatures"].append({ |
paul@78 | 449 | "keyid" : keyid, "cdate" : cdate, "expdate" : expdate, |
paul@78 | 450 | "userid" : userid |
paul@78 | 451 | }) |
paul@77 | 452 | |
paul@77 | 453 | return keys |
paul@77 | 454 | |
paul@105 | 455 | # Message serialisation functions, working around email module problems. |
paul@105 | 456 | |
paul@105 | 457 | def as_string(message): |
paul@105 | 458 | |
paul@105 | 459 | """ |
paul@105 | 460 | Return the string representation of 'message', attempting to preserve the |
paul@105 | 461 | precise original formatting. |
paul@105 | 462 | """ |
paul@105 | 463 | |
paul@105 | 464 | out = StringIO() |
paul@105 | 465 | generator = Generator(out, False, 0) # disable reformatting measures |
paul@105 | 466 | generator.flatten(message) |
paul@105 | 467 | return out.getvalue() |
paul@105 | 468 | |
paul@33 | 469 | # Message decoding functions. |
paul@33 | 470 | |
paul@33 | 471 | # Detect PGP/GPG-encoded payloads. |
paul@33 | 472 | # See: http://tools.ietf.org/html/rfc3156 |
paul@33 | 473 | |
paul@33 | 474 | def is_signed(message): |
paul@33 | 475 | mimetype = message.get_content_type() |
paul@33 | 476 | encoding = message.get_content_charset() |
paul@33 | 477 | |
paul@33 | 478 | return mimetype == "multipart/signed" and \ |
paul@33 | 479 | message.get_param("protocol") == "application/pgp-signature" |
paul@33 | 480 | |
paul@33 | 481 | def is_encrypted(message): |
paul@33 | 482 | mimetype = message.get_content_type() |
paul@33 | 483 | encoding = message.get_content_charset() |
paul@33 | 484 | |
paul@33 | 485 | return mimetype == "multipart/encrypted" and \ |
paul@33 | 486 | message.get_param("protocol") == "application/pgp-encrypted" |
paul@33 | 487 | |
paul@36 | 488 | def getContentAndSignature(message): |
paul@36 | 489 | |
paul@36 | 490 | """ |
paul@36 | 491 | Return the content and signature parts of the given RFC 3156 'message'. |
paul@36 | 492 | |
paul@36 | 493 | NOTE: RFC 3156 states that signed messages should employ a detached |
paul@36 | 494 | NOTE: signature but then shows "BEGIN PGP MESSAGE" for signatures |
paul@36 | 495 | NOTE: instead of "BEGIN PGP SIGNATURE". |
paul@36 | 496 | NOTE: The "micalg" parameter is currently not supported. |
paul@36 | 497 | """ |
paul@36 | 498 | |
paul@36 | 499 | try: |
paul@36 | 500 | content, signature = message.get_payload() |
paul@36 | 501 | return content, signature |
paul@36 | 502 | except ValueError: |
paul@36 | 503 | raise MoinMessageMissingPart |
paul@36 | 504 | |
paul@8 | 505 | # Communications functions. |
paul@6 | 506 | |
paul@20 | 507 | def timestamp(message): |
paul@20 | 508 | |
paul@20 | 509 | """ |
paul@20 | 510 | Timestamp the given 'message' so that its validity can be assessed by the |
paul@20 | 511 | recipient. |
paul@20 | 512 | """ |
paul@20 | 513 | |
paul@20 | 514 | datestr = formatdate() |
paul@20 | 515 | |
paul@20 | 516 | if not message.has_key("Date"): |
paul@20 | 517 | message.add_header("Date", datestr) |
paul@20 | 518 | else: |
paul@20 | 519 | message["Date"] = datestr |
paul@20 | 520 | |
paul@92 | 521 | def _getConnection(scheme): |
paul@92 | 522 | |
paul@92 | 523 | "Return the connection class for the given 'scheme'." |
paul@92 | 524 | |
paul@92 | 525 | if scheme == "http": |
paul@92 | 526 | return httplib.HTTPConnection |
paul@92 | 527 | elif scheme == "https": |
paul@92 | 528 | return httplib.HTTPSConnection |
paul@92 | 529 | else: |
paul@92 | 530 | raise MoinMessageError, "Communications protocol not supported: %s" % scheme |
paul@92 | 531 | |
paul@92 | 532 | def sendMessageForReading(message, url, method="PUT"): |
paul@6 | 533 | |
paul@44 | 534 | """ |
paul@44 | 535 | Send 'message' to the given 'url' using the given 'method' (using PUT as the |
paul@44 | 536 | default if omitted). |
paul@44 | 537 | """ |
paul@6 | 538 | |
paul@12 | 539 | scheme, host, port, path = parseURL(url) |
paul@105 | 540 | text = as_string(message) |
paul@6 | 541 | |
paul@92 | 542 | req = _getConnection(scheme)(host, port) |
paul@44 | 543 | req.request(method, path, text) |
paul@6 | 544 | resp = req.getresponse() |
paul@39 | 545 | |
paul@39 | 546 | if resp.status >= 400: |
paul@92 | 547 | raise MoinMessageError, "Message sending failed (%s): %s" % (resp.status, resp.read()) |
paul@92 | 548 | |
paul@92 | 549 | return resp |
paul@92 | 550 | |
paul@92 | 551 | def sendMessage(message, url, method="PUT"): |
paul@39 | 552 | |
paul@92 | 553 | """ |
paul@92 | 554 | Send 'message' to the given 'url' using the given 'method' (using PUT as the |
paul@92 | 555 | default if omitted). |
paul@92 | 556 | """ |
paul@92 | 557 | |
paul@92 | 558 | resp = sendMessageForReading(message, url, method) |
paul@6 | 559 | return resp.read() |
paul@6 | 560 | |
paul@12 | 561 | def parseURL(url): |
paul@12 | 562 | |
paul@12 | 563 | "Return the scheme, host, port and path for the given 'url'." |
paul@12 | 564 | |
paul@12 | 565 | scheme, host_port, path, query, fragment = urlsplit(url) |
paul@12 | 566 | host_port = host_port.split(":") |
paul@12 | 567 | |
paul@12 | 568 | if query: |
paul@12 | 569 | path += "?" + query |
paul@12 | 570 | |
paul@12 | 571 | if len(host_port) > 1: |
paul@12 | 572 | host = host_port[0] |
paul@12 | 573 | port = int(host_port[1]) |
paul@12 | 574 | else: |
paul@12 | 575 | host = host_port[0] |
paul@12 | 576 | port = 80 |
paul@12 | 577 | |
paul@12 | 578 | return scheme, host, port, path |
paul@12 | 579 | |
paul@6 | 580 | # vim: tabstop=4 expandtab shiftwidth=4 |