Changeset 73:f574beee3bca in ralphm-patches


Ignore:
Timestamp:
May 6, 2013, 10:01:18 PM (7 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.
Files:
1 added
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • async-observer.patch

    r72 r73  
    11# HG changeset patch
    2 # Parent 2076bac14221bf6aa7dc9337590b18604b100f32
     2# Parent ed052dd5cb7a57faf1693f78457401fd820fdd79
    33
    44diff --git a/wokkel/subprotocols.py b/wokkel/subprotocols.py
     
    2626+
    2727+        >>> class MyHandler(XMPPHandler):
    28 +        ...    def connectionMade(self):
     28+        ...    def connectionInitialized(self):
    2929+        ...       self.xmlstream.addObserver('/message', self.onMessage)
    3030+        ...    @asyncObserver
     
    104104     """
    105105     XMPP subprotocol mixin for handle incoming IQ stanzas.
     106@@ -401,15 +486,15 @@
     107 
     108     A typical way to use this mixin, is to set up L{xpath} observers on the
     109     C{xmlstream} to call handleRequest, for example in an overridden
     110-    L{XMPPHandler.connectionMade}. It is likely a good idea to only listen for
     111-    incoming iq get and/org iq set requests, and not for any iq, to prevent
     112-    hijacking incoming responses to outgoing iq requests. An example:
     113+    L{XMPPHandler.connectionInitialized}. It is likely a good idea to only
     114+    listen for incoming iq get and/org iq set requests, and not for any iq, to
     115+    prevent hijacking incoming responses to outgoing iq requests. An example:
     116 
     117         >>> QUERY_ROSTER = "/query[@xmlns='jabber:iq:roster']"
     118         >>> class MyHandler(XMPPHandler, IQHandlerMixin):
     119         ...    iqHandlers = {"/iq[@type='get']" + QUERY_ROSTER: 'onRosterGet',
     120         ...                  "/iq[@type='set']" + QUERY_ROSTER: 'onRosterSet'}
     121-        ...    def connectionMade(self):
     122+        ...    def connectionInitialized(self):
     123         ...        self.xmlstream.addObserver(
     124         ...          "/iq[@type='get' or @type='set']" + QUERY_ROSTER,
     125         ...          self.handleRequest)
    106126@@ -425,6 +510,7 @@
    107127 
  • c2s_stanza_handlers.patch

    r72 r73  
    11# HG changeset patch
    2 # Parent c104dd0a9d3fb840b53661822cf06728a5b23d8f
     2# Parent 70ce9e97aa58780ac7f75a177fe8c0d2aaf1bfc6
    33Add c2s protocol handlers for iq, message and presence stanzas.
    44
     
    4848 from wokkel.subprotocols import IQHandlerMixin
    4949 
    50 @@ -2004,3 +2004,419 @@
     50@@ -2031,3 +2031,418 @@
    5151         d = self.sessionManager.probePresence(user)
    5252         d.addCallback(cb)
     
    197197+        stanza = received[-1]
    198198+        self.assertEqual(u'Hello', stanza.body)
    199 +
    200199+
    201200+
     
    522521 
    523522+    observerPriority = 0
    524      stanzaFactory = Message
     523     messageFactory = Message
    525524 
    526525     def connectionInitialized(self):
     
    531530 
    532531     @asyncObserver
    533 @@ -1673,3 +1681,246 @@
     532@@ -1689,3 +1697,246 @@
    534533             presence = ProbePresence(recipient=entity,
    535534                                      sender=user.entity)
  • 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.
  • roster_server.patch

    r72 r73  
    2626+
    2727+
    28 +    def test_onRosterGet(self):
     28+    def test_getRosterReceived(self):
    2929+        """
    3030+        A roster get request should trigger getRoster with the request.
    3131+        """
    3232+        xml = """
    33 +          <iq type='get' from='user@example.org' to='user@example.org'>
     33+          <iq type='get' from='user@example.org/Home'>
    3434+            <query xmlns='jabber:iq:roster'/>
    3535+          </iq>
    3636+        """
    3737+
    38 +        def getRoster(request):
    39 +            self.assertEqual(JID('user@example.org'), request.sender)
     38+        def getRosterReceived(request):
     39+            self.assertEqual(JID('user@example.org/Home'), request.sender)
    4040+            item = xmppim.RosterItem(JID('other@example.org'), True, False,
    4141+                                 'The User')
     
    5454+            self.assertEquals(u'The User', item.getAttribute('name'))
    5555+
    56 +        self.service.getRoster = getRoster
     56+        self.service.getRosterReceived = getRosterReceived
    5757+        d = self.handleRequest(xml)
    5858+        d.addCallback(cb)
     
    6060+
    6161+
    62 +    def test_onRosterGetNotOverridden(self):
     62+    def test_getRosterReceivedNotOverridden(self):
    6363+        """
    6464+        If getRoster is not overridden, return feature-not-implemented.
    6565+        """
    6666+        xml = """
    67 +          <iq type='get' from='user@example.org' to='user@example.org'>
     67+          <iq type='get' from='user@example.org/Home'>
    6868+            <query xmlns='jabber:iq:roster'/>
    6969+          </iq>
     
    7575+
    7676+
    77 +    def test_onRosterSet(self):
     77+    def test_setRosterReceived(self):
    7878+        """
    7979+        Roster set is not yet supported.
    8080+        """
    8181+        xml = """
    82 +          <iq type='set' from='user@example.org' to='user@example.org'>
     82+          <iq type='set' from='user@example.org/Home'>
    8383+            <query xmlns='jabber:iq:roster'>
    8484+              <item jid='other@example.org' name='Other User'/>
     
    231231+            return roster.toElement()
    232232+
    233 +        d = self.getRoster(request)
     233+        d = self.getRosterReceived(request)
    234234+        d.addCallback(toResponse)
    235235+        return d
    236236+
    237237+
    238 +    def getRoster(self, request):
     238+    def getRosterReceived(self, request):
    239239+        """
    240240+        Called when the roster is requested.
  • series

    r72 r73  
    1 deprecate_prepareIDNName.patch
    21async-observer.patch
    32message-stanza.patch
     
    1413
    1514c2s_stanza_handlers.patch #+c2s
    16 c2s_example.patch
     15c2s_example.patch #+c2s
     16
     17pubsub_connectionInitialized.patch
    1718
    1819version.patch
  • session_manager.patch

    r72 r73  
    11# HG changeset patch
    2 # Parent fdef0cff7a57368fa21984593ef05e616039e2e2
     2# Parent 60135baa4e422ab203c780c9093c374481b17a7a
    33
    44diff --git a/wokkel/ewokkel.py b/wokkel/ewokkel.py
     
    346346     """
    347347     Tests for L{xmppim.Message}.
    348 @@ -1598,3 +1710,297 @@
     348@@ -1625,3 +1737,297 @@
    349349                          "was deprecated in Wokkel 0.8.0; "
    350350                          "please use MessageProtocol.messageReceived instead.",
     
    737737     """
    738738     A message stanza.
    739 @@ -1159,3 +1228,448 @@
     739@@ -1175,3 +1244,448 @@
    740740 
    741741             self.onMessage(message.element)
Note: See TracChangeset for help on using the changeset viewer.