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