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@0 | 10 | from MoinSupport import * |
paul@0 | 11 | from email.parser import Parser |
paul@1 | 12 | import os |
paul@0 | 13 | |
paul@0 | 14 | try: |
paul@0 | 15 | from cStringIO import StringIO |
paul@0 | 16 | except ImportError: |
paul@0 | 17 | from StringIO import StringIO |
paul@0 | 18 | |
paul@0 | 19 | Dependencies = ['pages'] |
paul@0 | 20 | |
paul@0 | 21 | class PostMessage: |
paul@0 | 22 | |
paul@0 | 23 | "A posted message handler." |
paul@0 | 24 | |
paul@0 | 25 | def __init__(self, pagename, request): |
paul@0 | 26 | |
paul@0 | 27 | """ |
paul@0 | 28 | On the page with the given 'pagename', use the given 'request' when |
paul@0 | 29 | reading posted messages, modifying the Wiki. |
paul@0 | 30 | """ |
paul@0 | 31 | |
paul@0 | 32 | self.pagename = pagename |
paul@0 | 33 | self.request = request |
paul@0 | 34 | self.page = Page(request, pagename) |
paul@0 | 35 | |
paul@0 | 36 | def do_action(self): |
paul@0 | 37 | request = self.request |
paul@0 | 38 | content_length = getHeader(request, "Content-Length", "HTTP") |
paul@0 | 39 | if content_length: |
paul@0 | 40 | content_length = int(content_length) |
paul@0 | 41 | |
paul@0 | 42 | # Get the message. |
paul@0 | 43 | |
paul@0 | 44 | message_text = StringIO(request.read(content_length)) |
paul@1 | 45 | self.handle_message(message_text) |
paul@1 | 46 | |
paul@1 | 47 | def handle_message(self, message_text): |
paul@1 | 48 | |
paul@1 | 49 | "Handle the given 'message_text'." |
paul@1 | 50 | |
paul@1 | 51 | request = self.request |
paul@1 | 52 | message = Parser().parse(message_text) |
paul@1 | 53 | mimetype = message.get_content_type() |
paul@1 | 54 | encoding = message.get_content_charset() |
paul@1 | 55 | |
paul@1 | 56 | # Detect PGP/GPG-encoded payloads. |
paul@1 | 57 | # See: http://tools.ietf.org/html/rfc3156 |
paul@1 | 58 | |
paul@1 | 59 | if mimetype == "multipart/encrypted" and \ |
paul@1 | 60 | message.get_param("protocol") == "application/pgp-encrypted": |
paul@1 | 61 | |
paul@1 | 62 | try: |
paul@1 | 63 | declaration, part = message.get_payload() |
paul@1 | 64 | except ValueError: |
paul@1 | 65 | writeHeaders(request, "text/plain", getMetadata(self.page), "415 Unsupported Media Type") |
paul@1 | 66 | request.write("There must be a declaration and a content part for signed uploads.") |
paul@1 | 67 | return |
paul@1 | 68 | |
paul@1 | 69 | # Verify the message format. |
paul@1 | 70 | |
paul@1 | 71 | if part.get_content_type() != "application/octet-stream": |
paul@1 | 72 | writeHeaders(request, "text/plain", getMetadata(self.page), "415 Unsupported Media Type") |
paul@1 | 73 | request.write("Encrypted data must be provided as application/octet-stream.") |
paul@1 | 74 | return |
paul@1 | 75 | |
paul@1 | 76 | # Locate the keyring. |
paul@1 | 77 | |
paul@1 | 78 | homedir = getattr(request.cfg, "postmessage_gpg_homedir") |
paul@1 | 79 | if not homedir: |
paul@1 | 80 | writeHeaders(request, "text/plain", getMetadata(self.page), "415 Unsupported Media Type") |
paul@1 | 81 | request.write("Encrypted data cannot currently be understood. Please notify the site administrator.") |
paul@1 | 82 | return |
paul@1 | 83 | |
paul@1 | 84 | # Decrypt the message text. |
paul@1 | 85 | |
paul@1 | 86 | to_child, from_child, error_child = os.popen3(["gpg", "--no-default-keyring", "--homedir", homedir, "--decrypt"]) |
paul@1 | 87 | to_child.write(part.get_payload()) |
paul@1 | 88 | to_child.close() |
paul@1 | 89 | #print >>open("/tmp/log.txt", "a"), error_child.read() |
paul@1 | 90 | |
paul@1 | 91 | # Handle the embedded message. |
paul@1 | 92 | |
paul@1 | 93 | try: |
paul@1 | 94 | self.handle_plaintext_message(from_child) |
paul@1 | 95 | finally: |
paul@1 | 96 | from_child.close() |
paul@1 | 97 | error_child.close() |
paul@1 | 98 | |
paul@1 | 99 | # Reject unsigned payloads. |
paul@1 | 100 | |
paul@1 | 101 | else: |
paul@1 | 102 | writeHeaders(request, "text/plain", getMetadata(self.page), "415 Unsupported Media Type") |
paul@1 | 103 | request.write("Only PGP/GPG-signed payloads are supported.") |
paul@1 | 104 | |
paul@1 | 105 | def handle_plaintext_message(self, message_text): |
paul@1 | 106 | |
paul@1 | 107 | "Handle the given 'message_text'." |
paul@1 | 108 | |
paul@1 | 109 | request = self.request |
paul@0 | 110 | message = Parser().parse(message_text) |
paul@0 | 111 | |
paul@0 | 112 | # Handle a single part. |
paul@0 | 113 | |
paul@0 | 114 | if not message.is_multipart(): |
paul@0 | 115 | self.handle_message_parts([message], to_replace(message)) |
paul@0 | 116 | |
paul@0 | 117 | # Handle multiple parts. |
paul@0 | 118 | |
paul@0 | 119 | # This can be a collection of updates, with each update potentially being a |
paul@0 | 120 | # collection of alternative representations. |
paul@0 | 121 | |
paul@0 | 122 | elif is_collection(message): |
paul@0 | 123 | for part in message.get_payload(): |
paul@0 | 124 | if part.is_multipart(): |
paul@0 | 125 | self.handle_message_parts(part.get_payload(), to_replace(part)) |
paul@0 | 126 | else: |
paul@0 | 127 | self.handle_message_parts([part], to_replace(part)) |
paul@0 | 128 | |
paul@0 | 129 | # Or it can be a collection of alternative representations for a single |
paul@0 | 130 | # update. |
paul@0 | 131 | |
paul@0 | 132 | else: |
paul@0 | 133 | self.handle_message_parts(message.get_payload(), to_replace(message)) |
paul@0 | 134 | |
paul@0 | 135 | # Default output. |
paul@0 | 136 | |
paul@0 | 137 | writeHeaders(request, "text/plain", getMetadata(self.page), "204 No Content") |
paul@0 | 138 | |
paul@0 | 139 | def handle_message_parts(self, parts, replace): |
paul@0 | 140 | |
paul@0 | 141 | """ |
paul@0 | 142 | Handle the given message 'parts', replacing the page content if |
paul@0 | 143 | 'replace' is set to a true value. |
paul@0 | 144 | """ |
paul@0 | 145 | |
paul@0 | 146 | # NOTE: Should either choose preferred content types or somehow retain them |
paul@0 | 147 | # NOTE: all but present one at a time. |
paul@0 | 148 | |
paul@0 | 149 | body = [] |
paul@0 | 150 | |
paul@0 | 151 | for part in parts: |
paul@1 | 152 | mimetype = part.get_content_type() |
paul@1 | 153 | encoding = part.get_content_charset() |
paul@0 | 154 | if mimetype == "text/moin": |
paul@0 | 155 | body.append(part.get_payload()) |
paul@0 | 156 | if replace: |
paul@0 | 157 | break |
paul@0 | 158 | |
paul@0 | 159 | if not replace: |
paul@0 | 160 | body.append(self.page.get_raw_body()) |
paul@0 | 161 | |
paul@0 | 162 | page_editor = PageEditor(self.request, self.pagename) |
paul@0 | 163 | page_editor.saveText("\n\n".join(body), 0) |
paul@0 | 164 | |
paul@0 | 165 | def is_collection(message): |
paul@0 | 166 | return message.get("Update-Type") == "Collection" |
paul@0 | 167 | |
paul@0 | 168 | def to_replace(message): |
paul@0 | 169 | return message.get("Update-Action") == "replace" |
paul@0 | 170 | |
paul@0 | 171 | # Action function. |
paul@0 | 172 | |
paul@0 | 173 | def execute(pagename, request): |
paul@0 | 174 | PostMessage(pagename, request).do_action() |
paul@0 | 175 | |
paul@0 | 176 | # vim: tabstop=4 expandtab shiftwidth=4 |