def iqReceived(self, cnx, iq): '''Default handler for IQ stanzas.''' typ = iq.getType() ns = iq.getQueryNS() if (NS_VERSION == ns) and ('get' == typ): name = Node('name') name.setData(LIB_NAME) version = Node('version') version.setData(LIB_VERSION) reply = iq.buildReply('result') query = reply.getQuery() query.addChild(node=name) query.addChild(node=version) cnx.send(reply) raise NodeProcessed elif (NS_LAST == ns) and ('get' == typ): if self.last is not None: reply = iq.buildReply('result') query = reply.getQuery() query.setAttr('seconds', (datetime.now() - self.last).seconds) cnx.send(reply) raise NodeProcessed else: debug("Unhandled IQ namespace '%s'." % ns)
def iqReceived(self, cnx, iq): '''IQ handler for the component''' typ = iq.getType() queries = iq.getChildren() # there should be only one if 0 == len(queries): return ns = queries[0].getNamespace() debug("OK we're handling IQ %s, ns=%s" % (typ, ns)) if NS_REGISTER == ns: if 'set' == typ: children = iq.getQueryChildren() if (0 != len(children)) and ('remove' == children[0].getName()): self.unregistrationRequested(iq) else: self.registrationRequested(iq) raise NodeProcessed elif 'get' == typ: instructions = Node('instructions') username = Node('username') user = UserAccount(iq.getFrom()) registered = user.isRegistered() if registered: instructions.setData(_(REGISTRATION, 'set_username')) username.setData(user.username) else: debug("A new user is preparing a registration") instructions.setData(_(REGISTRATION, 'introduction')) reply = iq.buildReply('result') query = reply.getQuery() if registered: query.addChild('registered') query.addChild(node=instructions) query.addChild(node=username) cnx.send(reply) raise NodeProcessed else: # Unkown namespace and type. The default handler will take care of it if we don't raise NodeProcessed. debug("Unknown IQ with ns '%s' and type '%s'." % (ns, typ)) elif NS_GATEWAY == ns: if 'get' == typ: reply = iq.buildReply('result') query = reply.getQuery() query.addChild('desc', payload=[_(ROSTER, 'address2jid_description')]) query.addChild('prompt', payload=[_(ROSTER, 'address2jid_prompt')]) cnx.send(reply) raise NodeProcessed elif 'set' == typ: children = iq.getQueryChildren() if (0 != len(children)) and ('prompt' == children[0].getName()): prompt = children[0].getData() debug("Someone wants to convert %s into a JID" % prompt) jid = Node('jid') try: jid.setData(Address(prompt).jid) except InvalidBitcoinAddressError: try: jid.setData(UserAccount(prompt).getLocalJID()) except UnknownUserError: reply = iq.buildReply(typ='error') reply.addChild(node=ErrorNode('item-not-found', 404, 'cancel', _(ROSTER, 'address2jid_invalid'))) cnx.send(reply) raise NodeProcessed reply = iq.buildReply('result') query = reply.getQuery() query.addChild(node=jid) cnx.send(reply) raise NodeProcessed elif NS_VCARD == ns: if 'get' == typ: reply = iq.buildReply('result') query = reply.getQuery() query.addChild('FN', payload=["%s v%s" % (LIB_NAME, LIB_VERSION)]) query.addChild('DESC', payload=[LIB_DESCRIPTION]) cnx.send(reply) raise NodeProcessed Addressable.iqReceived(self, cnx, iq)