diff src/plugins/plugin_xep_0115.py @ 2533:8d82a62fa098

core (disco), plugin XEP-0115: client use + capabilities hash improvment: - modified a couple of method from memory to use client instead of profile - XEP-0115: don't use unique hash for all clients anymore, as disco can be different between clients/components, and even between clients (different identity for instance). hash is now generated per client on each new session.
author Goffi <goffi@goffi.org>
date Sat, 24 Mar 2018 10:44:37 +0100
parents 67cc54b01a12
children 60758de1c227
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0115.py	Wed Mar 21 19:13:22 2018 +0100
+++ b/src/plugins/plugin_xep_0115.py	Sat Mar 24 10:44:37 2018 +0100
@@ -62,53 +62,47 @@
     def getHandler(self, client):
         return XEP_0115_handler(self, client.profile)
 
-    def _checkHash(self, disco_d, profile):
-        client = self.host.getClient(profile)
-        client.caps_optimize = None
+    @defer.inlineCallbacks
+    def profileConnected(self, client):
+        # we have to calculate hash for client
+        # because disco infos/identities may change between clients
 
-        if XEP_0115.cap_hash is None:
-            disco_d.addCallback(lambda dummy: self.host.hasFeature(client, NS_CAPS_OPTIMIZE))
-            def updateOptimize(optimize):
-                client.caps_optimize = optimize
-                if optimize:
-                    log.info(_("Caps optimisation enabled"))
-                    client.caps_sent = False
-                else:
-                    log.warning(_("Caps optimisation not available"))
-            disco_d.addCallback(updateOptimize)
-            disco_d.addCallback(lambda dummy: self.recalculateHash(profile))
-        return True
+        # optimize check
+        client._caps_optimize = yield self.host.hasFeature(client, NS_CAPS_OPTIMIZE)
+        if client._caps_optimize:
+            log.info(_(u"Caps optimisation enabled"))
+            client._caps_sent = False
+        else:
+            log.warning(_(u"Caps optimisation not available"))
 
-    def _presenceTrigger(self, client, obj):
-        if XEP_0115.cap_hash is not None:
-            if client.caps_optimize:
-                if client.caps_sent:
-                    return True
-                client.caps_sent = True
-            obj.addChild(XEP_0115.c_elt)
-        return True
-
-    @defer.inlineCallbacks
-    def recalculateHash(self, profile):
-        client = self.host.getClient(profile)
+        # hash generation
         _infos = yield client.discoHandler.info(client.jid, client.jid, '')
         disco_infos = disco.DiscoInfo()
         for item in _infos:
             disco_infos.append(item)
-        cap_hash = self.host.memory.disco.generateHash(disco_infos)
-        log.info("Our capability hash has been generated: [%s]" % cap_hash)
+        disco_infos = disco.DiscoInfo()
+        cap_hash = client._caps_hash = self.host.memory.disco.generateHash(disco_infos)
+        log.info("Our capability hash has been generated: [{cap_hash}]".format(
+            cap_hash = cap_hash))
         log.debug("Generating capability domish.Element")
         c_elt = domish.Element((NS_ENTITY_CAPABILITY, 'c'))
         c_elt['hash'] = 'sha-1'
         c_elt['node'] = C.APP_URL
         c_elt['ver'] = cap_hash
-        XEP_0115.cap_hash = cap_hash
-        XEP_0115.c_elt = c_elt
-        if client.caps_optimize:
-            client.caps_sent = False
+        client._caps_elt = c_elt
+        if client._caps_optimize:
+            client._caps_sent = False
         if cap_hash not in self.host.memory.disco.hashes:
             self.host.memory.disco.hashes[cap_hash] = disco_infos
-            self.host.memory.updateEntityData(client.jid, C.ENTITY_CAP_HASH, cap_hash, profile_key=profile)
+            self.host.memory.updateEntityData(client.jid, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile)
+
+    def _presenceTrigger(self, client, obj):
+        if client._caps_optimize:
+            if client._caps_sent:
+                return True
+            client.caps_sent = True
+        obj.addChild(client._caps_elt)
+        return True
 
 
 class XEP_0115_handler(XMPPHandler):