annotate libervia/backend/plugins/plugin_xep_0115.py @ 4119:ece304ec7077

plugin XEP-0167: fix key used to store metadata: rel 424
author Goffi <goffi@goffi.org>
date Tue, 03 Oct 2023 15:18:25 +0200
parents 4b842c1fb686
children 0d7bb4df2343
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
4 # SAT plugin for managing xep-0115
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3254
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
10 # (at your option) any later version.
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
15 # GNU Affero General Public License for more details.
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
19
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
20 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
21 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
22 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
23
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
24 log = getLogger(__name__)
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.xish import domish
941
c6d8fc63b1db core, plugins: host.getClient now raise an exception instead of returning None when no profile is found, plugins have been adapted consequently and a bit cleaned
Goffi <goffi@goffi.org>
parents: 916
diff changeset
26 from twisted.words.protocols.jabber import jid
1244
c3884a63efde plugin XEP-0115: better disco error handling
souliane <souliane@mailoo.org>
parents: 1232
diff changeset
27 from twisted.internet import defer, error
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
28 from zope.interface import implementer
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from wokkel import disco, iwokkel
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
30
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
31 try:
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from twisted.words.protocols.xmlstream import XMPPHandler
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
33 except ImportError:
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from wokkel.subprotocols import XMPPHandler
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
35
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
36 PRESENCE = "/presence"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
37 NS_ENTITY_CAPABILITY = "http://jabber.org/protocol/caps"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
38 NS_CAPS_OPTIMIZE = "http://jabber.org/protocol/caps#optimize"
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
39 CAPABILITY_UPDATE = PRESENCE + '/c[@xmlns="' + NS_ENTITY_CAPABILITY + '"]'
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
40
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
41 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
42 C.PI_NAME: "XEP 0115 Plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
43 C.PI_IMPORT_NAME: "XEP-0115",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
44 C.PI_TYPE: "XEP",
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
45 C.PI_MODES: C.PLUG_MODE_BOTH,
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
46 C.PI_PROTOCOLS: ["XEP-0115"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
47 C.PI_DEPENDENCIES: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
48 C.PI_MAIN: "XEP_0115",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
49 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
50 C.PI_DESCRIPTION: _("""Implementation of entity capabilities"""),
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
51 }
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
52
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
53
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
54 class XEP_0115(object):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
55 cap_hash = None # capabilities hash is class variable as it is common to all profiles
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
56
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
58 log.info(_("Plugin XEP_0115 initialization"))
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
60 host.trigger.add("Presence send", self._presence_trigger)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
61
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
62 def get_handler(self, client):
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
63 return XEP_0115_handler(self)
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
64
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
65 @defer.inlineCallbacks
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
66 def _prepare_caps(self, client):
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
67 # we have to calculate hash for client
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
68 # because disco infos/identities may change between clients
957
2eb4fef29795 plulgin XEP-0115: fixed caps_optimize init
Goffi <goffi@goffi.org>
parents: 953
diff changeset
69
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
70 # optimize check
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
71 client._caps_optimize = yield self.host.hasFeature(client, NS_CAPS_OPTIMIZE)
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
72 if client._caps_optimize:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
73 log.info(_("Caps optimisation enabled"))
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
74 client._caps_sent = False
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
75 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
76 log.warning(_("Caps optimisation not available"))
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
77
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
78 # hash generation
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
79 _infos = yield client.discoHandler.info(client.jid, client.jid, "")
962
b280cf4cdf25 plugin XEP-0115: fixed bad disco_infos construction
Goffi <goffi@goffi.org>
parents: 957
diff changeset
80 disco_infos = disco.DiscoInfo()
b280cf4cdf25 plugin XEP-0115: fixed bad disco_infos construction
Goffi <goffi@goffi.org>
parents: 957
diff changeset
81 for item in _infos:
b280cf4cdf25 plugin XEP-0115: fixed bad disco_infos construction
Goffi <goffi@goffi.org>
parents: 957
diff changeset
82 disco_infos.append(item)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
83 cap_hash = client._caps_hash = self.host.memory.disco.generate_hash(disco_infos)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
84 log.info(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
85 "Our capability hash has been generated: [{cap_hash}]".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
86 cap_hash=cap_hash
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
87 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
88 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
89 log.debug("Generating capability domish.Element")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
90 c_elt = domish.Element((NS_ENTITY_CAPABILITY, "c"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
91 c_elt["hash"] = "sha-1"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
92 c_elt["node"] = C.APP_URL
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
93 c_elt["ver"] = cap_hash
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
94 client._caps_elt = c_elt
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
95 if client._caps_optimize:
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
96 client._caps_sent = False
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
97 if cap_hash not in self.host.memory.disco.hashes:
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
98 self.host.memory.disco.hashes[cap_hash] = disco_infos
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
99 self.host.memory.update_entity_data(
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
100 client, client.jid, C.ENTITY_CAP_HASH, cap_hash
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
101 )
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
102
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
103 def _presence_add_elt(self, client, obj):
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
104 if client._caps_optimize:
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
105 if client._caps_sent:
2543
60758de1c227 plugin XEP-0115: fixed caps initial check
Goffi <goffi@goffi.org>
parents: 2533
diff changeset
106 return
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
107 client.caps_sent = True
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
108 obj.addChild(client._caps_elt)
2543
60758de1c227 plugin XEP-0115: fixed caps initial check
Goffi <goffi@goffi.org>
parents: 2533
diff changeset
109
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
110 def _presence_trigger(self, client, obj, presence_d):
2543
60758de1c227 plugin XEP-0115: fixed caps initial check
Goffi <goffi@goffi.org>
parents: 2533
diff changeset
111 if not hasattr(client, "_caps_optimize"):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
112 presence_d.addCallback(lambda __: self._prepare_caps(client))
2543
60758de1c227 plugin XEP-0115: fixed caps initial check
Goffi <goffi@goffi.org>
parents: 2533
diff changeset
113
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
114 presence_d.addCallback(lambda __: self._presence_add_elt(client, obj))
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
115 return True
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
116
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
117
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
118 @implementer(iwokkel.IDisco)
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
119 class XEP_0115_handler(XMPPHandler):
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
120
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
121 def __init__(self, plugin_parent):
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
122 self.plugin_parent = plugin_parent
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
123 self.host = plugin_parent.host
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
124
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
125 @property
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
126 def client(self):
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
127 return self.parent
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
128
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
129 def connectionInitialized(self):
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
130 self.xmlstream.addObserver(CAPABILITY_UPDATE, self.update)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
131
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
132 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
133 return [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
134 disco.DiscoFeature(NS_ENTITY_CAPABILITY),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
135 disco.DiscoFeature(NS_CAPS_OPTIMIZE),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
136 ]
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
137
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
138 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
139 return []
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
140
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def update(self, presence):
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
142 """
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
143 Manage the capabilities of the entity
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
144
1244
c3884a63efde plugin XEP-0115: better disco error handling
souliane <souliane@mailoo.org>
parents: 1232
diff changeset
145 Check if we know the version of this capabilities and get the capabilities if necessary
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
146 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
147 from_jid = jid.JID(presence["from"])
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
148 c_elem = next(presence.elements(NS_ENTITY_CAPABILITY, "c"))
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
149 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
150 c_ver = c_elem["ver"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
151 c_hash = c_elem["hash"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
152 c_node = c_elem["node"]
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
153 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
154 log.warning(_("Received invalid capabilities tag: %s") % c_elem.toXml())
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
155 return
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
156
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
157 if c_ver in self.host.memory.disco.hashes:
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
158 # we already know the hash, we update the jid entity
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
159 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
160 "hash [%(hash)s] already in cache, updating entity [%(jid)s]"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
161 % {"hash": c_ver, "jid": from_jid.full()}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
162 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
163 self.host.memory.update_entity_data(
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
164 self.client, from_jid, C.ENTITY_CAP_HASH, c_ver
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
165 )
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
166 return
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
167
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
168 if c_hash != "sha-1": # unknown hash method
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
169 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
170 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
171 "Unknown hash method for entity capabilities: [{hash_method}] "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
172 "(entity: {entity_jid}, node: {node})"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
173 )
2830
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
174 .format(hash_method = c_hash, entity_jid = from_jid, node = c_node)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
175 )
1244
c3884a63efde plugin XEP-0115: better disco error handling
souliane <souliane@mailoo.org>
parents: 1232
diff changeset
176
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
177 def cb(__):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
178 computed_hash = self.host.memory.get_entity_datum(
3254
6cf4bd6972c2 core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
179 self.client, from_jid, C.ENTITY_CAP_HASH
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
180 )
1244
c3884a63efde plugin XEP-0115: better disco error handling
souliane <souliane@mailoo.org>
parents: 1232
diff changeset
181 if computed_hash != c_ver:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
182 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
183 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
184 "Computed hash differ from given hash:\n"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
185 "given: [{given}]\n"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
186 "computed: [{computed}]\n"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
187 "(entity: {entity_jid}, node: {node})"
2830
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
188 ).format(
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
189 given = c_ver,
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
190 computed = computed_hash,
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
191 entity_jid = from_jid,
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
192 node = c_node,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
193 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
194 )
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
195
1244
c3884a63efde plugin XEP-0115: better disco error handling
souliane <souliane@mailoo.org>
parents: 1232
diff changeset
196 def eb(failure):
c3884a63efde plugin XEP-0115: better disco error handling
souliane <souliane@mailoo.org>
parents: 1232
diff changeset
197 if isinstance(failure.value, error.ConnectionDone):
c3884a63efde plugin XEP-0115: better disco error handling
souliane <souliane@mailoo.org>
parents: 1232
diff changeset
198 return
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
199 msg = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
200 failure.value.condition
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
201 if hasattr(failure.value, "condition")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
202 else failure.getErrorMessage()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
203 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
204 log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
205 _("Couldn't retrieve disco info for {jid}: {error}").format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
206 jid=from_jid.full(), error=msg
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
207 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
208 )
1244
c3884a63efde plugin XEP-0115: better disco error handling
souliane <souliane@mailoo.org>
parents: 1232
diff changeset
209
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
210 d = self.host.get_disco_infos(self.parent, from_jid)
1244
c3884a63efde plugin XEP-0115: better disco error handling
souliane <souliane@mailoo.org>
parents: 1232
diff changeset
211 d.addCallbacks(cb, eb)
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
212 # TODO: me must manage the full algorithm described at XEP-0115 #5.4 part 3