Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_xep_0115.py @ 4071:4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 11:49:51 +0200 |
parents | sat/plugins/plugin_xep_0115.py@524856bd7b19 |
children | 0d7bb4df2343 |
comparison
equal
deleted
inserted
replaced
4070:d10748475025 | 4071:4b842c1fb686 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # SAT plugin for managing xep-0115 | |
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from libervia.backend.core.i18n import _ | |
21 from libervia.backend.core.constants import Const as C | |
22 from libervia.backend.core.log import getLogger | |
23 | |
24 log = getLogger(__name__) | |
25 from twisted.words.xish import domish | |
26 from twisted.words.protocols.jabber import jid | |
27 from twisted.internet import defer, error | |
28 from zope.interface import implementer | |
29 from wokkel import disco, iwokkel | |
30 | |
31 try: | |
32 from twisted.words.protocols.xmlstream import XMPPHandler | |
33 except ImportError: | |
34 from wokkel.subprotocols import XMPPHandler | |
35 | |
36 PRESENCE = "/presence" | |
37 NS_ENTITY_CAPABILITY = "http://jabber.org/protocol/caps" | |
38 NS_CAPS_OPTIMIZE = "http://jabber.org/protocol/caps#optimize" | |
39 CAPABILITY_UPDATE = PRESENCE + '/c[@xmlns="' + NS_ENTITY_CAPABILITY + '"]' | |
40 | |
41 PLUGIN_INFO = { | |
42 C.PI_NAME: "XEP 0115 Plugin", | |
43 C.PI_IMPORT_NAME: "XEP-0115", | |
44 C.PI_TYPE: "XEP", | |
45 C.PI_MODES: C.PLUG_MODE_BOTH, | |
46 C.PI_PROTOCOLS: ["XEP-0115"], | |
47 C.PI_DEPENDENCIES: [], | |
48 C.PI_MAIN: "XEP_0115", | |
49 C.PI_HANDLER: "yes", | |
50 C.PI_DESCRIPTION: _("""Implementation of entity capabilities"""), | |
51 } | |
52 | |
53 | |
54 class XEP_0115(object): | |
55 cap_hash = None # capabilities hash is class variable as it is common to all profiles | |
56 | |
57 def __init__(self, host): | |
58 log.info(_("Plugin XEP_0115 initialization")) | |
59 self.host = host | |
60 host.trigger.add("Presence send", self._presence_trigger) | |
61 | |
62 def get_handler(self, client): | |
63 return XEP_0115_handler(self) | |
64 | |
65 @defer.inlineCallbacks | |
66 def _prepare_caps(self, client): | |
67 # we have to calculate hash for client | |
68 # because disco infos/identities may change between clients | |
69 | |
70 # optimize check | |
71 client._caps_optimize = yield self.host.hasFeature(client, NS_CAPS_OPTIMIZE) | |
72 if client._caps_optimize: | |
73 log.info(_("Caps optimisation enabled")) | |
74 client._caps_sent = False | |
75 else: | |
76 log.warning(_("Caps optimisation not available")) | |
77 | |
78 # hash generation | |
79 _infos = yield client.discoHandler.info(client.jid, client.jid, "") | |
80 disco_infos = disco.DiscoInfo() | |
81 for item in _infos: | |
82 disco_infos.append(item) | |
83 cap_hash = client._caps_hash = self.host.memory.disco.generate_hash(disco_infos) | |
84 log.info( | |
85 "Our capability hash has been generated: [{cap_hash}]".format( | |
86 cap_hash=cap_hash | |
87 ) | |
88 ) | |
89 log.debug("Generating capability domish.Element") | |
90 c_elt = domish.Element((NS_ENTITY_CAPABILITY, "c")) | |
91 c_elt["hash"] = "sha-1" | |
92 c_elt["node"] = C.APP_URL | |
93 c_elt["ver"] = cap_hash | |
94 client._caps_elt = c_elt | |
95 if client._caps_optimize: | |
96 client._caps_sent = False | |
97 if cap_hash not in self.host.memory.disco.hashes: | |
98 self.host.memory.disco.hashes[cap_hash] = disco_infos | |
99 self.host.memory.update_entity_data( | |
100 client, client.jid, C.ENTITY_CAP_HASH, cap_hash | |
101 ) | |
102 | |
103 def _presence_add_elt(self, client, obj): | |
104 if client._caps_optimize: | |
105 if client._caps_sent: | |
106 return | |
107 client.caps_sent = True | |
108 obj.addChild(client._caps_elt) | |
109 | |
110 def _presence_trigger(self, client, obj, presence_d): | |
111 if not hasattr(client, "_caps_optimize"): | |
112 presence_d.addCallback(lambda __: self._prepare_caps(client)) | |
113 | |
114 presence_d.addCallback(lambda __: self._presence_add_elt(client, obj)) | |
115 return True | |
116 | |
117 | |
118 @implementer(iwokkel.IDisco) | |
119 class XEP_0115_handler(XMPPHandler): | |
120 | |
121 def __init__(self, plugin_parent): | |
122 self.plugin_parent = plugin_parent | |
123 self.host = plugin_parent.host | |
124 | |
125 @property | |
126 def client(self): | |
127 return self.parent | |
128 | |
129 def connectionInitialized(self): | |
130 self.xmlstream.addObserver(CAPABILITY_UPDATE, self.update) | |
131 | |
132 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): | |
133 return [ | |
134 disco.DiscoFeature(NS_ENTITY_CAPABILITY), | |
135 disco.DiscoFeature(NS_CAPS_OPTIMIZE), | |
136 ] | |
137 | |
138 def getDiscoItems(self, requestor, target, nodeIdentifier=""): | |
139 return [] | |
140 | |
141 def update(self, presence): | |
142 """ | |
143 Manage the capabilities of the entity | |
144 | |
145 Check if we know the version of this capabilities and get the capabilities if necessary | |
146 """ | |
147 from_jid = jid.JID(presence["from"]) | |
148 c_elem = next(presence.elements(NS_ENTITY_CAPABILITY, "c")) | |
149 try: | |
150 c_ver = c_elem["ver"] | |
151 c_hash = c_elem["hash"] | |
152 c_node = c_elem["node"] | |
153 except KeyError: | |
154 log.warning(_("Received invalid capabilities tag: %s") % c_elem.toXml()) | |
155 return | |
156 | |
157 if c_ver in self.host.memory.disco.hashes: | |
158 # we already know the hash, we update the jid entity | |
159 log.debug( | |
160 "hash [%(hash)s] already in cache, updating entity [%(jid)s]" | |
161 % {"hash": c_ver, "jid": from_jid.full()} | |
162 ) | |
163 self.host.memory.update_entity_data( | |
164 self.client, from_jid, C.ENTITY_CAP_HASH, c_ver | |
165 ) | |
166 return | |
167 | |
168 if c_hash != "sha-1": # unknown hash method | |
169 log.warning( | |
170 _( | |
171 "Unknown hash method for entity capabilities: [{hash_method}] " | |
172 "(entity: {entity_jid}, node: {node})" | |
173 ) | |
174 .format(hash_method = c_hash, entity_jid = from_jid, node = c_node) | |
175 ) | |
176 | |
177 def cb(__): | |
178 computed_hash = self.host.memory.get_entity_datum( | |
179 self.client, from_jid, C.ENTITY_CAP_HASH | |
180 ) | |
181 if computed_hash != c_ver: | |
182 log.warning( | |
183 _( | |
184 "Computed hash differ from given hash:\n" | |
185 "given: [{given}]\n" | |
186 "computed: [{computed}]\n" | |
187 "(entity: {entity_jid}, node: {node})" | |
188 ).format( | |
189 given = c_ver, | |
190 computed = computed_hash, | |
191 entity_jid = from_jid, | |
192 node = c_node, | |
193 ) | |
194 ) | |
195 | |
196 def eb(failure): | |
197 if isinstance(failure.value, error.ConnectionDone): | |
198 return | |
199 msg = ( | |
200 failure.value.condition | |
201 if hasattr(failure.value, "condition") | |
202 else failure.getErrorMessage() | |
203 ) | |
204 log.error( | |
205 _("Couldn't retrieve disco info for {jid}: {error}").format( | |
206 jid=from_jid.full(), error=msg | |
207 ) | |
208 ) | |
209 | |
210 d = self.host.get_disco_infos(self.parent, from_jid) | |
211 d.addCallbacks(cb, eb) | |
212 # TODO: me must manage the full algorithm described at XEP-0115 #5.4 part 3 |