slixmpp/sleekxmpp/plugins/xep_0047/ibb.py

216 lines
7.6 KiB
Python
Raw Normal View History

import uuid
import logging
import threading
from sleekxmpp import Message, Iq
2012-03-12 04:15:01 +00:00
from sleekxmpp.exceptions import XMPPError
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
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-03-12 04:15:01 +00:00
name = 'xep_0047'
description = 'XEP-0047: In-band Bytestreams'
dependencies = set(['xep_0030'])
stanza = stanza
default_config = {
2013-02-19 09:00:04 +00:00
'block_size': 4096,
'max_block_size': 8192,
'window_size': 1,
2013-02-19 09:00:04 +00:00
'auto_accept': False,
}
2012-03-12 04:15:01 +00:00
def plugin_init(self):
2013-03-29 20:14:22 +00:00
self._streams = {}
self._pending_streams = {}
self._pending_lock = threading.Lock()
self._stream_lock = threading.Lock()
2013-02-19 09:00:04 +00:00
self._preauthed_sids_lock = threading.Lock()
self._preauthed_sids = {}
register_stanza_plugin(Iq, Open)
register_stanza_plugin(Iq, Close)
register_stanza_plugin(Iq, Data)
register_stanza_plugin(Message, Data)
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))
self.xmpp.register_handler(Callback(
'IBB Message Data',
StanzaPath('message/ibb_data'),
self._handle_data))
2013-02-19 09:00:04 +00:00
self.api.register(self._authorized, 'authorized', default=True)
self.api.register(self._authorized_sid, 'authorized_sid', default=True)
self.api.register(self._preauthorize_sid, 'preauthorize_sid', default=True)
2013-03-29 20:14:22 +00:00
self.api.register(self._get_stream, 'get_stream', default=True)
self.api.register(self._set_stream, 'set_stream', default=True)
self.api.register(self._del_stream, 'del_stream', default=True)
2013-02-19 09:00:04 +00:00
def plugin_end(self):
self.xmpp.remove_handler('IBB Open')
self.xmpp.remove_handler('IBB Close')
self.xmpp.remove_handler('IBB Data')
self.xmpp.remove_handler('IBB Message Data')
self.xmpp['xep_0030'].del_feature(feature='http://jabber.org/protocol/ibb')
def session_bind(self, jid):
self.xmpp['xep_0030'].add_feature('http://jabber.org/protocol/ibb')
2013-03-29 20:14:22 +00:00
def _get_stream(self, jid, sid, peer_jid, data):
return self._streams.get((jid, sid, peer_jid), None)
def _set_stream(self, jid, sid, peer_jid, stream):
self._streams[(jid, sid, peer_jid)] = stream
def _del_stream(self, jid, sid, peer_jid, data):
with self._streams_lock:
if (jid, sid, peer_jid) in self._streams:
del self._streams[(jid, sid, peer_jid)]
def _accept_stream(self, iq):
2013-02-19 09:00:04 +00:00
receiver = iq['to']
sender = iq['from']
sid = iq['ibb_open']['sid']
if self.api['authorized_sid'](receiver, sid, sender, iq):
return True
return self.api['authorized'](receiver, sid, sender, iq)
def _authorized(self, jid, sid, ifrom, iq):
if self.auto_accept:
if iq['ibb_open']['block_size'] <= self.max_block_size:
return True
return False
2013-02-19 09:00:04 +00:00
def _authorized_sid(self, jid, sid, ifrom, iq):
with self._preauthed_sids_lock:
if (jid, sid, ifrom) in self._preauthed_sids:
del self._preauthed_sids[(jid, sid, ifrom)]
return True
return False
def _preauthorize_sid(self, jid, sid, ifrom, data):
with self._preauthed_sids_lock:
self._preauthed_sids[(jid, sid, ifrom)] = True
def open_stream(self, jid, block_size=None, sid=None, window=1, use_messages=False,
ifrom=None, block=True, timeout=None, callback=None):
if sid is None:
sid = str(uuid.uuid4())
2013-02-19 09:00:04 +00:00
if block_size is None:
block_size = self.block_size
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-19 09:00:04 +00:00
iq['from'], iq['to'], window,
use_messages)
2012-02-03 15:03:46 +00:00
with self._stream_lock:
2013-03-29 20:14:22 +00:00
self._pending_streams[iq['id']] = stream
2012-02-03 15:03:46 +00:00
2013-03-29 20:14:22 +00:00
self._pending_streams[iq['id']] = stream
2012-02-03 15:03:46 +00:00
if block:
2012-02-03 15:03:46 +00:00
resp = iq.send(timeout=timeout)
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):
if iq['type'] == 'result':
with self._stream_lock:
2013-03-29 20:14:22 +00:00
stream = self._pending_streams.get(iq['id'], None)
if stream is not None:
log.debug('IBB stream (%s) accepted by %s', stream.sid, iq['from'])
stream.self_jid = iq['to']
stream.peer_jid = iq['from']
stream.stream_started.set()
self.api['set_stream'](stream.self_jid, stream.sid, stream.peer_jid, stream)
self.xmpp.event('ibb_stream_start', stream)
self.xmpp.event('stream:%s:%s' % (stream.sid, stream.peer_jid), stream)
with self._stream_lock:
2013-03-29 20:14:22 +00:00
if iq['id'] in self._pending_streams:
del self._pending_streams[iq['id']]
def _handle_open_request(self, iq):
sid = iq['ibb_open']['sid']
2013-02-19 09:00:04 +00:00
size = iq['ibb_open']['block_size'] or self.block_size
2013-03-29 20:14:22 +00:00
log.debug('Received IBB stream request from %s', iq['from'])
2013-02-19 09:00:04 +00:00
if not sid:
raise XMPPError(etype='modify', condition='bad-request')
if not self._accept_stream(iq):
2013-02-19 09:00:04 +00:00
raise XMPPError(etype='modify', condition='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,
2013-02-19 09:00:04 +00:00
iq['to'], iq['from'],
self.window_size)
stream.stream_started.set()
2013-03-29 20:14:22 +00:00
self.api['set_stream'](stream.self_jid, stream.sid, stream.peer_jid, stream)
iq.reply()
iq.send()
self.xmpp.event('ibb_stream_start', stream)
2013-02-19 09:00:04 +00:00
self.xmpp.event('stream:%s:%s' % (sid, stream.peer_jid), stream)
def _handle_data(self, stanza):
sid = stanza['ibb_data']['sid']
2013-03-29 20:14:22 +00:00
stream = self.api['get_stream'](stanza['to'], sid, stanza['from'])
2013-02-19 09:00:04 +00:00
if stream is not None and stanza['from'] == stream.peer_jid:
stream._recv_data(stanza)
else:
raise XMPPError('item-not-found')
def _handle_close(self, iq):
sid = iq['ibb_close']['sid']
2013-03-29 20:14:22 +00:00
stream = self.api['get_stream'](stanza['to'], sid, stanza['from'])
2013-02-19 09:00:04 +00:00
if stream is not None and iq['from'] == stream.peer_jid:
stream._closed(iq)
2013-03-29 20:14:22 +00:00
self.api['del_stream'](stream.self_jid, stream.sid, stream.peer_jid)
else:
raise XMPPError('item-not-found')