2012-02-02 17:27:23 +00:00
|
|
|
import uuid
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
|
|
|
|
from sleekxmpp import Message, Iq
|
2012-03-12 04:15:01 +00:00
|
|
|
from sleekxmpp.exceptions import XMPPError
|
2012-02-02 17:27:23 +00:00
|
|
|
from sleekxmpp.xmlstream.handler import Callback
|
|
|
|
from sleekxmpp.xmlstream.matcher import StanzaPath
|
|
|
|
from sleekxmpp.xmlstream import register_stanza_plugin
|
2012-03-12 04:15:01 +00:00
|
|
|
from sleekxmpp.plugins import BasePlugin
|
2012-02-02 17:27:23 +00:00
|
|
|
from sleekxmpp.plugins.xep_0047 import stanza, Open, Close, Data, IBBytestream
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2012-03-12 04:15:01 +00:00
|
|
|
class XEP_0047(BasePlugin):
|
2012-02-02 17:27:23 +00:00
|
|
|
|
2012-03-12 04:15:01 +00:00
|
|
|
name = 'xep_0047'
|
|
|
|
description = 'XEP-0047: In-band Bytestreams'
|
|
|
|
dependencies = set(['xep_0030'])
|
|
|
|
stanza = stanza
|
2012-07-27 06:04:16 +00:00
|
|
|
default_config = {
|
|
|
|
'max_block_size': 8192,
|
|
|
|
'window_size': 1,
|
|
|
|
'auto_accept': True,
|
|
|
|
'accept_stream': None
|
|
|
|
}
|
2012-02-02 17:27:23 +00:00
|
|
|
|
2012-03-12 04:15:01 +00:00
|
|
|
def plugin_init(self):
|
2012-02-02 17:27:23 +00:00
|
|
|
self.streams = {}
|
2012-07-27 06:04:16 +00:00
|
|
|
self.pending_streams = {}
|
2012-02-02 17:27:23 +00:00
|
|
|
self.pending_close_streams = {}
|
|
|
|
self._stream_lock = threading.Lock()
|
|
|
|
|
|
|
|
register_stanza_plugin(Iq, Open)
|
|
|
|
register_stanza_plugin(Iq, Close)
|
|
|
|
register_stanza_plugin(Iq, Data)
|
2013-02-12 02:08:34 +00:00
|
|
|
register_stanza_plugin(Message, Data)
|
2012-02-02 17:27:23 +00:00
|
|
|
|
|
|
|
self.xmpp.register_handler(Callback(
|
|
|
|
'IBB Open',
|
|
|
|
StanzaPath('iq@type=set/ibb_open'),
|
|
|
|
self._handle_open_request))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(Callback(
|
|
|
|
'IBB Close',
|
|
|
|
StanzaPath('iq@type=set/ibb_close'),
|
|
|
|
self._handle_close))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(Callback(
|
|
|
|
'IBB Data',
|
|
|
|
StanzaPath('iq@type=set/ibb_data'),
|
|
|
|
self._handle_data))
|
|
|
|
|
2013-02-12 02:08:34 +00:00
|
|
|
self.xmpp.register_handler(Callback(
|
|
|
|
'IBB Message Data',
|
|
|
|
StanzaPath('message/ibb_data'),
|
|
|
|
self._handle_data))
|
|
|
|
|
2012-07-10 08:37:44 +00:00
|
|
|
def plugin_end(self):
|
|
|
|
self.xmpp.remove_handler('IBB Open')
|
|
|
|
self.xmpp.remove_handler('IBB Close')
|
|
|
|
self.xmpp.remove_handler('IBB Data')
|
2013-02-12 02:08:34 +00:00
|
|
|
self.xmpp.remove_handler('IBB Message Data')
|
2012-07-10 08:37:44 +00:00
|
|
|
self.xmpp['xep_0030'].del_feature(feature='http://jabber.org/protocol/ibb')
|
|
|
|
|
|
|
|
def session_bind(self, jid):
|
2012-02-02 17:27:23 +00:00
|
|
|
self.xmpp['xep_0030'].add_feature('http://jabber.org/protocol/ibb')
|
|
|
|
|
|
|
|
def _accept_stream(self, iq):
|
|
|
|
if self.accept_stream is not None:
|
|
|
|
return self.accept_stream(iq)
|
|
|
|
if self.auto_accept:
|
|
|
|
if iq['ibb_open']['block_size'] <= self.max_block_size:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2013-02-12 02:08:34 +00:00
|
|
|
def open_stream(self, jid, block_size=4096, sid=None, window=1, use_messages=False,
|
2012-02-02 17:27:23 +00:00
|
|
|
ifrom=None, block=True, timeout=None, callback=None):
|
|
|
|
if sid is None:
|
|
|
|
sid = str(uuid.uuid4())
|
|
|
|
|
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['type'] = 'set'
|
|
|
|
iq['to'] = jid
|
|
|
|
iq['from'] = ifrom
|
|
|
|
iq['ibb_open']['block_size'] = block_size
|
|
|
|
iq['ibb_open']['sid'] = sid
|
|
|
|
iq['ibb_open']['stanza'] = 'iq'
|
|
|
|
|
2012-02-03 15:03:46 +00:00
|
|
|
stream = IBBytestream(self.xmpp, sid, block_size,
|
2013-02-12 02:08:34 +00:00
|
|
|
iq['to'], iq['from'], window,
|
|
|
|
use_messages)
|
2012-02-03 15:03:46 +00:00
|
|
|
|
2012-02-02 17:27:23 +00:00
|
|
|
with self._stream_lock:
|
|
|
|
self.pending_streams[iq['id']] = stream
|
2012-02-03 15:03:46 +00:00
|
|
|
|
|
|
|
self.pending_streams[iq['id']] = stream
|
|
|
|
|
2012-02-02 17:27:23 +00:00
|
|
|
if block:
|
2012-02-03 15:03:46 +00:00
|
|
|
resp = iq.send(timeout=timeout)
|
2012-02-02 17:27:23 +00:00
|
|
|
self._handle_opened_stream(resp)
|
|
|
|
return stream
|
2012-02-03 15:03:46 +00:00
|
|
|
else:
|
|
|
|
cb = None
|
|
|
|
if callback is not None:
|
|
|
|
def chained(resp):
|
|
|
|
self._handle_opened_stream(resp)
|
|
|
|
callback(resp)
|
|
|
|
cb = chained
|
|
|
|
else:
|
|
|
|
cb = self._handle_opened_stream
|
|
|
|
return iq.send(block=block, timeout=timeout, callback=cb)
|
|
|
|
|
|
|
|
def _handle_opened_stream(self, iq):
|
2012-02-02 17:27:23 +00:00
|
|
|
if iq['type'] == 'result':
|
|
|
|
with self._stream_lock:
|
2012-02-03 15:03:46 +00:00
|
|
|
stream = self.pending_streams.get(iq['id'], None)
|
|
|
|
if stream is not None:
|
|
|
|
stream.sender = iq['to']
|
|
|
|
stream.receiver = iq['from']
|
|
|
|
stream.stream_started.set()
|
|
|
|
self.streams[stream.sid] = stream
|
|
|
|
self.xmpp.event('ibb_stream_start', stream)
|
2012-02-02 17:27:23 +00:00
|
|
|
|
|
|
|
with self._stream_lock:
|
|
|
|
if iq['id'] in self.pending_streams:
|
|
|
|
del self.pending_streams[iq['id']]
|
|
|
|
|
|
|
|
def _handle_open_request(self, iq):
|
|
|
|
sid = iq['ibb_open']['sid']
|
|
|
|
size = iq['ibb_open']['block_size']
|
|
|
|
if not self._accept_stream(iq):
|
|
|
|
raise XMPPError('not-acceptable')
|
|
|
|
|
|
|
|
if size > self.max_block_size:
|
|
|
|
raise XMPPError('resource-constraint')
|
|
|
|
|
2012-02-03 15:03:46 +00:00
|
|
|
stream = IBBytestream(self.xmpp, sid, size,
|
2012-02-02 17:27:23 +00:00
|
|
|
iq['from'], iq['to'],
|
|
|
|
self.window_size)
|
|
|
|
stream.stream_started.set()
|
|
|
|
self.streams[sid] = stream
|
|
|
|
iq.reply()
|
|
|
|
iq.send()
|
|
|
|
|
|
|
|
self.xmpp.event('ibb_stream_start', stream)
|
|
|
|
|
2013-02-12 02:08:34 +00:00
|
|
|
def _handle_data(self, stanza):
|
|
|
|
sid = stanza['ibb_data']['sid']
|
2012-02-02 17:27:23 +00:00
|
|
|
stream = self.streams.get(sid, None)
|
2013-02-12 02:08:34 +00:00
|
|
|
if stream is not None and stanza['from'] != stream.sender:
|
|
|
|
stream._recv_data(stanza)
|
2012-02-02 17:27:23 +00:00
|
|
|
else:
|
|
|
|
raise XMPPError('item-not-found')
|
|
|
|
|
|
|
|
def _handle_close(self, iq):
|
|
|
|
sid = iq['ibb_close']['sid']
|
|
|
|
stream = self.streams.get(sid, None)
|
|
|
|
if stream is not None and iq['from'] != stream.sender:
|
|
|
|
stream._closed(iq)
|
|
|
|
else:
|
|
|
|
raise XMPPError('item-not-found')
|