X-Git-Url: https://mudpy.org/gitweb?p=mudpy.git;a=blobdiff_plain;f=lib%2Fmuff%2Fmuffcmds.py;h=033c74c52cb8512292de668e8ad30d88516a5130;hp=555526af173eb2f04a22f306ca85d2c55312a138;hb=994d6e52ce3d5c719991e8f798642cdb8a24b7d1;hpb=0f39af78818acbbee0b99145ff5ff303553027c6 diff --git a/lib/muff/muffcmds.py b/lib/muff/muffcmds.py index 555526a..033c74c 100644 --- a/lib/muff/muffcmds.py +++ b/lib/muff/muffcmds.py @@ -47,38 +47,39 @@ command_data.read(command_files) # this creates a list of commands mentioned in the data files command_list = command_data.sections() -def handle_user_input(user, input): +def handle_user_input(user, input_data): """The main handler, branches to a state-specific handler.""" - # TODO: change this to use a dict - if user.state == "active": handler_active(user, input) - elif user.state == "entering account name": handler_entering_account_name(user, input) - elif user.state == "checking password": handler_checking_password(user, input) - elif user.state == "checking new account name": handler_checking_new_account_name(user, input) - elif user.state == "entering new password": handler_entering_new_password(user, input) - elif user.state == "verifying new password": handler_verifying_new_password(user, input) + # the pairings of user state and command to run + handler_dictionary = { + "active": handler_active, + "entering account name": handler_entering_account_name, + "checking password": handler_checking_password, + "checking new account name": handler_checking_new_account_name, + "entering new password": handler_entering_new_password, + "verifying new password": handler_verifying_new_password + } + # check to make sure the state is expected, then call that handler + if user.state in handler_dictionary.keys(): + handler_dictionary[user.state](user, input_data) # if there's input with an unknown user state, something is wrong - else: handler_fallthrough(user, input) + else: handler_fallthrough(user, input_data) # since we got input, flag that the menu/prompt needs to be redisplayed user.menu_seen = False -def handler_entering_account_name(user, input): +def handler_entering_account_name(user, input_data): """Handle the login account name.""" # did the user enter anything? - if input: + if input_data: # keep only the first word and convert to lower-case - user.proposed_name = string.split(input)[0].lower() - - # try to get a password hash for the proposed name - user.get_passhash() + user.proposed_name = string.split(input_data)[0].lower() # if we have a password hash, time to request a password - # TODO: make get_passhash() return pass/fail and test that - if user.passhash: + if user.get_passhash(): user.state = "checking password" # otherwise, this could be a brand new user @@ -89,15 +90,14 @@ def handler_entering_account_name(user, input): user.state = "checking new account name" # if the user entered nothing for a name, then buhbye - # TODO: make a disconnect state instead of calling command_quit() else: - command_quit(user) + user.state = "disconnecting" -def handler_checking_password(user, input): +def handler_checking_password(user, input_data): """Handle the login account password.""" # does the hashed input equal the stored hash? - if md5.new(user.proposed_name + input).hexdigest() == user.passhash: + if md5.new(user.proposed_name + input_data).hexdigest() == user.passhash: # if so, set the username and load from cold storage user.name = user.proposed_name @@ -114,26 +114,24 @@ def handler_checking_password(user, input): user.error = "incorrect" # we've exceeded the maximum number of password failures, so disconnect - # TODO: make a disconnect state instead of calling command_quit() else: user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)") - command_quit(user) + user.state = "disconnecting" -def handler_checking_new_account_name(user, input): +def handler_checking_new_account_name(user, input_data): """Handle input for the new user menu.""" # if there's input, take the first character and lowercase it - if input: - choice = input.lower()[0] + if input_data: + choice = input_data.lower()[0] # if there's no input, use the default else: choice = muffmenu.get_default(user) # user selected to disconnect - # TODO: make a disconnect state instead of calling command_quit() if choice == "d": - command_quit(user) + user.state == "disconnecting" # go back to the login screen elif choice == "g": @@ -147,15 +145,15 @@ def handler_checking_new_account_name(user, input): else: user.error = "default" -def handler_entering_new_password(user, input): +def handler_entering_new_password(user, input_data): """Handle a new password entry.""" # make sure the password is strong--at least one upper, one lower and # one digit, seven or more characters in length - if len(input) > 6 and len(filter(lambda x: x>="0" and x<="9", input)) and len(filter(lambda x: x>="A" and x<="Z", input)) and len(filter(lambda x: x>="a" and x<="z", input)): + if len(input_data) > 6 and len(filter(lambda x: x>="0" and x<="9", input_data)) and len(filter(lambda x: x>="A" and x<="Z", input_data)) and len(filter(lambda x: x>="a" and x<="z", input_data)): # hash and store it, then move on to verification - user.passhash = md5.new(user.name + input).hexdigest() + user.passhash = md5.new(user.name + input_data).hexdigest() user.state = "verifying new password" # the password was weak, try again if you haven't tried too many times @@ -164,16 +162,15 @@ def handler_entering_new_password(user, input): user.error = "weak" # too many tries, so adios - # TODO: make a disconnect state instead of calling command_quit() else: user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)") - command_quit(user) + user.state = "disconnecting" -def handler_verifying_new_password(user, input): +def handler_verifying_new_password(user, input_data): """Handle the re-entered new password for verification.""" # hash the input and match it to storage - if md5.new(user.name + input).hexdigest() == user.passhash: + if md5.new(user.name + input_data).hexdigest() == user.passhash: # the hashes matched, so go active # TODO: branch to character creation and selection menus @@ -187,14 +184,11 @@ def handler_verifying_new_password(user, input): user.state = "entering new password" # otherwise, sayonara - # TODO: make a disconnect state instead of calling command_quit() else: user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)") - command_quit(user) + user.state = "disconnecting" -# TODO: um, input is a reserved word. better replace it in all sources with -# something else, like input_data -def handler_active(user, input): +def handler_active(user, input_data): """Handle input for active users.""" # users reaching this stage should be considered authenticated @@ -203,10 +197,10 @@ def handler_active(user, input): # split out the command (first word) and parameters (everything else) try: - inputlist = string.split(input, None, 1) + inputlist = string.split(input_data, None, 1) command = inputlist[0] except: - command = input + command = input_data try: parameters = inputlist[1] except: @@ -222,11 +216,10 @@ def handler_active(user, input): # no data matching the entered command word elif command: command_error(user, command, parameters) -# TODO: need a log function to handle conditions like this instead of print() -def handler_fallthrough(user, input): +def handler_fallthrough(user, input_data): """Input received in an unknown user state.""" - if input: - print("User \"" + user + "\" entered \"" + input + "\" while in unknown state \"" + user.state + "\".") + if input_data: + muffmisc.log("User \"" + user + "\" entered \"" + input_data + "\" while in unknown state \"" + user.state + "\".") def command_halt(user, command="", parameters=""): """Halt the world.""" @@ -254,15 +247,7 @@ def command_reload(user, command="", parameters=""): def command_quit(user, command="", parameters=""): """Quit the world.""" - - # save to cold storage - user.save() - - # close the connection - user.connection.close() - - # remove from the list - user.remove() + user.state = "disconnecting" def command_help(user, command="", parameters=""): """List available commands and provide help for commands.""" @@ -337,10 +322,10 @@ def command_say(user, command="", parameters=""): action = "ask" # say because the message ended in a singular period - # TODO: entering one period results in a double-period--oops! else: action = "say" - message += "." + if message.endswith("."): + message += "." # capitalize a list of words within the message # TODO: move this list to the config @@ -360,7 +345,7 @@ def command_error(user, command="", parameters=""): """Generic error for an unrecognized command word.""" # 90% of the time use a generic error - if random.random() > 0.1: + if random.randrange(10): message = "I'm not sure what \"" + command if parameters: message += " " + parameters