1.1 --- a/MoinMessage.py Sun Mar 10 01:19:48 2013 +0100
1.2 +++ b/MoinMessage.py Fri Apr 19 15:15:40 2013 +0200
1.3 @@ -11,6 +11,7 @@
1.4 from email.mime.multipart import MIMEMultipart
1.5 from email.mime.application import MIMEApplication
1.6 from email.mime.base import MIMEBase
1.7 +from email.utils import formatdate, parsedate
1.8 from subprocess import Popen, PIPE
1.9 from tempfile import mkstemp
1.10 from urlparse import urlsplit
1.11 @@ -25,10 +26,20 @@
1.12 "An update message."
1.13
1.14 def __init__(self, text=None):
1.15 + self.date = None
1.16 self.updates = []
1.17 if text:
1.18 self.parse_text(text)
1.19
1.20 + def init_date(self, message):
1.21 +
1.22 + "Obtain the date of the given 'message'."
1.23 +
1.24 + if message.has_key("Date"):
1.25 + self.date = parsedate(message["Date"])
1.26 + else:
1.27 + self.date = None
1.28 +
1.29 def parse_text(self, text):
1.30
1.31 "Parse the given 'text' as a message."
1.32 @@ -39,6 +50,8 @@
1.33
1.34 "Handle the given 'message', recording the separate updates."
1.35
1.36 + self.init_date(message)
1.37 +
1.38 # The message either consists of a collection of updates.
1.39
1.40 if message.is_multipart() and is_collection(message):
1.41 @@ -79,9 +92,15 @@
1.42 part.attach(alternative)
1.43 return part
1.44
1.45 - def get_payload(self):
1.46 + def get_payload(self, timestamped=True):
1.47
1.48 - "Get the multipart payload for the message."
1.49 + """
1.50 + Get the multipart payload for the message. If the 'timestamped'
1.51 + parameter is omitted or set to a true value, the payload will be given a
1.52 + date header set to the current date and time that can be used to assess
1.53 + the validity of a message and to determine whether it has already been
1.54 + received by a recipient.
1.55 + """
1.56
1.57 if len(self.updates) == 1:
1.58 message = self.updates[0]
1.59 @@ -91,6 +110,10 @@
1.60 for update in self.updates:
1.61 message.attach(update)
1.62
1.63 + if timestamped:
1.64 + timestamp(message)
1.65 + self.init_date(message)
1.66 +
1.67 return message
1.68
1.69 class Mailbox:
1.70 @@ -289,6 +312,20 @@
1.71
1.72 # Communications functions.
1.73
1.74 +def timestamp(message):
1.75 +
1.76 + """
1.77 + Timestamp the given 'message' so that its validity can be assessed by the
1.78 + recipient.
1.79 + """
1.80 +
1.81 + datestr = formatdate()
1.82 +
1.83 + if not message.has_key("Date"):
1.84 + message.add_header("Date", datestr)
1.85 + else:
1.86 + message["Date"] = datestr
1.87 +
1.88 def sendMessage(message, url):
1.89
1.90 "Send 'message' to the given 'url."