From 2908751020b61da5466ff15644628e29bd5bbd3a Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Wed, 5 Jan 2011 20:16:15 -0500 Subject: [PATCH 1/7] Allow JID objects to be compared with strings. Two JIDs match if they have the same full JID value. --- sleekxmpp/xmlstream/jid.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sleekxmpp/xmlstream/jid.py b/sleekxmpp/xmlstream/jid.py index d8f45b9..e5c49dc 100644 --- a/sleekxmpp/xmlstream/jid.py +++ b/sleekxmpp/xmlstream/jid.py @@ -124,3 +124,9 @@ class JID(object): def __repr__(self): return str(self) + + def __eq__(self, other): + """ + Two JIDs are considered equal if they have the same full JID value. + """ + return str(other) == str(self) From 13a2f719f4e31e48df05a5df8fa470d704e15528 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 7 Jan 2011 16:41:31 -0500 Subject: [PATCH 2/7] Add reattempt to ClientXMPP.connect --- sleekxmpp/clientxmpp.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sleekxmpp/clientxmpp.py b/sleekxmpp/clientxmpp.py index 32795e4..734c9a1 100644 --- a/sleekxmpp/clientxmpp.py +++ b/sleekxmpp/clientxmpp.py @@ -139,7 +139,7 @@ class ClientXMPP(BaseXMPP): log.debug("Session start has taken more than 15 seconds") self.disconnect(reconnect=self.auto_reconnect) - def connect(self, address=tuple()): + def connect(self, address=tuple(), reattempt=True): """ Connect to the XMPP server. @@ -148,7 +148,9 @@ class ClientXMPP(BaseXMPP): will be used. Arguments: - address -- A tuple containing the server's host and port. + address -- A tuple containing the server's host and port. + reattempt -- If True, reattempt the connection if an + error occurs. """ self.session_started_event.clear() if not address or len(address) < 2: @@ -190,7 +192,8 @@ class ClientXMPP(BaseXMPP): # If all else fails, use the server from the JID. address = (self.boundjid.host, 5222) - return XMLStream.connect(self, address[0], address[1], use_tls=True) + return XMLStream.connect(self, address[0], address[1], + use_tls=True, reattempt=reattempt) def register_feature(self, mask, pointer, breaker=False): """ From a8e36574873e86d75a5ca796ce212c10dd3266ff Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sat, 8 Jan 2011 10:58:47 -0500 Subject: [PATCH 3/7] Added new XEP-0059 plugin. Contributed by Erik Reuterborg Larsson (who). --- sleekxmpp/plugins/xep_0059/__init__.py | 10 ++ sleekxmpp/plugins/xep_0059/rsm.py | 118 ++++++++++++++++++ sleekxmpp/plugins/xep_0059/stanza.py | 108 +++++++++++++++++ tests/test_stanza_xep_0059.py | 106 ++++++++++++++++ tests/test_stream_xep_0059.py | 162 +++++++++++++++++++++++++ 5 files changed, 504 insertions(+) create mode 100644 sleekxmpp/plugins/xep_0059/__init__.py create mode 100644 sleekxmpp/plugins/xep_0059/rsm.py create mode 100644 sleekxmpp/plugins/xep_0059/stanza.py create mode 100644 tests/test_stanza_xep_0059.py create mode 100644 tests/test_stream_xep_0059.py diff --git a/sleekxmpp/plugins/xep_0059/__init__.py b/sleekxmpp/plugins/xep_0059/__init__.py new file mode 100644 index 0000000..3a9b8ed --- /dev/null +++ b/sleekxmpp/plugins/xep_0059/__init__.py @@ -0,0 +1,10 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2010 Nathanael C. Fritz, Erik Reuterborg Larsson + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +from sleekxmpp.plugins.xep_0059.stanza import Set +from sleekxmpp.plugins.xep_0059.rsm import ResultIterator, xep_0059 diff --git a/sleekxmpp/plugins/xep_0059/rsm.py b/sleekxmpp/plugins/xep_0059/rsm.py new file mode 100644 index 0000000..4a01596 --- /dev/null +++ b/sleekxmpp/plugins/xep_0059/rsm.py @@ -0,0 +1,118 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2010 Nathanael C. Fritz, Erik Reuterborg Larsson + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +import logging + +import sleekxmpp +from sleekxmpp import Iq +from sleekxmpp.plugins.base import base_plugin +from sleekxmpp.xmlstream import register_stanza_plugin +from sleekxmpp.plugins.xep_0059 import Set + + +log = logging.getLogger(__name__) + + +class ResultIterator(): + + """ + An iterator for Result Set Managment + """ + + def __init__(self, query, interface, amount=10, start=None, reverse=False): + """ + Arguments: + query -- The template query + interface -- The substanza of the query, for example disco_items + amount -- The max amounts of items to request per iteration + start -- From which item id to start + reverse -- If True, page backwards through the results + + Example: + q = Iq() + q['to'] = 'pubsub.example.com' + q['disco_items']['node'] = 'blog' + for i in ResultIterator(q, 'disco_items', '10'): + print i['disco_items']['items'] + + """ + self.query = query + self.amount = amount + self.start = start + self.interface = interface + self.reverse = reverse + + def __iter__(self): + return self + + def __next__(self): + return self.next() + + def next(self): + """ + Return the next page of results from a query. + + Note: If using backwards paging, then the next page of + results will be the items before the current page + of items. + """ + self.query[self.interface]['rsm']['before'] = self.reverse + self.query['id'] = self.query.stream.new_id() + self.query[self.interface]['rsm']['max'] = str(self.amount) + + if self.start and self.reverse: + self.query[self.interface]['rsm']['before'] = self.start + elif self.start: + self.query[self.interface]['rsm']['after'] = self.start + + r = self.query.send(block=True) + + if not r or not r[self.interface]['rsm']['first'] and \ + not r[self.interface]['rsm']['last']: + raise StopIteration + + if self.reverse: + self.start = r[self.interface]['rsm']['first'] + else: + self.start = r[self.interface]['rsm']['last'] + + return r + + +class xep_0059(base_plugin): + + """ + XEP-0050: Result Set Management + """ + + 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.""" + self.xmpp['xep_0030'].add_feature(Set.namespace) + + def iterate(self, stanza, interface): + """ + Create a new result set iterator for a given stanza query. + + Arguments: + stanza -- A stanza object to serve as a template for + queries made each iteration. For example, a + basic disco#items query. + interface -- The name of the substanza to which the + result set management stanza should be + appended. For example, for disco#items queries + the interface 'disco_items' should be used. + """ + return ResultIterator(stanza, interface) diff --git a/sleekxmpp/plugins/xep_0059/stanza.py b/sleekxmpp/plugins/xep_0059/stanza.py new file mode 100644 index 0000000..7c637d0 --- /dev/null +++ b/sleekxmpp/plugins/xep_0059/stanza.py @@ -0,0 +1,108 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2010 Nathanael C. Fritz, Erik Reuterborg Larsson + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +from sleekxmpp.xmlstream import ElementBase, ET +from sleekxmpp.plugins.xep_0030.stanza.items import DiscoItems + + +class Set(ElementBase): + + """ + XEP-0059 (Result Set Managment) can be used to manage the + results of queries. For example, limiting the number of items + per response or starting at certain positions. + + Example set stanzas: + + + + 2 + + + + + + + + + + conference.example.com + pubsub.example.com + + + + + Stanza Interface: + first_index -- The index attribute of + after -- The id defining from which item to start + before -- The id defining from which item to + start when browsing backwards + max -- Max amount per response + first -- Id for the first item in the response + last -- Id for the last item in the response + index -- Used to set an index to start from + count -- The number of remote items available + + Methods: + set_first_index -- Sets the index attribute for and + creates the element if it doesn't exist + get_first_index -- Returns the value of the index + attribute for + del_first_index -- Removes the index attribute for + but keeps the element + set_before -- Sets the value of , if the value is True + then the element will be created without a value + get_before -- Returns the value of , if it is + empty it will return True + + """ + namespace = 'http://jabber.org/protocol/rsm' + name = 'set' + plugin_attrib = 'rsm' + sub_interfaces = set(('first', 'after', 'before', 'count', + 'index', 'last', 'max')) + interfaces = set(('first_index', 'first', 'after', 'before', + 'count', 'index', 'last', 'max')) + + def set_first_index(self, val): + fi = self.find("{%s}first" % (self.namespace)) + if fi is not None: + if val: + fi.attrib['index'] = val + else: + del fi.attrib['index'] + elif val: + fi = ET.Element("{%s}first" % (self.namespace)) + fi.attrib['index'] = val + self.xml.append(fi) + + def get_first_index(self): + fi = self.find("{%s}first" % (self.namespace)) + if fi is not None: + return fi.attrib.get('index', '') + + def del_first_index(self): + fi = self.xml.find("{%s}first" % (self.namespace)) + if fi is not None: + del fi.attrib['index'] + + def set_before(self, val): + b = self.xml.find("{%s}before" % (self.namespace)) + if b is None and val == True: + self._set_sub_text('{%s}before' % self.namespace, '', True) + else: + self._set_sub_text('{%s}before' % self.namespace, val) + + def get_before(self): + b = self.xml.find("{%s}before" % (self.namespace)) + if b is not None and not b.text: + return True + elif b is not None: + return b.text + else: + return None diff --git a/tests/test_stanza_xep_0059.py b/tests/test_stanza_xep_0059.py new file mode 100644 index 0000000..913436a --- /dev/null +++ b/tests/test_stanza_xep_0059.py @@ -0,0 +1,106 @@ +from sleekxmpp.test import * +from sleekxmpp.plugins.xep_0059 import Set + + +class TestSetStanzas(SleekTest): + + def testSetFirstIndex(self): + s = Set() + s['first'] = 'id' + s.set_first_index('10') + self.check(s, """ + + id + + """) + + def testGetFirstIndex(self): + xml_string = """ + + id + + """ + s = Set(ET.fromstring(xml_string)) + expected = '10' + self.failUnless(s['first_index'] == expected) + + def testDelFirstIndex(self): + xml_string = """ + + id + + """ + s = Set(ET.fromstring(xml_string)) + del s['first_index'] + self.check(s, """ + + id + + """) + + def testSetBefore(self): + s = Set() + s['before'] = True + self.check(s, """ + + + + """) + + def testGetBefore(self): + xml_string = """ + + + + """ + s = Set(ET.fromstring(xml_string)) + expected = True + self.failUnless(s['before'] == expected) + + def testGetBefore(self): + xml_string = """ + + + + """ + s = Set(ET.fromstring(xml_string)) + del s['before'] + self.check(s, """ + + + """) + + def testSetBeforeVal(self): + s = Set() + s['before'] = 'id' + self.check(s, """ + + id + + """) + + def testGetBeforeVal(self): + xml_string = """ + + id + + """ + s = Set(ET.fromstring(xml_string)) + expected = 'id' + self.failUnless(s['before'] == expected) + + def testGetBeforeVal(self): + xml_string = """ + + id + + """ + s = Set(ET.fromstring(xml_string)) + del s['before'] + self.check(s, """ + + + """) + + +suite = unittest.TestLoader().loadTestsFromTestCase(TestSetStanzas) diff --git a/tests/test_stream_xep_0059.py b/tests/test_stream_xep_0059.py new file mode 100644 index 0000000..3a99842 --- /dev/null +++ b/tests/test_stream_xep_0059.py @@ -0,0 +1,162 @@ +import threading + +from sleekxmpp.test import * +from sleekxmpp.xmlstream import register_stanza_plugin +from sleekxmpp.plugins.xep_0030 import DiscoItems +from sleekxmpp.plugins.xep_0059 import ResultIterator, Set + + +class TestStreamSet(SleekTest): + + def setUp(self): + register_stanza_plugin(DiscoItems, Set) + + def tearDown(self): + self.stream_close() + + def iter(self, rev=False): + q = self.xmpp.Iq() + q['type'] = 'get' + it = ResultIterator(q, 'disco_items', '1', reverse=rev) + for i in it: + for j in i['disco_items']['items']: + self.items.append(j[0]) + + def testResultIterator(self): + self.items = [] + self.stream_start(mode='client') + t = threading.Thread(target=self.iter) + t.start() + self.send(""" + + + + 1 + + + + """) + self.recv(""" + + + + + item1 + + + + """) + self.send(""" + + + + 1 + item1 + + + + """) + self.recv(""" + + + + + item2 + + + + """) + self.send(""" + + + + 1 + item2 + + + + """) + self.recv(""" + + + + + + + + """) + t.join() + self.failUnless(self.items == ['item1', 'item2']) + + def testResultIteratorReverse(self): + self.items = [] + self.stream_start(mode='client') + + t = threading.Thread(target=self.iter, args=(True,)) + t.start() + + self.send(""" + + + + 1 + + + + + """) + self.recv(""" + + + + + item2 + + + + """) + self.send(""" + + + + 1 + item2 + + + + """) + self.recv(""" + + + + + item1 + + + + """) + self.send(""" + + + + 1 + item1 + + + + """) + self.recv(""" + + + + + + + + """) + + t.join() + self.failUnless(self.items == ['item2', 'item1']) + + +suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamSet) From 7c7fa0f00826f304dab12faad297ef339a7de2a0 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sat, 8 Jan 2011 11:19:31 -0500 Subject: [PATCH 4/7] Add support for XEP-0059 to XEP-0030 plugin. --- sleekxmpp/plugins/xep_0030/disco.py | 17 ++++++++-- tests/test_stream_xep_0030.py | 48 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/sleekxmpp/plugins/xep_0030/disco.py b/sleekxmpp/plugins/xep_0030/disco.py index 4958c93..b4f5e79 100644 --- a/sleekxmpp/plugins/xep_0030/disco.py +++ b/sleekxmpp/plugins/xep_0030/disco.py @@ -120,6 +120,11 @@ class xep_0030(base_plugin): 'jid': {}, 'node': {}} + def post_init(self): + """Handle cross-plugin dependencies.""" + if self.xmpp['xep_0059']: + register_stanza_plugin(DiscoItems, self.xmpp['xep_0059'].stanza.Set) + def set_node_handler(self, htype, jid=None, node=None, handler=None): """ Add a node handler for the given hierarchy level and @@ -292,6 +297,9 @@ class xep_0030(base_plugin): callback -- Optional callback to execute when a reply is received instead of blocking and waiting for the reply. + iterator -- If True, return a result set iterator using + the XEP-0059 plugin, if the plugin is loaded. + Otherwise the parameter is ignored. """ if local or jid is None: return self._run_node_handler('get_items', jid, node, kwargs) @@ -302,9 +310,12 @@ class xep_0030(base_plugin): iq['to'] = jid iq['type'] = 'get' iq['disco_items']['node'] = node if node else '' - return iq.send(timeout=kwargs.get('timeout', None), - block=kwargs.get('block', None), - callback=kwargs.get('callback', None)) + 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), + block=kwargs.get('block', None), + callback=kwargs.get('callback', None)) def set_items(self, jid=None, node=None, **kwargs): """ diff --git a/tests/test_stream_xep_0030.py b/tests/test_stream_xep_0030.py index 25a4102..c960fc7 100644 --- a/tests/test_stream_xep_0030.py +++ b/tests/test_stream_xep_0030.py @@ -1,3 +1,4 @@ +import sys import time import threading @@ -11,6 +12,7 @@ class TestStreamDisco(SleekTest): """ def tearDown(self): + sys.excepthook = sys.__excepthook__ self.stream_close() def testInfoEmptyDefaultNode(self): @@ -524,5 +526,51 @@ class TestStreamDisco(SleekTest): self.assertEqual(results, items, "Unexpected items: %s" % results) + def testGetItemsIterator(self): + """Test interaction between XEP-0030 and XEP-0059 plugins.""" + + raised_exceptions = [] + + def catch_exception(*args, **kwargs): + raised_exceptions.append(True) + + sys.excepthook = catch_exception + + self.stream_start(mode='client', + plugins=['xep_0030', 'xep_0059']) + + results = self.xmpp['xep_0030'].get_items(jid='foo@localhost', + node='bar', + iterator=True) + results.amount = 10 + + t = threading.Thread(name="get_items_iterator", + target=results.next) + t.start() + + self.send(""" + + + + 10 + + + + """) + self.recv(""" + + + + + + + """) + + t.join() + + self.assertEqual(raised_exceptions, [True], + "StopIteration was not raised: %s" % raised_exceptions) + suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamDisco) From 68ce47c9055f3ef485fe6810797032e7a5d8c370 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 9 Jan 2011 09:52:54 +0800 Subject: [PATCH 5/7] Allow XEP 0092 to send os information Doesn't send these information by default, only if provided in the config dict (as the 'os' key) --- sleekxmpp/plugins/xep_0092.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sleekxmpp/plugins/xep_0092.py b/sleekxmpp/plugins/xep_0092.py index c9b418f..8b5f868 100644 --- a/sleekxmpp/plugins/xep_0092.py +++ b/sleekxmpp/plugins/xep_0092.py @@ -18,6 +18,7 @@ class xep_0092(base.base_plugin): self.xep = "0092" self.name = self.config.get('name', 'SleekXMPP') self.version = self.config.get('version', '0.1-dev') + self.os = self.config.get('os', '') self.xmpp.add_handler("" % self.xmpp.default_ns, self.report_version, name='Sofware Version') def post_init(self): @@ -32,6 +33,10 @@ class xep_0092(base.base_plugin): name.text = self.name version = ET.Element('version') version.text = self.version + if self.os: + os = ET.Element('os') + os.text = self.os + query.append(os) query.append(name) query.append(version) iq.append(query) From 2076d506b469b932fc330743f2e56e76f52a045b Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sat, 8 Jan 2011 22:38:13 -0500 Subject: [PATCH 6/7] Update the XEP-0092 plugin to the new style. --- sleekxmpp/plugins/xep_0092.py | 61 ------------------ sleekxmpp/plugins/xep_0092/__init__.py | 11 ++++ sleekxmpp/plugins/xep_0092/stanza.py | 42 ++++++++++++ sleekxmpp/plugins/xep_0092/version.py | 88 ++++++++++++++++++++++++++ tests/test_stream_xep_0092.py | 69 ++++++++++++++++++++ 5 files changed, 210 insertions(+), 61 deletions(-) delete mode 100644 sleekxmpp/plugins/xep_0092.py create mode 100644 sleekxmpp/plugins/xep_0092/__init__.py create mode 100644 sleekxmpp/plugins/xep_0092/stanza.py create mode 100644 sleekxmpp/plugins/xep_0092/version.py create mode 100644 tests/test_stream_xep_0092.py diff --git a/sleekxmpp/plugins/xep_0092.py b/sleekxmpp/plugins/xep_0092.py deleted file mode 100644 index 8b5f868..0000000 --- a/sleekxmpp/plugins/xep_0092.py +++ /dev/null @@ -1,61 +0,0 @@ -""" - SleekXMPP: The Sleek XMPP Library - Copyright (C) 2010 Nathanael C. Fritz - This file is part of SleekXMPP. - - See the file LICENSE for copying permission. -""" -from xml.etree import cElementTree as ET -from . import base -from .. xmlstream.handler.xmlwaiter import XMLWaiter - -class xep_0092(base.base_plugin): - """ - XEP-0092 Software Version - """ - def plugin_init(self): - self.description = "Software Version" - self.xep = "0092" - self.name = self.config.get('name', 'SleekXMPP') - self.version = self.config.get('version', '0.1-dev') - self.os = self.config.get('os', '') - self.xmpp.add_handler("" % self.xmpp.default_ns, self.report_version, name='Sofware Version') - - def post_init(self): - base.base_plugin.post_init(self) - self.xmpp.plugin['xep_0030'].add_feature('jabber:iq:version') - - def report_version(self, xml): - iq = self.xmpp.makeIqResult(xml.get('id', 'unknown')) - iq.attrib['to'] = xml.get('from', self.xmpp.server) - query = ET.Element('{jabber:iq:version}query') - name = ET.Element('name') - name.text = self.name - version = ET.Element('version') - version.text = self.version - if self.os: - os = ET.Element('os') - os.text = self.os - query.append(os) - query.append(name) - query.append(version) - iq.append(query) - self.xmpp.send(iq) - - def getVersion(self, jid): - iq = self.xmpp.makeIqGet() - query = ET.Element('{jabber:iq:version}query') - iq.append(query) - iq.attrib['to'] = jid - iq.attrib['from'] = self.xmpp.boundjid.full - id = iq.get('id') - result = iq.send() - if result and result is not None and result.get('type', 'error') != 'error': - qry = result.find('{jabber:iq:version}query') - version = {} - for child in qry.getchildren(): - version[child.tag.split('}')[-1]] = child.text - return version - else: - return False - diff --git a/sleekxmpp/plugins/xep_0092/__init__.py b/sleekxmpp/plugins/xep_0092/__init__.py new file mode 100644 index 0000000..7c5bdb7 --- /dev/null +++ b/sleekxmpp/plugins/xep_0092/__init__.py @@ -0,0 +1,11 @@ +""" + 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. +""" + +from sleekxmpp.plugins.xep_0092 import stanza +from sleekxmpp.plugins.xep_0092.stanza import Version +from sleekxmpp.plugins.xep_0092.version import xep_0092 diff --git a/sleekxmpp/plugins/xep_0092/stanza.py b/sleekxmpp/plugins/xep_0092/stanza.py new file mode 100644 index 0000000..77654e3 --- /dev/null +++ b/sleekxmpp/plugins/xep_0092/stanza.py @@ -0,0 +1,42 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2010 Nathanael C. Fritz + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +from sleekxmpp.xmlstream import ElementBase, ET + + +class Version(ElementBase): + + """ + XMPP allows for an agent to advertise the name and version of the + underlying software libraries, as well as the operating system + that the agent is running on. + + Example version stanzas: + + + + + + + SleekXMPP + 1.0 + Linux + + + + Stanza Interface: + name -- The human readable name of the software. + version -- The specific version of the software. + os -- The name of the operating system running the program. + """ + + name = 'query' + namespace = 'jabber:iq:version' + plugin_attrib = 'software_version' + interfaces = set(('name', 'version', 'os')) + sub_interfaces = interfaces diff --git a/sleekxmpp/plugins/xep_0092/version.py b/sleekxmpp/plugins/xep_0092/version.py new file mode 100644 index 0000000..f59f881 --- /dev/null +++ b/sleekxmpp/plugins/xep_0092/version.py @@ -0,0 +1,88 @@ +""" + SleekXMPP: The Sleek XMPP Library + Copyright (C) 2010 Nathanael C. Fritz + This file is part of SleekXMPP. + + See the file LICENSE for copying permission. +""" + +import logging + +import sleekxmpp +from sleekxmpp import Iq +from sleekxmpp.xmlstream import register_stanza_plugin +from sleekxmpp.xmlstream.handler import Callback +from sleekxmpp.xmlstream.matcher import StanzaPath +from sleekxmpp.plugins.base import base_plugin +from sleekxmpp.plugins.xep_0092 import Version + + +log = logging.getLogger(__name__) + + +class xep_0092(base_plugin): + + """ + XEP-0092: Software Version + """ + + def plugin_init(self): + """ + Start the XEP-0092 plugin. + """ + self.xep = "0092" + self.description = "Software Version" + self.stanza = sleekxmpp.plugins.xep_0092.stanza + + self.name = self.config.get('name', 'SleekXMPP') + self.version = self.config.get('version', '0.1-dev') + self.os = self.config.get('os', '') + + self.getVersion = self.get_version + + self.xmpp.register_handler( + Callback('Software Version', + StanzaPath('iq/software_version'), + self._handle_version)) + + register_stanza_plugin(Iq, Version) + + def post_init(self): + """ + Handle cross-plugin dependencies. + """ + base_plugin.post_init(self) + self.xmpp.plugin['xep_0030'].add_feature('jabber:iq:version') + + def _handle_version(self, iq): + """ + Respond to a software version query. + + Arguments: + iq -- The Iq stanza containing the software version query. + """ + iq.reply() + iq['software_version']['name'] = self.name + iq['software_version']['version'] = self.version + iq['software_version']['os'] = self.os + iq.send() + + def get_version(self, jid, ifrom=None): + """ + Retrieve the software version of a remote agent. + + Arguments: + jid -- The JID of the entity to query. + """ + iq = self.xmpp.Iq() + iq['to'] = jid + if ifrom: + iq['from'] = ifrom + iq['type'] = 'get' + iq['query'] = Version.namespace + + result = iq.send() + + if result and result['type'] != 'error': + return result['software_version']._get_stanza_values() + return False diff --git a/tests/test_stream_xep_0092.py b/tests/test_stream_xep_0092.py new file mode 100644 index 0000000..4a03855 --- /dev/null +++ b/tests/test_stream_xep_0092.py @@ -0,0 +1,69 @@ +import threading + +from sleekxmpp.test import * + + +class TestStreamSet(SleekTest): + + def tearDown(self): + self.stream_close() + + def testHandleSoftwareVersionRequest(self): + self.stream_start(mode='client', plugins=['xep_0030', 'xep_0092']) + + self.xmpp['xep_0092'].name = 'SleekXMPP' + self.xmpp['xep_0092'].version = 'dev' + self.xmpp['xep_0092'].os = 'Linux' + + self.recv(""" + + + + """) + + self.send(""" + + + SleekXMPP + dev + Linux + + + """) + + def testMakeSoftwareVersionRequest(self): + results = [] + + def query(): + r = self.xmpp['xep_0092'].get_version('foo@bar') + results.append(r) + + self.stream_start(mode='client', plugins=['xep_0030', 'xep_0092']) + + t = threading.Thread(target=query) + t.start() + + self.send(""" + + + + """) + + self.recv(""" + + + Foo + 1.0 + Linux + + + """) + + t.join() + + expected = [{'name': 'Foo', 'version': '1.0', 'os':'Linux'}] + self.assertEqual(results, expected, + "Did not receive expected results: %s" % results) + + +suite = unittest.TestLoader().loadTestsFromTestCase(TestStreamSet) From acdf9e2d22dd604578f50d0a9b67c47e001da69f Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Sun, 9 Jan 2011 10:03:32 -0500 Subject: [PATCH 7/7] Need to run post_init properly. --- sleekxmpp/plugins/xep_0030/disco.py | 1 + sleekxmpp/plugins/xep_0059/rsm.py | 1 + 2 files changed, 2 insertions(+) diff --git a/sleekxmpp/plugins/xep_0030/disco.py b/sleekxmpp/plugins/xep_0030/disco.py index b4f5e79..6fd4e85 100644 --- a/sleekxmpp/plugins/xep_0030/disco.py +++ b/sleekxmpp/plugins/xep_0030/disco.py @@ -122,6 +122,7 @@ class xep_0030(base_plugin): def post_init(self): """Handle cross-plugin dependencies.""" + base_plugin.post_init(self) if self.xmpp['xep_0059']: register_stanza_plugin(DiscoItems, self.xmpp['xep_0059'].stanza.Set) diff --git a/sleekxmpp/plugins/xep_0059/rsm.py b/sleekxmpp/plugins/xep_0059/rsm.py index 4a01596..3590847 100644 --- a/sleekxmpp/plugins/xep_0059/rsm.py +++ b/sleekxmpp/plugins/xep_0059/rsm.py @@ -100,6 +100,7 @@ class xep_0059(base_plugin): def post_init(self): """Handle inter-plugin dependencies.""" + base_plugin.post_init(self) self.xmpp['xep_0030'].add_feature(Set.namespace) def iterate(self, stanza, interface):