def presenceReceived(self, cnx, prs): '''Presence received from a registered user. If any presence stanza is received from an unregistered user, don't even look at it. They should register first.''' frm = prs.getFrom() user = UserAccount(frm) if not user.isRegistered(): return #TODO: Send a registration-required error resource = frm.getResource() to = prs.getTo().getStripped() typ = prs.getType() if typ == 'subscribe': cnx.send(Presence(typ='subscribed', frm=to, to=user.jid)) self.sendBitcoinPresence(cnx, user) elif typ == 'subscribed': debug('We were allowed to see %s\'s presence.' % user) elif typ == 'unsubscribe': debug('Just received an "unsubscribe" presence stanza. What does that mean?') elif typ == 'unsubscribed': debug('Unsubscribed. Any interest in this information?') elif typ == 'probe': self.sendBitcoinPresence(cnx, user) elif (typ == 'available') or (typ is None): self.userResourceConnects(user, resource) elif typ == 'unavailable': self.userResourceDisconnects(user, resource) elif typ == 'error': debug('Presence error. TODO: Handle it by not sending presence updates to them until they send a non-error.') raise NodeProcessed
def messageReceived(self, cnx, msg): '''Message received, addressed to the component. The command execution can raise exceptions, but those will be taken care of by the caller (messageHandler())''' user = UserAccount(msg.getFrom()) if user.isRegistered(): try: address = Address(msg.getBody()) msg = Message(to=msg.getFrom(), frm=address.jid,\ body=_(ROSTER, 'address_start_chat').format(address=address), typ='chat') except InvalidBitcoinAddressError: (action, args) = parseCommand(msg.getBody()) if action is None: return msg = msg.buildReply(Command(action, args).execute(user)) msg.setType('chat') if user.checkBalance() is not None: self.sendBitcoinPresence(cnx, user) else: error = _(REGISTRATION, 'error_not_registered') msg = msg.buildReply(_(COMMANDS, 'error_message').format(message=error)) msg.setType('error') cnx.send(msg) raise NodeProcessed
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)