def fail_bad_negotiation(self, reason, fields=None): """ Send an error and cancels everything If fields is None, the remote party has given us a bad cryptographic value of some kind. Otherwise, list the fields we haven't implemented. """ err = nbxmpp.Error(nbxmpp.Message(), nbxmpp.ERR_FEATURE_NOT_IMPLEMENTED) err.T.error.T.text.setData(reason) if fields: feature = nbxmpp.Node(nbxmpp.NS_FEATURE + ' feature') for field in fields: fn = nbxmpp.Node('field') fn['var'] = field feature.addChild(node=feature) err.addChild(node=feature) self.send(err) self.status = None self.enable_encryption = False # this prevents the MAC check on decryption from succeeding, # preventing falsified messages from going through. self.km_o = ''
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 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 __send_hash(self): # Send hash in a session info checksum = nbxmpp.Node(tag='checksum', payload=[nbxmpp.Node(tag='file', payload=[self._compute_hash()])]) checksum.setNamespace(nbxmpp.NS_JINGLE_FILE_TRANSFER_5) self.session.__session_info(checksum) pjid = app.get_jid_without_resource(self.session.peerjid) file_info = {'name' : self.file_props.name, 'file-name' : self.file_props.file_name, 'hash' : self.file_props.hash_, 'size' : self.file_props.size, 'date' : self.file_props.date, 'peerjid' : pjid } self.session.connection.get_module('Jingle').set_file_info(file_info)
def get_metacontacts(self): self._log.info('Request') node = nbxmpp.Node('storage', attrs={'xmlns': 'storage:metacontacts'}) iq = nbxmpp.Iq('get', nbxmpp.NS_PRIVATE, payload=node) self._con.connection.SendAndCallForResponse( iq, self._metacontacts_received)
def _add_streamhosts_to_query(query, sender, port, hosts): for host in hosts: streamhost = nbxmpp.Node(tag='streamhost') query.addChild(node=streamhost) streamhost.setAttr('port', str(port)) streamhost.setAttr('host', host) streamhost.setAttr('jid', sender)
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 cancel_session(self): """ Called when user declines session in UI (when we aren't the initiator) """ reason = nbxmpp.Node('reason') reason.addChild('cancel') self._session_terminate(reason)
def _finished_encrypt(self, obj, msgenc=None, error=None, conn=None, callback=None): if error: log.error('python-gnupg error: %s', error) app.nec.push_incoming_event( MessageNotSentEvent( None, conn=conn, jid=obj.jid, message=obj.message, error=error, time_=time.time(), session=obj.session)) return self.cleanup_stanza(obj) if msgenc: obj.msg_iq.setBody(self._get_info_message()) obj.msg_iq.setTag( 'x', namespace=nbxmpp.NS_ENCRYPTED).setData(msgenc) eme_node = nbxmpp.Node('encryption', attrs={'xmlns': nbxmpp.NS_EME, 'namespace': nbxmpp.NS_ENCRYPTED}) obj.msg_iq.addChild(node=eme_node) # Set xhtml to None so it doesnt get logged obj.xhtml = None obj.encrypted = self.encryption_name self.add_additional_data(obj.additional_data) print_msg_to_log(obj.msg_iq) callback(obj)
def make_transport(self): transport = nbxmpp.Node('transport') transport.setNamespace(nbxmpp.NS_JINGLE_IBB) transport.setAttr('block-size', self.block_sz) transport.setAttr('sid', self.sid) return transport
def get_roster_delimiter(self): self._log.info('Request') node = nbxmpp.Node('storage', attrs={'xmlns': 'roster:delimiter'}) iq = nbxmpp.Iq('get', nbxmpp.NS_PRIVATE, payload=node) self._con.connection.SendAndCallForResponse(iq, self._delimiter_received)
def make_candidate(self, candidate): types = { Farstream.CandidateType.HOST: 'host', Farstream.CandidateType.SRFLX: 'srflx', Farstream.CandidateType.PRFLX: 'prflx', Farstream.CandidateType.RELAY: 'relay', Farstream.CandidateType.MULTICAST: 'multicast' } attrs = { 'component': candidate.component_id, 'foundation': '1', # hack 'generation': '0', 'ip': candidate.ip, 'network': '0', 'port': candidate.port, 'priority': int(candidate.priority), # hack 'id': gajim.get_an_id() } if candidate.type in types: attrs['type'] = types[candidate.type] if candidate.proto == Farstream.NetworkProtocol.UDP: attrs['protocol'] = 'udp' else: # we actually don't handle properly different tcp options in jingle attrs['protocol'] = 'tcp' return nbxmpp.Node('candidate', attrs=attrs)
def _build_invisible_rule(self): node = nbxmpp.Node('list', {'name': 'invisible'}) iq = nbxmpp.Iq('set', nbxmpp.NS_PRIVACY, payload=[node]) item = node.setTag('item', {'action': 'deny', 'order': '3'}) item.setTag('presence-out') return iq
def del_item(self, jid): """ Delete contact 'jid' from roster """ self._con.connection.send( nbxmpp.Iq('set', nbxmpp.NS_ROSTER, payload=[ nbxmpp.Node('item', {'jid': jid, 'subscription': 'remove'})]))
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 _build_invisible_rule(self): node = nbxmpp.Node('list', {'name': 'invisible'}) iq = nbxmpp.Iq('set', nbxmpp.NS_PRIVACY, payload=[node]) if self._account in app.interface.status_sent_to_groups and \ len(app.interface.status_sent_to_groups[self._account]) > 0: for group in app.interface.status_sent_to_groups[self._account]: item = node.setTag( 'item', { 'type': 'group', 'value': group, 'action': 'allow', 'order': '1' }) item.setTag('presence-out') if self._account in app.interface.status_sent_to_users and \ len(app.interface.status_sent_to_users[self._account]) > 0: for jid in app.interface.status_sent_to_users[self._account]: item = node.setTag('item', { 'type': 'jid', 'value': jid, 'action': 'allow', 'order': '2' }) item.setTag('presence-out') item = node.setTag('item', {'action': 'deny', 'order': '3'}) item.setTag('presence-out') return iq
def _build_node(self, data): item = nbxmpp.Node('geoloc', {'xmlns': nbxmpp.NS_LOCATION}) if data is None: return item for field in LOCATION_DATA: if data.get(field, False): item.addChild(field, payload=data[field]) return item
def _on_proxy_auth_ok(self, proxy): log.info('proxy auth ok for %s', str(proxy)) # send activate request to proxy, send activated confirmation to peer if not self.connection: return sesn = self.connection.get_module('Jingle').get_jingle_session( self.ourjid, self.file_props.sid) if sesn is None: return iq = nbxmpp.Iq(to=proxy['jid'], frm=self.ourjid, typ='set') auth_id = "au_" + proxy['sid'] iq.setID(auth_id) query = iq.setTag('query', namespace=nbxmpp.NS_BYTESTREAM) query.setAttr('sid', proxy['sid']) activate = query.setTag('activate') activate.setData(sesn.peerjid) iq.setID(auth_id) self.connection.connection.send(iq) content = nbxmpp.Node('content') content.setAttr('creator', 'initiator') content_object = self.get_content() content.setAttr('name', content_object.name) transport = nbxmpp.Node('transport') transport.setNamespace(nbxmpp.NS_JINGLE_BYTESTREAM) transport.setAttr('sid', proxy['sid']) activated = nbxmpp.Node('activated') cid = None if 'cid' in proxy: cid = proxy['cid'] else: for host in self.candidates: if host['host'] == proxy['host'] and host['jid'] == proxy['jid'] \ and host['port'] == proxy['port']: cid = host['candidate_id'] break if cid is None: raise Exception('cid is missing') activated.setAttr('cid', cid) transport.addChild(node=activated) content.addChild(node=transport) sesn.send_transport_info(content)
def __content(self, payload=None): """ Build a XML content-wrapper for our data """ if payload is None: payload = [] return nbxmpp.Node('content', attrs={'name': self.name, 'creator': self.creator}, payload=payload)
def set_default_list(self, name=None): self._log.info('Set default list: %s', name) attr = {} if name: attr['name'] = name node = nbxmpp.Node('default', attr) iq = nbxmpp.Iq('set', nbxmpp.NS_PRIVACY, payload=[node]) self._con.connection.SendAndCallForResponse( iq, self._default_result_handler, {})
def _create_pgp_legacy_message(self, stanza, payload): stanza.setBody(self._get_info_message()) stanza.setTag('x', namespace=nbxmpp.NS_ENCRYPTED).setData(payload) eme_node = nbxmpp.Node('encryption', attrs={ 'xmlns': nbxmpp.NS_EME, 'namespace': nbxmpp.NS_ENCRYPTED }) stanza.addChild(node=eme_node)
def end_session(self): """ Called when user stops or cancel session in UI """ reason = nbxmpp.Node('reason') if self.state == JingleStates.ACTIVE: reason.addChild('success') else: reason.addChild('cancel') self._session_terminate(reason)
def _sendCand(self, args): if 'candError' in args: self.jft.nominated_cand['our-cand'] = False self.jft.send_error_candidate() return # Send candidate used streamhost = args['streamhost'] self.jft.nominated_cand['our-cand'] = streamhost content = nbxmpp.Node('content') content.setAttr('creator', 'initiator') content.setAttr('name', self.jft.name) transport = nbxmpp.Node('transport') transport.setNamespace(nbxmpp.NS_JINGLE_BYTESTREAM) transport.setAttr('sid', self.jft.transport.sid) candidateused = nbxmpp.Node('candidate-used') candidateused.setAttr('cid', streamhost['cid']) transport.addChild(node=candidateused) content.addChild(node=transport) self.jft.session.send_transport_info(content)
def iter_codecs(self): codecs = self.p2psession.props.codecs_without_config for codec in codecs: attrs = { 'name': codec.encoding_name, 'id': codec.id, 'channels': codec.channels } if codec.clock_rate: attrs['clockrate'] = codec.clock_rate if codec.optional_params: payload = list( nbxmpp.Node('parameter', { 'name': p.name, 'value': p.value }) for p in codec.optional_params) else: payload = [] yield nbxmpp.Node('payload-type', attrs, payload)
def _build_node(self, data): item = nbxmpp.Node('mood', {'xmlns': nbxmpp.NS_MOOD}) if data is None: return mood, text = data if mood: item.addChild(mood) if text: item.addChild('text', payload=text) return item
def get_bookmark_publish_options(): options = nbxmpp.Node(nbxmpp.NS_DATA + ' x', attrs={'type': 'submit'}) f = options.addChild('field', attrs={ 'var': 'FORM_TYPE', 'type': 'hidden' }) f.setTagData('value', nbxmpp.NS_PUBSUB_PUBLISH_OPTIONS) f = options.addChild('field', attrs={'var': 'pubsub#access_model'}) f.setTagData('value', 'whitelist') return options
def send_stream_header(self): self.Dispatcher._metastream = nbxmpp.Node('stream:stream') self.Dispatcher._metastream.setNamespace(self.Namespace) self.Dispatcher._metastream.setAttr('version', '1.0') self.Dispatcher._metastream.setAttr('xmlns:stream', Namespace.STREAMS) self.Dispatcher._metastream.setAttr('from', self.conn_holder.zeroconf.name) if self.to: self.Dispatcher._metastream.setAttr('to', self.to) self.Dispatcher.send("<?xml version='1.0'?>%s>" % str(self.Dispatcher._metastream)[:-2])
def make_transport(self, candidates=None): """ Build a transport stanza with the given candidates (or self.candidates if candidates is None) """ if not candidates: candidates = list(self._iter_candidates()) else: candidates = (self.make_candidate(candidate) for candidate in candidates) transport = nbxmpp.Node('transport', payload=candidates) return transport
def __init__(self): Gtk.Window.__init__(self, title="Data Form Test") self.set_default_size(600, 600) options = { 'left-width': 100, 'form-width': 435, } self._widget = DataFormWidget(extend_form(node=nbxmpp.Node(node=FORM)), options) self.add(self._widget) self.show()
def get_metacontacts(self): if not app.settings.get('metacontacts_enabled'): self._con.connect_machine() return self._log.info('Request') node = nbxmpp.Node('storage', attrs={'xmlns': 'storage:metacontacts'}) iq = nbxmpp.Iq('get', Namespace.PRIVATE, payload=node) self._con.connection.SendAndCallForResponse( iq, self._metacontacts_received)