annotate sat/memory/disco.py @ 3034:ca6c695c65da

tools (common/email): fixed email sending after python 3 port
author Goffi <goffi@goffi.org>
date Fri, 16 Aug 2019 17:06:52 +0200
parents ab2696e34d29
children fee60f17ebac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
1 #!/usr/bin/env python3
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
3
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
4 # SAT: a jabber client
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2768
diff changeset
5 # Copyright (C) 2009-2019 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
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 667
diff changeset
20 from sat.core.i18n import _
951
027a054c6dda core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents: 944
diff changeset
21 from sat.core import exceptions
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 973
diff changeset
22 from sat.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: 973
diff changeset
24 log = getLogger(__name__)
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
25 from twisted.words.protocols.jabber import jid
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
26 from twisted.words.protocols.jabber.error import StanzaError
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
27 from twisted.internet import defer
1189
7d640c303140 core (disco): added a timeout to get items
Goffi <goffi@goffi.org>
parents: 993
diff changeset
28 from twisted.internet import reactor
1493
1285c714a6cc core (disco): use of failure.Failure when raising FeatureNotFound to avoid a jump in debugger in debug mode
Goffi <goffi@goffi.org>
parents: 1491
diff changeset
29 from twisted.python import failure
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
30 from sat.core.constants import Const as C
1684
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
31 from sat.tools import xml_tools
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
32 from sat.memory import persistent
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
33 from wokkel import disco
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
34 from base64 import b64encode
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
35 from hashlib import sha1
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
36
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
37
1189
7d640c303140 core (disco): added a timeout to get items
Goffi <goffi@goffi.org>
parents: 993
diff changeset
38 TIMEOUT = 15
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
39 CAP_HASH_ERROR = "ERROR"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
40
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
41
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
42 class HashGenerationError(Exception):
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
43 pass
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
44
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
45
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
46 class ByteIdentity(object):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
47 """This class manage identity as bytes (needed for i;octet sort), it is used for the hash generation"""
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
48
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
49 def __init__(self, identity, lang=None):
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
50 assert isinstance(identity, disco.DiscoIdentity)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
51 self.category = identity.category.encode("utf-8")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
52 self.idType = identity.type.encode("utf-8")
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
53 self.name = identity.name.encode("utf-8") if identity.name else b""
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
54 self.lang = lang.encode("utf-8") if lang is not None else b""
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
55
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
56 def __bytes__(self):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
57 return b"%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
58
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
59
1684
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
60 class HashManager(object):
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
61 """map object which manage hashes
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
62
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
63 persistent storage is update when a new hash is added
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
64 """
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
65
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
66 def __init__(self, persistent):
1752
cbcc223c323a core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
Goffi <goffi@goffi.org>
parents: 1684
diff changeset
67 self.hashes = {
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
68 CAP_HASH_ERROR: disco.DiscoInfo() # used when we can't get disco infos
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
69 }
1684
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
70 self.persistent = persistent
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
71
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
72 def __getitem__(self, key):
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
73 return self.hashes[key]
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
74
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
75 def __setitem__(self, hash_, disco_info):
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
76 if hash_ in self.hashes:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
77 log.debug("ignoring hash set: it is already known")
1684
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
78 return
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
79 self.hashes[hash_] = disco_info
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
80 self.persistent[hash_] = disco_info.toElement().toXml()
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
81
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
82 def __contains__(self, hash_):
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
83 return self.hashes.__contains__(hash_)
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
84
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
85 def load(self):
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
86 def fillHashes(hashes):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
87 for hash_, xml in hashes.items():
1684
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
88 element = xml_tools.ElementParser()(xml)
2587
e70023e84974 core (disco): fixed disco capabilities (empty disco was used)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 disco_info = disco.DiscoInfo.fromElement(element)
e70023e84974 core (disco): fixed disco capabilities (empty disco was used)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 if not disco_info.features and not disco_info.identities:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
91 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
92 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
93 "no feature/identity found in disco element (hash: {cap_hash}), ignoring: {xml}"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
94 ).format(cap_hash=hash_, xml=xml)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
95 )
2587
e70023e84974 core (disco): fixed disco capabilities (empty disco was used)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
96 else:
e70023e84974 core (disco): fixed disco capabilities (empty disco was used)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
97 self.hashes[hash_] = disco_info
e70023e84974 core (disco): fixed disco capabilities (empty disco was used)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
98
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
99 log.info("Disco hashes loaded")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
100
1684
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
101 d = self.persistent.load()
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
102 d.addCallback(fillHashes)
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
103 return d
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
104
1752
cbcc223c323a core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
Goffi <goffi@goffi.org>
parents: 1684
diff changeset
105
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
106 class Discovery(object):
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
107 """ Manage capabilities of entities """
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
108
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
109 def __init__(self, host):
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
110 self.host = host
1684
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
111 # TODO: remove legacy hashes
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
112
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
113 def load(self):
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
114 """Load persistent hashes"""
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
115 self.hashes = HashManager(persistent.PersistentDict("disco"))
373ce871b0f3 core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents: 1675
diff changeset
116 return self.hashes.load()
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
117
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
118 @defer.inlineCallbacks
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
119 def hasFeature(self, client, feature, jid_=None, node=""):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
120 """Tell if an entity has the required feature
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
121
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
122 @param feature: feature namespace
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
123 @param jid_: jid of the target, or None for profile's server
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
124 @param node(unicode): optional node to use for disco request
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
125 @return: a Deferred which fire a boolean (True if feature is available)
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
126 """
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
127 disco_infos = yield self.getInfos(client, jid_, node)
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
128 defer.returnValue(feature in disco_infos.features)
951
027a054c6dda core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents: 944
diff changeset
129
027a054c6dda core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents: 944
diff changeset
130 @defer.inlineCallbacks
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
131 def checkFeature(self, client, feature, jid_=None, node=""):
951
027a054c6dda core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents: 944
diff changeset
132 """Like hasFeature, but raise an exception is feature is not Found
027a054c6dda core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents: 944
diff changeset
133
027a054c6dda core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents: 944
diff changeset
134 @param feature: feature namespace
027a054c6dda core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents: 944
diff changeset
135 @param jid_: jid of the target, or None for profile's server
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
136 @param node(unicode): optional node to use for disco request
951
027a054c6dda core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents: 944
diff changeset
137
027a054c6dda core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents: 944
diff changeset
138 @raise: exceptions.FeatureNotFound
027a054c6dda core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents: 944
diff changeset
139 """
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
140 disco_infos = yield self.getInfos(client, jid_, node)
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
141 if not feature in disco_infos.features:
1493
1285c714a6cc core (disco): use of failure.Failure when raising FeatureNotFound to avoid a jump in debugger in debug mode
Goffi <goffi@goffi.org>
parents: 1491
diff changeset
142 raise failure.Failure(exceptions.FeatureNotFound)
1491
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
143
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
144 @defer.inlineCallbacks
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
145 def checkFeatures(self, client, features, jid_=None, identity=None, node=""):
1491
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
146 """Like checkFeature, but check several features at once, and check also identity
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
147
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
148 @param features(iterable[unicode]): features to check
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
149 @param jid_(jid.JID): jid of the target, or None for profile's server
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
150 @param node(unicode): optional node to use for disco request
1491
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
151 @param identity(None, tuple(unicode, unicode): if not None, the entity must have an identity with this (category, type) tuple
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
152
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
153 @raise: exceptions.FeatureNotFound
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
154 """
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
155 disco_infos = yield self.getInfos(client, jid_, node)
1491
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
156 if not set(features).issubset(disco_infos.features):
1493
1285c714a6cc core (disco): use of failure.Failure when raising FeatureNotFound to avoid a jump in debugger in debug mode
Goffi <goffi@goffi.org>
parents: 1491
diff changeset
157 raise failure.Failure(exceptions.FeatureNotFound())
1491
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
158
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
159 if identity is not None and identity not in disco_infos.identities:
1493
1285c714a6cc core (disco): use of failure.Failure when raising FeatureNotFound to avoid a jump in debugger in debug mode
Goffi <goffi@goffi.org>
parents: 1491
diff changeset
160 raise failure.Failure(exceptions.FeatureNotFound())
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
161
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
162 def getInfos(self, client, jid_=None, node="", use_cache=True):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
163 """get disco infos from jid_, filling capability hash if needed
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
164
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
165 @param jid_: jid of the target, or None for profile's server
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
166 @param node(unicode): optional node to use for disco request
2342
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
167 @param use_cache(bool): if True, use cached data if available
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
168 @return: a Deferred which fire disco.DiscoInfo
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
169 """
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
170 if jid_ is None:
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
171 jid_ = jid.JID(client.jid.host)
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
172 try:
2342
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
173 if not use_cache:
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
174 # we ignore cache, so we pretend we haven't found it
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
175 raise KeyError
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
176 cap_hash = self.host.memory.getEntityData(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
177 jid_, [C.ENTITY_CAP_HASH], client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
178 )[C.ENTITY_CAP_HASH]
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
179 except (KeyError, exceptions.UnknownEntityError):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
180 # capability hash is not available, we'll compute one
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
181 def infosCb(disco_infos):
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
182 cap_hash = self.generateHash(disco_infos)
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
183 self.hashes[cap_hash] = disco_infos
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
184 self.host.memory.updateEntityData(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
185 jid_, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
186 )
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
187 return disco_infos
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
188
1752
cbcc223c323a core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
Goffi <goffi@goffi.org>
parents: 1684
diff changeset
189 def infosEb(fail):
cbcc223c323a core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
Goffi <goffi@goffi.org>
parents: 1684
diff changeset
190 if fail.check(defer.CancelledError):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
191 reason = "request time-out"
2768
cb34af35af6f core (disco): client.disco.requestInfo failure are re-raised:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
192 fail = failure.Failure(exceptions.TimeOutError(fail.message))
1752
cbcc223c323a core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
Goffi <goffi@goffi.org>
parents: 1684
diff changeset
193 else:
cbcc223c323a core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
Goffi <goffi@goffi.org>
parents: 1684
diff changeset
194 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
195 reason = str(fail.value)
1752
cbcc223c323a core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
Goffi <goffi@goffi.org>
parents: 1684
diff changeset
196 except AttributeError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
197 reason = str(fail)
2768
cb34af35af6f core (disco): client.disco.requestInfo failure are re-raised:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
198
cb34af35af6f core (disco): client.disco.requestInfo failure are re-raised:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
199 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
200 "Error while requesting disco infos from {jid}: {reason}".format(
2768
cb34af35af6f core (disco): client.disco.requestInfo failure are re-raised:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
201 jid=jid_.full(), reason=reason
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
202 )
2768
cb34af35af6f core (disco): client.disco.requestInfo failure are re-raised:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
203 )
cb34af35af6f core (disco): client.disco.requestInfo failure are re-raised:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
204
cb34af35af6f core (disco): client.disco.requestInfo failure are re-raised:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
205 # XXX we set empty disco in cache, to avoid getting an error or waiting
cb34af35af6f core (disco): client.disco.requestInfo failure are re-raised:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
206 # for a timeout again the next time
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
207 self.host.memory.updateEntityData(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
208 jid_, C.ENTITY_CAP_HASH, CAP_HASH_ERROR, profile_key=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
209 )
2768
cb34af35af6f core (disco): client.disco.requestInfo failure are re-raised:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
210 raise fail
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
211
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
212 d = client.disco.requestInfo(jid_, nodeIdentifier=node)
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
213 d.addCallback(infosCb)
1752
cbcc223c323a core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
Goffi <goffi@goffi.org>
parents: 1684
diff changeset
214 d.addErrback(infosEb)
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
215 return d
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
216 else:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
217 disco_infos = self.hashes[cap_hash]
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
218 return defer.succeed(disco_infos)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
219
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
220 @defer.inlineCallbacks
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
221 def getItems(self, client, jid_=None, node="", use_cache=True):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
222 """get disco items from jid_, cache them for our own server
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
223
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
224 @param jid_(jid.JID): jid of the target, or None for profile's server
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
225 @param node(unicode): optional node to use for disco request
2342
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
226 @param use_cache(bool): if True, use cached data if available
963
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
227 @return: a Deferred which fire disco.DiscoItems
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
228 """
2407
cf9b276f4a08 core (disco): do not cache disco items when node is set
Goffi <goffi@goffi.org>
parents: 2342
diff changeset
229 server_jid = jid.JID(client.jid.host)
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
230 if jid_ is None:
2407
cf9b276f4a08 core (disco): do not cache disco items when node is set
Goffi <goffi@goffi.org>
parents: 2342
diff changeset
231 jid_ = server_jid
cf9b276f4a08 core (disco): do not cache disco items when node is set
Goffi <goffi@goffi.org>
parents: 2342
diff changeset
232
cf9b276f4a08 core (disco): do not cache disco items when node is set
Goffi <goffi@goffi.org>
parents: 2342
diff changeset
233 if jid_ == server_jid and not node:
cf9b276f4a08 core (disco): do not cache disco items when node is set
Goffi <goffi@goffi.org>
parents: 2342
diff changeset
234 # we cache items only for our own server and if node is not set
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
235 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
236 items = self.host.memory.getEntityData(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
237 jid_, ["DISCO_ITEMS"], client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
238 )["DISCO_ITEMS"]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
239 log.debug("[%s] disco items are in cache" % jid_.full())
2342
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
240 if not use_cache:
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
241 # we ignore cache, so we pretend we haven't found it
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
242 raise KeyError
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
243 except (KeyError, exceptions.UnknownEntityError):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
244 log.debug("Caching [%s] disco items" % jid_.full())
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
245 items = yield client.disco.requestItems(jid_, nodeIdentifier=node)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
246 self.host.memory.updateEntityData(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
247 jid_, "DISCO_ITEMS", items, profile_key=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
248 )
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
249 else:
1752
cbcc223c323a core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
Goffi <goffi@goffi.org>
parents: 1684
diff changeset
250 try:
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
251 items = yield client.disco.requestItems(jid_, nodeIdentifier=node)
1752
cbcc223c323a core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
Goffi <goffi@goffi.org>
parents: 1684
diff changeset
252 except StanzaError as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
253 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
254 "Error while requesting items for {jid}: {reason}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
255 jid=jid_.full(), reason=e.condition
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
256 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
257 )
1752
cbcc223c323a core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
Goffi <goffi@goffi.org>
parents: 1684
diff changeset
258 items = disco.DiscoItems()
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
259
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
260 defer.returnValue(items)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
261
1493
1285c714a6cc core (disco): use of failure.Failure when raising FeatureNotFound to avoid a jump in debugger in debug mode
Goffi <goffi@goffi.org>
parents: 1491
diff changeset
262 def _infosEb(self, failure_, entity_jid):
1285c714a6cc core (disco): use of failure.Failure when raising FeatureNotFound to avoid a jump in debugger in debug mode
Goffi <goffi@goffi.org>
parents: 1491
diff changeset
263 failure_.trap(StanzaError)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
264 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
265 _("Error while requesting [%(jid)s]: %(error)s")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
266 % {"jid": entity_jid.full(), "error": failure_.getErrorMessage()}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
267 )
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
268
2431
a9fac72825a2 memory (disco): added findServiceEntity helper method to get first service of a kind
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
269 def findServiceEntity(self, client, category, type_, jid_=None):
a9fac72825a2 memory (disco): added findServiceEntity helper method to get first service of a kind
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
270 """Helper method to find first available entity from findServiceEntities
a9fac72825a2 memory (disco): added findServiceEntity helper method to get first service of a kind
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
271
a9fac72825a2 memory (disco): added findServiceEntity helper method to get first service of a kind
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
272 args are the same as for [findServiceEntities]
a9fac72825a2 memory (disco): added findServiceEntity helper method to get first service of a kind
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
273 @return (jid.JID, None): found entity
a9fac72825a2 memory (disco): added findServiceEntity helper method to get first service of a kind
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
274 """
a9fac72825a2 memory (disco): added findServiceEntity helper method to get first service of a kind
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
275 d = self.host.findServiceEntities(client, "pubsub", "service")
a9fac72825a2 memory (disco): added findServiceEntity helper method to get first service of a kind
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
276 d.addCallback(lambda entities: entities.pop() if entities else None)
a9fac72825a2 memory (disco): added findServiceEntity helper method to get first service of a kind
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
277 return d
a9fac72825a2 memory (disco): added findServiceEntity helper method to get first service of a kind
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
278
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
279 def findServiceEntities(self, client, category, type_, jid_=None):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
280 """Return all available items of an entity which correspond to (category, type_)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
281
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
282 @param category: identity's category
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
283 @param type_: identitiy's type
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
284 @param jid_: the jid of the target server (None for profile's server)
1460
c7fd121a6180 core: getProfileName no raise ProfileUnknownError + minor doc fixes
Goffi <goffi@goffi.org>
parents: 1416
diff changeset
285 @return: a set of found entities
1675
fd143578fe89 plugin XEP-0065: fixed bad error handling
Goffi <goffi@goffi.org>
parents: 1552
diff changeset
286 @raise defer.CancelledError: the request timed out
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
287 """
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
288 found_entities = set()
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
289
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
290 def infosCb(infos, entity_jid):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
291 if (category, type_) in infos.identities:
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
292 found_entities.add(entity_jid)
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
293
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
294 def gotItems(items):
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
295 defers_list = []
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
296 for item in items:
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
297 info_d = self.getInfos(client, item.entity)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
298 info_d.addCallbacks(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
299 infosCb, self._infosEb, [item.entity], None, [item.entity]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
300 )
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
301 defers_list.append(info_d)
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
302 return defer.DeferredList(defers_list)
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
303
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
304 d = self.getItems(client, jid_)
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
305 d.addCallback(gotItems)
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2712
diff changeset
306 d.addCallback(lambda __: found_entities)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
307 reactor.callLater(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
308 TIMEOUT, d.cancel
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
309 ) # FIXME: one bad service make a general timeout
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
310 return d
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
311
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
312 def findFeaturesSet(self, client, features, identity=None, jid_=None):
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
313 """Return entities (including jid_ and its items) offering features
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
314
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
315 @param features: iterable of features which must be present
3014
b6abf8af87db plugin ad-hoc D-Bus: fixed warning when no media player is found
Goffi <goffi@goffi.org>
parents: 3012
diff changeset
316 @param identity(None, tuple(unicode, unicode)): if not None, accept only this
b6abf8af87db plugin ad-hoc D-Bus: fixed warning when no media player is found
Goffi <goffi@goffi.org>
parents: 3012
diff changeset
317 (category/type) identity
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
318 @param jid_: the jid of the target server (None for profile's server)
1552
e0bde0d0b321 core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents: 1493
diff changeset
319 @param profile: %(doc_profile)s
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
320 @return: a set of found entities
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
321 """
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
322 if jid_ is None:
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
323 jid_ = jid.JID(client.jid.host)
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
324 features = set(features)
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
325 found_entities = set()
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
326
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
327 def infosCb(infos, entity):
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
328 if entity is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
329 log.warning(_("received an item without jid"))
2533
8d82a62fa098 core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
330 return
1491
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
331 if identity is not None and identity not in infos.identities:
704ca56f5ca9 core (disco): added checkFeatures to check several features at once + identities are now managed with a tuple in findFeaturesSet and checkFeatures
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
332 return
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
333 if features.issubset(infos.features):
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
334 found_entities.add(entity)
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
335
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
336 def gotItems(items):
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
337 defer_list = []
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
338 for entity in [jid_] + [item.entity for item in items]:
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
339 infos_d = self.getInfos(client, entity)
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
340 infos_d.addCallbacks(infosCb, self._infosEb, [entity], None, [entity])
1484
05b821a0ff5a core (disco): fixed bad indentation resulting in bad result in findFeaturesSet
Goffi <goffi@goffi.org>
parents: 1460
diff changeset
341 defer_list.append(infos_d)
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
342 return defer.DeferredList(defer_list)
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
343
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
344 d = self.getItems(client, jid_)
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
345 d.addCallback(gotItems)
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2712
diff changeset
346 d.addCallback(lambda __: found_entities)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
347 reactor.callLater(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
348 TIMEOUT, d.cancel
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
349 ) # FIXME: one bad service make a general timeout
973
4a8903021fda core (disco): findFeaturesSet and findServiceEntities don't use inlineCallbacks anymore, that allow a better error management (no more anonying debugger raise when discoInfo fails in debug mode)
Goffi <goffi@goffi.org>
parents: 963
diff changeset
350 return d
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
351
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
352 def generateHash(self, services):
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
353 """ Generate a unique hash for given service
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
354
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
355 hash algorithm is the one described in XEP-0115
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
356 @param services: iterable of disco.DiscoIdentity/disco.DiscoFeature, as returned by discoHandler.info
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
357
282
6a0c6d8e119d added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff changeset
358 """
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
359 s = []
2830
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
360 # identities
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
361 byte_identities = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
362 ByteIdentity(service)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
363 for service in services
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
364 if isinstance(service, disco.DiscoIdentity)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
365 ] # FIXME: lang must be managed here
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
366 byte_identities.sort(key=lambda i: i.lang)
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
367 byte_identities.sort(key=lambda i: i.idType)
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
368 byte_identities.sort(key=lambda i: i.category)
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
369 for identity in byte_identities:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
370 s.append(bytes(identity))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
371 s.append(b"<")
2830
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
372 # features
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
373 byte_features = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
374 service.encode("utf-8")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
375 for service in services
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
376 if isinstance(service, disco.DiscoFeature)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
377 ]
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
378 byte_features.sort() # XXX: the default sort has the same behaviour as the requested RFC 4790 i;octet sort
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
379 for feature in byte_features:
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
380 s.append(feature)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
381 s.append(b"<")
2830
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
382
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
383 # extensions
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
384 ext = list(services.extensions.values())
2830
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
385 ext.sort(key=lambda f: f.formNamespace.encode('utf-8'))
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
386 for extension in ext:
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
387 s.append(extension.formNamespace.encode('utf-8'))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
388 s.append(b"<")
2830
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
389 fields = extension.fieldList
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
390 fields.sort(key=lambda f: f.var.encode('utf-8'))
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
391 for field in fields:
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
392 s.append(field.var.encode('utf-8'))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
393 s.append(b"<")
2830
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
394 values = [v.encode('utf-8') for v in field.values]
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
395 values.sort()
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
396 for value in values:
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
397 s.append(value)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
398 s.append(b"<")
2830
770ec685ff1f core (disco): added missing disco extensions when generating the cap hash.
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
399
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
400 cap_hash = b64encode(sha1(b"".join(s)).digest()).decode('utf-8')
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
401 log.debug(_("Capability hash generated: [{cap_hash}]").format(cap_hash=cap_hash))
944
e1842ebcb2f3 core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents: 941
diff changeset
402 return cap_hash
963
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
403
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
404 @defer.inlineCallbacks
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
405 def _discoInfos(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
406 self, entity_jid_s, node="", use_cache=True, profile_key=C.PROF_KEY_NONE
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
407 ):
963
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
408 """ Discovery method for the bridge
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
409 @param entity_jid_s: entity we want to discover
2342
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
410 @param use_cache(bool): if True, use cached data if available
2150
91347fe95384 core(memory.disco), bridge: discoInfos and discoItems now handle node
Goffi <goffi@goffi.org>
parents: 2148
diff changeset
411 @param node(unicode): optional node to use
963
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
412
1552
e0bde0d0b321 core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents: 1493
diff changeset
413 @return: list of tuples
e0bde0d0b321 core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents: 1493
diff changeset
414 """
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
415 client = self.host.getClient(profile_key)
963
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
416 entity = jid.JID(entity_jid_s)
2342
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
417 disco_infos = yield self.getInfos(client, entity, node, use_cache)
1414
159d16336f87 core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
418 extensions = {}
2712
c3f59c1dcb0a core (memory/disco): normalise booleans in extensions
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
419 # FIXME: should extensions be serialised using tools.common.data_format?
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
420 for form_type, form in list(disco_infos.extensions.items()):
1414
159d16336f87 core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
421 fields = []
159d16336f87 core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
422 for field in form.fieldList:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
423 data = {"type": field.fieldType}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
424 for attr in ("var", "label", "desc"):
1414
159d16336f87 core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
425 value = getattr(field, attr)
159d16336f87 core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
426 if value is not None:
159d16336f87 core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
427 data[attr] = value
159d16336f87 core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
428
1416
a419da93afef core (disco): fixed bad indentation in _discoInfos (used by bridge)
Goffi <goffi@goffi.org>
parents: 1414
diff changeset
429 values = [field.value] if field.value is not None else field.values
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
430 if field.fieldType == "boolean":
3012
2224fbbd45dd memory (disco): fixed boolean to string conversion
Goffi <goffi@goffi.org>
parents: 2830
diff changeset
431 values = [C.boolConst(v) for v in values]
1416
a419da93afef core (disco): fixed bad indentation in _discoInfos (used by bridge)
Goffi <goffi@goffi.org>
parents: 1414
diff changeset
432 fields.append((data, values))
1414
159d16336f87 core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
433
159d16336f87 core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
434 extensions[form_type or ""] = fields
159d16336f87 core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents: 1409
diff changeset
435
2712
c3f59c1dcb0a core (memory/disco): normalise booleans in extensions
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
436 defer.returnValue((
c3f59c1dcb0a core (memory/disco): normalise booleans in extensions
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
437 disco_infos.features,
c3f59c1dcb0a core (memory/disco): normalise booleans in extensions
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
438 [(cat, type_, name or "")
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
439 for (cat, type_), name in list(disco_infos.identities.items())],
2712
c3f59c1dcb0a core (memory/disco): normalise booleans in extensions
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
440 extensions))
963
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
441
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
442 def items2tuples(self, disco_items):
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
443 """convert disco items to tuple of strings
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
444
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
445 @param disco_items(iterable[disco.DiscoItem]): items
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
446 @return G(tuple[unicode,unicode,unicode]): serialised items
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
447 """
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
448 for item in disco_items:
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
449 if not item.entity:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
450 log.warning(_("invalid item (no jid)"))
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
451 continue
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
452 yield (item.entity.full(), item.nodeIdentifier or "", item.name or "")
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
453
963
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
454 @defer.inlineCallbacks
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
455 def _discoItems(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3014
diff changeset
456 self, entity_jid_s, node="", use_cache=True, profile_key=C.PROF_KEY_NONE
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2587
diff changeset
457 ):
963
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
458 """ Discovery method for the bridge
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
459
963
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
460 @param entity_jid_s: entity we want to discover
2150
91347fe95384 core(memory.disco), bridge: discoInfos and discoItems now handle node
Goffi <goffi@goffi.org>
parents: 2148
diff changeset
461 @param node(unicode): optional node to use
2342
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
462 @param use_cache(bool): if True, use cached data if available
1552
e0bde0d0b321 core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents: 1493
diff changeset
463 @return: list of tuples"""
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
464 client = self.host.getClient(profile_key)
963
723f28cd15c7 core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents: 961
diff changeset
465 entity = jid.JID(entity_jid_s)
2342
f047d5410040 core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
Goffi <goffi@goffi.org>
parents: 2150
diff changeset
466 disco_items = yield self.getItems(client, entity, node, use_cache)
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2096
diff changeset
467 ret = list(self.items2tuples(disco_items))
2096
efe31f0881fb core (memory/disco): recover getItems on bridge's discoItems method if an item is empty
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
468 defer.returnValue(ret)