Changeset 69:cc2fc0173c4d in ralphm-patches


Ignore:
Timestamp:
Oct 27, 2012, 1:55:56 AM (7 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Use stanzas, use methods on SessionManager? to retrieve sessions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • c2s_stanza_handlers.patch

    r67 r69  
    11# HG changeset patch
    2 # Parent 0010b81dc2caab48246d1e3d3d4f555af549688c
     2# Parent eb898b91399636715feb9530c3d3e0090628926a
    33Add c2s protocol handlers for iq, message and presence stanzas.
    44
     
    9595+
    9696+sessionManager.connectionManager = c2sFactory
     97diff --git a/wokkel/client.py b/wokkel/client.py
     98--- a/wokkel/client.py
     99+++ b/wokkel/client.py
     100@@ -589,13 +589,38 @@
     101         return defer.succeed(entity)
     102 
     103 
     104+    def lookupSessions(self, entity):
     105+        """
     106+        Return all sessions for a user.
     107+
     108+        @param entity: Entity to retrieve sessions for. This the resource part
     109+            will be ignored.
     110+        @type entity: L{JID<twisted.words.protocols.jabber.jid.JID>}
     111+
     112+        @return: Mapping of sessions keyed by resource.
     113+        @rtype: C{dict}
     114+        """
     115+        localpart = entity.user
     116+
     117+        try:
     118+            return self.sessions[localpart]
     119+        except:
     120+            return {}
     121+
     122+
     123     def lookupSession(self, entity):
     124-        localpart = entity.user
     125-        resource = entity.resource
     126+        """
     127+        Return the session for a particular resource of an entity.
     128 
     129-        userSessions = self.sessions[localpart]
     130-        session = userSessions[resource]
     131-        return session
     132+        @param entity: Entity to retrieve sessions for.
     133+        @type entity: L{JID<twisted.words.protocols.jabber.jid.JID>}
     134+
     135+        @return: C{UserSession}.
     136+        """
     137+
     138+        userSessions = self.lookupSessions(entity)
     139+        return userSessions[entity.resource]
     140+
     141 
     142 
     143     def unbindResource(self, session, reason=None):
    97144diff --git a/wokkel/test/test_xmppim.py b/wokkel/test/test_xmppim.py
    98145--- a/wokkel/test/test_xmppim.py
     
    241288 
    242289 
    243 @@ -1067,6 +1075,440 @@
     290@@ -1035,6 +1043,13 @@
     291         return element
     292 
     293 
     294+    @classmethod
     295+    def fromElement(cls, element):
     296+        stanza = super(Message, cls).fromElement(element)
     297+        stanza.stanzaType = stanza.stanzaType or 'normal'
     298+        return stanza
     299+
     300+
     301 
     302 class MessageProtocol(XMPPHandler):
     303     """
     304@@ -1067,6 +1082,441 @@
    244305 
    245306 
     
    270331+            return
    271332+
    272 +        try:
    273 +            recipient = JID(iq['to'])
    274 +        except KeyError:
     333+        stanza = Stanza.fromElement(iq)
     334+        recipient = stanza.recipient
     335+
     336+        if not recipient:
     337+            # This stanza doesn't have a recipient, ignore it.
    275338+            return
    276 +
    277 +        if not recipient.user:
     339+        elif not recipient.user:
    278340+            # This is not for an account, ignore it
    279341+            return
     
    285347+            return
    286348+
    287 +        userSessions = self.sessionManager.sessions.get(recipient.user,
    288 +                                                        {})
     349+        userSessions = self.sessionManager.lookupSessions(recipient)
    289350+        if recipient.resource in userSessions:
    290351+            self.sessionManager.deliverStanza(iq, recipient)
     
    292353+            # Full JID without connected resource, return error
    293354+            exc = error.StanzaError('service-unavailable')
    294 +            if iq['type'] in ('result', 'error'):
     355+            if stanza.stanzaType in ('result', 'error'):
    295356+                log.err(exc, 'Could not deliver IQ response')
    296357+            else:
     
    312373+
    313374+
    314 +    def onMessage(self, message):
     375+    def onMessage(self, element):
    315376+        """
    316377+        Handler for message stanzas to user accounts.
    317378+        """
    318379+
    319 +        if message.handled:
     380+        if element.handled:
    320381+            return
    321382+
     383+        message = Message.fromElement(element)
     384+        recipient = message.recipient
     385+        stanzaType = message.stanzaType or 'normal'
     386+
    322387+        try:
    323 +            recipient = JID(message['to'])
    324 +        except KeyError:
    325 +            return
    326 +
    327 +        stanzaType = message.getAttribute('type', 'normal')
    328 +
    329 +        try:
     388+            if not recipient:
     389+                # This stanza doesn't have a recipient, ignore it.
     390+                return
    330391+            if not recipient.user:
    331392+                # This is not for an account, ignore it
     
    335396+                return
    336397+            elif recipient.resource:
    337 +                userSessions = self.sessionManager.sessions.get(recipient.user,
    338 +                                                                {})
     398+                userSessions = self.sessionManager.lookupSessions(recipient)
    339399+                if recipient.resource in userSessions:
    340 +                    self.sessionManager.deliverStanza(message, recipient)
     400+                    self.sessionManager.deliverStanza(element, recipient)
    341401+                else:
    342402+                    if stanzaType in ('normal', 'chat', 'headline'):
    343 +                        self.onMessageBareJID(message, recipient.userhostJID())
     403+                        self.onMessageBareJID(message)
    344404+                    elif stanzaType == 'error':
    345405+                        log.msg("Dropping message to unconnected resource %r" %
     
    348408+                        raise error.StanzaError('service-unavailable')
    349409+            else:
    350 +                self.onMessageBareJID(message, recipient)
     410+                self.onMessageBareJID(message)
    351411+        except error.StanzaError, exc:
    352412+            if stanzaType == 'error':
    353413+                log.err(exc, "Undeliverable error")
    354414+            else:
    355 +                self.send(exc.toResponse(message))
    356 +
    357 +        message.handled = True
    358 +
    359 +
    360 +    def onMessageBareJID(self, message, bareJID):
    361 +        stanzaType = message.getAttribute('type', 'normal')
    362 +
    363 +        userSessions = self.sessionManager.sessions.get(bareJID.user, {})
     415+                self.send(exc.toResponse(element))
     416+
     417+        element.handled = True
     418+
     419+
     420+    def onMessageBareJID(self, message):
     421+        userSessions = self.sessionManager.lookupSessions(message.recipient)
    364422+
    365423+        recipients = set()
    366424+
    367 +        if stanzaType == 'headline':
    368 +            for session in userSessions:
     425+        if message.stanzaType == 'headline':
     426+            for session in userSessions.itervalues():
    369427+                if session.presence.priority >= 0:
    370428+                    recipients.add(session.entity)
    371 +        elif stanzaType in ('chat', 'normal'):
     429+        elif message.stanzaType in ('chat', 'normal'):
    372430+            priorities = {}
    373431+            for session in userSessions.itervalues():
     
    377435+                if priority >= 0:
    378436+                    priorities.setdefault(priority, set()).add(session.entity)
     437+            if priorities:
    379438+                maxPriority = max(priorities.keys())
    380439+                recipients.update(priorities[maxPriority])
    381 +        elif stanzaType == 'groupchat':
     440+        elif message.stanzaType == 'groupchat':
    382441+            raise error.StanzaError('service-unavailable')
    383442+
    384443+        if recipients:
    385444+            for recipient in recipients:
    386 +                self.sessionManager.deliverStanza(message, recipient)
    387 +        elif stanzaType in ('chat', 'normal'):
     445+                self.sessionManager.deliverStanza(message.element, recipient)
     446+        elif message.stanzaType in ('chat', 'normal'):
    388447+            raise error.StanzaError('service-unavailable')
    389448+        else:
    390449+            # silently discard
    391 +            log.msg("Discarding message to %r" % message['to'])
     450+            log.msg("Discarding message to %r" % message.recipient)
    392451+
    393452+
     
    535594+        # save presence
    536595+        self.presences[user][resource] = presence
    537 +        self.sessionManager.sessions[user][resource].presence = presence
     596+        session = self.sessionManager.lookupSession(fromJID)
     597+        session.presence = presence
    538598+
    539599+        return True
     
    556616+        toJID = presence.recipient
    557617+
    558 +        if toJID.user not in self.roster:
     618+        if not toJID.user:
     619+            # This is not for an account, ignore it.
    559620+            return False
    560 +
    561 +        if toJID.user in self.presences:
     621+        elif toJID.user not in self.roster:
     622+            # This is not for a known account, ignore it.
     623+            return False
     624+        elif toJID.user not in self.presences:
     625+            # No available resource, drop it.
     626+            return True
     627+        else:
    562628+            for resource in self.presences[toJID.user]:
    563629+                resourceJID = JID(tuple=(toJID.user,
     
    566632+                self.sessionManager.deliverStanza(presence.element, resourceJID)
    567633+            self.remotePresences[toJID.user][fromJID] = presence
    568 +        else:
    569 +            # no such user or no available resource, ignore this stanza
    570 +            pass
    571 +
    572 +        return True
     634+            return True
    573635+
    574636+
Note: See TracChangeset for help on using the changeset viewer.