Changeset 73:f574beee3bca in ralphm-patches for message-stanza.patch
- Timestamp:
- May 6, 2013, 10:01:18 PM (9 years ago)
- Branch:
- default
- Children:
- 74:355afce3af27, 75:9b7b8b99da61
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
message-stanza.patch
r72 r73 1 1 # HG changeset patch 2 # Parent 4d9f583550dd048922d89ea09c60cd082b6c2c65 2 # Parent a94e14d79311f8efde3bd001ce4b5c8bafbb8094 3 Parse message stanzas into Message objects. 4 5 This replaces `MessageProtocol.onMessage` with `messageReceived` and 6 `errorReceived`. 7 8 Incoming messages stanzas are parsed by the `fromElement` class method 9 of the class set in `MessageProtocol.messageFactory`. This defaults to 10 the `Message` class. The resulting object is passed to 11 `messageReceived`. 12 13 If 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. 3 18 4 19 diff --git a/wokkel/test/test_xmppim.py b/wokkel/test/test_xmppim.py 5 20 --- a/wokkel/test/test_xmppim.py 6 21 +++ b/wokkel/test/test_xmppim.py 7 @@ -1336,3 +1336, 186@@22 @@ -1336,3 +1336,213 @@ 8 23 d = self.handleRequest(xml) 9 24 d.addCallback(cb) … … 112 127 + stanza = received[-1] 113 128 + 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) 114 156 + 115 157 + … … 220 262 221 263 def __init__(self, recipient=None, sender=None, body=None, subject=None): 222 @@ -1031,39 +1034, 68@@264 @@ -1031,39 +1034,84 @@ 223 265 element = Stanza.toElement(self) 224 266 … … 246 288 Generic XMPP subprotocol handler for incoming message stanzas. 247 289 + 248 + @cvar stanzaFactory: The class to parse messages into. Its C{fromElement}290 + @cvar messageFactory: The class to parse messages into. Its C{fromElement} 249 291 + will be called with the received L{domish.Element}. Defaults to 250 292 + L{Message}. … … 252 294 253 295 - messageTypes = None, 'normal', 'chat', 'headline', 'groupchat' 254 + stanzaFactory = Message296 + messageFactory = Message 255 297 256 298 def connectionInitialized(self): … … 264 306 + @asyncObserver 265 307 + 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) 268 314 269 315 - if messageType == 'error': … … 272 318 - if messageType not in self.messageTypes: 273 319 - message["type"] = 'normal' 274 - 320 + def errorReceived(self, stanza): 321 + """ 322 + Called when a message stanza of type error was received. 323 275 324 - 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 277 335 - def onMessage(self, message): 336 + 278 337 + def messageReceived(self, message): 279 338 """ … … 295 354 + DeprecationWarning) 296 355 + 297 + if message.stanzaType == u'error':298 + return False299 +300 356 + if message.stanzaType == u'normal': 301 357 + # Override stanza type for backwards compatibility.
Note: See TracChangeset
for help on using the changeset viewer.