Imported from archive.
[mudpy.git] / lib / muff / muffcmds.py
index 033c74c..8f1562c 100644 (file)
@@ -26,52 +26,44 @@ import muff
 for module in muff.__all__:
        exec("import " + module)
 
-# does the files:commands setting exist yet?
-try:
-       if muffconf.config_data.get("files", "commands"): pass
+def handle_user_input(user):
+       """The main handler, branches to a state-specific handler."""
 
-# if not, reload the muffconf module
-except AttributeError:
-       reload(muffconf)
+       # check to make sure the state is expected, then call that handler
+       if "handler_" + user.state in globals():
+               exec("handler_" + user.state + "(user)")
+       else:
+               generic_menu_handler(user)
 
-# now we can safely nab the command path setting and build a list of data files
-command_path = muffconf.config_data.get("files", "commands")
-command_files = []
-for each_file in os.listdir(command_path):
-       command_files.append(command_path + "/" + each_file)
+       # since we got input, flag that the menu/prompt needs to be redisplayed
+       user.menu_seen = False
 
-# read the command data files
-command_data = ConfigParser.SafeConfigParser()
-command_data.read(command_files)
+       # if the user's client echo is off, send a blank line for aesthetics
+       if not user.echoing: user.send("", "")
 
-# this creates a list of commands mentioned in the data files
-command_list = command_data.sections()
+def generic_menu_handler(user):
+       """A generic menu choice handler."""
 
-def handle_user_input(user, input_data):
-       """The main handler, branches to a state-specific handler."""
+       # get a lower-case representation of the next line of input
+       if user.input_queue:
+               choice = user.input_queue.pop(0)
+               if choice: choice = choice.lower()
+       else: choice = ""
 
-       # 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)
+       # run any script related to this choice
+       exec(muffmenu.get_choice_action(user, choice))
 
-       # if there's input with an unknown user state, something is wrong
-       else: handler_fallthrough(user, input_data)
+       # move on to the next state or return an error
+       new_state = muffmenu.get_choice_branch(user, choice)
+       if new_state: user.state = new_state
+       else: user.error = "default"
 
-       # since we got input, flag that the menu/prompt needs to be redisplayed
-       user.menu_seen = False
-
-def handler_entering_account_name(user, input_data):
+def handler_entering_account_name(user):
        """Handle the login account name."""
 
+       # get the next waiting line of input
+       input_data = user.input_queue.pop(0)
+
        # did the user enter anything?
        if input_data:
                
@@ -80,36 +72,39 @@ def handler_entering_account_name(user, input_data):
 
                # if we have a password hash, time to request a password
                if user.get_passhash():
-                       user.state = "checking password"
+                       user.state = "checking_password"
 
                # otherwise, this could be a brand new user
                else:
                        user.name = user.proposed_name
                        user.proposed_name = None
                        user.load()
-                       user.state = "checking new account name"
+                       muffmisc.log("New user: " + user.name)
+                       user.state = "checking_new_account_name"
 
        # if the user entered nothing for a name, then buhbye
        else:
                user.state = "disconnecting"
 
-def handler_checking_password(user, input_data):
+def handler_checking_password(user):
        """Handle the login account password."""
 
+       # get the next waiting line of input
+       input_data = user.input_queue.pop(0)
+
        # does the hashed input equal the stored hash?
        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
                del(user.proposed_name)
-               user.load()
-
-               # now go active
-               # TODO: branch to character creation and selection menus
-               user.state = "active"
+               if not user.replace_old_connections():
+                       user.load()
+                       user.authenticate()
+                       user.state = "main_utility"
 
        # if at first your hashes don't match, try, try again
-       elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
+       elif user.password_tries < muffconf.getint("general", "password_tries"):
                user.password_tries += 1
                user.error = "incorrect"
 
@@ -118,46 +113,52 @@ def handler_checking_password(user, input_data):
                user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
                user.state = "disconnecting"
 
-def handler_checking_new_account_name(user, input_data):
+def handler_checking_new_account_name(user):
        """Handle input for the new user menu."""
 
+       # get the next waiting line of input
+       input_data = user.input_queue.pop(0)
+
        # if there's input, take the first character and lowercase it
        if input_data:
                choice = input_data.lower()[0]
 
        # if there's no input, use the default
        else:
-               choice = muffmenu.get_default(user)
+               choice = muffmenu.get_default_menu_choice(user.state)
 
        # user selected to disconnect
        if choice == "d":
-               user.state == "disconnecting"
+               user.state = "disconnecting"
 
        # go back to the login screen
        elif choice == "g":
-               user.state = "entering account name"
+               user.state = "entering_account_name"
 
        # new user, so ask for a password
        elif choice == "n":
