Example #1
0
    def handle(self, message={}):
        try:
            return BaseController.handle(self, message)
        except KeyError:
            pass

        key = message.get("k")
        value = message.get("v")
        if key != "msg" or len(value) == 0:
            return

        if self.state == State.main_menu:
            if value.lower() == "c":
                pass
            else:
                try:
                    number_choice = int(value) - 1
                    if number_choice < 0:
                        raise ValueError

                except ValueError:
                    return self.syntax_error()

                selected_universe = self.fetch_nth_universe(number_choice)
                if not selected_universe:
                    return self.main_menu_view(error_message="No such number was found..")

                print("Selected universe:", selected_universe)
                self.connection.controller = PlayController(self.connection, self.runtime, self.account_id, selected_universe[0].id)
Example #2
0
    def handle(self, message={}):
        print("Handle message", message)
        try:
            return BaseController.handle(self, message)
        except KeyError:
            pass

        key = message.get("k")
        value = message.get("v", "")



        # Messages processed in standard mode check only for startswith commands, otherwise default to default action
        if key == "msg":
            # Look for a matching startswith handler
            for startswith_command in self._startswith.keys():
                if value.startswith(startswith_command):
                    print("Startswith command found", startswith_command)
                    cleaned_value = re.sub(re.escape(startswith_command), "", value, 1, re.IGNORECASE)
                    return self._startswith[startswith_command](self, startswith=startswith_command, value=cleaned_value)

            return self._commands.get("default")(self, value=value)

        # TODO command mode
        elif key == "cmd":
            tokens = value.split(" ")
            command = tokens[0].lower()
            print("Command", command)
            print("Self.commands", self._commands)
            raise NotImplementedError


        """

        # Look for a matching handler
        if command in self._commands:
            print("Command found", command)
            return self._commands[command](self, command, tokens[1:])



        # Look for a matching dynamic command handler
        for dcommand in self._dynamic_commands:
            try:
                return dcommand(self, command, tokens[1:])
            except NotImplementedError:
                continue
        """
        return self.syntax_error()
Example #3
0
    def handle(self, message={}):
        """
        Handle a received message in login state

        :param message:
        :return:
        """
        try:
            BaseController.handle(self, message)
        except KeyError:
            key = message.get("k")
            value = message.get("v")
            if key != "msg" or len(value) == 0:
                return

            # State - Login username
            if self.state == State.login_username:
                account = Account.get(lambda account: account.name.lower() == value.lower())

                if account:
                    self.account_id = account.id
                    self.request_password()
                    self.state = State.login_password
                else:
                    self.send_offtopic("Create new account with this name y/n?")
                    self.account_name = value
                    self.state = State.create_account

            # State - Login password
            elif self.state == State.login_password:
                account = Account[self.account_id]

                if self.hash_password_with_salt(account.password, self.dynamic_salt) == value:
                    self.send_offtopic("Login OK")
                    # Set controller to menu controller
                    self.connection.controller = MenuController(self.connection, self.runtime, self.account_id)
                    return

                else:
                    logging.info("Expected password: {}".format(
                        self.hash_password_with_salt(account.password, self.dynamic_salt)))
                    logging.info("Received password: {}".format( value))
                    self.send_offtopic("Login fail")
                    self.state = State.login_username
                    self.greeting()

            # State - Create account
            elif self.state == State.create_account:
                if value.lower()[0] == "y":
                    self.request_password(True, False)
                    self.state = State.create_password
                else:
                    self.greeting()
                    self.state = State.login_username

            # State - Create account - password
            elif self.state == State.create_password:
                self.password = value
                logging.info("Received password 1: {}".format(value))

                self.request_password(False)
                self.state = State.create_password_repeat

            # State - Create account - password repeat
            elif self.state == State.create_password_repeat:
                dynamic_password = self.hash_password_with_salt(self.password, self.dynamic_salt)
                logging.info("Received password 2: {}".format(value))

                if dynamic_password == value:
                    self.state = State.login_username
                    self.send_offtopic("New account has been created, you may now login.")
                    self.greeting()
                    account = Account(name=self.account_name,
                                      password=self.password,
                                      salt=self.static_salt)
                    db.commit()

                else:
                    self.send_offtopic("Password mismatch. Account not created.")
                    self.greeting()
                    self.state = State.login_username