paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - PostMessage Action |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2012 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@0 | 9 | from MoinMoin.PageEditor import PageEditor |
paul@2 | 10 | from MoinMoin.log import getLogger |
paul@0 | 11 | from MoinSupport import * |
paul@8 | 12 | from MoinMessage import GPG, MoinMessageError |
paul@0 | 13 | from email.parser import Parser |
paul@0 | 14 | |
paul@0 | 15 | try: |
paul@0 | 16 | from cStringIO import StringIO |
paul@0 | 17 | except ImportError: |
paul@0 | 18 | from StringIO import StringIO |
paul@0 | 19 | |
paul@0 | 20 | Dependencies = ['pages'] |
paul@0 | 21 | |
paul@0 | 22 | class PostMessage: |
paul@0 | 23 | |
paul@0 | 24 | "A posted message handler." |
paul@0 | 25 | |
paul@0 | 26 | def __init__(self, pagename, request): |
paul@0 | 27 | |
paul@0 | 28 | """ |
paul@0 | 29 | On the page with the given 'pagename', use the given 'request' when |
paul@0 | 30 | reading posted messages, modifying the Wiki. |
paul@0 | 31 | """ |
paul@0 | 32 | |
paul@0 | 33 | self.pagename = pagename |
paul@0 | 34 | self.request = request |
paul@0 | 35 | self.page = Page(request, pagename) |
paul@0 | 36 | |
paul@0 | 37 | def do_action(self): |
paul@0 | 38 | request = self.request |
paul@0 | 39 | content_length = getHeader(request, "Content-Length", "HTTP") |
paul@0 | 40 | if content_length: |
paul@0 | 41 | content_length = int(content_length) |
paul@0 | 42 | |
paul@5 | 43 | self.handle_message_text(request.read(content_length)) |
paul@0 | 44 | |
paul@5 | 45 | def handle_message_text(self, message_text): |
paul@1 | 46 | |
paul@1 | 47 | "Handle the given 'message_text'." |
paul@1 | 48 | |
paul@5 | 49 | message = Parser().parse(StringIO(message_text)) |
paul@5 | 50 | self.handle_message(message) |
paul@5 | 51 | |
paul@5 | 52 | def handle_message(self, message): |
paul@5 | 53 | |
paul@5 | 54 | "Handle the given 'message'." |
paul@5 | 55 | |
paul@1 | 56 | request = self.request |
paul@1 | 57 | mimetype = message.get_content_type() |
paul@1 | 58 | encoding = message.get_content_charset() |
paul@1 | 59 | |
paul@1 | 60 | # Detect PGP/GPG-encoded payloads. |
paul@1 | 61 | # See: http://tools.ietf.org/html/rfc3156 |
paul@1 | 62 | |
paul@3 | 63 | if mimetype == "multipart/signed" and \ |
paul@3 | 64 | message.get_param("protocol") == "application/pgp-signature": |
paul@1 | 65 | |
paul@5 | 66 | self.handle_signed_message(message) |
paul@2 | 67 | |
paul@5 | 68 | elif mimetype == "multipart/encrypted" and \ |
paul@5 | 69 | message.get_param("protocol") == "application/pgp-encrypted": |
paul@2 | 70 | |
paul@5 | 71 | self.handle_encrypted_message(message) |
paul@1 | 72 | |
paul@1 | 73 | # Reject unsigned payloads. |
paul@1 | 74 | |
paul@1 | 75 | else: |
paul@1 | 76 | writeHeaders(request, "text/plain", getMetadata(self.page), "415 Unsupported Media Type") |
paul@1 | 77 | request.write("Only PGP/GPG-signed payloads are supported.") |
paul@1 | 78 | |
paul@5 | 79 | def handle_encrypted_message(self, message): |
paul@5 | 80 | |
paul@5 | 81 | "Handle the given encrypted 'message'." |
paul@5 | 82 | |
paul@5 | 83 | request = self.request |
paul@5 | 84 | |
paul@5 | 85 | try: |
paul@5 | 86 | declaration, content = message.get_payload() |
paul@5 | 87 | except ValueError: |
paul@5 | 88 | writeHeaders(request, "text/plain", getMetadata(self.page), "415 Unsupported Media Type") |
paul@5 | 89 | request.write("There must be a declaration and a content part for encrypted uploads.") |
paul@5 | 90 | return |
paul@5 | 91 | |
paul@5 | 92 | # Verify the message format. |
paul@5 | 93 | |
paul@5 | 94 | if content.get_content_type() != "application/octet-stream": |
paul@5 | 95 | writeHeaders(request, "text/plain", getMetadata(self.page), "415 Unsupported Media Type") |
paul@5 | 96 | request.write("Encrypted data must be provided as application/octet-stream.") |
paul@5 | 97 | return |
paul@5 | 98 | |
paul@5 | 99 | homedir = self.get_homedir() |
paul@5 | 100 | if not homedir: |
paul@5 | 101 | return |
paul@5 | 102 | |
paul@8 | 103 | gpg = GPG(homedir) |
paul@1 | 104 | |
paul@8 | 105 | # Get the decrypted message text. |
paul@5 | 106 | |
paul@5 | 107 | try: |
paul@8 | 108 | text = gpg.decryptMessage(content.get_payload()) |
paul@5 | 109 | |
paul@8 | 110 | # Log non-fatal errors. |
paul@5 | 111 | |
paul@8 | 112 | if gpg.errors: |
paul@8 | 113 | getLogger(__name__).warning(gpg.errors) |
paul@5 | 114 | |
paul@8 | 115 | # Handle the embedded message. |
paul@8 | 116 | |
paul@8 | 117 | self.handle_message_text(text) |
paul@5 | 118 | |
paul@8 | 119 | # Otherwise, reject the unverified message. |
paul@5 | 120 | |
paul@8 | 121 | except MoinMessageError: |
paul@8 | 122 | writeHeaders(request, "text/plain", getMetadata(self.page), "403 Forbidden") |
paul@8 | 123 | request.write("The message could not be decrypted.") |
paul@5 | 124 | |
paul@5 | 125 | def handle_signed_message(self, message): |
paul@5 | 126 | |
paul@5 | 127 | "Handle the given signed 'message'." |
paul@5 | 128 | |
paul@5 | 129 | request = self.request |
paul@1 | 130 | |
paul@5 | 131 | # NOTE: RFC 3156 states that signed messages should employ a detached |
paul@5 | 132 | # NOTE: signature but then shows "BEGIN PGP MESSAGE" for signatures |
paul@5 | 133 | # NOTE: instead of "BEGIN PGP SIGNATURE". |
paul@5 | 134 | # NOTE: The "micalg" parameter is currently not supported. |
paul@5 | 135 | |
paul@5 | 136 | try: |
paul@5 | 137 | content, signature = message.get_payload() |
paul@5 | 138 | except ValueError: |
paul@5 | 139 | writeHeaders(request, "text/plain", getMetadata(self.page), "415 Unsupported Media Type") |
paul@5 | 140 | request.write("There must be a content part and a signature for signed uploads.") |
paul@5 | 141 | return |
paul@5 | 142 | |
paul@5 | 143 | # Verify the message format. |
paul@5 | 144 | |
paul@5 | 145 | if signature.get_content_type() != "application/pgp-signature": |
paul@5 | 146 | writeHeaders(request, "text/plain", getMetadata(self.page), "415 Unsupported Media Type") |
paul@5 | 147 | request.write("Signature data must be provided in the second part as application/pgp-signature.") |
paul@5 | 148 | return |
paul@5 | 149 | |
paul@5 | 150 | homedir = self.get_homedir() |
paul@5 | 151 | if not homedir: |
paul@5 | 152 | return |
paul@5 | 153 | |
paul@8 | 154 | gpg = GPG(homedir) |
paul@3 | 155 | |
paul@8 | 156 | # Verify the message. |
paul@8 | 157 | |
paul@5 | 158 | try: |
paul@8 | 159 | gpg.verifyMessage(signature.get_payload(), content.as_string()) |
paul@5 | 160 | |
paul@8 | 161 | # Log non-fatal errors. |
paul@5 | 162 | |
paul@8 | 163 | if gpg.errors: |
paul@8 | 164 | getLogger(__name__).warning(gpg.errors) |
paul@5 | 165 | |
paul@5 | 166 | # Handle the embedded message. |
paul@3 | 167 | |
paul@8 | 168 | self.handle_message_content(content) |
paul@5 | 169 | |
paul@8 | 170 | # Otherwise, reject the unverified message. |
paul@5 | 171 | |
paul@8 | 172 | except MoinMessageError: |
paul@8 | 173 | writeHeaders(request, "text/plain", getMetadata(self.page), "403 Forbidden") |
paul@8 | 174 | request.write("The message could not be verified.") |
paul@5 | 175 | |
paul@5 | 176 | def handle_message_content(self, message): |
paul@5 | 177 | |
paul@5 | 178 | "Handle the given 'message'." |
paul@3 | 179 | |
paul@1 | 180 | request = self.request |
paul@0 | 181 | |
paul@0 | 182 | # Handle a single part. |
paul@0 | 183 | |
paul@0 | 184 | if not message.is_multipart(): |
paul@0 | 185 | self.handle_message_parts([message], to_replace(message)) |
paul@0 | 186 | |
paul@0 | 187 | # Handle multiple parts. |
paul@0 | 188 | |
paul@0 | 189 | # This can be a collection of updates, with each update potentially being a |
paul@0 | 190 | # collection of alternative representations. |
paul@0 | 191 | |
paul@0 | 192 | elif is_collection(message): |
paul@0 | 193 | for part in message.get_payload(): |
paul@0 | 194 | if part.is_multipart(): |
paul@0 | 195 | self.handle_message_parts(part.get_payload(), to_replace(part)) |
paul@0 | 196 | else: |
paul@0 | 197 | self.handle_message_parts([part], to_replace(part)) |
paul@0 | 198 | |
paul@0 | 199 | # Or it can be a collection of alternative representations for a single |
paul@0 | 200 | # update. |
paul@0 | 201 | |
paul@0 | 202 | else: |
paul@0 | 203 | self.handle_message_parts(message.get_payload(), to_replace(message)) |
paul@0 | 204 | |
paul@0 | 205 | # Default output. |
paul@0 | 206 | |
paul@0 | 207 | writeHeaders(request, "text/plain", getMetadata(self.page), "204 No Content") |
paul@0 | 208 | |
paul@0 | 209 | def handle_message_parts(self, parts, replace): |
paul@0 | 210 | |
paul@0 | 211 | """ |
paul@0 | 212 | Handle the given message 'parts', replacing the page content if |
paul@0 | 213 | 'replace' is set to a true value. |
paul@0 | 214 | """ |
paul@0 | 215 | |
paul@0 | 216 | # NOTE: Should either choose preferred content types or somehow retain them |
paul@0 | 217 | # NOTE: all but present one at a time. |
paul@0 | 218 | |
paul@0 | 219 | body = [] |
paul@0 | 220 | |
paul@0 | 221 | for part in parts: |
paul@1 | 222 | mimetype = part.get_content_type() |
paul@1 | 223 | encoding = part.get_content_charset() |
paul@0 | 224 | if mimetype == "text/moin": |
paul@0 | 225 | body.append(part.get_payload()) |
paul@0 | 226 | if replace: |
paul@0 | 227 | break |
paul@0 | 228 | |
paul@0 | 229 | if not replace: |
paul@0 | 230 | body.append(self.page.get_raw_body()) |
paul@0 | 231 | |
paul@0 | 232 | page_editor = PageEditor(self.request, self.pagename) |
paul@0 | 233 | page_editor.saveText("\n\n".join(body), 0) |
paul@0 | 234 | |
paul@5 | 235 | def get_homedir(self): |
paul@5 | 236 | |
paul@5 | 237 | "Locate the GPG home directory." |
paul@5 | 238 | |
paul@5 | 239 | homedir = getattr(self.request.cfg, "postmessage_gpg_homedir") |
paul@5 | 240 | if not homedir: |
paul@5 | 241 | writeHeaders(request, "text/plain", getMetadata(self.page), "415 Unsupported Media Type") |
paul@5 | 242 | request.write("Encoded data cannot currently be understood. Please notify the site administrator.") |
paul@5 | 243 | return homedir |
paul@5 | 244 | |
paul@0 | 245 | def is_collection(message): |
paul@2 | 246 | return message.get("Update-Type") == "collection" |
paul@0 | 247 | |
paul@0 | 248 | def to_replace(message): |
paul@0 | 249 | return message.get("Update-Action") == "replace" |
paul@0 | 250 | |
paul@0 | 251 | # Action function. |
paul@0 | 252 | |
paul@0 | 253 | def execute(pagename, request): |
paul@0 | 254 | PostMessage(pagename, request).do_action() |
paul@0 | 255 | |
paul@0 | 256 | # vim: tabstop=4 expandtab shiftwidth=4 |