mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 19:19:54 +00:00
Merge branch 'develop' into stream_features
This commit is contained in:
commit
9a6eb333e6
4 changed files with 205 additions and 0 deletions
11
sleekxmpp/plugins/xep_0066/__init__.py
Normal file
11
sleekxmpp/plugins/xep_0066/__init__.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
"""
|
||||||
|
SleekXMPP: The Sleek XMPP Library
|
||||||
|
Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout
|
||||||
|
This file is part of SleekXMPP.
|
||||||
|
|
||||||
|
See the file LICENSE for copying permission.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from sleekxmpp.plugins.xep_0066 import stanza
|
||||||
|
from sleekxmpp.plugins.xep_0066.stanza import OOB, OOBTransfer
|
||||||
|
from sleekxmpp.plugins.xep_0066.oob import xep_0066
|
89
sleekxmpp/plugins/xep_0066/oob.py
Normal file
89
sleekxmpp/plugins/xep_0066/oob.py
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
"""
|
||||||
|
SleekXMPP: The Sleek XMPP Library
|
||||||
|
Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout
|
||||||
|
This file is part of SleekXMPP.
|
||||||
|
|
||||||
|
See the file LICENSE for copying permission.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
from sleekxmpp.stanza import Message, Presence, 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_0066 import stanza
|
||||||
|
|
||||||
|
|
||||||
|
class xep_0066(base_plugin):
|
||||||
|
|
||||||
|
"""
|
||||||
|
XEP-0066: Out-of-Band Data
|
||||||
|
|
||||||
|
Out-of-Band Data is a basic method for transferring files between
|
||||||
|
XMPP agents. The URL of the resource in question is sent to the receiving
|
||||||
|
entity, which then downloads the resource before responding to the OOB
|
||||||
|
request. OOB is also used as a generic means to transmit URLs in other
|
||||||
|
stanzas to indicate where to find additional information.
|
||||||
|
|
||||||
|
Also see <http://www.xmpp.org/extensions/xep-0066.html>.
|
||||||
|
|
||||||
|
Events:
|
||||||
|
oob_transfer -- Raised when a request to download a resource
|
||||||
|
has been received.
|
||||||
|
|
||||||
|
Methods:
|
||||||
|
send_oob -- Send a request to another entity to download a file
|
||||||
|
or other addressable resource.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def plugin_init(self):
|
||||||
|
"""Start the XEP-0066 plugin."""
|
||||||
|
self.xep = '0066'
|
||||||
|
self.description = 'Out-of-Band Transfer'
|
||||||
|
self.stanza = stanza
|
||||||
|
|
||||||
|
register_stanza_plugin(Iq, stanza.OOBTransfer)
|
||||||
|
register_stanza_plugin(Message, stanza.OOB)
|
||||||
|
register_stanza_plugin(Presence, stanza.OOB)
|
||||||
|
|
||||||
|
self.xmpp.register_handler(
|
||||||
|
Callback('OOB Transfer',
|
||||||
|
StanzaPath('iq@type=set/oob_transfer'),
|
||||||
|
self._handle_transfer))
|
||||||
|
|
||||||
|
def post_init(self):
|
||||||
|
"""Handle cross-plugin dependencies."""
|
||||||
|
base_plugin.post_init(self)
|
||||||
|
self.xmpp['xep_0030'].add_feature(stanza.OOBTransfer.namespace)
|
||||||
|
self.xmpp['xep_0030'].add_feature(stanza.OOB.namespace)
|
||||||
|
|
||||||
|
def send_oob(self, to, url, desc=None, ifrom=None, **iqargs):
|
||||||
|
"""
|
||||||
|
Initiate a basic file transfer by sending the URL of
|
||||||
|
a file or other resource.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
url -- The URL of the resource to transfer.
|
||||||
|
desc -- An optional human readable description of the item
|
||||||
|
that is to be transferred.
|
||||||
|
ifrom -- Specifiy the sender's JID.
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
iq = self.xmpp.Iq()
|
||||||
|
iq['type'] = 'set'
|
||||||
|
iq['to'] = to
|
||||||
|
if ifrom:
|
||||||
|
iq['from'] = ifrom
|
||||||
|
iq['oob_transfer']['url'] = url
|
||||||
|
iq['oob_transfer']['desc'] = desc
|
||||||
|
return iq.send(**iqargs)
|
||||||
|
|
||||||
|
def _handle_transfer(self, iq):
|
||||||
|
"""Handle receiving an out-of-band transfer request."""
|
||||||
|
self.xmpp.event('oob_transfer', iq)
|
33
sleekxmpp/plugins/xep_0066/stanza.py
Normal file
33
sleekxmpp/plugins/xep_0066/stanza.py
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
"""
|
||||||
|
SleekXMPP: The Sleek XMPP Library
|
||||||
|
Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout
|
||||||
|
This file is part of SleekXMPP.
|
||||||
|
|
||||||
|
See the file LICENSE for copying permission.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from sleekxmpp.xmlstream import ElementBase
|
||||||
|
|
||||||
|
|
||||||
|
class OOBTransfer(ElementBase):
|
||||||
|
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
|
||||||
|
name = 'query'
|
||||||
|
namespace = 'jabber:iq:oob'
|
||||||
|
plugin_attrib = 'oob_transfer'
|
||||||
|
interfaces = set(('url', 'desc', 'sid'))
|
||||||
|
sub_interfaces = set(('url', 'desc'))
|
||||||
|
|
||||||
|
|
||||||
|
class OOB(ElementBase):
|
||||||
|
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
|
||||||
|
name = 'x'
|
||||||
|
namespace = 'jabber:x:oob'
|
||||||
|
plugin_attrib = 'oob'
|
||||||
|
interfaces = set(('url', 'desc'))
|
||||||
|
sub_interfaces = interfaces
|
72
tests/test_stream_xep_0066.py
Normal file
72
tests/test_stream_xep_0066.py
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
|
||||||
|
from sleekxmpp.test import *
|
||||||
|
|
||||||
|
|
||||||
|
class TestOOB(SleekTest):
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.stream_close()
|
||||||
|
|
||||||
|
def testSendOOB(self):
|
||||||
|
"""Test sending an OOB transfer request."""
|
||||||
|
self.stream_start(plugins=['xep_0066', 'xep_0030'])
|
||||||
|
|
||||||
|
url = 'http://github.com/fritzy/SleekXMPP/blob/master/README'
|
||||||
|
|
||||||
|
t = threading.Thread(
|
||||||
|
name='send_oob',
|
||||||
|
target=self.xmpp['xep_0066'].send_oob,
|
||||||
|
args=('user@example.com', url),
|
||||||
|
kwargs={'desc': 'SleekXMPP README'})
|
||||||
|
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
self.send("""
|
||||||
|
<iq to="user@example.com" type="set" id="1">
|
||||||
|
<query xmlns="jabber:iq:oob">
|
||||||
|
<url>http://github.com/fritzy/SleekXMPP/blob/master/README</url>
|
||||||
|
<desc>SleekXMPP README</desc>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
|
||||||
|
self.recv("""
|
||||||
|
<iq id="1" type="result"
|
||||||
|
to="tester@localhost"
|
||||||
|
from="user@example.com" />
|
||||||
|
""")
|
||||||
|
|
||||||
|
t.join()
|
||||||
|
|
||||||
|
def testReceiveOOB(self):
|
||||||
|
"""Test receiving an OOB request."""
|
||||||
|
self.stream_start(plugins=['xep_0066', 'xep_0030'])
|
||||||
|
|
||||||
|
events = []
|
||||||
|
|
||||||
|
def receive_oob(iq):
|
||||||
|
events.append(iq['oob_transfer']['url'])
|
||||||
|
|
||||||
|
self.xmpp.add_event_handler('oob_transfer', receive_oob)
|
||||||
|
|
||||||
|
self.recv("""
|
||||||
|
<iq to="tester@localhost"
|
||||||
|
from="user@example.com"
|
||||||
|
type="set" id="1">
|
||||||
|
<query xmlns="jabber:iq:oob">
|
||||||
|
<url>http://github.com/fritzy/SleekXMPP/blob/master/README</url>
|
||||||
|
<desc>SleekXMPP README</desc>
|
||||||
|
</query>
|
||||||
|
</iq>
|
||||||
|
""")
|
||||||
|
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
self.assertEqual(events,
|
||||||
|
['http://github.com/fritzy/SleekXMPP/blob/master/README'],
|
||||||
|
'URL was not received: %s' % events)
|
||||||
|
|
||||||
|
|
||||||
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestOOB)
|
Loading…
Reference in a new issue