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