Changeset 40:75ee881b216f in ralphm-patches


Ignore:
Timestamp:
Feb 22, 2010, 6:21:30 PM (10 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Fix up outbound presence handling, prepare for presence subs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • xmpp_client_service.patch

    r39 r40  
    11diff -r 62f841ed2a99 doc/examples/client_service.tac
    22--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
    3 +++ b/doc/examples/client_service.tac   Sun Feb 21 14:28:09 2010 +0100
     3+++ b/doc/examples/client_service.tac   Mon Feb 22 18:20:52 2010 +0100
    44@@ -0,0 +1,74 @@
    55+from twisted.application import service, strports
     
    7979diff -r 62f841ed2a99 wokkel/client.py
    8080--- a/wokkel/client.py  Sat Feb 13 18:57:26 2010 +0100
    81 +++ b/wokkel/client.py  Sun Feb 21 14:28:09 2010 +0100
     81+++ b/wokkel/client.py  Mon Feb 22 18:20:52 2010 +0100
    8282@@ -10,13 +10,27 @@
    8383 that should probably eventually move there.
     
    9494+from twisted.words.protocols.jabber import client, error, sasl, xmlstream
    9595+from twisted.words.protocols.jabber.jid import JID, internJID
    96 +from twisted.words.xish import domish
     96+from twisted.words.xish import domish, utility
    9797 
    9898 from wokkel import generic
     
    120120     def __init__(self, jid, password):
    121121         xmlstream.ConnectAuthenticator.__init__(self, jid.host)
    122 @@ -186,3 +200,338 @@
     122@@ -186,3 +200,349 @@
    123123     c = XMPPClientConnector(reactor, domain, factory)
    124124     c.connect()
     
    398398+        self.connectionManager = None
    399399+        self.sessions = {}
     400+        self.clientStream = utility.EventDispatcher()
     401+        self.clientStream.addObserver('/*', self.routeOrDeliver, -1)
    400402+
    401403+
     
    445447+            element['from'] = sender.full()
    446448+
     449+        self.clientStream.dispatch(element)
     450+
     451+
     452+    def routeOrDeliver(self, element):
     453+        if element.handled:
     454+            return
     455+
    447456+        if (not element.hasAttribute('to') or
    448 +                internJID(element['to']).host == self.domain):
     457+            internJID(element['to']).host == self.domain):
    449458+            # This stanza is for local delivery
     459+            log.msg("Delivering locally: %r" % element.toXml())
    450460+            self.xmlstream.dispatch(element)
    451461+        else:
    452 +            # This stanza is for remote delivery
     462+            # This stanza is for remote routing
     463+            log.msg("Routing remotely: %r" % element.toXml())
    453464+            self.xmlstream.send(element)
    454465+
     
    461472diff -r 62f841ed2a99 wokkel/component.py
    462473--- a/wokkel/component.py       Sat Feb 13 18:57:26 2010 +0100
    463 +++ b/wokkel/component.py       Sun Feb 21 14:28:09 2010 +0100
     474+++ b/wokkel/component.py       Mon Feb 22 18:20:52 2010 +0100
    464475@@ -313,12 +313,24 @@
    465476         """
     
    491502diff -r 62f841ed2a99 wokkel/xmppim.py
    492503--- a/wokkel/xmppim.py  Sat Feb 13 18:57:26 2010 +0100
    493 +++ b/wokkel/xmppim.py  Sun Feb 21 14:28:09 2010 +0100
     504+++ b/wokkel/xmppim.py  Mon Feb 22 18:20:52 2010 +0100
    494505@@ -12,8 +12,11 @@
    495506 All of it should eventually move to Twisted.
     
    651662         self.send(Presence(to=entity, type='unsubscribed'))
    652663 
    653 @@ -478,7 +481,7 @@
     664@@ -395,7 +398,10 @@
     665         self.xmlstream.addObserver("/presence", self._onPresence)
     666 
     667 
     668-    def _onPresence(self, element):
     669+    def parsePresence(self, element):
     670+        """
     671+        Parse presence.
     672+        """
     673         stanza = Stanza.fromElement(element)
     674 
     675         presenceType = stanza.stanzaType or 'available'
     676@@ -405,7 +411,12 @@
     677         except KeyError:
     678             return
     679 
     680-        presence = parser.fromElement(element)
     681+        return parser.fromElement(element)
     682+
     683+
     684+    def _onPresence(self, element):
     685+        presence = self.parsePresence(element)
     686+        presenceType = presence.stanzaType or 'available'
     687 
     688         try:
     689             handler = getattr(self, '%sReceived' % presenceType)
     690@@ -478,7 +489,7 @@
    654691 
    655692         @param recipient: Optional Recipient to which the presence should be
     
    660697         @param show: Optional detailed presence information. One of C{'away'},
    661698             C{'xa'}, C{'chat'}, C{'dnd'}.
    662 @@ -503,7 +506,7 @@
     699@@ -503,7 +514,7 @@
    663700         Send unavailable presence.
    664701 
     
    669706         @param statuses: dictionary of natural language descriptions of the
    670707             availability status, keyed by the language descriptor. A status
    671 @@ -520,7 +523,7 @@
     708@@ -520,7 +531,7 @@
    672709         Send subscription request
    673710 
     
    678715         presence = SubscriptionPresence(recipient=recipient, sender=sender)
    679716         presence.stanzaType = 'subscribe'
    680 @@ -532,7 +535,7 @@
     717@@ -532,7 +543,7 @@
    681718         Send unsubscription request
    682719 
     
    687724         presence = SubscriptionPresence(recipient=recipient, sender=sender)
    688725         presence.stanzaType = 'unsubscribe'
    689 @@ -544,7 +547,7 @@
     726@@ -544,7 +555,7 @@
    690727         Send subscription confirmation.
    691728 
     
    696733         presence = SubscriptionPresence(recipient=recipient, sender=sender)
    697734         presence.stanzaType = 'subscribed'
    698 @@ -556,7 +559,7 @@
     735@@ -556,7 +567,7 @@
    699736         Send unsubscription confirmation.
    700737 
     
    705742         presence = SubscriptionPresence(recipient=recipient, sender=sender)
    706743         presence.stanzaType = 'unsubscribed'
    707 @@ -568,7 +571,7 @@
     744@@ -568,7 +579,7 @@
    708745         Send presence probe.
    709746 
     
    714751         presence = ProbePresence(recipient=recipient, sender=sender)
    715752         self.send(presence.toElement())
    716 @@ -652,7 +655,7 @@
     753@@ -652,7 +663,7 @@
    717754 
    718755 
     
    723760         item.name = element.getAttribute('name')
    724761         subscription = element.getAttribute('subscription')
    725 @@ -715,7 +718,7 @@
     762@@ -715,7 +726,7 @@
    726763         itemElement = iq.query.item
    727764 
     
    732769             item = self._parseRosterItem(iq.query.item)
    733770             self.onRosterSet(item)
    734 @@ -763,7 +766,7 @@
     771@@ -763,7 +774,7 @@
    735772     def _onRosterGet(self, iq):
    736773         iq.handled = True
     
    741778         d.addErrback(lambda _: error.ErrorStanza('internal-error').toResponse(iq))
    742779         d.addBoth(self.send)
    743 @@ -808,3 +811,380 @@
     780@@ -808,3 +819,416 @@
    744781         """
    745782         Called when a message stanza was received.
     
    863900+
    864901+        userSessions = self.sessionManager.sessions.get(bareJID.user, {})
    865 +        print userSessions
    866902+
    867903+        recipients = set()
     
    927963+        self.remotePresences = {} # user -> remote entity -> presence
    928964+
     965+        self.sessionManager.clientStream.addObserver('/presence',
     966+                                                     self._onPresenceOutbound)
     967+
    929968+
    930969+    def _broadcastToOtherResources(self, presence):
     
    10671106+
    10681107+
    1069 +    def getDirection(self, presence):
    1070 +        if not presence.recipient:
    1071 +            if presence.sender.host == self.domain:
    1072 +                # broadcast presence from local domain
    1073 +                return 'Broadcast'
    1074 +            else:
    1075 +                raise Exception("Unexpected missing to address")
    1076 +        else:
    1077 +            if presence.sender.host == self.domain:
    1078 +                # directed presence from local domain
    1079 +                return 'Directed'
    1080 +            elif presence.recipient.host == self.domain:
    1081 +                # incoming remote presence
    1082 +                return 'Inbound'
    1083 +            else:
    1084 +                raise Exception("Badly routed presence")
     1108+    def _onPresenceOutbound(self, element):
     1109+        log.msg("Got outbound presence: %r" % element.toXml())
     1110+        presence = self.parsePresence(element)
     1111+
     1112+        presenceType = presence.stanzaType or 'available'
     1113+        method = '%sReceivedOutbound' % presenceType
     1114+        print method
     1115+
     1116+        try:
     1117+            handler = getattr(self, method)
     1118+        except AttributeError:
     1119+            return
     1120+        else:
     1121+            element.handled = True
     1122+            handler(presence)
     1123+
    10851124+
    10861125+    def availableReceived(self, presence):
    1087 +        direction = self.getDirection(presence)
    1088 +        handler = getattr(self, "_on_available%s" % direction)
    1089 +        if handler:
    1090 +            handler(presence)
    1091 +            presence.handled = True
    1092 +        else:
    1093 +            print "Unhandled: %r" % presence.element.toXml()
    1094 +
    1095 +
    1096 +    def unavailableReceived(self, presence):
    1097 +        direction = self.getDirection(presence)
    1098 +        handler = getattr(self, "_on_unavailable%s" % direction)
    1099 +        if handler:
    1100 +            handler(presence)
    1101 +            presence.handled = True
    1102 +        else:
    1103 +            print "Unhandled: %r" % presence.element.toXml()
    1104 +
     1126+        self._on_availableInbound(presence)
     1127+
     1128+
     1129+    def availableReceivedOutbound(self, presence):
     1130+        if presence.recipient:
     1131+            pass # self._on_availableDirected(presence)
     1132+        else:
     1133+            self._on_availableBroadcast(presence)
     1134+
     1135+
     1136+#    def unavailableReceived(self, presence):
     1137+#        self._on_unavailableInbound(presence)
     1138+
     1139+
     1140+    def unavailableReceivedOutbound(self, presence):
     1141+        if presence.recipient:
     1142+            pass # self._on_unavailableDirected(presence)
     1143+        else:
     1144+            self._on_unavailableBroadcast(presence)
     1145+
     1146+
     1147+    def subscribedReceivedOutbound(self, presence):
     1148+        log.msg("%r subscribed %s to its presence" % (presence.sender,
     1149+                                                      presence.recipient))
     1150+        #self.send(presence.element)
     1151+
     1152+
     1153+    def unsubscribedReceivedOutbound(self, presence):
     1154+        log.msg("%r unsubscribed %s from its presence" % (presence.sender,
     1155+                                                          presence.recipient))
     1156+        #self.send(presence.element)
     1157+
     1158+
     1159+    def subscribeReceived(self, presence):
     1160+        log.msg("%r requests subscription to %s" % (presence.sender,
     1161+                                                    presence.recipient))
     1162+
     1163+
     1164+    def subscribeReceivedOutbound(self, presence):
     1165+        log.msg("%r requests subscription to %s" % (presence.sender,
     1166+                                                    presence.recipient))
     1167+        self.send(presence.element)
     1168+
     1169+
     1170+    def unsubscribeReceived(self, presence):
     1171+        log.msg("%r requests unsubscription from %s" % (presence.sender,
     1172+                                                        presence.recipient))
     1173+
     1174+    def unsubscribeReceivedOutbound(self, presence):
     1175+        log.msg("%r requests unsubscription from %s" % (presence.sender,
     1176+                                                        presence.recipient))
     1177+        self.send(presence.element)
    11051178+
    11061179+
Note: See TracChangeset for help on using the changeset viewer.