-               user.state = "entering new password"
+               user.state = "entering_new_password"
 
        # user entered a non-existent option
        else:
                user.error = "default"
 
-def handler_entering_new_password(user, input_data):
+def handler_entering_new_password(user):
        """Handle a new password entry."""
 
+       # get the next waiting line of input
+       input_data = user.input_queue.pop(0)
+
        # make sure the password is strong--at least one upper, one lower and
        # one digit, seven or more characters in length
        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_data).hexdigest()
-               user.state = "verifying new password"
+               user.state = "verifying_new_password"
 
        # the password was weak, try again if you haven't tried too many times
-       elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
+       elif user.password_tries < muffconf.getint("general", "password_tries"):
                user.password_tries += 1
                user.error = "weak"
 
@@ -166,34 +167,37 @@ def handler_entering_new_password(user, input_data):
                user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
                user.state = "disconnecting"
 
-def handler_verifying_new_password(user, input_data):
+def handler_verifying_new_password(user):
        """Handle the re-entered new password for verification."""
 
+       # get the next waiting line of input
+       input_data = user.input_queue.pop(0)
+
        # hash the input and match it to storage
        if md5.new(user.name + input_data).hexdigest() == user.passhash:
+               user.authenticate()
+               user.save()
 
                # the hashes matched, so go active
-               # TODO: branch to character creation and selection menus
-               user.state = "active"
+               if not user.replace_old_connections(): user.state = "main_utility"
 
        # go back to entering the new password as long as you haven't tried
        # too many times
-       elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
+       elif user.password_tries < muffconf.getint("general", "password_tries"):
                user.password_tries += 1
                user.error = "differs"
-               user.state = "entering new password"
+               user.state = "entering_new_password"
 
        # otherwise, sayonara
        else:
                user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
                user.state = "disconnecting"
 
-def handler_active(user, input_data):
+def handler_active(user):
        """Handle input for active users."""
 
-       # users reaching this stage should be considered authenticated
-       # TODO: this should actually happen before or in load() instead
-       if not user.authenticated: user.authenticated = True
+       # get the next waiting line of input
+       input_data = user.input_queue.pop(0)
 
        # split out the command (first word) and parameters (everything else)
        try:
@@ -211,27 +215,22 @@ def handler_active(user, input_data):
        command = command.lower()
 
        # the command matches a command word for which we have data
-       if command in command_list: exec("command_" + command + "(user, command, parameters)")
+       if command in muffuniv.universe.commands.keys():
+               exec(muffuniv.universe.commands[command].get("action"))
 
        # no data matching the entered command word
        elif command: command_error(user, command, parameters)
 
-def handler_fallthrough(user, input_data):
-       """Input received in an unknown 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."""
 
-       # let everyone know
-       # TODO: optionally take input for the message
-       muffmisc.broadcast(user.name + " halts the world.")
+       # see if there's a message or use a generic one
+       if parameters: message = "Halting: " + parameters
+       else: message = "User " + user.name + " halted the world."
 
-       # save everyone
-       # TODO: probably want a misc function for this
-       for each_user in muffvars.userlist:
-               each_user.save()
+       # let everyone know
+       muffmisc.broadcast(message)
+       muffmisc.log(message)
 
        # set a flag to terminate the world
        muffvars.terminate_world = True
@@ -239,8 +238,9 @@ def command_halt(user, command="", parameters=""):
 def command_reload(user, command="", parameters=""):
        """Reload all code modules, configs and data."""
 
-       # let the user know
+       # let the user know and log
        user.send("Reloading all code modules, configs and data.")
+       muffmisc.log("User " + user.name + " reloaded the world.")
 
        # set a flag to reload
        muffvars.reload_modules = True
@@ -249,6 +249,10 @@ def command_quit(user, command="", parameters=""):
        """Quit the world."""
        user.state = "disconnecting"
 
+def command_time(user, command="", parameters=""):
+       """Show the current world time in elapsed increments."""
+       user.send(muffuniv.universe.internals["counters"].get("elapsed") + " increments elapsed since the world was created.")
+
 def command_help(user, command="", parameters=""):
        """List available commands and provide help for commands."""
 
@@ -256,19 +260,17 @@ def command_help(user, command="", parameters=""):
        if parameters:
 
                # is the command word one for which we have data?
-               if parameters in command_list:
+               if parameters in muffuniv.universe.commands.keys():
 
                        # add a description if provided
-                       try:
-                               description = command_data.get(parameters, "description")
-                       except:
+                       description = muffuniv.universe.commands[parameters].get("description")
+                       if not description:
                                description = "(no short description provided)"
-                       output = "$(grn)" + parameters + "$(nrm) - " + command_data.get(parameters, "description") + "$(eol)$(eol)"
+                       output = "$(grn)" + parameters + "$(nrm) - " + description + "$(eol)$(eol)"
 
                        # add the help text if provided
