def send_question(self, obj, jid): if obj.mtype != 'chat' and obj.mtype != 'normal': log.info('Anti_spam wrong message type: %s', obj.mtype) return # only for 'chat' messages if obj.receipt_request_tag and obj.mtype == 'chat': receipt = nbxmpp.Message(to=obj.fjid, typ='chat') receipt.setTag('received', namespace='urn:xmpp:receipts', attrs={'id': obj.id_}) if obj.thread_id: receipt.setThread(obj.thread_id) gajim.connections[obj.conn.name].connection.send(receipt, now=True) question = self.config['msgtxt_question'] log.info('Anti_spam enabled for %s, question: %s', jid, question) message = _('Antispam enabled. Please answer the question. The message must only ' + \ 'contain the answer. (Messages sent before the correct answer, will be lost): ') \ + question if obj.mtype == 'chat': stanza = nbxmpp.Message(to=jid, body=message, typ=obj.mtype) else: # for 'normal' type stanza = nbxmpp.Message(to=jid, body=message, subject='Antispam enabled', typ=obj.mtype) gajim.connections[obj.conn.name].connection.send(stanza, now=True)
def negotiate_archiving(self): self.negotiated = {} request = nbxmpp.Message() feature = request.NT.feature feature.setNamespace(nbxmpp.NS_FEATURE) x = nbxmpp.DataForm(typ='form') x.addChild(node=nbxmpp.DataField(name='FORM_TYPE', value='urn:xmpp:ssn', typ='hidden')) x.addChild(node=nbxmpp.DataField(name='accept', value='1', typ='boolean', required=True)) x.addChild(node=nbxmpp.DataField(name='logging', typ='list-single', options=self.archiving_logging_preference(), required=True)) x.addChild(node=nbxmpp.DataField(name='disclosure', typ='list-single', options=['never'], required=True)) x.addChild(node=nbxmpp.DataField(name='security', typ='list-single', options=['none'], required=True)) feature.addChild(node=x) self.status = 'requested-archiving' self.send(request)
def send_invitation(self): msg = nbxmpp.Message() invite = msg.NT.invite invite.setNamespace(NS_GAMES) invite.setAttr('type', 'new') game = invite.NT.game game.setAttr('var', NS_GAMES_TICTACTOE) x = nbxmpp.DataForm(typ='submit') f = x.setField('role') f.setType('list-single') f.setValue('x') f = x.setField('rows') f.setType('text-single') f.setValue(str(self.base.plugin.config['board_size'])) f = x.setField('cols') f.setType('text-single') f.setValue(str(self.base.plugin.config['board_size'])) f = x.setField('strike') f.setType('text-single') f.setValue(str(self.base.plugin.config['board_size'])) game.addChild(node=x) self.send(msg)
def decline_invitation(self): msg = nbxmpp.Message() terminate = msg.NT.decline terminate.setNamespace(NS_GAMES) self.send(msg)
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 send_contacts(self, contacts, fjid, type_='message'): if not app.account_is_connected(self._account): return if type_ == 'message': if len(contacts) == 1: msg = _('Sent contact: "%(jid)s" (%(name)s)') % { 'jid': contacts[0].get_full_jid(), 'name': contacts[0].get_shown_name() } else: msg = _('Sent contacts:') for contact in contacts: msg += '\n "%s" (%s)' % (contact.get_full_jid(), contact.get_shown_name()) stanza = nbxmpp.Message(to=app.get_jid_without_resource(fjid), body=msg) elif type_ == 'iq': stanza = nbxmpp.Iq(to=fjid, typ='set') x = stanza.addChild(name='x', namespace=nbxmpp.NS_ROSTERX) for contact in contacts: name = contact.get_shown_name() x.addChild(name='item', attrs={ 'action': 'add', 'jid': contact.jid, 'name': name }) log.info('Send contact: %s %s', contact.jid, name) self._con.connection.send(stanza)
def invited(self, msg): self.read_invitation(msg) # the number of the move about to be made self.next_move_id = 1 # display the board self.board = TicTacToeBoard(self, self.rows, self.cols) # accept the invitation, join the game response = nbxmpp.Message() join = response.NT.join join.setNamespace(NS_GAMES) self.send(response) if self.role_o == 'x': self.role_s = 'o' self.their_turn() else: self.role_s = 'x' self.role_o = 'o' self.our_turn()
def archiving_accepted(self, form): negotiated = {} ask_user = {} not_acceptable = [] if form['logging'] not in self.archiving_logging_preference(): raise self.negotiated['logging'] = form['logging'] accept = nbxmpp.Message() feature = accept.NT.feature feature.setNamespace(nbxmpp.NS_FEATURE) result = nbxmpp.DataForm(typ='result') result.addChild(node=nbxmpp.DataField(name='FORM_TYPE', value='urn:xmpp:ssn')) result.addChild(node=nbxmpp.DataField(name='accept', value='1')) feature.addChild(node=result) self.send(accept) if self.negotiated['logging'] == 'mustnot': self.loggable = False log.debug('archiving session accepted: %s' % self.loggable) self.status = 'active' self.archiving = True if self.control: self.control.print_archiving_session_details()
def decline(self, room, to, reason=None): if not app.account_is_connected(self._account): return message = nbxmpp.Message(to=room) muc_user = message.addChild('x', namespace=nbxmpp.NS_MUC_USER) decline = muc_user.addChild('decline', attrs={'to': to}) if reason: decline.setTagData('reason', reason) self._con.connection.send(message)
def receive_chat_msg(self, jid, msgtxt): '''simulate receiving a chat message from jid''' msg = nbxmpp.Message() msg.setBody(msgtxt) msg.setType('chat') xml = """<message from='%s' id='1' type='chat'><body>%s</body> <thread>123</thread></message>""" % (jid, msgtxt) stanza = nbxmpp.protocol.Message(node=nbxmpp.simplexml.XML2Node(xml)) self.conn._messageCB(None, stanza)
def end_game(self, reason): msg = nbxmpp.Message() terminate = msg.NT.terminate terminate.setNamespace(NS_GAMES) terminate.setAttr('reason', reason) self.send(msg) self.received = self.game_over
def cleanup_stanza(obj): ''' We make sure only allowed tags are in the stanza ''' stanza = nbxmpp.Message(to=obj.msg_iq.getTo(), typ=obj.msg_iq.getType()) stanza.setThread(obj.msg_iq.getThread()) for tag, ns in ALLOWED_TAGS: node = obj.msg_iq.getTag(tag, namespace=ns) if node: stanza.addChild(node=node) obj.msg_iq = stanza
def send_question(self, obj, jid): if obj.mtype != 'chat' and obj.mtype != 'normal': log.info('Anti_spam wrong message type: %s', obj.mtype) return question = self.config['msgtxt_question'] log.info('Anti_spam enabled for %s, question: %s', jid, question) message = _('Antispam enabled. Please answer the question. The message must only ' + \ 'contain the answer. (Messages sent before the correct answer, will be lost): ') \ + question if obj.mtype == 'chat': stanza = nbxmpp.Message(to=jid, body=message, typ=obj.mtype) else: # for 'normal' type stanza = nbxmpp.Message(to=jid, body=message, subject='Antispam enabled', typ=obj.mtype) app.connections[obj.conn.name].connection.send(stanza)
def request_voice(self, room): if not app.account_is_connected(self._account): return message = nbxmpp.Message(to=room) x = nbxmpp.DataForm(typ='submit') x.addChild(node=nbxmpp.DataField(name='FORM_TYPE', value=nbxmpp.NS_MUC + '#request')) x.addChild(node=nbxmpp.DataField( name='muc#role', value='participant', typ='text-single')) message.addChild(node=x) self._con.connection.send(message)
def _build_direct_invite(self, room, to, reason, continue_): message = nbxmpp.Message(to=to) attrs = {'jid': room} if reason: attrs['reason'] = reason if continue_: attrs['continue'] = 'true' password = app.gc_passwords.get(room, None) if password: attrs['password'] = password message.addChild(name='x', attrs=attrs, namespace=nbxmpp.NS_CONFERENCE) return message
def _build_mediated_invite(self, room, to, reason, continue_): message = nbxmpp.Message(to=room) muc_user = message.addChild('x', namespace=nbxmpp.NS_MUC_USER) invite = muc_user.addChild('invite', attrs={'to': to}) if continue_: invite.addChild(name='continue') if reason: invite.setTagData('reason', reason) password = app.gc_passwords.get(room, None) if password: muc_user.setTagData('password', password) return message
def inject(self, msg, appdata=None): log.debug('inject(appdata=%s)', appdata) msg = unicode(msg) account = self.user.accountname stanza = nbxmpp.Message(to=self.peer, body=msg, typ='chat') if appdata is not None: session = appdata.get('session', None) if session is not None: stanza.setThread(session.thread_id) add_message_processing_hints(stanza) gajim.connections[account].connection.send(stanza, now=True)
def on_ok_button_clicked(self, widget): acceptance = nbxmpp.Message(self.jid) acceptance.setThread(self.session.thread_id) feature = acceptance.NT.feature feature.setNamespace(nbxmpp.NS_FEATURE) form = self.data_form_widget.data_form form.setAttr('type', 'submit') feature.addChild(node=form) app.connections[self.account].send_stanza(acceptance) self.window.destroy()
def send_move(self, row, column): msg = nbxmpp.Message() msg.setType('chat') turn = msg.NT.turn turn.setNamespace(NS_GAMES) move = turn.NT.move move.setNamespace(NS_GAMES_TICTACTOE) move.setAttr('row', str(row)) move.setAttr('col', str(column)) move.setAttr('id', str(self.next_move_id)) self.send(msg)
def on_cancel_button_clicked(self, widget): rejection = nbxmpp.Message(self.jid) rejection.setThread(self.session.thread_id) feature = rejection.NT.feature feature.setNamespace(nbxmpp.NS_FEATURE) x = nbxmpp.DataForm(typ='submit') x.addChild(node=nbxmpp.DataField('FORM_TYPE', value='urn:xmpp:ssn')) x.addChild( node=nbxmpp.DataField('accept', value='false', typ='boolean')) feature.addChild(node=x) app.connections[self.account].send_stanza(rejection) self.window.destroy()
def reject_negotiation(self, body=None): msg = nbxmpp.Message() feature = msg.NT.feature feature.setNamespace(nbxmpp.NS_FEATURE) x = nbxmpp.DataForm(typ='submit') x.addChild(node=nbxmpp.DataField(name='FORM_TYPE', value='urn:xmpp:ssn')) x.addChild(node=nbxmpp.DataField(name='accept', value='0')) feature.addChild(node=x) if body: msg.setBody(body) self.send(msg) self.cancelled_negotiation()
def inject(self, msg, appdata=None): log.debug('inject(appdata=%s)', appdata) msg = unicode(msg) account = self.user.accountname if not nb_xmpp: stanza = common.xmpp.Message(to=self.peer, body=msg, typ='chat') else: stanza = nbxmpp.Message(to=self.peer, body=msg, typ='chat') if appdata is not None: session = appdata.get('session', None) if session is not None: stanza.setThread(session.thread_id) conn = gajim.connections[account] if conn.carbons_enabled: stanza.addChild(name='private', namespace=nbxmpp.NS_CARBONS) conn.connection.send(stanza, now=True)
def terminate(self, send_termination = True): # only send termination message if we've sent a message and think they # have XEP-0201 support if send_termination and self.last_send > 0 and \ (self.received_thread_id or self.last_receive == 0): msg = nbxmpp.Message() feature = msg.NT.feature feature.setNamespace(nbxmpp.NS_FEATURE) x = nbxmpp.DataForm(typ='submit') x.addChild(node=nbxmpp.DataField(name='FORM_TYPE', value='urn:xmpp:ssn')) x.addChild(node=nbxmpp.DataField(name='terminate', value='1')) feature.addChild(node=x) self.send(msg) self.status = None
def respond_archiving(self, form): field = form.getField('logging') options = [x[1] for x in field.getOptions()] values = field.getValues() logging = self.archiving_logging_preference(options) self.negotiated['logging'] = logging response = nbxmpp.Message() feature = response.NT.feature feature.setNamespace(nbxmpp.NS_FEATURE) x = nbxmpp.DataForm(typ='submit') x.addChild(node=nbxmpp.DataField(name='FORM_TYPE', value='urn:xmpp:ssn')) x.addChild(node=nbxmpp.DataField(name='accept', value='true')) x.addChild(node=nbxmpp.DataField(name='logging', value=logging)) self.status = 'responded-archiving' feature.addChild(node=x) if not logging: response = nbxmpp.Error(response, nbxmpp.ERR_NOT_ACCEPTABLE) feature = nbxmpp.Node(nbxmpp.NS_FEATURE + ' feature') n = nbxmpp.Node('field') n['var'] = 'logging' feature.addChild(node=n) response.T.error.addChild(node=feature) self.send(response)
def set_subject(self, room_jid, subject): if not app.account_is_connected(self._account): return message = nbxmpp.Message(room_jid, typ='groupchat', subject=subject) log.info('Set subject for %s', room_jid) self._con.connection.send(message)
def build_message_stanza(self, message): own_jid = self._con.get_own_jid() stanza = nbxmpp.Message(to=message.jid, body=message.message, typ=message.type_, subject=message.subject, xhtml=message.xhtml) if message.correct_id: stanza.setTag('replace', attrs={'id': message.correct_id}, namespace=Namespace.CORRECT) # XEP-0359 message.message_id = generate_id() stanza.setID(message.message_id) stanza.setOriginID(message.message_id) if message.label: stanza.addChild(node=message.label.to_node()) # XEP-0172: user_nickname if message.user_nick: stanza.setTag('nick', namespace=Namespace.NICK).setData(message.user_nick) # XEP-0203 # TODO: Seems delayed is not set anywhere if message.delayed: timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(message.delayed)) stanza.addChild('delay', namespace=Namespace.DELAY2, attrs={ 'from': str(own_jid), 'stamp': timestamp }) # XEP-0224 if message.attention: stanza.setTag('attention', namespace=Namespace.ATTENTION) # XEP-0066 if message.oob_url is not None: oob = stanza.addChild('x', namespace=Namespace.X_OOB) oob.addChild('url').setData(message.oob_url) # XEP-0184 if not own_jid.bare_match(message.jid): if message.message and not message.is_groupchat: stanza.setReceiptRequest() # Mark Message as MUC PM if message.contact.is_pm_contact: stanza.setTag('x', namespace=Namespace.MUC_USER) # XEP-0085 if message.chatstate is not None: stanza.setTag(message.chatstate, namespace=Namespace.CHATSTATES) if not message.message: stanza.setTag('no-store', namespace=Namespace.MSG_HINTS) # XEP-0333 if message.message: stanza.setMarkable() if message.marker: marker, id_ = message.marker stanza.setMarker(marker, id_) # Add other nodes if message.nodes is not None: for node in message.nodes: stanza.addChild(node=node) return stanza
def _build_answer_receipt(to, receipt_id): receipt = nbxmpp.Message(to=to, typ='chat') receipt.setTag('received', namespace='urn:xmpp:receipts', attrs={'id': receipt_id}) return receipt
def set_announce(self, jid, subject=None, body=None): message = nbxmpp.Message(to=jid, body=body, subject=subject) self._nbxmpp().send(message)
def accept_e2e_bob(self, form): """ 4.5 esession accept (bob) """ response = nbxmpp.Message() init = response.NT.init init.setNamespace(nbxmpp.NS_ESESSION_INIT) x = nbxmpp.DataForm(typ='result') for field in ('nonce', 'dhkeys', 'rshashes', 'identity', 'mac'): # FIXME: will do nothing in real world... assert field in form.asDict(), "alice's form didn't have a %s field" \ % field # 4.5.1 generating provisory session keys e = crypto.decode_mpi(base64.b64decode(form['dhkeys'])) p = dh.primes[self.modp] if crypto.sha256(crypto.encode_mpi(e)) != self.negotiated['He']: raise NegotiationError('SHA256(e) != He') k = self.get_shared_secret(e, self.y, p) self.kc_o, self.km_o, self.ks_o = self.generate_initiator_keys(k) # 4.5.2 verifying alice's identity self.verify_identity(form, e, False, 'a') # 4.5.4 generating bob's final session keys srs = b'' srses = secrets.secrets().retained_secrets(self.conn.name, self.jid.getStripped()) rshashes = [base64.b64decode(rshash) for rshash in form.getField( 'rshashes').getValues()] for s in srses: secret = s[0] if self.hmac(self.n_o, secret) in rshashes: srs = secret break # other shared secret # (we're not using one) oss = b'' k = crypto.sha256(k + srs + oss) self.kc_s, self.km_s, self.ks_s = self.generate_responder_keys(k) self.kc_o, self.km_o, self.ks_o = self.generate_initiator_keys(k) # 4.5.5 if srs: srshash = self.hmac(srs, b'Shared Retained Secret') else: srshash = crypto.random_bytes(32) x.addChild(node=nbxmpp.DataField(name='FORM_TYPE', value='urn:xmpp:ssn')) x.addChild(node=nbxmpp.DataField(name='nonce', value=base64.b64encode( self.n_o).decode('utf-8'))) x.addChild(node=nbxmpp.DataField(name='srshash', value=base64.b64encode( srshash).decode('utf-8'))) for datafield in self.make_identity(x, self.d): x.addChild(node=datafield) init.addChild(node=x) self.send(response) self.do_retained_secret(k, srs) if self.negotiated['logging'] == 'mustnot': self.loggable = False self.status = 'active' self.enable_encryption = True if self.control: self.control.print_esession_details() self.stop_archiving_for_session()
def send_displayed(self, jid, account): stanza_rec = self.last_markable.pop(jid, None) if stanza_rec: stanza_send = nbxmpp.Message(to=stanza_rec.getFrom(), typ='chat') stanza_send.addChild("displayed", namespace=self.CHAT_MARKERS_NS, attrs={'id': stanza_rec.getID()}) gajim.connections[account].connection.send(stanza_send, now=True)