paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - PostMessage Action |
paul@0 | 4 | |
paul@105 | 5 | @copyright: 2012, 2013, 2014 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@16 | 9 | from MoinMoin.Page import Page |
paul@0 | 10 | from MoinMoin.PageEditor import PageEditor |
paul@29 | 11 | from MoinSupport import getMetadata, writeHeaders |
paul@105 | 12 | from MoinMessage import is_collection, to_replace, to_store, get_update_action, \ |
paul@105 | 13 | as_string |
paul@29 | 14 | from MoinMessageSupport import MoinMessageAction |
paul@0 | 15 | |
paul@0 | 16 | Dependencies = ['pages'] |
paul@0 | 17 | |
paul@29 | 18 | class PostMessage(MoinMessageAction): |
paul@0 | 19 | |
paul@0 | 20 | "A posted message handler." |
paul@0 | 21 | |
paul@30 | 22 | def handle_message_object(self, message): |
paul@125 | 23 | self.do_as_user(self.new_user, self._handle_message_object, [message]) |
paul@125 | 24 | |
paul@125 | 25 | def _handle_message_object(self, message): |
paul@5 | 26 | |
paul@30 | 27 | "Handle the given 'message' object." |
paul@3 | 28 | |
paul@1 | 29 | request = self.request |
paul@0 | 30 | |
paul@20 | 31 | # Handle each update. |
paul@20 | 32 | |
paul@85 | 33 | all_successful = True |
paul@116 | 34 | any_successful = False |
paul@85 | 35 | |
paul@15 | 36 | for update in message.updates: |
paul@15 | 37 | |
paul@15 | 38 | # Handle a single part. |
paul@0 | 39 | |
paul@21 | 40 | if not is_collection(update): |
paul@116 | 41 | success = self.handle_message_parts(message, [update], update) |
paul@0 | 42 | |
paul@15 | 43 | # Or a collection of alternative representations for a single |
paul@15 | 44 | # update. |
paul@0 | 45 | |
paul@15 | 46 | else: |
paul@116 | 47 | success = self.handle_message_parts(message, update.get_payload(), update) |
paul@116 | 48 | |
paul@116 | 49 | all_successful = all_successful and success |
paul@116 | 50 | any_successful = any_successful or success |
paul@0 | 51 | |
paul@0 | 52 | # Default output. |
paul@0 | 53 | |
paul@116 | 54 | if any_successful: |
paul@116 | 55 | writeHeaders(request, "text/plain", getMetadata(self.page), "200 OK") |
paul@116 | 56 | else: |
paul@116 | 57 | writeHeaders(request, "text/plain", getMetadata(self.page), "403 Forbidden") |
paul@116 | 58 | |
paul@85 | 59 | if all_successful: |
paul@85 | 60 | request.write("All updates were successful.") |
paul@116 | 61 | elif any_successful: |
paul@116 | 62 | request.write("Some updates were unsuccessful.") |
paul@85 | 63 | else: |
paul@116 | 64 | request.write("No updates were successful.") |
paul@0 | 65 | |
paul@102 | 66 | def handle_message_parts(self, message, parts, update): |
paul@0 | 67 | |
paul@0 | 68 | """ |
paul@102 | 69 | From the given 'message', handle the given 'parts', using the original |
paul@102 | 70 | 'update' to determine whether the content is to replace or update page |
paul@102 | 71 | content, or whether it will be placed in a message store. |
paul@0 | 72 | """ |
paul@0 | 73 | |
paul@80 | 74 | request = self.request |
paul@80 | 75 | |
paul@85 | 76 | # Test for privileges to change the page or message store. |
paul@85 | 77 | |
paul@85 | 78 | update_action = get_update_action(update) |
paul@85 | 79 | |
paul@85 | 80 | if not self.can_perform_action(update_action): |
paul@85 | 81 | return False |
paul@85 | 82 | |
paul@16 | 83 | # Handle the different update actions. |
paul@16 | 84 | # Update a message store for the page. |
paul@16 | 85 | |
paul@16 | 86 | if to_store(update): |
paul@80 | 87 | |
paul@126 | 88 | # Add any recipient from the original message. |
paul@126 | 89 | |
paul@126 | 90 | if self.message.has_key("To") and not update.has_key("To"): |
paul@126 | 91 | update["To"] = self.message["To"] |
paul@126 | 92 | |
paul@80 | 93 | # Add any authenticated user. |
paul@80 | 94 | # Note that where messages are signed by the real author, encrypted, |
paul@80 | 95 | # and then signed for sending, the authenticated user here is not |
paul@80 | 96 | # the real author. |
paul@80 | 97 | |
paul@81 | 98 | update["Moin-User"] = request.user and request.user.valid and request.user.name or None |
paul@80 | 99 | |
paul@102 | 100 | # Propagate time information using the unintuitive time functions. |
paul@102 | 101 | |
paul@102 | 102 | if message.date: |
paul@137 | 103 | update["Date"] = message.date |
paul@102 | 104 | |
paul@105 | 105 | self.store.append(as_string(update)) |
paul@0 | 106 | |
paul@16 | 107 | # Update the page. |
paul@16 | 108 | |
paul@16 | 109 | else: |
paul@16 | 110 | # NOTE: Should either choose preferred content types or somehow retain them |
paul@16 | 111 | # NOTE: all but present one at a time. |
paul@16 | 112 | |
paul@16 | 113 | body = [] |
paul@16 | 114 | replace = to_replace(update) |
paul@0 | 115 | |
paul@16 | 116 | for part in parts: |
paul@16 | 117 | mimetype = part.get_content_type() |
paul@16 | 118 | encoding = part.get_content_charset() |
paul@16 | 119 | if mimetype == "text/moin": |
paul@42 | 120 | payload = part.get_payload(decode=True) |
paul@42 | 121 | body.append(encoding and unicode(payload, encoding) or payload) |
paul@16 | 122 | if replace: |
paul@16 | 123 | break |
paul@0 | 124 | |
paul@16 | 125 | if not replace: |
paul@16 | 126 | body.append(self.page.get_raw_body()) |
paul@0 | 127 | |
paul@80 | 128 | page_editor = PageEditor(request, self.pagename) |
paul@16 | 129 | page_editor.saveText("\n\n".join(body), 0) |
paul@0 | 130 | |
paul@16 | 131 | # Refresh the page. |
paul@11 | 132 | |
paul@80 | 133 | self.page = Page(request, self.pagename) |
paul@11 | 134 | |
paul@85 | 135 | return True |
paul@85 | 136 | |
paul@0 | 137 | # Action function. |
paul@0 | 138 | |
paul@0 | 139 | def execute(pagename, request): |
paul@12 | 140 | PostMessage(pagename, request).do_action() # instead of render |
paul@0 | 141 | |
paul@0 | 142 | # vim: tabstop=4 expandtab shiftwidth=4 |