mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-23 19:19:53 +00:00
Update the XEP-0092 plugin to the new style.
This commit is contained in:
parent
68ce47c905
commit
2076d506b4
5 changed files with 210 additions and 61 deletions
|
@ -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("<iq type='get' xmlns='%s'><query xmlns='jabber:iq:version' /></iq>" % 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
|
||||
|
11
sleekxmpp/plugins/xep_0092/__init__.py
Normal file
11
sleekxmpp/plugins/xep_0092/__init__.py
Normal file
|
@ -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
|
42
sleekxmpp/plugins/xep_0092/stanza.py
Normal file
42
sleekxmpp/plugins/xep_0092/stanza.py
Normal file
|
@ -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:
|
||||
<iq type="get">
|
||||
<query xmlns="jabber:iq:version" />
|
||||
</iq>
|
||||
|
||||
<iq type="result">
|
||||
<query xmlns="jabber:iq:version">
|
||||
<name>SleekXMPP</name>
|
||||
<version>1.0</version>
|
||||
<os>Linux</os>
|
||||
</query>
|
||||
</iq>
|
||||
|
||||
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
|
88
sleekxmpp/plugins/xep_0092/version.py
Normal file
88
sleekxmpp/plugins/xep_0092/version.py
Normal file
|
@ -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
|
69
tests/test_stream_xep_0092.py
Normal file
69
tests/test_stream_xep_0092.py
Normal file
|
@ -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("""
|
||||
<iq type="get" id="1">
|
||||
<query xmlns="jabber:iq:version" />
|
||||
</iq>
|
||||
""")
|
||||
|
||||
self.send("""
|
||||
<iq type="result" id="1">
|
||||
<query xmlns="jabber:iq:version">
|
||||
<name>SleekXMPP</name>
|
||||
<version>dev</version>
|
||||
<os>Linux</os>
|
||||
</query>
|
||||
</iq>
|
||||
""")
|
||||
|
||||
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("""
|
||||
<iq type="get" id="1" to="foo@bar">
|
||||
<query xmlns="jabber:iq:version" />
|
||||
</iq>
|
||||
""")
|
||||
|
||||
self.recv("""
|
||||
<iq type="result" id="1" from="foo@bar" to="tester@localhost">
|
||||
<query xmlns="jabber:iq:version">
|
||||
<name>Foo</name>
|
||||
<version>1.0</version>
|
||||
<os>Linux</os>
|
||||
</query>
|
||||
</iq>
|
||||
""")
|
||||
|
||||
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)
|
Loading…
Reference in a new issue