Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
2532:772447ec070f | 2533:8d82a62fa098 |
---|---|
60 host.trigger.add("Presence send", self._presenceTrigger) | 60 host.trigger.add("Presence send", self._presenceTrigger) |
61 | 61 |
62 def getHandler(self, client): | 62 def getHandler(self, client): |
63 return XEP_0115_handler(self, client.profile) | 63 return XEP_0115_handler(self, client.profile) |
64 | 64 |
65 def _checkHash(self, disco_d, profile): | 65 @defer.inlineCallbacks |
66 client = self.host.getClient(profile) | 66 def profileConnected(self, client): |
67 client.caps_optimize = None | 67 # we have to calculate hash for client |
68 # because disco infos/identities may change between clients | |
68 | 69 |
69 if XEP_0115.cap_hash is None: | 70 # optimize check |
70 disco_d.addCallback(lambda dummy: self.host.hasFeature(client, NS_CAPS_OPTIMIZE)) | 71 client._caps_optimize = yield self.host.hasFeature(client, NS_CAPS_OPTIMIZE) |
71 def updateOptimize(optimize): | 72 if client._caps_optimize: |
72 client.caps_optimize = optimize | 73 log.info(_(u"Caps optimisation enabled")) |
73 if optimize: | 74 client._caps_sent = False |
74 log.info(_("Caps optimisation enabled")) | 75 else: |
75 client.caps_sent = False | 76 log.warning(_(u"Caps optimisation not available")) |
76 else: | |
77 log.warning(_("Caps optimisation not available")) | |
78 disco_d.addCallback(updateOptimize) | |
79 disco_d.addCallback(lambda dummy: self.recalculateHash(profile)) | |
80 return True | |
81 | 77 |
82 def _presenceTrigger(self, client, obj): | 78 # hash generation |
83 if XEP_0115.cap_hash is not None: | |
84 if client.caps_optimize: | |
85 if client.caps_sent: | |
86 return True | |
87 client.caps_sent = True | |
88 obj.addChild(XEP_0115.c_elt) | |
89 return True | |
90 | |
91 @defer.inlineCallbacks | |
92 def recalculateHash(self, profile): | |
93 client = self.host.getClient(profile) | |
94 _infos = yield client.discoHandler.info(client.jid, client.jid, '') | 79 _infos = yield client.discoHandler.info(client.jid, client.jid, '') |
95 disco_infos = disco.DiscoInfo() | 80 disco_infos = disco.DiscoInfo() |
96 for item in _infos: | 81 for item in _infos: |
97 disco_infos.append(item) | 82 disco_infos.append(item) |
98 cap_hash = self.host.memory.disco.generateHash(disco_infos) | 83 disco_infos = disco.DiscoInfo() |
99 log.info("Our capability hash has been generated: [%s]" % cap_hash) | 84 cap_hash = client._caps_hash = self.host.memory.disco.generateHash(disco_infos) |
85 log.info("Our capability hash has been generated: [{cap_hash}]".format( | |
86 cap_hash = cap_hash)) | |
100 log.debug("Generating capability domish.Element") | 87 log.debug("Generating capability domish.Element") |
101 c_elt = domish.Element((NS_ENTITY_CAPABILITY, 'c')) | 88 c_elt = domish.Element((NS_ENTITY_CAPABILITY, 'c')) |
102 c_elt['hash'] = 'sha-1' | 89 c_elt['hash'] = 'sha-1' |
103 c_elt['node'] = C.APP_URL | 90 c_elt['node'] = C.APP_URL |
104 c_elt['ver'] = cap_hash | 91 c_elt['ver'] = cap_hash |
105 XEP_0115.cap_hash = cap_hash | 92 client._caps_elt = c_elt |
106 XEP_0115.c_elt = c_elt | 93 if client._caps_optimize: |
107 if client.caps_optimize: | 94 client._caps_sent = False |
108 client.caps_sent = False | |
109 if cap_hash not in self.host.memory.disco.hashes: | 95 if cap_hash not in self.host.memory.disco.hashes: |
110 self.host.memory.disco.hashes[cap_hash] = disco_infos | 96 self.host.memory.disco.hashes[cap_hash] = disco_infos |
111 self.host.memory.updateEntityData(client.jid, C.ENTITY_CAP_HASH, cap_hash, profile_key=profile) | 97 self.host.memory.updateEntityData(client.jid, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile) |
98 | |
99 def _presenceTrigger(self, client, obj): | |
100 if client._caps_optimize: | |
101 if client._caps_sent: | |
102 return True | |
103 client.caps_sent = True | |
104 obj.addChild(client._caps_elt) | |
105 return True | |
112 | 106 |
113 | 107 |
114 class XEP_0115_handler(XMPPHandler): | 108 class XEP_0115_handler(XMPPHandler): |
115 implements(iwokkel.IDisco) | 109 implements(iwokkel.IDisco) |
116 | 110 |