-                       try:
-                               help_text = command_data.get(parameters, "help")
-                       except:
+                       help_text = muffuniv.universe.commands[parameters].get("help")
+                       if not help_text:
                                help_text = "No help is provided for this command."
                        output += help_text
 
@@ -281,14 +283,13 @@ def command_help(user, command="", parameters=""):
 
                # give a sorted list of commands with descriptions if provided
                output = "These are the commands available to you:$(eol)$(eol)"
-               sorted_commands = command_list
+               sorted_commands = muffuniv.universe.commands.keys()
                sorted_commands.sort()
                for item in sorted_commands:
-                       try:
-                               description = command_data.get(item, "description")
-                       except:
+                       description = muffuniv.universe.commands[item].get("description")
+                       if not description:
                                description = "(no short description provided)"
-                       output += "   $(grn)" + item + "$(nrm) - " + command_data.get(item, "description") + "$(eol)"
+                       output += "   $(grn)" + item + "$(nrm) - " + description + "$(eol)"
                output += "$(eol)Enter \"help COMMAND\" for help on a command named \"COMMAND\"."
 
        # send the accumulated output to the user
@@ -297,40 +298,42 @@ def command_help(user, command="", parameters=""):
 def command_say(user, command="", parameters=""):
        """Speak to others in the same room."""
 
+       # check for replacement macros
+       if muffmisc.replace_macros(user, parameters, True) != parameters:
+               user.send("You cannot speak $_(replacement macros).")
+
        # the user entered a message
-       if parameters:
+       elif parameters:
 
                # get rid of quote marks on the ends of the message and
                # capitalize the first letter
                message = parameters.strip("\"'`").capitalize()
 
-               # exclaim because the message ended in an exclamation mark
-               # TODO: use the ends() function instead of an index throughout
-               if message[-1] == "!":
-                       action = "exclaim"
+               # a dictionary of punctuation:action pairs
+               actions = {}
+               for option in muffconf.config_data.options("language"):
+                       if option.startswith("punctuation_"):
+                               action = option.split("_")[1]
+                               for mark in muffconf.config_data.get("language", option).split():
+                                               actions[mark] = action
 
-               # begin because the message ended in miscellaneous punctuation
-               elif message[-1] in [ ",", "-", ":", ";" ]:
-                       action = "begin"
+               # set the default action
+               action = actions[muffconf.config_data.get("language", "default_punctuation")]
 
-               # muse because the message ended in an ellipsis
-               elif message[-3:] == "...":
-                       action = "muse"
+               # match the punctuation used, if any, to an action
+               default_punctuation = muffconf.config_data.get("language", "default_punctuation")
+               for mark in actions.keys():
+                       if message.endswith(mark) and mark != default_punctuation:
+                               action = actions[mark]
+                               break
 
-               # ask because the message ended in a question mark
-               elif message[-1] == "?":
-                       action = "ask"
-
-               # say because the message ended in a singular period
-               else:
-                       action = "say"
-                       if message.endswith("."):
-                               message += "."
+               # if the action is default and there is no mark, add one
+               if action == actions[default_punctuation] and not message.endswith(default_punctuation):
+                       message += default_punctuation
 
                # capitalize a list of words within the message
-               # TODO: move this list to the config
-               capitalization = [ "i", "i'd", "i'll" ]
-               for word in capitalization:
+               capitalize = muffconf.get("language", "capitalize").split()
+               for word in capitalize:
                        message = message.replace(" " + word + " ", " " + word.capitalize() + " ")
 
                # tell the room
@@ -341,6 +344,27 @@ def command_say(user, command="", parameters=""):
        else:
                user.send("What do you want to say?")
 
+def command_show(user, command="", parameters=""):
+       """Show program data."""
+       if parameters == "avatars":
+               message = "These are the avatars managed by your account:$(eol)"
+               avatars = user.list_avatar_names()
+               avatars.sort()
+               for avatar in avatars: message += "$(eol)   $(grn)" + avatar + "$(nrm)"
+       elif parameters == "files":
+               message = "These are the current files containing the universe:$(eol)"
+               keys = muffuniv.universe.files.keys()
+               keys.sort()
+               for key in keys: message += "$(eol)   $(grn)" + key + "$(nrm)"
+       elif parameters == "universe":
+               message = "These are the current elements in the universe:$(eol)"
+               keys = muffuniv.universe.contents.keys()
+               keys.sort()
+               for key in keys: message += "$(eol)   $(grn)" + key + "$(nrm)"
+       elif parameters: message = "I don't know what \"" + parameters + "\" is."
+       else: message = "What do you want to show?"
+       user.send(message)
+
 def command_error(user, command="", parameters=""):
        """Generic error for an unrecognized command word."""