Changeset 73:f574beee3bca in ralphm-patches for message-stanza.patch


Ignore:
Timestamp:
May 6, 2013, 10:01:18 PM (8 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Children:
74:355afce3af27, 75:9b7b8b99da61
Message:

Minor cleanups, improved error message handling, and upstreamed patch.

  • Upstreamed deprecation of prepareIDNName.
  • MessageProtocol? now properly handles error message stanzas.
  • Renamed methods for RosterServerProtocol?.
  • Prefer connectionInitialized for setting up stanza observers.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • message-stanza.patch

    r72 r73  
    11# HG changeset patch
    2 # Parent 4d9f583550dd048922d89ea09c60cd082b6c2c65
     2# Parent a94e14d79311f8efde3bd001ce4b5c8bafbb8094
     3Parse message stanzas into Message objects.
     4
     5This replaces `MessageProtocol.onMessage` with `messageReceived` and
     6`errorReceived`.
     7
     8Incoming messages stanzas are parsed by the `fromElement` class method
     9of the class set in `MessageProtocol.messageFactory`. This defaults to
     10the `Message` class.  The resulting object is passed to
     11`messageReceived`.
     12
     13If the message type was `error`, the message is parsed as a
     14`wokkel.generic.ErrorStanza` instead, and passed to `errorReceived`.
     15
     16`Message` now normalizes the message type of empty or unknown values to
     17`normal` per RFC 6120.
    318
    419diff --git a/wokkel/test/test_xmppim.py b/wokkel/test/test_xmppim.py
    520--- a/wokkel/test/test_xmppim.py
    621+++ b/wokkel/test/test_xmppim.py
    7 @@ -1336,3 +1336,186 @@
     22@@ -1336,3 +1336,213 @@
    823         d = self.handleRequest(xml)
    924         d.addCallback(cb)
     
    112127+        stanza = received[-1]
    113128+        self.assertEqual(u'Hello', stanza.body)
     129+
     130+
     131+    def test_errorReceived(self):
     132+        """
     133+        Message stanzas of type error are received by errorReceived.
     134+        """
     135+        messagesReceived = []
     136+        errorsReceived = []
     137+        self.patch(self.protocol, 'messageReceived', messagesReceived.append)
     138+        self.patch(self.protocol, 'errorReceived', errorsReceived.append)
     139+
     140+        xml = """
     141+          <message to='example.org' type='error'>
     142+            <error type='cancel'>
     143+              <service-unavailable
     144+                  xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
     145+            </error>
     146+          </message>
     147+        """
     148+
     149+        message = parseXml(xml)
     150+        self.stub.send(message)
     151+
     152+        self.assertEqual(0, len(messagesReceived))
     153+        self.assertEqual(1, len(errorsReceived))
     154+        stanza = errorsReceived[-1]
     155+        self.assertEqual(u'service-unavailable', stanza.exception.condition)
    114156+
    115157+
     
    220262 
    221263     def __init__(self, recipient=None, sender=None, body=None, subject=None):
    222 @@ -1031,39 +1034,68 @@
     264@@ -1031,39 +1034,84 @@
    223265         element = Stanza.toElement(self)
    224266 
     
    246288     Generic XMPP subprotocol handler for incoming message stanzas.
    247289+
    248 +    @cvar stanzaFactory: The class to parse messages into. Its C{fromElement}
     290+    @cvar messageFactory: The class to parse messages into. Its C{fromElement}
    249291+        will be called with the received L{domish.Element}. Defaults to
    250292+        L{Message}.
     
    252294 
    253295-    messageTypes = None, 'normal', 'chat', 'headline', 'groupchat'
    254 +    stanzaFactory = Message
     296+    messageFactory = Message
    255297 
    256298     def connectionInitialized(self):
     
    264306+    @asyncObserver
    265307+    def _onMessage(self, element):
    266 +        stanza = self.stanzaFactory.fromElement(element)
    267 +        return self.messageReceived(stanza)
     308+        if element.getAttribute(u'type') == u'error':
     309+            stanza = ErrorStanza.fromElement(element)
     310+            return self.errorReceived(stanza)
     311+        else:
     312+            stanza = self.messageFactory.fromElement(element)
     313+            return self.messageReceived(stanza)
    268314 
    269315-        if messageType == 'error':
     
    272318-        if messageType not in self.messageTypes:
    273319-            message["type"] = 'normal'
    274 -
     320+    def errorReceived(self, stanza):
     321+        """
     322+        Called when a message stanza of type error was received.
     323 
    275324-        self.onMessage(message)
    276 -
     325+        @type stanza: L{ErrorStanza}
     326+        """
     327+        if hasattr(self, 'onMessage'):
     328+            warnings.warn(
     329+                "wokkel.xmppim.MessageProtocol.onMessage "
     330+                "was deprecated in Wokkel 0.8.0; "
     331+                "please use MessageProtocol.messageReceived instead.",
     332+                DeprecationWarning)
     333+            return False
     334 
    277335-    def onMessage(self, message):
     336+
    278337+    def messageReceived(self, message):
    279338         """
     
    295354+                DeprecationWarning)
    296355+
    297 +            if message.stanzaType == u'error':
    298 +                return False
    299 +
    300356+            if message.stanzaType == u'normal':
    301357+                # Override stanza type for backwards compatibility.
Note: See TracChangeset for help on using the changeset viewer.