def IBBMessageHandler(self, conn, stanza): """ Receive next portion of incoming datastream and store it write it to temporary file. Used internally. """ sid = stanza.getTagAttr('data', 'sid') seq = stanza.getTagAttr('data', 'seq') data = stanza.getTagData('data') log.debug('ReceiveHandler called sid->%s seq->%s' % (sid, seq)) try: seq = int(seq) data = base64.b64decode(data.encode('utf-8')).decode('utf-8') except Exception: seq = '' data = '' err = None file_props = FilesProp.getFilePropByTransportSid(self.name, sid) if file_props is None: err = nbxmpp.ERR_ITEM_NOT_FOUND else: if not data: err = nbxmpp.ERR_BAD_REQUEST elif seq != file_props.seq: err = nbxmpp.ERR_UNEXPECTED_REQUEST else: log.debug('Successfull receive sid->%s %s+%s bytes' % (sid, file_props.fp.tell(), len(data))) file_props.seq += 1 file_props.started = True file_props.fp.write(data) current_time = time.time() file_props.elapsed_time += current_time - file_props.last_time file_props.last_time = current_time file_props.received_len += len(data) gajim.socks5queue.progress_transfer_cb(self.name, file_props) if file_props.received_len >= file_props.size: file_props.completed = True if err: log.debug('Error on receive: %s' % err) conn.send( nbxmpp.Error(nbxmpp.Iq( to=stanza.getFrom(), frm=stanza.getTo(), payload=[nbxmpp.Node(nbxmpp.NS_IBB + ' close')]), err, reply=0)) else: return True
def IBBMessageHandler(self, conn, stanza): """ Receive next portion of incoming datastream and store it write it to temporary file. Used internally. """ sid = stanza.getTagAttr('data', 'sid') seq = stanza.getTagAttr('data', 'seq') data = stanza.getTagData('data') log.debug('ReceiveHandler called sid->%s seq->%s' % (sid, seq)) try: seq = int(seq) data = base64.decodestring(data) except Exception: seq = '' data = '' err = None file_props = FilesProp.getFilePropByTransportSid(self.name, sid) if file_props is None: err = nbxmpp.ERR_ITEM_NOT_FOUND else: if not data: err = nbxmpp.ERR_BAD_REQUEST elif seq <> file_props.seq: err = nbxmpp.ERR_UNEXPECTED_REQUEST else: log.debug('Successfull receive sid->%s %s+%s bytes' % (sid, file_props.fp.tell(), len(data))) file_props.seq += 1 file_props.started = True file_props.fp.write(data) current_time = time.time() file_props.elapsed_time += current_time - file_props.last_time file_props.last_time = current_time file_props.received_len += len(data) gajim.socks5queue.progress_transfer_cb(self.name, file_props) if file_props.received_len >= file_props.size: file_props.completed = True if err: log.debug('Error on receive: %s' % err) conn.send(nbxmpp.Error(nbxmpp.Iq(to=stanza.getFrom(), frm=stanza.getTo(), payload=[nbxmpp.Node(nbxmpp.NS_IBB + ' close')]), err, reply=0)) else: return True
def IBBAllIqHandler(self, conn, stanza): """ Handle remote side reply about if it agree or not to receive our datastream. Used internally. Raises xmpppy event specfiying if the data transfer is agreed upon. """ syn_id = stanza.getID() log.debug('IBBAllIqHandler called syn_id->%s' % syn_id) for file_props in FilesProp.getAllFileProp(): if not file_props.direction or not file_props.connected: # It's socks5 bytestream # Or we closed the IBB stream continue if file_props.syn_id == syn_id: if stanza.getType() == 'error': if file_props.direction[0] == '<': conn.Event('IBB', 'ERROR ON RECEIVE', file_props) else: conn.Event('IBB', 'ERROR ON SEND', file_props) elif stanza.getType() == 'result': if file_props.direction[0] == '|': file_props.direction = file_props.direction[1:] self.SendHandler() else: conn.send( nbxmpp.Error(stanza, nbxmpp.ERR_UNEXPECTED_REQUEST)) break else: if stanza.getTag('data'): sid = stanza.getTagAttr('data', 'sid') file_props = FilesProp.getFilePropByTransportSid( self.name, sid) if file_props.connected and self.IBBMessageHandler( conn, stanza): reply = stanza.buildReply('result') reply.delChild('data') conn.send(reply) raise nbxmpp.NodeProcessed elif syn_id == self.last_sent_ibb_id: self.SendHandler()
def StreamCloseHandler(self, conn, stanza): """ Handle stream closure due to all data transmitted. Raise xmpppy event specifying successfull data receive. """ sid = stanza.getTagAttr('close', 'sid') log.debug('StreamCloseHandler called sid->%s' % sid) # look in sending files file_props = FilesProp.getFilePropByTransportSid(self.name, sid) if file_props: reply = stanza.buildReply('result') reply.delChild('close') conn.send(reply) # look in receiving files file_props.fp.close() file_props.completed = file_props.received_len >= file_props.size if not file_props.completed: file_props.error = -1 gajim.socks5queue.complete_transfer_cb(self.name, file_props) else: conn.send(nbxmpp.Error(stanza, nbxmpp.ERR_ITEM_NOT_FOUND))
def StreamCloseHandler(self, conn, stanza): """ Handle stream closure due to all data transmitted. Raise xmpppy event specifying successfull data receive. """ sid = stanza.getTagAttr('close', 'sid') log.debug('StreamCloseHandler called sid->%s' % sid) # look in sending files file_props = FilesProp.getFilePropByTransportSid(self.name, sid) if file_props: reply = stanza.buildReply('result') reply.delChild('close') conn.send(reply) # look in receiving files file_props.fp.close() file_props.completed = file_props.received_len >= file_props.size if not file_props.completed: file_props.error = -1 gajim.socks5queue.complete_transfer_cb(self.name, file_props) else: conn.send(nbxmpp.Error(stanza, nbxmpp.ERR_ITEM_NOT_FOUND))
def IBBAllIqHandler(self, conn, stanza): """ Handle remote side reply about if it agree or not to receive our datastream. Used internally. Raises xmpppy event specfiying if the data transfer is agreed upon. """ syn_id = stanza.getID() log.debug('IBBAllIqHandler called syn_id->%s' % syn_id) for file_props in FilesProp.getAllFileProp(): if not file_props.direction or not file_props.connected: # It's socks5 bytestream # Or we closed the IBB stream continue if file_props.syn_id == syn_id: if stanza.getType() == 'error': if file_props.direction[0] == '<': conn.Event('IBB', 'ERROR ON RECEIVE', file_props) else: conn.Event('IBB', 'ERROR ON SEND', file_props) elif stanza.getType() == 'result': if file_props.direction[0] == '|': file_props.direction = file_props.direction[1:] self.SendHandler() else: conn.send(nbxmpp.Error(stanza, nbxmpp.ERR_UNEXPECTED_REQUEST)) break else: if stanza.getTag('data'): sid = stanza.getTagAttr('data', 'sid') file_props = FilesProp.getFilePropByTransportSid(self.name, sid) if file_props.connected and self.IBBMessageHandler(conn, stanza): reply = stanza.buildReply('result') reply.delChild('data') conn.send(reply) raise nbxmpp.NodeProcessed elif syn_id == self.last_sent_ibb_id: self.SendHandler()
def StreamOpenHandler(self, conn, stanza): """ Handles opening of new incoming stream. Used internally. """ err = None sid = stanza.getTagAttr('open', 'sid') blocksize = stanza.getTagAttr('open', 'block-size') log.debug('StreamOpenHandler called sid->%s blocksize->%s' % (sid, blocksize)) file_props = FilesProp.getFilePropByTransportSid(self.name, sid) try: blocksize = int(blocksize) except: err = nbxmpp.ERR_BAD_REQUEST if not sid or not blocksize: err = nbxmpp.ERR_BAD_REQUEST elif not file_props: err = nbxmpp.ERR_UNEXPECTED_REQUEST if err: rep = nbxmpp.Error(stanza, err) else: log.debug("Opening stream: id %s, block-size %s" % (sid, blocksize)) rep = nbxmpp.Protocol('iq', stanza.getFrom(), 'result', stanza.getTo(), {'id': stanza.getID()}) file_props.block_size = blocksize file_props.direction = '<' file_props.seq = 0 file_props.received_len = 0 file_props.last_time = time.time() file_props.error = 0 file_props.paused = False file_props.connected = True file_props.completed = False file_props.disconnect_cb = None file_props.continue_cb = None file_props.syn_id = stanza.getID() file_props.fp = open(file_props.file_name, 'w') conn.send(rep)
def StreamOpenHandler(self, conn, stanza): """ Handles opening of new incoming stream. Used internally. """ err = None sid = stanza.getTagAttr('open', 'sid') blocksize = stanza.getTagAttr('open', 'block-size') log.debug('StreamOpenHandler called sid->%s blocksize->%s' % (sid, blocksize)) file_props = FilesProp.getFilePropByTransportSid(self.name, sid) try: blocksize = int(blocksize) except: err = nbxmpp.ERR_BAD_REQUEST if not sid or not blocksize: err = nbxmpp.ERR_BAD_REQUEST elif not file_props: err = nbxmpp.ERR_UNEXPECTED_REQUEST if err: rep = nbxmpp.Error(stanza, err) else: log.debug("Opening stream: id %s, block-size %s" % (sid, blocksize)) rep = nbxmpp.Protocol('iq', stanza.getFrom(), 'result', stanza.getTo(), {'id': stanza.getID()}) file_props.block_size = blocksize file_props.direction = '<' file_props.seq = 0 file_props.received_len = 0 file_props.last_time = time.time() file_props.error = 0 file_props.paused = False file_props.connected = True file_props.completed = False file_props.disconnect_cb = None file_props.continue_cb = None file_props.syn_id = stanza.getID() file_props.fp = open(file_props.file_name, 'w') conn.send(rep)