Update XEP-0030 and XEP-0059 to new system.

This commit is contained in:
Lance Stout 2012-03-11 18:30:47 -07:00
parent 1a153487c3
commit 8b06d10415
4 changed files with 50 additions and 48 deletions

View file

@ -11,17 +11,18 @@ import logging
import sleekxmpp
from sleekxmpp import Iq
from sleekxmpp.exceptions import XMPPError, IqError, IqTimeout
from sleekxmpp.plugins.base import base_plugin
from sleekxmpp.plugins import BasePlugin, register_plugin
from sleekxmpp.xmlstream.handler import Callback
from sleekxmpp.xmlstream.matcher import StanzaPath
from sleekxmpp.xmlstream import register_stanza_plugin, ElementBase, ET, JID
from sleekxmpp.plugins.xep_0030 import DiscoInfo, DiscoItems, StaticDisco
from sleekxmpp.plugins.xep_0030 import stanza, DiscoInfo, DiscoItems
from sleekxmpp.plugins.xep_0030 import StaticDisco
log = logging.getLogger(__name__)
class xep_0030(base_plugin):
class XEP_0030(BasePlugin):
"""
XEP-0030: Service Discovery
@ -85,14 +86,15 @@ class xep_0030(base_plugin):
add_item --
"""
name = 'xep_0030'
description = 'XEP-0030: Service Discovery'
dependencies = set()
stanza = stanza
def plugin_init(self):
"""
Start the XEP-0030 plugin.
"""
self.xep = '0030'
self.description = 'Service Discovery'
self.stanza = sleekxmpp.plugins.xep_0030.stanza
self.xmpp.register_handler(
Callback('Disco Info',
StanzaPath('iq/disco_info'),
@ -117,19 +119,12 @@ class xep_0030(base_plugin):
'del_identity', 'add_feature', 'del_feature', 'add_item',
'del_item', 'del_identities', 'del_features', 'cache_info',
'get_cached_info', 'supports', 'has_identity']
self.default_handlers = {}
self._handlers = {}
for op in self._disco_ops:
self._add_disco_op(op, getattr(self.static, op))
def post_init(self):
"""Handle cross-plugin dependencies."""
base_plugin.post_init(self)
if 'xep_0059' in self.xmpp.plugin:
register_stanza_plugin(DiscoItems,
self.xmpp['xep_0059'].stanza.Set)
def _add_disco_op(self, op, default_handler):
self.default_handlers[op] = default_handler
self._handlers[op] = {'global': default_handler,
@ -242,7 +237,7 @@ class xep_0030(base_plugin):
self.del_node_handler(op, jid, node)
self.set_node_handler(op, jid, node, self.default_handlers[op])
def supports(self, jid=None, node=None, feature=None, local=False,
def supports(self, jid=None, node=None, feature=None, local=False,
cached=True, ifrom=None):
"""
Check if a JID supports a given feature.
@ -274,14 +269,14 @@ class xep_0030(base_plugin):
'local': local,
'cached': cached}
return self._run_node_handler('supports', jid, node, ifrom, data)
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:
True -- The identity is provided
True -- The identity is provided
False -- The identity is not listed
None -- Nothing could be found due to a timeout
@ -311,8 +306,8 @@ class xep_0030(base_plugin):
'local': local,
'cached': cached}
return self._run_node_handler('has_identity', jid, node, ifrom, data)
def get_info(self, jid=None, node=None, local=False,
def get_info(self, jid=None, node=None, local=False,
cached=None, **kwargs):
"""
Retrieve the disco#info results from a given JID/node combination.
@ -362,7 +357,7 @@ class xep_0030(base_plugin):
if local or jid in (None, ''):
log.debug("Looking up local disco#info data " + \
"for %s, node %s.", jid, node)
info = self._run_node_handler('get_info',
info = self._run_node_handler('get_info',
jid, node, kwargs.get('ifrom', None), kwargs)
info = self._fix_default_info(info)
return self._wrap(kwargs.get('ifrom', None), jid, info)
@ -370,11 +365,11 @@ class xep_0030(base_plugin):
if cached:
log.debug("Looking up cached disco#info data " + \
"for %s, node %s.", jid, node)
info = self._run_node_handler('get_cached_info',
info = self._run_node_handler('get_cached_info',
jid, node, kwargs.get('ifrom', None), kwargs)
if info is not None:
return self._wrap(kwargs.get('ifrom', None), jid, info)
iq = self.xmpp.Iq()
# Check dfrom parameter for backwards compatibility
iq['from'] = kwargs.get('ifrom', kwargs.get('dfrom', ''))
@ -426,7 +421,7 @@ class xep_0030(base_plugin):
Otherwise the parameter is ignored.
"""
if local or jid is None:
items = self._run_node_handler('get_items',
items = self._run_node_handler('get_items',
jid, node, kwargs.get('ifrom', None), kwargs)
return self._wrap(kwargs.get('ifrom', None), jid, items)
@ -794,7 +789,10 @@ class xep_0030(base_plugin):
return payload
register_plugin(XEP_0030)
# Retain some backwards compatibility
xep_0030.getInfo = xep_0030.get_info
xep_0030.getItems = xep_0030.get_items
xep_0030.make_static = xep_0030.restore_defaults
xep_0030 = XEP_0030
XEP_0030.getInfo = XEP_0030.get_info
XEP_0030.getItems = XEP_0030.get_items
XEP_0030.make_static = XEP_0030.restore_defaults

View file

@ -147,4 +147,3 @@ class DiscoItem(ElementBase):
register_stanza_plugin(DiscoItems, DiscoItem, iterable=True)

View file

@ -12,7 +12,6 @@ import threading
import sleekxmpp
from sleekxmpp import Iq
from sleekxmpp.exceptions import XMPPError, IqError, IqTimeout
from sleekxmpp.plugins.base import base_plugin
from sleekxmpp.xmlstream.handler import Callback
from sleekxmpp.xmlstream.matcher import StanzaPath
from sleekxmpp.xmlstream import register_stanza_plugin, ElementBase, ET, JID
@ -104,7 +103,7 @@ class StaticDisco(object):
ifrom = ifrom.full
if (jid, node, ifrom) not in self.nodes:
return False
return True
return True
# =================================================================
# Node Handlers
@ -117,7 +116,7 @@ class StaticDisco(object):
# additional parameters that will be passed to other calls.
#
# This implementation does not allow different responses based on
# the requester's JID, except for cached results. To do that,
# the requester's JID, except for cached results. To do that,
# register a custom node handler.
def supports(self, jid, node, ifrom, data):
@ -148,7 +147,7 @@ class StaticDisco(object):
return False
try:
info = self.disco.get_info(jid=jid, node=node,
info = self.disco.get_info(jid=jid, node=node,
ifrom=ifrom, **data)
info = self.disco._wrap(ifrom, jid, info, True)
features = info['disco_info']['features']
@ -179,7 +178,7 @@ class StaticDisco(object):
be skipped, even if a result has already been
cached. Defaults to false.
"""
identity = (data.get('category', None),
identity = (data.get('category', None),
data.get('itype', None),
data.get('lang', None))
@ -192,7 +191,7 @@ class StaticDisco(object):
return True
try:
info = self.disco.get_info(jid=jid, node=node,
info = self.disco.get_info(jid=jid, node=node,
ifrom=ifrom, **data)
info = self.disco._wrap(ifrom, jid, info, True)
trunc = lambda i: (i[0], i[1], i[2])
@ -202,7 +201,6 @@ class StaticDisco(object):
except IqTimeout:
return None
def get_info(self, jid, node, ifrom, data):
"""
Return the stored info data for the requested JID/node combination.
@ -343,7 +341,8 @@ class StaticDisco(object):
"""
with self.lock:
self.add_node(jid, node)
self.get_node(jid, node)['info'].add_feature(data.get('feature', ''))
self.get_node(jid, node)['info'].add_feature(
data.get('feature', ''))
def set_features(self, jid, node, ifrom, data):
"""
@ -366,7 +365,8 @@ class StaticDisco(object):
"""
with self.lock:
if self.node_exists(jid, node):
self.get_node(jid, node)['info'].del_feature(data.get('feature', ''))
self.get_node(jid, node)['info'].del_feature(
data.get('feature', ''))
def del_features(self, jid, node, ifrom, data):
"""

View file

@ -10,9 +10,9 @@ import logging
import sleekxmpp
from sleekxmpp import Iq
from sleekxmpp.plugins.base import base_plugin
from sleekxmpp.plugins import BasePlugin, register_plugin
from sleekxmpp.xmlstream import register_stanza_plugin
from sleekxmpp.plugins.xep_0059 import Set
from sleekxmpp.plugins.xep_0059 import stanza, Set
from sleekxmpp.exceptions import XMPPError
@ -85,7 +85,7 @@ class ResultIterator():
num_items = len(r[self.interface]['substanzas'])
if first + num_items == count:
raise StopIteration
if self.reverse:
self.start = r[self.interface]['rsm']['first']
else:
@ -96,24 +96,24 @@ class ResultIterator():
raise StopIteration
class xep_0059(base_plugin):
class XEP_0059(BasePlugin):
"""
XEP-0050: Result Set Management
"""
name = 'xep_0059'
description = 'XEP-0059: Result Set Management'
dependencies = set(['xep_0030'])
stanza = stanza
def plugin_init(self):
"""
Start the XEP-0059 plugin.
"""
self.xep = '0059'
self.description = 'Result Set Management'
self.stanza = sleekxmpp.plugins.xep_0059.stanza
def post_init(self):
"""Handle inter-plugin dependencies."""
base_plugin.post_init(self)
self.xmpp['xep_0030'].add_feature(Set.namespace)
register_stanza_plugin(self.xmpp['xep_0030'].stanza.DiscoItems,
self.stanza.Set)
def iterate(self, stanza, interface):
"""
@ -129,3 +129,8 @@ class xep_0059(base_plugin):
the interface 'disco_items' should be used.
"""
return ResultIterator(stanza, interface)
register_plugin(XEP_0059)
xep_0059 = XEP_0059