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 email.mime.text import MIMEText |
paul@6 | 15 | from subprocess import Popen, PIPE |
paul@8 | 16 | from tempfile import mkstemp |
paul@6 | 17 | import httplib |
paul@8 | 18 | import os |
paul@6 | 19 | |
paul@6 | 20 | class Message: |
paul@6 | 21 | |
paul@6 | 22 | "An update message." |
paul@6 | 23 | |
paul@6 | 24 | def __init__(self): |
paul@6 | 25 | self.updates = [] |
paul@6 | 26 | |
paul@6 | 27 | def add_update(self, alternatives): |
paul@6 | 28 | if len(alternatives) > 1: |
paul@6 | 29 | part = MIMEMultipart() |
paul@6 | 30 | for alternative in alternatives: |
paul@6 | 31 | part.attach(alternative) |
paul@6 | 32 | self.updates.append(part) |
paul@6 | 33 | else: |
paul@6 | 34 | self.updates.append(alternatives[0]) |
paul@6 | 35 | |
paul@6 | 36 | def get_payload(self): |
paul@6 | 37 | if len(self.updates) == 1: |
paul@6 | 38 | message = self.updates[0] |
paul@6 | 39 | else: |
paul@6 | 40 | message = MIMEMultipart() |
paul@6 | 41 | message.add_header("Update-Type", "collection") |
paul@6 | 42 | for update in self.updates: |
paul@6 | 43 | message.attach(update) |
paul@6 | 44 | |
paul@6 | 45 | return message |
paul@6 | 46 | |
paul@6 | 47 | class MoinMessageError(Exception): |
paul@6 | 48 | pass |
paul@6 | 49 | |
paul@8 | 50 | class GPG: |
paul@8 | 51 | |
paul@8 | 52 | "A wrapper around the gpg command using a particular configuration." |
paul@6 | 53 | |
paul@8 | 54 | def __init__(self, homedir=None): |
paul@8 | 55 | self.conf_args = [] |
paul@6 | 56 | |
paul@8 | 57 | if homedir: |
paul@8 | 58 | self.conf_args += ["--homedir", homedir] |
paul@8 | 59 | |
paul@8 | 60 | self.errors = None |
paul@6 | 61 | |
paul@8 | 62 | def run(self, args, text=None): |
paul@6 | 63 | |
paul@8 | 64 | """ |
paul@8 | 65 | Invoke gpg with the given 'args', supplying the given 'text' to the |
paul@8 | 66 | command directly or, if 'text' is omitted, using a file provided as part |
paul@8 | 67 | of the 'args' if appropriate. |
paul@6 | 68 | |
paul@8 | 69 | Failure to complete the operation will result in a MoinMessageError |
paul@8 | 70 | being raised. |
paul@8 | 71 | """ |
paul@8 | 72 | |
paul@8 | 73 | cmd = Popen(["gpg"] + self.conf_args + list(args), stdin=PIPE, stdout=PIPE, stderr=PIPE) |
paul@6 | 74 | |
paul@8 | 75 | if text: |
paul@8 | 76 | cmd.stdin.write(text) |
paul@8 | 77 | cmd.stdin.close() |
paul@6 | 78 | |
paul@8 | 79 | self.errors = cmd.stderr.read() |
paul@8 | 80 | |
paul@8 | 81 | try: |
paul@8 | 82 | text = cmd.stdout.read() |
paul@8 | 83 | |
paul@8 | 84 | # Test for a zero result. |
paul@6 | 85 | |
paul@8 | 86 | if not cmd.wait(): |
paul@8 | 87 | return text |
paul@8 | 88 | else: |
paul@8 | 89 | raise MoinMessageError, errors |
paul@8 | 90 | |
paul@8 | 91 | finally: |
paul@8 | 92 | cmd.stdout.close() |
paul@8 | 93 | cmd.stderr.close() |
paul@6 | 94 | |
paul@8 | 95 | def verifyMessage(self, signature, content): |
paul@8 | 96 | |
paul@8 | 97 | "Using the given 'signature', verify the given message 'content'." |
paul@6 | 98 | |
paul@8 | 99 | # Write the detached signature and content to files. |
paul@8 | 100 | |
paul@8 | 101 | signature_fd, signature_filename = mkstemp() |
paul@8 | 102 | content_fd, content_filename = mkstemp() |
paul@6 | 103 | |
paul@8 | 104 | try: |
paul@8 | 105 | signature_fp = os.fdopen(signature_fd, "w") |
paul@8 | 106 | content_fp = os.fdopen(content_fd, "w") |
paul@8 | 107 | try: |
paul@8 | 108 | signature_fp.write(signature) |
paul@8 | 109 | content_fp.write(content) |
paul@8 | 110 | finally: |
paul@8 | 111 | signature_fp.close() |
paul@8 | 112 | content_fp.close() |
paul@6 | 113 | |
paul@8 | 114 | # Verify the message text. |
paul@6 | 115 | |
paul@8 | 116 | self.run(["--verify", signature_filename, content_filename]) |
paul@6 | 117 | |
paul@8 | 118 | finally: |
paul@8 | 119 | os.remove(signature_filename) |
paul@8 | 120 | os.remove(content_filename) |
paul@8 | 121 | |
paul@8 | 122 | def signMessage(self, message, keyid): |
paul@6 | 123 | |
paul@8 | 124 | """ |
paul@8 | 125 | Return a signed version of 'message' using the given 'keyid'. |
paul@8 | 126 | """ |
paul@6 | 127 | |
paul@8 | 128 | text = message.as_string() |
paul@8 | 129 | signature = self.run(["--armor", "-u", keyid, "--detach-sig"], text) |
paul@8 | 130 | |
paul@8 | 131 | # Make the container for the message. |
paul@8 | 132 | |
paul@8 | 133 | signed_message = MIMEMultipart("signed", protocol="application/pgp-signature") |
paul@8 | 134 | signed_message.attach(message) |
paul@6 | 135 | |
paul@8 | 136 | signature_part = MIMEBase("application", "pgp-signature") |
paul@8 | 137 | signature_part.set_payload(signature) |
paul@8 | 138 | signed_message.attach(signature_part) |
paul@8 | 139 | |
paul@8 | 140 | return signed_message |
paul@8 | 141 | |
paul@8 | 142 | def decryptMessage(self, message): |
paul@6 | 143 | |
paul@8 | 144 | "Return a decrypted version of 'message'." |
paul@8 | 145 | |
paul@8 | 146 | return self.run(["--decrypt"], message) |
paul@6 | 147 | |
paul@8 | 148 | def encryptMessage(self, message, keyid): |
paul@6 | 149 | |
paul@8 | 150 | """ |
paul@8 | 151 | Return an encrypted version of 'message' using the given 'keyid'. |
paul@8 | 152 | """ |
paul@6 | 153 | |
paul@8 | 154 | text = message.as_string() |
paul@8 | 155 | encrypted = self.run(["--armor", "-r", keyid, "--encrypt", "--trust-model", "always"], text) |
paul@8 | 156 | |
paul@8 | 157 | # Make the container for the message. |
paul@8 | 158 | |
paul@8 | 159 | encrypted_message = MIMEMultipart("encrypted", protocol="application/pgp-encrypted") |
paul@8 | 160 | |
paul@8 | 161 | # For encrypted content, add the declaration and content. |
paul@6 | 162 | |
paul@8 | 163 | declaration = MIMEBase("application", "pgp-encrypted") |
paul@8 | 164 | declaration.set_payload("Version: 1") |
paul@8 | 165 | encrypted_message.attach(declaration) |
paul@6 | 166 | |
paul@8 | 167 | content = MIMEApplication(encrypted, "octet-stream", encode_noop) |
paul@8 | 168 | encrypted_message.attach(content) |
paul@6 | 169 | |
paul@8 | 170 | return encrypted_message |
paul@8 | 171 | |
paul@8 | 172 | # Communications functions. |
paul@6 | 173 | |
paul@6 | 174 | def sendMessage(message, host, path): |
paul@6 | 175 | |
paul@6 | 176 | "Send 'message' to the given 'host' using the specified URL 'path'." |
paul@6 | 177 | |
paul@6 | 178 | text = message.as_string() |
paul@6 | 179 | |
paul@6 | 180 | req = httplib.HTTPConnection(host) |
paul@6 | 181 | req.request("PUT", path, text) # {"Content-Length" : len(text)} |
paul@6 | 182 | resp = req.getresponse() |
paul@6 | 183 | return resp.read() |
paul@6 | 184 | |
paul@6 | 185 | # vim: tabstop=4 expandtab shiftwidth=4 |