2 SleekXMPP: The Sleek XMPP Library
3 Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
4 This file is part of SleekXMPP.
6 See the file LICENSE for copying permission.
12 from sleekxmpp
import Iq
13 from sleekxmpp
.exceptions
import XMPPError
, IqError
, IqTimeout
14 from sleekxmpp
.xmlstream
import JID
15 from sleekxmpp
.plugins
.xep_0030
import DiscoInfo
, DiscoItems
18 log
= logging
.getLogger(__name__
)
21 class StaticDisco(object):
24 While components will likely require fully dynamic handling
25 of service discovery information, most clients and simple bots
26 only need to manage a few disco nodes that will remain mostly
29 StaticDisco provides a set of node handlers that will store
30 static sets of disco info and items in memory.
33 nodes -- A dictionary mapping (JID, node) tuples to a dict
34 containing a disco#info and a disco#items stanza.
35 xmpp -- The main SleekXMPP object.
38 def __init__(self
, xmpp
, disco
):
40 Create a static disco interface. Sets of disco#info and
41 disco#items are maintained for every given JID and node
42 combination. These stanzas are used to store disco
43 information in memory without any additional processing.
46 xmpp -- The main SleekXMPP object.
51 self
.lock
= threading
.RLock()
53 def add_node(self
, jid
=None, node
=None, ifrom
=None):
55 Create a new set of stanzas for the provided
56 JID and node combination.
59 jid -- The JID that will own the new stanzas.
60 node -- The node that will own the new stanzas.
64 jid
= self
.xmpp
.boundjid
.full
69 if isinstance(ifrom
, JID
):
71 if (jid
, node
, ifrom
) not in self
.nodes
:
72 self
.nodes
[(jid
, node
, ifrom
)] = {'info': DiscoInfo(),
73 'items': DiscoItems()}
74 self
.nodes
[(jid
, node
, ifrom
)]['info']['node'] = node
75 self
.nodes
[(jid
, node
, ifrom
)]['items']['node'] = node
77 def get_node(self
, jid
=None, node
=None, ifrom
=None):
80 jid
= self
.xmpp
.boundjid
.full
85 if isinstance(ifrom
, JID
):
87 if (jid
, node
, ifrom
) not in self
.nodes
:
88 self
.add_node(jid
, node
, ifrom
)
89 return self
.nodes
[(jid
, node
, ifrom
)]
91 def node_exists(self
, jid
=None, node
=None, ifrom
=None):
94 jid
= self
.xmpp
.boundjid
.full
99 if isinstance(ifrom
, JID
):
101 if (jid
, node
, ifrom
) not in self
.nodes
:
105 # =================================================================
108 # Each handler accepts four arguments: jid, node, ifrom, and data.
109 # The jid and node parameters together determine the set of info
110 # and items stanzas that will be retrieved or added. Additionally,
111 # the ifrom value allows for cached results when results vary based
112 # on the requester's JID. The data parameter is a dictionary with
113 # additional parameters that will be passed to other calls.
115 # This implementation does not allow different responses based on
116 # the requester's JID, except for cached results. To do that,
117 # register a custom node handler.
119 def supports(self
, jid
, node
, ifrom
, data
):
121 Check if a JID supports a given feature.
123 The data parameter may provide:
124 feature -- The feature to check for support.
125 local -- If true, then the query is for a JID/node
126 combination handled by this Sleek instance and
127 no stanzas need to be sent.
128 Otherwise, a disco stanza must be sent to the
129 remove JID to retrieve the info.
130 cached -- If true, then look for the disco info data from
131 the local cache system. If no results are found,
132 send the query as usual. The self.use_cache
133 setting must be set to true for this option to
134 be useful. If set to false, then the cache will
135 be skipped, even if a result has already been
136 cached. Defaults to false.
138 feature
= data
.get('feature', None)
140 data
= {'local': data
.get('local', False),
141 'cached': data
.get('cached', True)}
147 info
= self
.disco
.get_info(jid
=jid
, node
=node
,
149 info
= self
.disco
._wrap
(ifrom
, jid
, info
, True)
150 features
= info
['disco_info']['features']
151 return feature
in features
157 def has_identity(self
, jid
, node
, ifrom
, data
):
159 Check if a JID has a given identity.
161 The data parameter may provide:
162 category -- The category of the identity to check.
163 itype -- The type of the identity to check.
164 lang -- The language of the identity to check.
165 local -- If true, then the query is for a JID/node
166 combination handled by this Sleek instance and
167 no stanzas need to be sent.
168 Otherwise, a disco stanza must be sent to the
169 remove JID to retrieve the info.
170 cached -- If true, then look for the disco info data from
171 the local cache system. If no results are found,
172 send the query as usual. The self.use_cache
173 setting must be set to true for this option to
174 be useful. If set to false, then the cache will
175 be skipped, even if a result has already been
176 cached. Defaults to false.
178 identity
= (data
.get('category', None),
179 data
.get('itype', None),
180 data
.get('lang', None))
182 data
= {'local': data
.get('local', False),
183 'cached': data
.get('cached', True)}
186 info
= self
.disco
.get_info(jid
=jid
, node
=node
,
188 info
= self
.disco
._wrap
(ifrom
, jid
, info
, True)
189 trunc
= lambda i
: (i
[0], i
[1], i
[2])
190 return identity
in map(trunc
, info
['disco_info']['identities'])
196 def get_info(self
, jid
, node
, ifrom
, data
):
198 Return the stored info data for the requested JID/node combination.
200 The data parameter is not used.
203 if not self
.node_exists(jid
, node
):
207 raise XMPPError(condition
='item-not-found')
209 return self
.get_node(jid
, node
)['info']
211 def set_info(self
, jid
, node
, ifrom
, data
):
213 Set the entire info stanza for a JID/node at once.
215 The data parameter is a disco#info substanza.
218 self
.add_node(jid
, node
)
219 self
.get_node(jid
, node
)['info'] = data
221 def del_info(self
, jid
, node
, ifrom
, data
):
223 Reset the info stanza for a given JID/node combination.
225 The data parameter is not used.
228 if self
.node_exists(jid
, node
):
229 self
.get_node(jid
, node
)['info'] = DiscoInfo()
231 def get_items(self
, jid
, node
, ifrom
, data
):
233 Return the stored items data for the requested JID/node combination.
235 The data parameter is not used.
238 if not self
.node_exists(jid
, node
):
242 raise XMPPError(condition
='item-not-found')
244 return self
.get_node(jid
, node
)['items']
246 def set_items(self
, jid
, node
, ifrom
, data
):
248 Replace the stored items data for a JID/node combination.
250 The data parameter may provide:
251 items -- A set of items in tuple format.
254 items
= data
.get('items', set())
255 self
.add_node(jid
, node
)
256 self
.get_node(jid
, node
)['items']['items'] = items
258 def del_items(self
, jid
, node
, ifrom
, data
):
260 Reset the items stanza for a given JID/node combination.
262 The data parameter is not used.
265 if self
.node_exists(jid
, node
):
266 self
.get_node(jid
, node
)['items'] = DiscoItems()
268 def add_identity(self
, jid
, node
, ifrom
, data
):
270 Add a new identity to te JID/node combination.
272 The data parameter may provide:
273 category -- The general category to which the agent belongs.
274 itype -- A more specific designation with the category.
275 name -- Optional human readable name for this identity.
276 lang -- Optional standard xml:lang value.
279 self
.add_node(jid
, node
)
280 self
.get_node(jid
, node
)['info'].add_identity(
281 data
.get('category', ''),
282 data
.get('itype', ''),
283 data
.get('name', None),
284 data
.get('lang', None))
286 def set_identities(self
, jid
, node
, ifrom
, data
):
288 Add or replace all identities for a JID/node combination.
290 The data parameter should include:
291 identities -- A list of identities in tuple form:
292 (category, type, name, lang)
295 identities
= data
.get('identities', set())
296 self
.add_node(jid
, node
)
297 self
.get_node(jid
, node
)['info']['identities'] = identities
299 def del_identity(self
, jid
, node
, ifrom
, data
):
301 Remove an identity from a JID/node combination.
303 The data parameter may provide:
304 category -- The general category to which the agent belonged.
305 itype -- A more specific designation with the category.
306 name -- Optional human readable name for this identity.
307 lang -- Optional, standard xml:lang value.
310 if self
.node_exists(jid
, node
):
311 self
.get_node(jid
, node
)['info'].del_identity(
312 data
.get('category', ''),
313 data
.get('itype', ''),
314 data
.get('name', None),
315 data
.get('lang', None))
317 def del_identities(self
, jid
, node
, ifrom
, data
):
319 Remove all identities from a JID/node combination.
321 The data parameter is not used.
324 if self
.node_exists(jid
, node
):
325 del self
.get_node(jid
, node
)['info']['identities']
327 def add_feature(self
, jid
, node
, ifrom
, data
):
329 Add a feature to a JID/node combination.
331 The data parameter should include:
332 feature -- The namespace of the supported feature.
335 self
.add_node(jid
, node
)
336 self
.get_node(jid
, node
)['info'].add_feature(
337 data
.get('feature', ''))
339 def set_features(self
, jid
, node
, ifrom
, data
):
341 Add or replace all features for a JID/node combination.
343 The data parameter should include:
344 features -- The new set of supported features.
347 features
= data
.get('features', set())
348 self
.add_node(jid
, node
)
349 self
.get_node(jid
, node
)['info']['features'] = features
351 def del_feature(self
, jid
, node
, ifrom
, data
):
353 Remove a feature from a JID/node combination.
355 The data parameter should include:
356 feature -- The namespace of the removed feature.
359 if self
.node_exists(jid
, node
):
360 self
.get_node(jid
, node
)['info'].del_feature(
361 data
.get('feature', ''))
363 def del_features(self
, jid
, node
, ifrom
, data
):
365 Remove all features from a JID/node combination.
367 The data parameter is not used.
370 if not self
.node_exists(jid
, node
):
372 del self
.get_node(jid
, node
)['info']['features']
374 def add_item(self
, jid
, node
, ifrom
, data
):
376 Add an item to a JID/node combination.
378 The data parameter may include:
379 ijid -- The JID for the item.
380 inode -- Optional additional information to reference
381 non-addressable items.
382 name -- Optional human readable name for the item.
385 self
.add_node(jid
, node
)
386 self
.get_node(jid
, node
)['items'].add_item(
387 data
.get('ijid', ''),
388 node
=data
.get('inode', ''),
389 name
=data
.get('name', ''))
391 def del_item(self
, jid
, node
, ifrom
, data
):
393 Remove an item from a JID/node combination.
395 The data parameter may include:
396 ijid -- JID of the item to remove.
397 inode -- Optional extra identifying information.
400 if self
.node_exists(jid
, node
):
401 self
.get_node(jid
, node
)['items'].del_item(
402 data
.get('ijid', ''),
403 node
=data
.get('inode', None))
405 def cache_info(self
, jid
, node
, ifrom
, data
):
407 Cache disco information for an external JID.
409 The data parameter is the Iq result stanza
410 containing the disco info to cache, or
411 the disco#info substanza itself.
414 if isinstance(data
, Iq
):
415 data
= data
['disco_info']
417 self
.add_node(jid
, node
, ifrom
)
418 self
.get_node(jid
, node
, ifrom
)['info'] = data
420 def get_cached_info(self
, jid
, node
, ifrom
, data
):
422 Retrieve cached disco info data.
424 The data parameter is not used.
427 if not self
.node_exists(jid
, node
, ifrom
):
430 return self
.get_node(jid
, node
, ifrom
)['info']