示例#1
0
 def registrationRequested(self, iq):
     '''A registration request was received. If an invalid username is
        choosen, the whole registration fails. Also, although it's normally
        impossible, we're being paranoid and forbid registrations from JIDs
        that don't contain a dot, since they might conflict with usernames.
     '''
     frm = iq.getFrom()
     info("Registration request from %s" % frm)
     if -1 == frm.getStripped().find('.'):
         reply = iq.buildReply(typ='error')
         reply.addChild(node=ErrorNode('not-acceptable', 500, 'cancel', _(REGISTRATION, 'error_invalid_jid')))
         self.send(reply)
         warning("Possible hacking attempt: JID '%s' (no dot!) tried to register to the gateway." % frm.getStripped())
         return
     isUpdate = False
     user = UserAccount(frm)
     requestedUsername = ''
     for child in iq.getQueryChildren():
         if 'username' == child.getName():
             requestedUsername = child.getData()
             break
     try:
         user.username = requestedUsername
         info("%s changed username to '%s'" % (user, user.username))
     except UsernameNotAvailableError:
         if 0 == len(requestedUsername):
             msg = 'error_missing_username'
         else:
             msg = 'error_invalid_username'
         reply = iq.buildReply(typ='error')
         reply.addChild(node=ErrorNode('not-acceptable', 406, 'modify', _(REGISTRATION, msg)))
         self.send(reply)
         return
     try:
         user.register()
         new_address = user.createAddress()
         self.addAddressToRoster(new_address, user)
     except AlreadyRegisteredError:
         info("(actually just an update)")
         isUpdate = True
     self.send(Iq(typ='result', to=frm, frm=self.jid, attrs={'id': iq.getID()}))
     if not isUpdate:
         self.send(Presence(typ='subscribe', to=frm.getStripped(), frm=self.jid))