1.1 --- a/MoinMessage.py Wed Jan 08 01:55:57 2014 +0100
1.2 +++ b/MoinMessage.py Thu Jan 09 16:52:05 2014 +0100
1.3 @@ -8,6 +8,7 @@
1.4
1.5 from email import message_from_string
1.6 from email.encoders import encode_noop
1.7 +from email.generator import Generator
1.8 from email.mime.multipart import MIMEMultipart
1.9 from email.mime.application import MIMEApplication
1.10 from email.mime.base import MIMEBase
1.11 @@ -20,6 +21,11 @@
1.12 import httplib
1.13 import os
1.14
1.15 +try:
1.16 + from cStringIO import StringIO
1.17 +except ImportError:
1.18 + from StringIO import StringIO
1.19 +
1.20 def is_collection(message):
1.21 return message.get("Update-Type") == "collection"
1.22
1.23 @@ -279,8 +285,8 @@
1.24
1.25 # Verify the message.
1.26
1.27 - fingerprint, identity = self.verifyMessageText(signature.get_payload(decode=True), content.as_string())
1.28 - return fingerprint, identity, getOriginalContent(content)
1.29 + fingerprint, identity = self.verifyMessageText(signature.get_payload(decode=True), as_string(content))
1.30 + return fingerprint, identity, content
1.31
1.32 def signMessage(self, message, keyid):
1.33
1.34 @@ -288,18 +294,14 @@
1.35 Return a signed version of 'message' using the given 'keyid'.
1.36 """
1.37
1.38 - # Make a representation-insensitive container for the message.
1.39 -
1.40 - content = MIMEApplication(message.as_string())
1.41 -
1.42 # Sign the container's representation.
1.43
1.44 - signature = self.run(["--armor", "-u", keyid, "--detach-sig"], content.as_string())
1.45 + signature = self.run(["--armor", "-u", keyid, "--detach-sig"], as_string(message))
1.46
1.47 # Make the container for the message.
1.48
1.49 signed_message = MIMEMultipart("signed", protocol="application/pgp-signature")
1.50 - signed_message.attach(content)
1.51 + signed_message.attach(message)
1.52
1.53 signature_part = MIMEBase("application", "pgp-signature")
1.54 signature_part.set_payload(signature)
1.55 @@ -339,7 +341,7 @@
1.56 Return an encrypted version of 'message' using the given 'keyid'.
1.57 """
1.58
1.59 - text = message.as_string()
1.60 + text = as_string(message)
1.61 encrypted = self.run(["--armor", "-r", keyid, "--encrypt", "--trust-model", "always"], text)
1.62
1.63 # Make the container for the message.
1.64 @@ -450,6 +452,20 @@
1.65
1.66 return keys
1.67
1.68 +# Message serialisation functions, working around email module problems.
1.69 +
1.70 +def as_string(message):
1.71 +
1.72 + """
1.73 + Return the string representation of 'message', attempting to preserve the
1.74 + precise original formatting.
1.75 + """
1.76 +
1.77 + out = StringIO()
1.78 + generator = Generator(out, False, 0) # disable reformatting measures
1.79 + generator.flatten(message)
1.80 + return out.getvalue()
1.81 +
1.82 # Message decoding functions.
1.83
1.84 # Detect PGP/GPG-encoded payloads.
1.85 @@ -486,15 +502,6 @@
1.86 except ValueError:
1.87 raise MoinMessageMissingPart
1.88
1.89 -def getOriginalContent(content):
1.90 -
1.91 - """
1.92 - Extract the actual content inside the signed message. This reverses the
1.93 - wrapping up of signed content in a representation-insensitive container.
1.94 - """
1.95 -
1.96 - return Parser().parsestr(content.get_payload(decode=True))
1.97 -
1.98 # Communications functions.
1.99
1.100 def timestamp(message):
1.101 @@ -530,7 +537,7 @@
1.102 """
1.103
1.104 scheme, host, port, path = parseURL(url)
1.105 - text = message.as_string()
1.106 + text = as_string(message)
1.107
1.108 req = _getConnection(scheme)(host, port)
1.109 req.request(method, path, text)
2.1 --- a/MoinMessageSupport.py Wed Jan 08 01:55:57 2014 +0100
2.2 +++ b/MoinMessageSupport.py Thu Jan 09 16:52:05 2014 +0100
2.3 @@ -16,8 +16,7 @@
2.4 from TokenSupport import getIdentifiers
2.5 from MoinMessage import GPG, Message, MoinMessageError, \
2.6 MoinMessageMissingPart, MoinMessageBadContent, \
2.7 - is_signed, is_encrypted, getContentAndSignature, \
2.8 - getOriginalContent
2.9 + is_signed, is_encrypted, getContentAndSignature
2.10 from email.parser import Parser
2.11 import time
2.12
2.13 @@ -167,7 +166,7 @@
2.14 # Handle the embedded message.
2.15
2.16 content, signature = getContentAndSignature(message)
2.17 - self.handle_message_content(getOriginalContent(content))
2.18 + self.handle_message_content(content)
2.19
2.20 # Reject any unverified message.
2.21
3.1 --- a/README.txt Wed Jan 08 01:55:57 2014 +0100
3.2 +++ b/README.txt Thu Jan 09 16:52:05 2014 +0100
3.3 @@ -342,7 +342,6 @@
3.4
3.5 python tests/test_message.py collection update 'An update to the wiki.' \
3.6 'Another update.' \
3.7 -| python tests/test_message_wrap.py \
3.8 > test.txt \
3.9 && cat test.txt \
3.10 | gpg --armor -u 1C1AAF83 --detach-sig \
4.1 --- a/actions/PostMessage.py Wed Jan 08 01:55:57 2014 +0100
4.2 +++ b/actions/PostMessage.py Thu Jan 09 16:52:05 2014 +0100
4.3 @@ -2,14 +2,15 @@
4.4 """
4.5 MoinMoin - PostMessage Action
4.6
4.7 - @copyright: 2012, 2013 by Paul Boddie <paul@boddie.org.uk>
4.8 + @copyright: 2012, 2013, 2014 by Paul Boddie <paul@boddie.org.uk>
4.9 @license: GNU GPL (v2 or later), see COPYING.txt for details.
4.10 """
4.11
4.12 from MoinMoin.Page import Page
4.13 from MoinMoin.PageEditor import PageEditor
4.14 from MoinSupport import getMetadata, writeHeaders
4.15 -from MoinMessage import is_collection, to_replace, to_store, get_update_action
4.16 +from MoinMessage import is_collection, to_replace, to_store, get_update_action, \
4.17 + as_string
4.18 from MoinMessageSupport import MoinMessageAction
4.19
4.20 Dependencies = ['pages']
4.21 @@ -83,7 +84,7 @@
4.22 if message.date:
4.23 update["Date"] = message.date.as_RFC2822_datetime_string()
4.24
4.25 - self.store.append(update.as_string())
4.26 + self.store.append(as_string(update))
4.27
4.28 # Update the page.
4.29
5.1 --- a/actions/SendMessage.py Wed Jan 08 01:55:57 2014 +0100
5.2 +++ b/actions/SendMessage.py Thu Jan 09 16:52:05 2014 +0100
5.3 @@ -2,7 +2,7 @@
5.4 """
5.5 MoinMoin - SendMessage Action
5.6
5.7 - @copyright: 2012, 2013 by Paul Boddie <paul@boddie.org.uk>
5.8 + @copyright: 2012, 2013, 2014 by Paul Boddie <paul@boddie.org.uk>
5.9 @license: GNU GPL (v2 or later), see COPYING.txt for details.
5.10 """
5.11
5.12 @@ -11,7 +11,8 @@
5.13 from MoinMoin.log import getLogger
5.14 from MoinMoin.Page import Page
5.15 from MoinMoin import config
5.16 -from MoinMessage import GPG, MoinMessageError, Message, sendMessage, timestamp
5.17 +from MoinMessage import GPG, MoinMessageError, Message, sendMessage, timestamp, \
5.18 + as_string
5.19 from MoinMessageSupport import get_signing_users, get_recipients, get_relays, \
5.20 get_recipient_details, MoinMessageRecipientError
5.21 from MoinSupport import *
5.22 @@ -330,13 +331,13 @@
5.23 elif type == "page":
5.24 page = Page(request, location)
5.25 outbox = ItemStore(page, "messages", "message-locks")
5.26 - outbox.append(message.as_string())
5.27 + outbox.append(as_string(message))
5.28
5.29 # Or queue the message in a special outbox.
5.30
5.31 else:
5.32 outbox = ItemStore(request.page, "outgoing-messages", "outgoing-message-locks")
5.33 - outbox.append(message.as_string())
5.34 + outbox.append(as_string(message))
5.35
5.36 return 1, _("Message sent!")
5.37
6.1 --- a/tests/test_message_wrap.py Wed Jan 08 01:55:57 2014 +0100
6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
6.3 @@ -1,19 +0,0 @@
6.4 -#!/usr/bin/env python
6.5 -
6.6 -from email.mime.application import MIMEApplication
6.7 -import sys
6.8 -
6.9 -if __name__ == "__main__":
6.10 - text = sys.stdin.read()
6.11 -
6.12 - # Make a representation-insensitive container for the message.
6.13 -
6.14 - content = MIMEApplication(text)
6.15 -
6.16 - # Show the resulting message text.
6.17 -
6.18 - text = content.as_string()
6.19 -
6.20 - print text
6.21 -
6.22 -# vim: tabstop=4 expandtab shiftwidth=4
7.1 --- a/tests/test_sign_wrap.py Wed Jan 08 01:55:57 2014 +0100
7.2 +++ b/tests/test_sign_wrap.py Thu Jan 09 16:52:05 2014 +0100
7.3 @@ -4,6 +4,7 @@
7.4 from email.mime.application import MIMEApplication
7.5 from email.mime.base import MIMEBase
7.6 from email import message_from_string
7.7 +from MoinMessage import as_string
7.8 import sys
7.9
7.10 if __name__ == "__main__":
7.11 @@ -26,7 +27,7 @@
7.12
7.13 # Show the resulting message text.
7.14
7.15 - text = message.as_string()
7.16 + text = as_string(message)
7.17
7.18 print text
7.19
8.1 --- a/tests/test_verify.py Wed Jan 08 01:55:57 2014 +0100
8.2 +++ b/tests/test_verify.py Thu Jan 09 16:52:05 2014 +0100
8.3 @@ -1,6 +1,6 @@
8.4 #!/usr/bin/env python
8.5
8.6 -from MoinMessage import GPG
8.7 +from MoinMessage import GPG, as_string
8.8 from email.parser import Parser
8.9 import sys
8.10
8.11 @@ -12,6 +12,6 @@
8.12 gpg = GPG(sys.argv[1])
8.13 else:
8.14 gpg = GPG()
8.15 - print gpg.verifyMessageText(signature.get_payload(), content.as_string())
8.16 + print gpg.verifyMessageText(signature.get_payload(decode=True), as_string(content))
8.17
8.18 # vim: tabstop=4 expandtab shiftwidth=4