Mercurial > libervia-backend
annotate src/memory/disco.py @ 1745:5ca3caefcf98
core (params): removed unused values
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 15 Dec 2015 16:25:37 +0100 |
parents | 373ce871b0f3 |
children | cbcc223c323a |
rev | line source |
---|---|
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
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 |
1396 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 | 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 |
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
973
diff
changeset
|
23 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
|
24 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
|
25 from twisted.words.protocols.jabber.error import StanzaError |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
26 from twisted.internet import defer |
1189
7d640c303140
core (disco): added a timeout to get items
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
27 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
|
28 from twisted.python import failure |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
29 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
|
30 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
|
31 from sat.memory import persistent |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
32 from wokkel import disco |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
33 from base64 import b64encode |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
34 from hashlib import sha1 |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 |
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 PRESENCE = '/presence' |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 NS_ENTITY_CAPABILITY = 'http://jabber.org/protocol/caps' |
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 + '"]' |
1189
7d640c303140
core (disco): added a timeout to get items
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
40 TIMEOUT = 15 |
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) |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 self.category = identity.category.encode('utf-8') |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 self.idType = identity.type.encode('utf-8') |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 self.name = identity.name.encode('utf-8') if identity.name else '' |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
54 self.lang = lang.encode('utf-8') if lang is not None else '' |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 def __str__(self): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 return "%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): |
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
|
67 self.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
|
68 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
|
69 |
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 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
|
71 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
|
72 |
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 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
|
74 if hash_ in self.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
|
75 log.debug(u"ignoring hash set: it is already known") |
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 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
|
77 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
|
78 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
|
79 |
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 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
|
81 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
|
82 |
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 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
|
84 def fillHashes(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
|
85 for hash_, xml in hashes.iteritems(): |
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 element = xml_tools.ElementParser()(xml) |
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
|
87 self.hashes[hash_] = disco.DiscoInfo.fromElement(element) |
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 log.info(u"Disco hashes loaded") |
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
|
89 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
|
90 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
|
91 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
|
92 |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
93 class Discovery(object): |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
94 """ Manage capabilities of entities """ |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 def __init__(self, host): |
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 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
|
98 # 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
|
99 |
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
|
100 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
|
101 """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
|
102 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
|
103 return self.hashes.load() |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
105 @defer.inlineCallbacks |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
106 def hasFeature(self, feature, jid_=None, profile=C.PROF_KEY_NONE): |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
107 """Tell if an entity has the required feature |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
108 |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
109 @param feature: feature namespace |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
110 @param jid_: jid of the target, or 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
|
111 @param profile: %(doc_profile)s |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
112 @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
|
113 """ |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
114 disco_infos = yield self.getInfos(jid_, profile) |
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
|
115 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
|
116 |
027a054c6dda
core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents:
944
diff
changeset
|
117 @defer.inlineCallbacks |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
118 def checkFeature(self, feature, jid_=None, profile=C.PROF_KEY_NONE): |
951
027a054c6dda
core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents:
944
diff
changeset
|
119 """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
|
120 |
027a054c6dda
core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents:
944
diff
changeset
|
121 @param feature: feature namespace |
027a054c6dda
core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents:
944
diff
changeset
|
122 @param jid_: jid of the target, or 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
|
123 @param profile: %(doc_profile)s |
951
027a054c6dda
core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents:
944
diff
changeset
|
124 |
027a054c6dda
core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents:
944
diff
changeset
|
125 @raise: exceptions.FeatureNotFound |
027a054c6dda
core (disco): added checkFeature method + fixed hasFeature test
Goffi <goffi@goffi.org>
parents:
944
diff
changeset
|
126 """ |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
127 disco_infos = yield self.getInfos(jid_, profile) |
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 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
|
129 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
|
130 |
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
|
131 @defer.inlineCallbacks |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
132 def checkFeatures(self, features, jid_=None, identity=None, profile=C.PROF_KEY_NONE): |
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
|
133 """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
|
134 |
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
|
135 @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
|
136 @param jid_(jid.JID): jid of the target, or None for profile's server |
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
|
137 @param identity(None, tuple(unicode, unicode): if not None, the entity must have an identity with this (category, type) tuple |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
138 @param profile: %(doc_profile)s |
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
|
139 |
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
|
140 @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
|
141 """ |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
142 disco_infos = yield self.getInfos(jid_, profile) |
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 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
|
144 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
|
145 |
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 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
|
147 raise failure.Failure(exceptions.FeatureNotFound()) |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
149 def getInfos(self, jid_=None, profile=C.PROF_KEY_NONE): |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
150 """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
|
151 |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
152 @param jid_: jid of the target, or 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
|
153 @param profile: %(doc_profile)s |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
154 @return: a Deferred which fire disco.DiscoInfo |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
155 """ |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
156 client = self.host.getClient(profile) |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
157 if jid_ is None: |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
158 jid_ = jid.JID(client.jid.host) |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
159 try: |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
160 cap_hash = self.host.memory.getEntityData(jid_, [C.ENTITY_CAP_HASH], client.profile)[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
|
161 except (KeyError, exceptions.UnknownEntityError): |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
162 # 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
|
163 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
|
164 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
|
165 self.hashes[cap_hash] = disco_infos |
1315
be3a301540c0
core (memory): updateEntityData now accept a "silent" argument to avoid sending signal to frontends when updating an attribute with "signalOnUpdate" flag.
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
166 self.host.memory.updateEntityData(jid_, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile) |
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
|
167 return 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
|
168 d = client.disco.requestInfo(jid_) |
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
|
169 d.addCallback(infosCb) |
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
|
170 return d |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1216
diff
changeset
|
171 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1216
diff
changeset
|
172 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
|
173 return defer.succeed(disco_infos) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
174 |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
175 @defer.inlineCallbacks |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
176 def getItems(self, jid_=None, nodeIdentifier='', profile=C.PROF_KEY_NONE): |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
177 """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
|
178 |
1216
8ad37c3d58a9
memory (disco): add nodeIdentifier parameter to getItems
souliane <souliane@mailoo.org>
parents:
1189
diff
changeset
|
179 @param jid_ (jid.JID): jid of the target, or None for profile's server |
8ad37c3d58a9
memory (disco): add nodeIdentifier parameter to getItems
souliane <souliane@mailoo.org>
parents:
1189
diff
changeset
|
180 @param nodeIdentifier (str): optional node identifier |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
181 @param profile: %(doc_profile)s |
963
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
182 @return: a Deferred which fire disco.DiscoItems |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
183 """ |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
184 client = self.host.getClient(profile) |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
185 if jid_ is None: |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
186 jid_ = jid.JID(client.jid.host) |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
187 # we cache items only for our own server |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
188 try: |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
189 items = self.host.memory.getEntityData(jid_, ["DISCO_ITEMS"], client.profile)["DISCO_ITEMS"] |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
190 log.debug(u"[%s] disco items are in cache" % jid_.full()) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1216
diff
changeset
|
191 except (KeyError, exceptions.UnknownEntityError): |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
192 log.debug(u"Caching [%s] disco items" % jid_.full()) |
1216
8ad37c3d58a9
memory (disco): add nodeIdentifier parameter to getItems
souliane <souliane@mailoo.org>
parents:
1189
diff
changeset
|
193 items = yield client.disco.requestItems(jid_, nodeIdentifier) |
1315
be3a301540c0
core (memory): updateEntityData now accept a "silent" argument to avoid sending signal to frontends when updating an attribute with "signalOnUpdate" flag.
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
194 self.host.memory.updateEntityData(jid_, "DISCO_ITEMS", items, profile_key=client.profile) |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
195 else: |
1216
8ad37c3d58a9
memory (disco): add nodeIdentifier parameter to getItems
souliane <souliane@mailoo.org>
parents:
1189
diff
changeset
|
196 items = yield client.disco.requestItems(jid_, nodeIdentifier) |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
197 |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
198 defer.returnValue(items) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
199 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
200 |
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
|
201 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
|
202 failure_.trap(StanzaError) |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
203 log.warning(_(u"Error while requesting [%(jid)s]: %(error)s") % {'jid': entity_jid.full(), |
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
|
204 'error': failure_.getErrorMessage()}) |
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
|
205 |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
206 def findServiceEntities(self, category, type_, jid_=None, profile=C.PROF_KEY_NONE): |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
207 """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
|
208 |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
209 @param category: identity's category |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
210 @param type_: identitiy's type |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
211 @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
|
212 @param profile: %(doc_profile)s |
1460
c7fd121a6180
core: getProfileName no raise ProfileUnknownError + minor doc fixes
Goffi <goffi@goffi.org>
parents:
1416
diff
changeset
|
213 @return: a set of found entities |
1675
fd143578fe89
plugin XEP-0065: fixed bad error handling
Goffi <goffi@goffi.org>
parents:
1552
diff
changeset
|
214 @raise defer.CancelledError: the request timed out |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
215 """ |
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
|
216 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
|
217 |
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
|
218 def infosCb(infos, entity_jid): |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
219 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
|
220 found_entities.add(entity_jid) |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 |
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
|
222 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
|
223 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
|
224 for item in items: |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
225 info_d = self.getInfos(item.entity, profile) |
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
|
226 info_d.addCallbacks(infosCb, self._infosEb, [item.entity], None, [item.entity]) |
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
|
227 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
|
228 return defer.DeferredList(defers_list) |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
229 |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
230 d = self.getItems(jid_, profile=profile) |
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
|
231 d.addCallback(gotItems) |
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
|
232 d.addCallback(lambda dummy: found_entities) |
1189
7d640c303140
core (disco): added a timeout to get items
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
233 reactor.callLater(TIMEOUT, d.cancel) # 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
|
234 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
|
235 |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
236 def findFeaturesSet(self, features, identity=None, jid_=None, profile=C.PROF_KEY_NONE): |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
237 """Return entities (including jid_ and its items) offering features |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
238 |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
239 @param features: iterable of features which must be present |
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
|
240 @param identity(None, tuple(unicode, unicode)): if not None, accept only this (category/type) identity |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
241 @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
|
242 @param profile: %(doc_profile)s |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
243 @return: a set of found entities |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
244 """ |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
245 client = self.host.getClient(profile) |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
246 if jid_ is None: |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
247 jid_ = jid.JID(client.jid.host) |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
248 features = set(features) |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
249 found_entities = set() |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 |
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
|
251 def infosCb(infos, entity): |
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
|
252 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
|
253 return |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
254 if features.issubset(infos.features): |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
255 found_entities.add(entity) |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 |
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
|
257 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
|
258 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
|
259 for entity in [jid_] + [item.entity for item in items]: |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
260 infos_d = self.getInfos(entity, profile) |
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
|
261 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
|
262 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
|
263 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
|
264 |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
265 d = self.getItems(jid_, profile=profile) |
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
|
266 d.addCallback(gotItems) |
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
|
267 d.addCallback(lambda dummy: found_entities) |
1189
7d640c303140
core (disco): added a timeout to get items
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
268 reactor.callLater(TIMEOUT, d.cancel) # 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
|
269 return d |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
270 |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
271 def generateHash(self, services): |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
272 """ Generate a unique hash for given service |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
273 |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
274 hash algorithm is the one described in XEP-0115 |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
275 @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
|
276 |
282
6a0c6d8e119d
added plugin xep-0115: entity capabilities
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 """ |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
278 s = [] |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
279 byte_identities = [ByteIdentity(service) for service in services if isinstance(service, disco.DiscoIdentity)] # FIXME: lang must be managed here |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
280 byte_identities.sort(key=lambda i: i.lang) |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
281 byte_identities.sort(key=lambda i: i.idType) |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
282 byte_identities.sort(key=lambda i: i.category) |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
283 for identity in byte_identities: |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
284 s.append(str(identity)) |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
285 s.append('<') |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
286 byte_features = [service.encode('utf-8') for service in services if isinstance(service, disco.DiscoFeature)] |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
287 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
|
288 for feature in byte_features: |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
289 s.append(feature) |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
290 s.append('<') |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
291 #TODO: manage XEP-0128 data form here |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
292 cap_hash = b64encode(sha1(''.join(s)).digest()) |
1409
3265a2639182
massive (preventive) addition of 'u' (unicode) before the strings passed to logging functions
souliane <souliane@mailoo.org>
parents:
1396
diff
changeset
|
293 log.debug(_(u'Capability hash generated: [%s]') % cap_hash) |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
941
diff
changeset
|
294 return cap_hash |
963
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
295 |
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
296 @defer.inlineCallbacks |
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
297 def _discoInfos(self, entity_jid_s, profile_key=C.PROF_KEY_NONE): |
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
298 """ Discovery method for the bridge |
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
299 @param entity_jid_s: entity we want to discover |
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
300 |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
301 @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
|
302 """ |
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
303 profile = self.host.memory.getProfileName(profile_key) |
963
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
304 entity = jid.JID(entity_jid_s) |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
305 disco_infos = yield self.getInfos(entity, profile) |
1414
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
306 extensions = {} |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
307 for form_type, form in disco_infos.extensions.items(): |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
308 fields = [] |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
309 for field in form.fieldList: |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
310 data = {'type': field.fieldType} |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
311 for attr in ('var', 'label', 'desc'): |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
312 value = getattr(field, attr) |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
313 if value is not None: |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
314 data[attr] = value |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
315 |
1416
a419da93afef
core (disco): fixed bad indentation in _discoInfos (used by bridge)
Goffi <goffi@goffi.org>
parents:
1414
diff
changeset
|
316 values = [field.value] if field.value is not None else field.values |
a419da93afef
core (disco): fixed bad indentation in _discoInfos (used by bridge)
Goffi <goffi@goffi.org>
parents:
1414
diff
changeset
|
317 fields.append((data, values)) |
1414
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
318 |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
319 extensions[form_type or ""] = fields |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
320 |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
321 defer.returnValue((disco_infos.features, |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
322 [(cat, type_, name or '') for (cat, type_), name in disco_infos.identities.items()], |
159d16336f87
core, bridge, jp: management of service discovery extensions (XEP-0128)
Goffi <goffi@goffi.org>
parents:
1409
diff
changeset
|
323 extensions)) |
963
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
324 |
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
325 @defer.inlineCallbacks |
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
326 def _discoItems(self, entity_jid_s, profile_key=C.PROF_KEY_NONE): |
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
327 """ Discovery method for the bridge |
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
328 @param entity_jid_s: entity we want to discover |
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
329 |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
330 @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
|
331 profile = self.host.memory.getProfileName(profile_key) |
963
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
332 entity = jid.JID(entity_jid_s) |
1552
e0bde0d0b321
core (disco): use of “profile” instead of “profile_key” in several disco methods
Goffi <goffi@goffi.org>
parents:
1493
diff
changeset
|
333 disco_items = yield self.getItems(entity, profile=profile) |
963
723f28cd15c7
core (disco): added discoInfos and discoItems bridge methods
Goffi <goffi@goffi.org>
parents:
961
diff
changeset
|
334 defer.returnValue([(item.entity.full(), item.nodeIdentifier or '', item.name or '') for item in disco_items]) |