2010-12-09 23:57:27 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
|
|
|
See the file LICENSE for copying permission.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from sleekxmpp import Iq
|
2012-03-12 01:40:30 +00:00
|
|
|
from sleekxmpp.plugins import BasePlugin
|
2010-12-09 23:57:27 +00:00
|
|
|
from sleekxmpp.xmlstream.handler import Callback
|
|
|
|
from sleekxmpp.xmlstream.matcher import StanzaPath
|
2012-03-12 03:43:21 +00:00
|
|
|
from sleekxmpp.xmlstream import register_stanza_plugin, JID
|
2012-03-12 01:30:47 +00:00
|
|
|
from sleekxmpp.plugins.xep_0030 import stanza, DiscoInfo, DiscoItems
|
|
|
|
from sleekxmpp.plugins.xep_0030 import StaticDisco
|
2010-12-09 23:57:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2012-03-12 01:30:47 +00:00
|
|
|
class XEP_0030(BasePlugin):
|
2010-12-09 23:57:27 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
XEP-0030: Service Discovery
|
|
|
|
|
2010-12-13 20:58:59 +00:00
|
|
|
Service discovery in XMPP allows entities to discover information about
|
|
|
|
other agents in the network, such as the feature sets supported by a
|
|
|
|
client, or signposts to other, related entities.
|
|
|
|
|
|
|
|
Also see <http://www.xmpp.org/extensions/xep-0030.html>.
|
|
|
|
|
2010-12-15 23:57:45 +00:00
|
|
|
The XEP-0030 plugin works using a hierarchy of dynamic
|
|
|
|
node handlers, ranging from global handlers to specific
|
|
|
|
JID+node handlers. The default set of handlers operate
|
|
|
|
in a static manner, storing disco information in memory.
|
|
|
|
However, custom handlers may use any available backend
|
|
|
|
storage mechanism desired, such as SQLite or Redis.
|
|
|
|
|
|
|
|
Node handler hierarchy:
|
|
|
|
JID | Node | Level
|
|
|
|
---------------------
|
|
|
|
None | None | Global
|
|
|
|
Given | None | All nodes for the JID
|
|
|
|
None | Given | Node on self.xmpp.boundjid
|
|
|
|
Given | Given | A single node
|
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
Stream Handlers:
|
2010-12-13 20:58:59 +00:00
|
|
|
Disco Info -- Any Iq stanze that includes a query with the
|
|
|
|
namespace http://jabber.org/protocol/disco#info.
|
|
|
|
Disco Items -- Any Iq stanze that includes a query with the
|
|
|
|
namespace http://jabber.org/protocol/disco#items.
|
2010-12-09 23:57:27 +00:00
|
|
|
|
|
|
|
Events:
|
2010-12-13 20:58:59 +00:00
|
|
|
disco_info -- Received a disco#info Iq query result.
|
|
|
|
disco_items -- Received a disco#items Iq query result.
|
|
|
|
disco_info_query -- Received a disco#info Iq query request.
|
|
|
|
disco_items_query -- Received a disco#items Iq query request.
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2010-12-15 23:57:45 +00:00
|
|
|
Attributes:
|
2011-03-23 00:42:43 +00:00
|
|
|
stanza -- A reference to the module containing the
|
|
|
|
stanza classes provided by this plugin.
|
|
|
|
static -- Object containing the default set of
|
|
|
|
static node handlers.
|
|
|
|
default_handlers -- A dictionary mapping operations to the default
|
|
|
|
global handler (by default, the static handlers).
|
|
|
|
xmpp -- The main SleekXMPP object.
|
2010-12-15 23:57:45 +00:00
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
Methods:
|
2010-12-15 23:57:45 +00:00
|
|
|
set_node_handler -- Assign a handler to a JID/node combination.
|
|
|
|
del_node_handler -- Remove a handler from a JID/node combination.
|
|
|
|
get_info -- Retrieve disco#info data, locally or remote.
|
|
|
|
get_items -- Retrieve disco#items data, locally or remote.
|
|
|
|
set_identities --
|
|
|
|
set_features --
|
|
|
|
set_items --
|
|
|
|
del_items --
|
2010-12-09 23:57:27 +00:00
|
|
|
del_identity --
|
|
|
|
del_feature --
|
|
|
|
del_item --
|
2010-12-15 23:57:45 +00:00
|
|
|
add_identity --
|
|
|
|
add_feature --
|
|
|
|
add_item --
|
2010-12-09 23:57:27 +00:00
|
|
|
"""
|
|
|
|
|
2012-03-12 01:30:47 +00:00
|
|
|
name = 'xep_0030'
|
|
|
|
description = 'XEP-0030: Service Discovery'
|
|
|
|
dependencies = set()
|
|
|
|
stanza = stanza
|
2012-07-27 06:04:16 +00:00
|
|
|
default_config = {
|
|
|
|
'use_cache': True,
|
|
|
|
'wrap_results': False
|
|
|
|
}
|
2012-03-12 01:30:47 +00:00
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
def plugin_init(self):
|
2010-12-15 23:57:45 +00:00
|
|
|
"""
|
|
|
|
Start the XEP-0030 plugin.
|
|
|
|
"""
|
2010-12-09 23:57:27 +00:00
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Disco Info',
|
|
|
|
StanzaPath('iq/disco_info'),
|
|
|
|
self._handle_disco_info))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Disco Items',
|
|
|
|
StanzaPath('iq/disco_items'),
|
|
|
|
self._handle_disco_items))
|
|
|
|
|
|
|
|
register_stanza_plugin(Iq, DiscoInfo)
|
|
|
|
register_stanza_plugin(Iq, DiscoItems)
|
|
|
|
|
2012-01-15 01:12:39 +00:00
|
|
|
self.static = StaticDisco(self.xmpp, self)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2011-12-28 16:46:13 +00:00
|
|
|
self._disco_ops = [
|
|
|
|
'get_info', 'set_info', 'set_identities', 'set_features',
|
|
|
|
'get_items', 'set_items', 'del_items', 'add_identity',
|
|
|
|
'del_identity', 'add_feature', 'del_feature', 'add_item',
|
|
|
|
'del_item', 'del_identities', 'del_features', 'cache_info',
|
2011-12-31 01:52:44 +00:00
|
|
|
'get_cached_info', 'supports', 'has_identity']
|
2012-03-12 01:30:47 +00:00
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
for op in self._disco_ops:
|
2012-04-03 17:58:31 +00:00
|
|
|
self.api.register(getattr(self.static, op), op, default=True)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2011-03-23 00:42:43 +00:00
|
|
|
def _add_disco_op(self, op, default_handler):
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api.register(default_handler, op)
|
|
|
|
self.api.register_default(default_handler, op)
|
2011-03-23 00:42:43 +00:00
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
def set_node_handler(self, htype, jid=None, node=None, handler=None):
|
|
|
|
"""
|
2010-12-13 20:58:59 +00:00
|
|
|
Add a node handler for the given hierarchy level and
|
|
|
|
handler type.
|
|
|
|
|
|
|
|
Node handlers are ordered in a hierarchy where the
|
|
|
|
most specific handler is executed. Thus, a fallback,
|
|
|
|
global handler can be used for the majority of cases
|
|
|
|
with a few node specific handler that override the
|
|
|
|
global behavior.
|
|
|
|
|
|
|
|
Node handler hierarchy:
|
|
|
|
JID | Node | Level
|
|
|
|
---------------------
|
|
|
|
None | None | Global
|
|
|
|
Given | None | All nodes for the JID
|
|
|
|
None | Given | Node on self.xmpp.boundjid
|
|
|
|
Given | Given | A single node
|
|
|
|
|
|
|
|
Handler types:
|
|
|
|
get_info
|
|
|
|
get_items
|
|
|
|
set_identities
|
|
|
|
set_features
|
|
|
|
set_items
|
|
|
|
del_items
|
2010-12-21 16:33:03 +00:00
|
|
|
del_identities
|
2010-12-13 20:58:59 +00:00
|
|
|
del_identity
|
|
|
|
del_feature
|
2010-12-21 16:33:03 +00:00
|
|
|
del_features
|
2010-12-13 20:58:59 +00:00
|
|
|
del_item
|
|
|
|
add_identity
|
|
|
|
add_feature
|
|
|
|
add_item
|
2010-12-15 22:58:15 +00:00
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
Arguments:
|
2010-12-13 20:58:59 +00:00
|
|
|
htype -- The operation provided by the handler.
|
2010-12-15 22:58:15 +00:00
|
|
|
jid -- The JID the handler applies to. May be narrowed
|
|
|
|
further if a node is given.
|
|
|
|
node -- The particular node the handler is for. If no JID
|
|
|
|
is given, then the self.xmpp.boundjid.full is
|
|
|
|
assumed.
|
|
|
|
handler -- The handler function to use.
|
2010-12-09 23:57:27 +00:00
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api.register(handler, htype, jid, node)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
|
|
|
def del_node_handler(self, htype, jid, node):
|
|
|
|
"""
|
2010-12-13 20:58:59 +00:00
|
|
|
Remove a handler type for a JID and node combination.
|
|
|
|
|
|
|
|
The next handler in the hierarchy will be used if one
|
|
|
|
exists. If removing the global handler, make sure that
|
|
|
|
other handlers exist to process existing nodes.
|
|
|
|
|
|
|
|
Node handler hierarchy:
|
|
|
|
JID | Node | Level
|
|
|
|
---------------------
|
|
|
|
None | None | Global
|
|
|
|
Given | None | All nodes for the JID
|
|
|
|
None | Given | Node on self.xmpp.boundjid
|
|
|
|
Given | Given | A single node
|
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
Arguments:
|
2010-12-13 20:58:59 +00:00
|
|
|
htype -- The type of handler to remove.
|
|
|
|
jid -- The JID from which to remove the handler.
|
|
|
|
node -- The node from which to remove the handler.
|
2010-12-09 23:57:27 +00:00
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api.unregister(htype, jid, node)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2011-03-23 00:42:43 +00:00
|
|
|
def restore_defaults(self, jid=None, node=None, handlers=None):
|
2010-12-09 23:57:27 +00:00
|
|
|
"""
|
2011-03-23 00:42:43 +00:00
|
|
|
Change all or some of a node's handlers to the default
|
2010-12-09 23:57:27 +00:00
|
|
|
handlers. Useful for manually overriding the contents
|
|
|
|
of a node that would otherwise be handled by a JID level
|
|
|
|
or global level dynamic handler.
|
|
|
|
|
2011-03-23 00:42:43 +00:00
|
|
|
The default is to use the built-in static handlers, but that
|
|
|
|
may be changed by modifying self.default_handlers.
|
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
Arguments:
|
|
|
|
jid -- The JID owning the node to modify.
|
|
|
|
node -- The node to change to using static handlers.
|
|
|
|
handlers -- Optional list of handlers to change to the
|
2011-03-23 00:42:43 +00:00
|
|
|
default version. If provided, only these
|
2010-12-09 23:57:27 +00:00
|
|
|
handlers will be changed. Otherwise, all
|
2011-03-23 00:42:43 +00:00
|
|
|
handlers will use the default version.
|
2010-12-09 23:57:27 +00:00
|
|
|
"""
|
|
|
|
if handlers is None:
|
|
|
|
handlers = self._disco_ops
|
|
|
|
for op in handlers:
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api.restore_default(op, jid, node)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2012-03-12 01:30:47 +00:00
|
|
|
def supports(self, jid=None, node=None, feature=None, local=False,
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
cached=True, ifrom=None):
|
|
|
|
"""
|
|
|
|
Check if a JID supports a given feature.
|
|
|
|
|
|
|
|
Return values:
|
|
|
|
True -- The feature is supported
|
|
|
|
False -- The feature is not listed as supported
|
|
|
|
None -- Nothing could be found due to a timeout
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- Request info from this JID.
|
|
|
|
node -- The particular node to query.
|
|
|
|
feature -- The name of the feature to check.
|
|
|
|
local -- If true, then the query is for a JID/node
|
|
|
|
combination handled by this Sleek instance and
|
|
|
|
no stanzas need to be sent.
|
|
|
|
Otherwise, a disco stanza must be sent to the
|
|
|
|
remove JID to retrieve the info.
|
|
|
|
cached -- If true, then look for the disco info data from
|
|
|
|
the local cache system. If no results are found,
|
|
|
|
send the query as usual. The self.use_cache
|
|
|
|
setting must be set to true for this option to
|
|
|
|
be useful. If set to false, then the cache will
|
|
|
|
be skipped, even if a result has already been
|
|
|
|
cached. Defaults to false.
|
|
|
|
ifrom -- Specifiy the sender's JID.
|
|
|
|
"""
|
2011-12-31 01:52:44 +00:00
|
|
|
data = {'feature': feature,
|
|
|
|
'local': local,
|
|
|
|
'cached': cached}
|
2012-03-31 06:02:48 +00:00
|
|
|
return self.api['supports'](jid, node, ifrom, data)
|
2012-03-12 01:30:47 +00:00
|
|
|
|
2011-12-31 01:52:44 +00:00
|
|
|
def has_identity(self, jid=None, node=None, category=None, itype=None,
|
|
|
|
lang=None, local=False, cached=True, ifrom=None):
|
|
|
|
"""
|
|
|
|
Check if a JID provides a given identity.
|
|
|
|
|
|
|
|
Return values:
|
2012-03-12 01:30:47 +00:00
|
|
|
True -- The identity is provided
|
2011-12-31 01:52:44 +00:00
|
|
|
False -- The identity is not listed
|
|
|
|
None -- Nothing could be found due to a timeout
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
|
2011-12-31 01:52:44 +00:00
|
|
|
Arguments:
|
|
|
|
jid -- Request info from this JID.
|
|
|
|
node -- The particular node to query.
|
|
|
|
category -- The category of the identity to check.
|
|
|
|
itype -- The type of the identity to check.
|
|
|
|
lang -- The language of the identity to check.
|
|
|
|
local -- If true, then the query is for a JID/node
|
|
|
|
combination handled by this Sleek instance and
|
|
|
|
no stanzas need to be sent.
|
|
|
|
Otherwise, a disco stanza must be sent to the
|
|
|
|
remove JID to retrieve the info.
|
|
|
|
cached -- If true, then look for the disco info data from
|
|
|
|
the local cache system. If no results are found,
|
|
|
|
send the query as usual. The self.use_cache
|
|
|
|
setting must be set to true for this option to
|
|
|
|
be useful. If set to false, then the cache will
|
|
|
|
be skipped, even if a result has already been
|
|
|
|
cached. Defaults to false.
|
|
|
|
ifrom -- Specifiy the sender's JID.
|
|
|
|
"""
|
|
|
|
data = {'category': category,
|
|
|
|
'itype': itype,
|
|
|
|
'lang': lang,
|
|
|
|
'local': local,
|
|
|
|
'cached': cached}
|
2012-03-31 06:02:48 +00:00
|
|
|
return self.api['has_identity'](jid, node, ifrom, data)
|
2012-03-12 01:30:47 +00:00
|
|
|
|
|
|
|
def get_info(self, jid=None, node=None, local=False,
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
cached=None, **kwargs):
|
2010-12-09 23:57:27 +00:00
|
|
|
"""
|
2010-12-15 22:58:15 +00:00
|
|
|
Retrieve the disco#info results from a given JID/node combination.
|
|
|
|
|
|
|
|
Info may be retrieved from both local resources and remote agents;
|
|
|
|
the local parameter indicates if the information should be gathered
|
|
|
|
by executing the local node handlers, or if a disco#info stanza
|
|
|
|
must be generated and sent.
|
|
|
|
|
2011-01-05 00:39:10 +00:00
|
|
|
If requesting items from a local JID/node, then only a DiscoInfo
|
|
|
|
stanza will be returned. Otherwise, an Iq stanza will be returned.
|
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
Arguments:
|
2010-12-15 22:58:15 +00:00
|
|
|
jid -- Request info from this JID.
|
|
|
|
node -- The particular node to query.
|
|
|
|
local -- If true, then the query is for a JID/node
|
|
|
|
combination handled by this Sleek instance and
|
|
|
|
no stanzas need to be sent.
|
|
|
|
Otherwise, a disco stanza must be sent to the
|
|
|
|
remove JID to retrieve the info.
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
cached -- If true, then look for the disco info data from
|
|
|
|
the local cache system. If no results are found,
|
|
|
|
send the query as usual. The self.use_cache
|
|
|
|
setting must be set to true for this option to
|
|
|
|
be useful. If set to false, then the cache will
|
|
|
|
be skipped, even if a result has already been
|
|
|
|
cached. Defaults to false.
|
2010-12-17 04:52:17 +00:00
|
|
|
ifrom -- Specifiy the sender's JID.
|
2010-12-15 22:58:15 +00:00
|
|
|
block -- If true, block and wait for the stanzas' reply.
|
|
|
|
timeout -- The time in seconds to block while waiting for
|
2011-01-04 23:38:21 +00:00
|
|
|
a reply. If None, then wait indefinitely. The
|
|
|
|
timeout value is only used when block=True.
|
2010-12-15 22:58:15 +00:00
|
|
|
callback -- Optional callback to execute when a reply is
|
|
|
|
received instead of blocking and waiting for
|
|
|
|
the reply.
|
2010-12-09 23:57:27 +00:00
|
|
|
"""
|
2011-12-31 01:51:41 +00:00
|
|
|
if jid is not None and not isinstance(jid, JID):
|
|
|
|
jid = JID(jid)
|
|
|
|
if self.xmpp.is_component:
|
|
|
|
if jid.domain == self.xmpp.boundjid.domain:
|
|
|
|
local = True
|
|
|
|
else:
|
|
|
|
if str(jid) == str(self.xmpp.boundjid):
|
|
|
|
local = True
|
2012-01-12 00:39:55 +00:00
|
|
|
jid = jid.full
|
2012-04-08 20:00:07 +00:00
|
|
|
elif jid in (None, ''):
|
|
|
|
local = True
|
2011-12-31 01:51:41 +00:00
|
|
|
|
2012-04-08 20:00:07 +00:00
|
|
|
if local:
|
2010-12-09 23:57:27 +00:00
|
|
|
log.debug("Looking up local disco#info data " + \
|
2011-11-19 20:07:57 +00:00
|
|
|
"for %s, node %s.", jid, node)
|
2012-06-19 08:29:48 +00:00
|
|
|
info = self.api['get_info'](jid, node,
|
|
|
|
kwargs.get('ifrom', None),
|
2012-03-31 06:02:48 +00:00
|
|
|
kwargs)
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
info = self._fix_default_info(info)
|
|
|
|
return self._wrap(kwargs.get('ifrom', None), jid, info)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
if cached:
|
|
|
|
log.debug("Looking up cached disco#info data " + \
|
|
|
|
"for %s, node %s.", jid, node)
|
2012-06-19 08:29:48 +00:00
|
|
|
info = self.api['get_cached_info'](jid, node,
|
|
|
|
kwargs.get('ifrom', None),
|
2012-03-31 06:02:48 +00:00
|
|
|
kwargs)
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
if info is not None:
|
|
|
|
return self._wrap(kwargs.get('ifrom', None), jid, info)
|
2012-03-12 01:30:47 +00:00
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
iq = self.xmpp.Iq()
|
2010-12-28 20:37:02 +00:00
|
|
|
# Check dfrom parameter for backwards compatibility
|
|
|
|
iq['from'] = kwargs.get('ifrom', kwargs.get('dfrom', ''))
|
2010-12-09 23:57:27 +00:00
|
|
|
iq['to'] = jid
|
|
|
|
iq['type'] = 'get'
|
|
|
|
iq['disco_info']['node'] = node if node else ''
|
|
|
|
return iq.send(timeout=kwargs.get('timeout', None),
|
2011-02-09 14:12:44 +00:00
|
|
|
block=kwargs.get('block', True),
|
2010-12-09 23:57:27 +00:00
|
|
|
callback=kwargs.get('callback', None))
|
|
|
|
|
2011-12-28 16:46:13 +00:00
|
|
|
def set_info(self, jid=None, node=None, info=None):
|
|
|
|
"""
|
|
|
|
Set the disco#info data for a JID/node based on an existing
|
|
|
|
disco#info stanza.
|
|
|
|
"""
|
|
|
|
if isinstance(info, Iq):
|
|
|
|
info = info['disco_info']
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['set_info'](jid, node, None, info)
|
2011-12-28 16:46:13 +00:00
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
def get_items(self, jid=None, node=None, local=False, **kwargs):
|
|
|
|
"""
|
2010-12-15 22:58:15 +00:00
|
|
|
Retrieve the disco#items results from a given JID/node combination.
|
|
|
|
|
|
|
|
Items may be retrieved from both local resources and remote agents;
|
|
|
|
the local parameter indicates if the items should be gathered by
|
|
|
|
executing the local node handlers, or if a disco#items stanza must
|
|
|
|
be generated and sent.
|
|
|
|
|
2011-01-05 00:39:10 +00:00
|
|
|
If requesting items from a local JID/node, then only a DiscoItems
|
|
|
|
stanza will be returned. Otherwise, an Iq stanza will be returned.
|
|
|
|
|
2010-12-09 23:57:27 +00:00
|
|
|
Arguments:
|
2010-12-15 22:58:15 +00:00
|
|
|
jid -- Request info from this JID.
|
|
|
|
node -- The particular node to query.
|
|
|
|
local -- If true, then the query is for a JID/node
|
|
|
|
combination handled by this Sleek instance and
|
|
|
|
no stanzas need to be sent.
|
|
|
|
Otherwise, a disco stanza must be sent to the
|
|
|
|
remove JID to retrieve the items.
|
2010-12-17 04:52:17 +00:00
|
|
|
ifrom -- Specifiy the sender's JID.
|
2010-12-15 22:58:15 +00:00
|
|
|
block -- If true, block and wait for the stanzas' reply.
|
|
|
|
timeout -- The time in seconds to block while waiting for
|
|
|
|
a reply. If None, then wait indefinitely.
|
|
|
|
callback -- Optional callback to execute when a reply is
|
|
|
|
received instead of blocking and waiting for
|
|
|
|
the reply.
|
2011-01-08 16:19:31 +00:00
|
|
|
iterator -- If True, return a result set iterator using
|
|
|
|
the XEP-0059 plugin, if the plugin is loaded.
|
|
|
|
Otherwise the parameter is ignored.
|
2010-12-09 23:57:27 +00:00
|
|
|
"""
|
|
|
|
if local or jid is None:
|
2012-06-19 08:29:48 +00:00
|
|
|
items = self.api['get_items'](jid, node,
|
|
|
|
kwargs.get('ifrom', None),
|
2012-03-31 06:02:48 +00:00
|
|
|
kwargs)
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
return self._wrap(kwargs.get('ifrom', None), jid, items)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
|
|
|
iq = self.xmpp.Iq()
|
2010-12-28 20:37:02 +00:00
|
|
|
# Check dfrom parameter for backwards compatibility
|
|
|
|
iq['from'] = kwargs.get('ifrom', kwargs.get('dfrom', ''))
|
2010-12-09 23:57:27 +00:00
|
|
|
iq['to'] = jid
|
|
|
|
iq['type'] = 'get'
|
|
|
|
iq['disco_items']['node'] = node if node else ''
|
2011-01-08 16:19:31 +00:00
|
|
|
if kwargs.get('iterator', False) and self.xmpp['xep_0059']:
|
|
|
|
return self.xmpp['xep_0059'].iterate(iq, 'disco_items')
|
|
|
|
else:
|
|
|
|
return iq.send(timeout=kwargs.get('timeout', None),
|
2011-02-09 13:58:00 +00:00
|
|
|
block=kwargs.get('block', True),
|
2011-01-08 16:19:31 +00:00
|
|
|
callback=kwargs.get('callback', None))
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2010-12-15 23:57:45 +00:00
|
|
|
def set_items(self, jid=None, node=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Set or replace all items for the specified JID/node combination.
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2010-12-15 23:57:45 +00:00
|
|
|
The given items must be in a list or set where each item is a
|
|
|
|
tuple of the form: (jid, node, name).
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2010-12-15 23:57:45 +00:00
|
|
|
Arguments:
|
|
|
|
jid -- The JID to modify.
|
|
|
|
node -- Optional node to modify.
|
|
|
|
items -- A series of items in tuple format.
|
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['set_items'](jid, node, None, kwargs)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
|
|
|
def del_items(self, jid=None, node=None, **kwargs):
|
2010-12-15 23:57:45 +00:00
|
|
|
"""
|
|
|
|
Remove all items from the given JID/node combination.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID to modify.
|
|
|
|
node -- Optional node to modify.
|
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['del_items'](jid, node, None, kwargs)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2011-01-19 17:08:28 +00:00
|
|
|
def add_item(self, jid='', name='', node=None, subnode='', ijid=None):
|
2010-12-15 23:57:45 +00:00
|
|
|
"""
|
|
|
|
Add a new item element to the given JID/node combination.
|
|
|
|
|
|
|
|
Each item is required to have a JID, but may also specify
|
|
|
|
a node value to reference non-addressable entities.
|
|
|
|
|
|
|
|
Arguments:
|
2010-12-28 20:37:02 +00:00
|
|
|
jid -- The JID for the item.
|
2010-12-15 23:57:45 +00:00
|
|
|
name -- Optional name for the item.
|
2010-12-28 20:37:02 +00:00
|
|
|
node -- The node to modify.
|
|
|
|
subnode -- Optional node for the item.
|
|
|
|
ijid -- The JID to modify.
|
2010-12-15 23:57:45 +00:00
|
|
|
"""
|
2011-01-19 22:27:53 +00:00
|
|
|
if not jid:
|
|
|
|
jid = self.xmpp.boundjid.full
|
2010-12-28 20:37:02 +00:00
|
|
|
kwargs = {'ijid': jid,
|
|
|
|
'name': name,
|
|
|
|
'inode': subnode}
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['add_item'](ijid, node, None, kwargs)
|
2010-12-15 23:57:45 +00:00
|
|
|
|
|
|
|
def del_item(self, jid=None, node=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Remove a single item from the given JID/node combination.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID to modify.
|
|
|
|
node -- The node to modify.
|
|
|
|
ijid -- The item's JID.
|
|
|
|
inode -- The item's node.
|
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['del_item'](jid, node, None, kwargs)
|
2010-12-15 23:57:45 +00:00
|
|
|
|
2011-02-14 18:49:43 +00:00
|
|
|
def add_identity(self, category='', itype='', name='',
|
|
|
|
node=None, jid=None, lang=None):
|
2010-12-15 23:57:45 +00:00
|
|
|
"""
|
|
|
|
Add a new identity to the given JID/node combination.
|
|
|
|
|
|
|
|
Each identity must be unique in terms of all four identity
|
|
|
|
components: category, type, name, and language.
|
|
|
|
|
|
|
|
Multiple, identical category/type pairs are allowed only
|
|
|
|
if the xml:lang values are different. Likewise, multiple
|
|
|
|
category/type/xml:lang pairs are allowed so long as the
|
|
|
|
names are different. A category and type is always required.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
category -- The identity's category.
|
|
|
|
itype -- The identity's type.
|
|
|
|
name -- Optional name for the identity.
|
|
|
|
lang -- Optional two-letter language code.
|
2010-12-28 20:37:02 +00:00
|
|
|
node -- The node to modify.
|
|
|
|
jid -- The JID to modify.
|
2010-12-15 23:57:45 +00:00
|
|
|
"""
|
2010-12-28 20:37:02 +00:00
|
|
|
kwargs = {'category': category,
|
|
|
|
'itype': itype,
|
|
|
|
'name': name,
|
|
|
|
'lang': lang}
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['add_identity'](jid, node, None, kwargs)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2010-12-28 20:37:02 +00:00
|
|
|
def add_feature(self, feature, node=None, jid=None):
|
2010-12-15 23:57:45 +00:00
|
|
|
"""
|
|
|
|
Add a feature to a JID/node combination.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
feature -- The namespace of the supported feature.
|
2010-12-28 20:37:02 +00:00
|
|
|
node -- The node to modify.
|
|
|
|
jid -- The JID to modify.
|
2010-12-15 23:57:45 +00:00
|
|
|
"""
|
2010-12-28 20:37:02 +00:00
|
|
|
kwargs = {'feature': feature}
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['add_feature'](jid, node, None, kwargs)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
|
|
|
def del_identity(self, jid=None, node=None, **kwargs):
|
2010-12-15 23:57:45 +00:00
|
|
|
"""
|
|
|
|
Remove an identity from the given JID/node combination.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID to modify.
|
|
|
|
node -- The node to modify.
|
|
|
|
category -- The identity's category.
|
|
|
|
itype -- The identity's type value.
|
|
|
|
name -- Optional, human readable name for the identity.
|
|
|
|
lang -- Optional, the identity's xml:lang value.
|
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['del_identity'](jid, node, None, kwargs)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
|
|
|
def del_feature(self, jid=None, node=None, **kwargs):
|
2010-12-15 23:57:45 +00:00
|
|
|
"""
|
|
|
|
Remove a feature from a given JID/node combination.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID to modify.
|
|
|
|
node -- The node to modify.
|
|
|
|
feature -- The feature's namespace.
|
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['del_feature'](jid, node, None, kwargs)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2010-12-15 23:57:45 +00:00
|
|
|
def set_identities(self, jid=None, node=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Add or replace all identities for the given JID/node combination.
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2010-12-15 23:57:45 +00:00
|
|
|
The identities must be in a set where each identity is a tuple
|
|
|
|
of the form: (category, type, lang, name)
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID to modify.
|
|
|
|
node -- The node to modify.
|
|
|
|
identities -- A set of identities in tuple form.
|
|
|
|
lang -- Optional, xml:lang value.
|
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['set_identities'](jid, node, None, kwargs)
|
2010-12-15 23:57:45 +00:00
|
|
|
|
|
|
|
def del_identities(self, jid=None, node=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Remove all identities for a JID/node combination.
|
|
|
|
|
|
|
|
If a language is specified, only identities using that
|
|
|
|
language will be removed.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID to modify.
|
|
|
|
node -- The node to modify.
|
|
|
|
lang -- Optional. If given, only remove identities
|
|
|
|
using this xml:lang value.
|
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['del_identities'](jid, node, None, kwargs)
|
2010-12-15 23:57:45 +00:00
|
|
|
|
|
|
|
def set_features(self, jid=None, node=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Add or replace the set of supported features
|
|
|
|
for a JID/node combination.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID to modify.
|
|
|
|
node -- The node to modify.
|
|
|
|
features -- The new set of supported features.
|
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['set_features'](jid, node, None, kwargs)
|
2010-12-15 23:57:45 +00:00
|
|
|
|
|
|
|
def del_features(self, jid=None, node=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Remove all features from a JID/node combination.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
jid -- The JID to modify.
|
|
|
|
node -- The node to modify.
|
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
self.api['del_features'](jid, node, None, kwargs)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
2011-12-31 01:51:02 +00:00
|
|
|
def _run_node_handler(self, htype, jid, node=None, ifrom=None, data={}):
|
2010-12-09 23:57:27 +00:00
|
|
|
"""
|
|
|
|
Execute the most specific node handler for the given
|
|
|
|
JID/node combination.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
htype -- The handler type to execute.
|
|
|
|
jid -- The JID requested.
|
|
|
|
node -- The node requested.
|
2010-12-13 20:58:59 +00:00
|
|
|
data -- Optional, custom data to pass to the handler.
|
2010-12-09 23:57:27 +00:00
|
|
|
"""
|
2012-03-31 06:02:48 +00:00
|
|
|
return self.api[htype](jid, node, ifrom, data)
|
2010-12-09 23:57:27 +00:00
|
|
|
|
|
|
|
def _handle_disco_info(self, iq):
|
|
|
|
"""
|
|
|
|
Process an incoming disco#info stanza. If it is a get
|
|
|
|
request, find and return the appropriate identities
|
|
|
|
and features. If it is an info result, fire the
|
|
|
|
disco_info event.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
iq -- The incoming disco#items stanza.
|
|
|
|
"""
|
|
|
|
if iq['type'] == 'get':
|
|
|
|
log.debug("Received disco info query from " + \
|
2011-11-19 20:07:57 +00:00
|
|
|
"<%s> to <%s>.", iq['from'], iq['to'])
|
2012-07-10 05:22:05 +00:00
|
|
|
info = self.api['get_info'](iq['to'],
|
2012-03-31 06:02:48 +00:00
|
|
|
iq['disco_info']['node'],
|
|
|
|
iq['from'],
|
|
|
|
iq)
|
2011-12-13 03:38:32 +00:00
|
|
|
if isinstance(info, Iq):
|
2012-04-08 20:00:07 +00:00
|
|
|
info['id'] = iq['id']
|
2011-12-13 03:38:32 +00:00
|
|
|
info.send()
|
|
|
|
else:
|
|
|
|
iq.reply()
|
|
|
|
if info:
|
|
|
|
info = self._fix_default_info(info)
|
|
|
|
iq.set_payload(info.xml)
|
|
|
|
iq.send()
|
2010-12-09 23:57:27 +00:00
|
|
|
elif iq['type'] == 'result':
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
log.debug("Received disco info result from " + \
|
|
|
|
"<%s> to <%s>.", iq['from'], iq['to'])
|
|
|
|
if self.use_cache:
|
|
|
|
log.debug("Caching disco info result from " \
|
|
|
|
"<%s> to <%s>.", iq['from'], iq['to'])
|
2011-12-28 16:37:05 +00:00
|
|
|
if self.xmpp.is_component:
|
|
|
|
ito = iq['to'].full
|
|
|
|
else:
|
|
|
|
ito = None
|
2012-07-10 05:22:05 +00:00
|
|
|
self.api['cache_info'](iq['from'],
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
iq['disco_info']['node'],
|
2011-12-28 16:37:05 +00:00
|
|
|
ito,
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
iq)
|
2010-12-09 23:57:27 +00:00
|
|
|
self.xmpp.event('disco_info', iq)
|
|
|
|
|
|
|
|
def _handle_disco_items(self, iq):
|
|
|
|
"""
|
|
|
|
Process an incoming disco#items stanza. If it is a get
|
|
|
|
request, find and return the appropriate items. If it
|
|
|
|
is an items result, fire the disco_items event.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
iq -- The incoming disco#items stanza.
|
|
|
|
"""
|
|
|
|
if iq['type'] == 'get':
|
|
|
|
log.debug("Received disco items query from " + \
|
2011-11-19 20:07:57 +00:00
|
|
|
"<%s> to <%s>.", iq['from'], iq['to'])
|
2012-07-10 05:22:05 +00:00
|
|
|
items = self.api['get_items'](iq['to'],
|
2011-12-13 00:34:24 +00:00
|
|
|
iq['disco_items']['node'],
|
2012-07-10 05:22:05 +00:00
|
|
|
iq['from'],
|
2011-12-13 00:34:24 +00:00
|
|
|
iq)
|
2011-12-13 03:38:32 +00:00
|
|
|
if isinstance(items, Iq):
|
|
|
|
items.send()
|
|
|
|
else:
|
|
|
|
iq.reply()
|
|
|
|
if items:
|
|
|
|
iq.set_payload(items.xml)
|
|
|
|
iq.send()
|
2010-12-09 23:57:27 +00:00
|
|
|
elif iq['type'] == 'result':
|
2011-12-31 01:50:15 +00:00
|
|
|
log.debug("Received disco items result from " + \
|
2011-11-19 20:07:57 +00:00
|
|
|
"%s to %s.", iq['from'], iq['to'])
|
2010-12-09 23:57:27 +00:00
|
|
|
self.xmpp.event('disco_items', iq)
|
|
|
|
|
|
|
|
def _fix_default_info(self, info):
|
|
|
|
"""
|
|
|
|
Disco#info results for a JID are required to include at least
|
|
|
|
one identity and feature. As a default, if no other identity is
|
|
|
|
provided, SleekXMPP will use either the generic component or the
|
|
|
|
bot client identity. A the standard disco#info feature will also be
|
|
|
|
added if no features are provided.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
info -- The disco#info quest (not the full Iq stanza) to modify.
|
|
|
|
"""
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
result = info
|
|
|
|
if isinstance(info, Iq):
|
2012-02-18 19:56:10 +00:00
|
|
|
info = info['disco_info']
|
2010-12-09 23:57:27 +00:00
|
|
|
if not info['node']:
|
|
|
|
if not info['identities']:
|
|
|
|
if self.xmpp.is_component:
|
2011-12-31 01:50:15 +00:00
|
|
|
log.debug("No identity found for this entity. " + \
|
2010-12-09 23:57:27 +00:00
|
|
|
"Using default component identity.")
|
|
|
|
info.add_identity('component', 'generic')
|
|
|
|
else:
|
2011-12-31 01:50:15 +00:00
|
|
|
log.debug("No identity found for this entity. " + \
|
2010-12-09 23:57:27 +00:00
|
|
|
"Using default client identity.")
|
|
|
|
info.add_identity('client', 'bot')
|
|
|
|
if not info['features']:
|
2011-12-31 01:50:15 +00:00
|
|
|
log.debug("No features found for this entity. " + \
|
2010-12-09 23:57:27 +00:00
|
|
|
"Using default disco#info feature.")
|
|
|
|
info.add_feature(info.namespace)
|
Add caching support to xep_0030.
New plugin configuration options:
use_cache - Enable caching disco info results. Defaults to True
wrap_results - Always return disco results in an Iq stanza. Defaults
to False
Node handler changes:
Handlers now take four arguments: jid, node, ifrom, data
Most older style handlers will still work, depending on if they
raise a TypeError for incorrect number of arguments. Handlers that
used *args may not work.
New get_info options:
cached - Passing cached=True to get_info() will attempt to load
results from the cache. If nothing is found, a query
will be sent as normal. If set to False, the cache
will be skipped, even if it contains results.
New method:
supports() - Given a JID/node pair and a feature, return True
if the feature is supported, False if not, and
None if there was a timeout. By default, the search
will use the cache.
2011-12-28 15:07:33 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
def _wrap(self, ito, ifrom, payload, force=False):
|
|
|
|
"""
|
|
|
|
Ensure that results are wrapped in an Iq stanza
|
|
|
|
if self.wrap_results has been set to True.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
ito -- The JID to use as the 'to' value
|
|
|
|
ifrom -- The JID to use as the 'from' value
|
|
|
|
payload -- The disco data to wrap
|
|
|
|
force -- Force wrapping, regardless of self.wrap_results
|
|
|
|
"""
|
|
|
|
if (force or self.wrap_results) and not isinstance(payload, Iq):
|
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
# Since we're simulating a result, we have to treat
|
|
|
|
# the 'from' and 'to' values opposite the normal way.
|
|
|
|
iq['to'] = self.xmpp.boundjid if ito is None else ito
|
|
|
|
iq['from'] = self.xmpp.boundjid if ifrom is None else ifrom
|
|
|
|
iq['type'] = 'result'
|
|
|
|
iq.append(payload)
|
|
|
|
return iq
|
|
|
|
return payload
|