Fix some errors in the IBB plugin.

This commit is contained in:
Lance Stout 2013-03-29 13:14:22 -07:00
parent 3732139fc3
commit fae39e1ab4
2 changed files with 40 additions and 24 deletions

View file

@ -38,7 +38,7 @@ class IBBReceiver(sleekxmpp.ClientXMPP):
self.register_plugin('xep_0030') # Service Discovery self.register_plugin('xep_0030') # Service Discovery
self.register_plugin('xep_0047', { self.register_plugin('xep_0047', {
'accept_stream': self.accept_stream 'auto_accept': True
}) # In-band Bytestreams }) # In-band Bytestreams
# The session_start event will be triggered when # The session_start event will be triggered when
@ -48,7 +48,7 @@ class IBBReceiver(sleekxmpp.ClientXMPP):
# our roster. # our roster.
self.add_event_handler("session_start", self.start) self.add_event_handler("session_start", self.start)
self.add_event_handler("ibb_stream_start", self.stream_opened) self.add_event_handler("ibb_stream_start", self.stream_opened, threaded=True)
self.add_event_handler("ibb_stream_data", self.stream_data) self.add_event_handler("ibb_stream_data", self.stream_data)
def start(self, event): def start(self, event):
@ -69,7 +69,7 @@ class IBBReceiver(sleekxmpp.ClientXMPP):
def accept_stream(self, iq): def accept_stream(self, iq):
""" """
Check that it is ok to accept a stream request. Check that it is ok to accept a stream request.
Controlling stream acceptance can be done via either: Controlling stream acceptance can be done via either:
- setting 'auto_accept' to False in the plugin - setting 'auto_accept' to False in the plugin
@ -83,9 +83,7 @@ class IBBReceiver(sleekxmpp.ClientXMPP):
return True return True
def stream_opened(self, stream): def stream_opened(self, stream):
# NOTE: IBB streams are bi-directional, so the original sender is print('Stream opened: %s from %s' % (stream.sid, stream.peer_jid))
# now the opened stream's receiver.
print('Stream opened: %s from %s' % (stream.sid, stream.receiver))
# You could run a loop reading from the stream using stream.recv(), # You could run a loop reading from the stream using stream.recv(),
# or use the ibb_stream_data event. # or use the ibb_stream_data event.

View file

@ -28,9 +28,9 @@ class XEP_0047(BasePlugin):
} }
def plugin_init(self): def plugin_init(self):
self.streams = {} self._streams = {}
self.pending_streams = {} self._pending_streams = {}
self.pending_close_streams = {} self._pending_lock = threading.Lock()
self._stream_lock = threading.Lock() self._stream_lock = threading.Lock()
self._preauthed_sids_lock = threading.Lock() self._preauthed_sids_lock = threading.Lock()
@ -64,6 +64,9 @@ class XEP_0047(BasePlugin):
self.api.register(self._authorized, 'authorized', default=True) self.api.register(self._authorized, 'authorized', default=True)
self.api.register(self._authorized_sid, 'authorized_sid', default=True) self.api.register(self._authorized_sid, 'authorized_sid', default=True)
self.api.register(self._preauthorize_sid, 'preauthorize_sid', default=True) self.api.register(self._preauthorize_sid, 'preauthorize_sid', default=True)
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)
def plugin_end(self): def plugin_end(self):
self.xmpp.remove_handler('IBB Open') self.xmpp.remove_handler('IBB Open')
@ -75,6 +78,17 @@ class XEP_0047(BasePlugin):
def session_bind(self, jid): def session_bind(self, jid):
self.xmpp['xep_0030'].add_feature('http://jabber.org/protocol/ibb') self.xmpp['xep_0030'].add_feature('http://jabber.org/protocol/ibb')
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): def _accept_stream(self, iq):
receiver = iq['to'] receiver = iq['to']
sender = iq['from'] sender = iq['from']
@ -121,9 +135,9 @@ class XEP_0047(BasePlugin):
use_messages) use_messages)
with self._stream_lock: with self._stream_lock:
self.pending_streams[iq['id']] = stream self._pending_streams[iq['id']] = stream
self.pending_streams[iq['id']] = stream self._pending_streams[iq['id']] = stream
if block: if block:
resp = iq.send(timeout=timeout) resp = iq.send(timeout=timeout)
@ -143,23 +157,26 @@ class XEP_0047(BasePlugin):
def _handle_opened_stream(self, iq): def _handle_opened_stream(self, iq):
if iq['type'] == 'result': if iq['type'] == 'result':
with self._stream_lock: with self._stream_lock:
stream = self.pending_streams.get(iq['id'], None) stream = self._pending_streams.get(iq['id'], None)
if stream is not None: if stream is not None:
stream.self_jid = iq['to'] log.debug('IBB stream (%s) accepted by %s', stream.sid, iq['from'])
stream.peer_jid = iq['from'] stream.self_jid = iq['to']
stream.stream_started.set() stream.peer_jid = iq['from']
self.streams[stream.sid] = stream stream.stream_started.set()
self.xmpp.event('ibb_stream_start', stream) self.api['set_stream'](stream.self_jid, stream.sid, stream.peer_jid, stream)
self.xmpp.event('stream:%s:%s' % (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: with self._stream_lock:
if iq['id'] in self.pending_streams: if iq['id'] in self._pending_streams:
del self.pending_streams[iq['id']] del self._pending_streams[iq['id']]
def _handle_open_request(self, iq): def _handle_open_request(self, iq):
sid = iq['ibb_open']['sid'] sid = iq['ibb_open']['sid']
size = iq['ibb_open']['block_size'] or self.block_size size = iq['ibb_open']['block_size'] or self.block_size
log.debug('Received IBB stream request from %s', iq['from'])
if not sid: if not sid:
raise XMPPError(etype='modify', condition='bad-request') raise XMPPError(etype='modify', condition='bad-request')
@ -173,7 +190,7 @@ class XEP_0047(BasePlugin):
iq['to'], iq['from'], iq['to'], iq['from'],
self.window_size) self.window_size)
stream.stream_started.set() stream.stream_started.set()
self.streams[sid] = stream self.api['set_stream'](stream.self_jid, stream.sid, stream.peer_jid, stream)
iq.reply() iq.reply()
iq.send() iq.send()
@ -182,7 +199,7 @@ class XEP_0047(BasePlugin):
def _handle_data(self, stanza): def _handle_data(self, stanza):
sid = stanza['ibb_data']['sid'] sid = stanza['ibb_data']['sid']
stream = self.streams.get(sid, None) stream = self.api['get_stream'](stanza['to'], sid, stanza['from'])
if stream is not None and stanza['from'] == stream.peer_jid: if stream is not None and stanza['from'] == stream.peer_jid:
stream._recv_data(stanza) stream._recv_data(stanza)
else: else:
@ -190,8 +207,9 @@ class XEP_0047(BasePlugin):
def _handle_close(self, iq): def _handle_close(self, iq):
sid = iq['ibb_close']['sid'] sid = iq['ibb_close']['sid']
stream = self.streams.get(sid, None) stream = self.api['get_stream'](stanza['to'], sid, stanza['from'])
if stream is not None and iq['from'] == stream.peer_jid: if stream is not None and iq['from'] == stream.peer_jid:
stream._closed(iq) stream._closed(iq)
self.api['del_stream'](stream.self_jid, stream.sid, stream.peer_jid)
else: else:
raise XMPPError('item-not-found') raise XMPPError('item-not-found')