1.1 --- a/MoinMessage.py Sun Oct 28 22:27:35 2012 +0100
1.2 +++ b/MoinMessage.py Mon Oct 29 22:36:16 2012 +0100
1.3 @@ -17,12 +17,38 @@
1.4 import httplib
1.5 import os
1.6
1.7 +def is_collection(message):
1.8 + return message.get("Update-Type") == "collection"
1.9 +
1.10 class Message:
1.11
1.12 "An update message."
1.13
1.14 - def __init__(self):
1.15 + def __init__(self, text=None):
1.16 self.updates = []
1.17 + if text:
1.18 + self.parse_text(text)
1.19 +
1.20 + def parse_text(self, text):
1.21 +
1.22 + "Parse the given 'text' as a message."
1.23 +
1.24 + self.handle_message(message_from_string(text))
1.25 +
1.26 + def handle_message(self, message):
1.27 +
1.28 + "Handle the given 'message', recording the separate updates."
1.29 +
1.30 + # The message either consists of a collection of updates.
1.31 +
1.32 + if message.is_multipart() and is_collection(message):
1.33 + for part in message.get_payload():
1.34 + self.updates.append(part)
1.35 +
1.36 + # Or the message is a single update.
1.37 +
1.38 + else:
1.39 + self.updates.append(message)
1.40
1.41 def add_update(self, alternatives):
1.42
1.43 @@ -54,6 +80,43 @@
1.44
1.45 return message
1.46
1.47 +class Mailbox:
1.48 +
1.49 + "A collection of messages within a multipart message."
1.50 +
1.51 + def __init__(self, text=None):
1.52 + self.messages = []
1.53 + if text:
1.54 + self.parse_text(text)
1.55 +
1.56 + def parse_text(self, text):
1.57 +
1.58 + "Parse the given 'text' as a mailbox."
1.59 +
1.60 + message = message_from_string(text)
1.61 +
1.62 + if message.is_multipart():
1.63 + for part in message.get_payload():
1.64 + self.messages.append(part)
1.65 + else:
1.66 + self.messages.append(message)
1.67 +
1.68 + def add_message(self, message):
1.69 +
1.70 + "Add the given 'message' to the mailbox."
1.71 +
1.72 + self.messages.append(message)
1.73 +
1.74 + def get_payload(self):
1.75 +
1.76 + "Get the multipart payload for the mailbox."
1.77 +
1.78 + mailbox = MIMEMultipart()
1.79 + for message in self.messages:
1.80 + mailbox.attach(message)
1.81 +
1.82 + return mailbox
1.83 +
1.84 class MoinMessageError(Exception):
1.85 pass
1.86
2.1 --- a/actions/PostMessage.py Sun Oct 28 22:27:35 2012 +0100
2.2 +++ b/actions/PostMessage.py Mon Oct 29 22:36:16 2012 +0100
2.3 @@ -10,7 +10,7 @@
2.4 from MoinMoin.log import getLogger
2.5 from MoinMoin.user import User
2.6 from MoinSupport import *
2.7 -from MoinMessage import GPG, MoinMessageError
2.8 +from MoinMessage import GPG, Message, MoinMessageError
2.9 from email.parser import Parser
2.10
2.11 try:
2.12 @@ -199,34 +199,29 @@
2.13 writeHeaders(request, "text/plain", getMetadata(self.page), "403 Forbidden")
2.14 request.write("The message could not be verified.")
2.15
2.16 - def handle_message_content(self, message):
2.17 + def handle_message_content(self, content):
2.18
2.19 - "Handle the given 'message'."
2.20 + "Handle the given message 'content'."
2.21
2.22 request = self.request
2.23
2.24 - # Handle a single part.
2.25 -
2.26 - if not message.is_multipart():
2.27 - self.handle_message_parts([message], to_replace(message))
2.28 + # Interpret the content as one or more updates.
2.29
2.30 - # Handle multiple parts.
2.31 + message = Message()
2.32 + message.handle_message(content)
2.33
2.34 - # This can be a collection of updates, with each update potentially being a
2.35 - # collection of alternative representations.
2.36 + for update in message.updates:
2.37 +
2.38 + # Handle a single part.
2.39
2.40 - elif is_collection(message):
2.41 - for part in message.get_payload():
2.42 - if part.is_multipart():
2.43 - self.handle_message_parts(part.get_payload(), to_replace(part))
2.44 - else:
2.45 - self.handle_message_parts([part], to_replace(part))
2.46 + if not update.is_multipart():
2.47 + self.handle_message_parts([update], to_replace(update))
2.48
2.49 - # Or it can be a collection of alternative representations for a single
2.50 - # update.
2.51 + # Or a collection of alternative representations for a single
2.52 + # update.
2.53
2.54 - else:
2.55 - self.handle_message_parts(message.get_payload(), to_replace(message))
2.56 + else:
2.57 + self.handle_message_parts(update.get_payload(), to_replace(update))
2.58
2.59 # Default output.
2.60
3.1 --- a/tests/test_message.py Sun Oct 28 22:27:35 2012 +0100
3.2 +++ b/tests/test_message.py Mon Oct 29 22:36:16 2012 +0100
3.3 @@ -1,17 +1,16 @@
3.4 #!/usr/bin/env python
3.5
3.6 -from email.mime.multipart import MIMEMultipart
3.7 +from MoinMessage import Message
3.8 from email.mime.text import MIMEText
3.9 import sys
3.10
3.11 if __name__ == "__main__":
3.12 - message = MIMEMultipart()
3.13 + message = Message()
3.14
3.15 for arg in sys.argv[1:]:
3.16 - text = MIMEText(arg, "moin")
3.17 - message.attach(text)
3.18 + message.add_update([MIMEText(arg, "moin")])
3.19
3.20 - text = message.as_string()
3.21 + text = message.get_payload()
3.22 print text
3.23
3.24 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tests/test_message_input.py Mon Oct 29 22:36:16 2012 +0100
4.3 @@ -0,0 +1,13 @@
4.4 +#!/usr/bin/env python
4.5 +
4.6 +from MoinMessage import Message
4.7 +import sys
4.8 +
4.9 +if __name__ == "__main__":
4.10 + message = Message(sys.stdin.read())
4.11 +
4.12 + for update in message.updates:
4.13 + print update.get_payload()
4.14 + print
4.15 +
4.16 +# vim: tabstop=4 expandtab shiftwidth=4