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(message, [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(message, 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, message, parts, update): 53 54 """ 55 From the given 'message', handle the given 'parts', using the original 56 'update' to determine whether the content is to replace or update page 57 content, or 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 # Propagate time information using the unintuitive time functions. 82 83 if message.date: 84 update["Date"] = message.date.as_RFC2822_datetime_string() 85 86 self.store.append(update.as_string()) 87 88 # Update the page. 89 90 else: 91 # NOTE: Should either choose preferred content types or somehow retain them 92 # NOTE: all but present one at a time. 93 94 body = [] 95 replace = to_replace(update) 96 97 for part in parts: 98 mimetype = part.get_content_type() 99 encoding = part.get_content_charset() 100 if mimetype == "text/moin": 101 payload = part.get_payload(decode=True) 102 body.append(encoding and unicode(payload, encoding) or payload) 103 if replace: 104 break 105 106 if not replace: 107 body.append(self.page.get_raw_body()) 108 109 page_editor = PageEditor(request, self.pagename) 110 page_editor.saveText("\n\n".join(body), 0) 111 112 # Refresh the page. 113 114 self.page = Page(request, self.pagename) 115 116 return True 117 118 # Action function. 119 120 def execute(pagename, request): 121 PostMessage(pagename, request).do_action() # instead of render 122 123 # vim: tabstop=4 expandtab shiftwidth=4