def SendHandler(self): """ Send next portion of data if it is time to do it. Used internally. """ log.debug('SendHandler called') for file_props in FilesProp.getAllFileProp(): if not file_props.direction: # it's socks5 bytestream continue sid = file_props.sid if file_props.direction[:2] == '|>': # We waitthat other part accept stream continue if file_props.direction[0] == '>': if file_props.paused: continue if not file_props.connected: #TODO: Reply with out of order error continue chunk = file_props.fp.read(file_props.block_size) if chunk: datanode = nbxmpp.Node( nbxmpp.NS_IBB + ' data', { 'sid': file_props.transport_sid, 'seq': file_props.seq }, base64.b64encode( chunk.encode('utf-8')).decode('utf-8')) file_props.seq += 1 file_props.started = True if file_props.seq == 65536: file_props.seq = 0 self.last_sent_ibb_id = self.connection.send( nbxmpp.Protocol(name='iq', to=file_props.direction[1:], typ='set', payload=[datanode])) 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(chunk) gajim.socks5queue.progress_transfer_cb( self.name, file_props) else: # notify the other side about stream closing # notify the local user about sucessfull send # delete the local stream self.connection.send( nbxmpp.Protocol( 'iq', file_props.direction[1:], 'set', payload=[ nbxmpp.Node(nbxmpp.NS_IBB + ' close', {'sid': file_props.transport_sid}) ])) file_props.completed = True
def send_stanza(self, stanza): # send a stanza untouched if not self.connection: return if not isinstance(stanza, nbxmpp.Node): stanza = nbxmpp.Protocol(node=stanza) self.connection.send(stanza)
def _on_send(self, *args): if not self._selected_send_account: return if not app.account_is_available(self._selected_send_account): # If offline or connecting ErrorDialog( _('Connection not available'), _('Please make sure you are connected with \'%s\'.') % self._selected_send_account) return buffer_ = self._ui.input_entry.get_buffer() begin_iter, end_iter = buffer_.get_bounds() stanza = buffer_.get_text(begin_iter, end_iter, True) if stanza: try: node = nbxmpp.Node(node=stanza) except Exception as error: ErrorDialog(_('Invalid Node'), str(error)) return if node.getName() in ('message', 'presence', 'iq'): # Parse stanza again if its a message, presence or iq and # set jabber:client as toplevel namespace # Use type Protocol so nbxmpp counts the stanza for # stream management node = nbxmpp.Protocol(node=stanza, attrs={'xmlns': 'jabber:client'}) app.connections[self._selected_send_account].connection.send(node) self.last_stanza = stanza buffer_.set_text('')
def OpenStream(self, sid, to, fp, blocksize=4096): """ Start new stream. You should provide stream id 'sid', the endpoind jid 'to', the file object containing info for send 'fp'. Also the desired blocksize can be specified. Take into account that recommended stanza size is 4k and IBB uses base64 encoding that increases size of data by 1/3. """ if not nbxmpp.JID(to).getResource(): return file_props = FilesProp.getFilePropBySid(sid) file_props.direction = '|>' + to file_props.block_size = blocksize file_props.fp = fp file_props.seq = 0 file_props.error = 0 file_props.paused = False file_props.received_len = 0 file_props.last_time = time.time() file_props.connected = True file_props.completed = False file_props.disconnect_cb = None file_props.continue_cb = None syn = nbxmpp.Protocol('iq', to, 'set', payload=[nbxmpp.Node( nbxmpp.NS_IBB + ' open', {'sid': file_props.transport_sid, 'block-size': blocksize, 'stanza': 'iq'})]) self.connection.send(syn) file_props.syn_id = syn.getID() return file_props
def CloseIBBStream(self, file_props): file_props.connected = False file_props.fp.close() file_props.stopped = True to = file_props.receiver if file_props.direction == '<': to = file_props.sender self.connection.send( nbxmpp.Protocol('iq', to, 'set', payload=[ nbxmpp.Node(nbxmpp.NS_IBB + ' close', {'sid': file_props.transport_sid}) ])) if file_props.completed: app.socks5queue.complete_transfer_cb(self.name, file_props) elif file_props.session_type == 'jingle': peerjid = \ file_props.receiver if file_props.type_ == 's' else file_props.sender session = self.get_jingle_session(peerjid, file_props.sid, 'file') # According to the xep, the initiator also cancels the jingle session # if there are no more files to send using IBB if session.weinitiate: session.cancel_session()
def start_resolve(self, connection): """ Request network address from proxy """ self.state = S_STARTED self.active_connection = connection iq = nbxmpp.Protocol(name='iq', to=self.proxy, typ='get') query = iq.setTag('query') query.setNamespace(nbxmpp.NS_BYTESTREAM) connection.send(iq)
def _on_connect_success(self): log.debug('Host successfully connected %s:%s', self.host, self.port) iq = nbxmpp.Protocol(name='iq', to=self.jid, typ='set') query = iq.setTag('query') query.setNamespace(nbxmpp.NS_BYTESTREAM) query.setAttr('sid', self.sid) activate = query.setTag('activate') activate.setData('[email protected]/test2') if self.active_connection: log.debug('Activating bytestream on %s:%s', self.host, self.port) self.active_connection.SendAndCallForResponse( iq, self._result_received) self.state = S_ACTIVATED else: self.state = S_INITIAL
def on_send(self, *args): if app.connections[self.account].connected <= 1: # if offline or connecting ErrorDialog( _('Connection not available'), _('Please make sure you are connected with "%s".') % self.account) return buffer_ = self.input.get_buffer() begin_iter, end_iter = buffer_.get_bounds() stanza = buffer_.get_text(begin_iter, end_iter, True) if stanza: try: node = nbxmpp.Protocol(node=stanza) if node.getNamespace() == UNDECLARED: node.setNamespace(nbxmpp.NS_CLIENT) except Exception as error: ErrorDialog(_('Invalid Node'), str(error)) return app.connections[self.account].connection.send(node) buffer_.set_text('')
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 SendHandler(self, file_props): """ Send next portion of data if it is time to do it. Used internally. """ log.debug('SendHandler called') if file_props.completed: self.CloseIBBStream(file_props) if file_props.paused: return if not file_props.connected: #TODO: Reply with out of order error return chunk = file_props.fp.read(file_props.block_size) if chunk: datanode = nbxmpp.Node(nbxmpp.NS_IBB + ' data', { 'sid': file_props.transport_sid, 'seq': file_props.seq }, base64.b64encode(chunk).decode('ascii')) file_props.seq += 1 file_props.started = True if file_props.seq == 65536: file_props.seq = 0 file_props.syn_id = self.connection.send( nbxmpp.Protocol(name='iq', to=file_props.receiver, typ='set', payload=[datanode])) 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(chunk) if file_props.size == file_props.received_len: file_props.completed = True app.socks5queue.progress_transfer_cb(self.name, file_props) else: log.debug('Nothing to read, but file not completed')