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