Imported from archive.
[mudpy.git] / lib / muff / muffcmds.py
index 555526a..6ade3b1 100644 (file)
@@ -28,16 +28,18 @@ for module in muff.__all__:
 
 # does the files:commands setting exist yet?
 try:
-       if muffconf.config_data.get("files", "commands"): pass
+       if muffconf.get("files", "commands"): pass
 
 # if not, reload the muffconf module
 except AttributeError:
        reload(muffconf)
 
 # 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_path = muffconf.get("files", "commands")
+command_files_index = ConfigParser.SafeConfigParser()
+command_files_index.read(command_path + "/index")
 command_files = []
-for each_file in os.listdir(command_path):
+for each_file in command_files_index.get("index", "files").split():
        command_files.append(command_path + "/" + each_file)
 
 # read the command data files
@@ -47,38 +49,29 @@ 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):
        """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)
-
-       # if there's input with an unknown user state, something is wrong
-       else: handler_fallthrough(user, input)
+       # check to make sure the state is expected, then call that handler
+       exec("handler_" + user.state.replace(" ", "_") + "(user)")
 
        # 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):
        """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:
+       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
@@ -86,54 +79,66 @@ def handler_entering_account_name(user, input):
                        user.name = user.proposed_name
                        user.proposed_name = None
                        user.load()
+                       muffmisc.log("New user: " + user.name)
                        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):
        """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).hexdigest() == user.passhash:
+       if md5.new(user.proposed_name + input_data).hexdigest() == user.passhash:
 
                # if so, set the username and load from cold storage
+               # TODO: branch to character creation and selection menus
                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 = "active"
 
        # 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"
 
        # 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_disconnecting(user):
+       """Waiting for the user's connection to close."""
+       pass
 
-def handler_checking_new_account_name(user, input):
+def handler_disconnecting_duplicates(user):
+       """Waiting for duplicate connections to close."""
+       pass
+
+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:
-               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,66 +152,69 @@ 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):
        """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) > 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
-       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"
 
        # 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):
        """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).hexdigest() == user.passhash:
+       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 = "active"
 
        # 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"
 
        # 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):
        """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:
-               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,23 +230,16 @@ 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):
-       """Input received in an unknown user state."""
-       if input:
-               print("User \"" + user + "\" entered \"" + input + "\" 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
@@ -246,23 +247,21 @@ 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
 
 def command_quit(user, command="", parameters=""):
        """Quit the world."""
+       user.state = "disconnecting"
 
-       # save to cold storage
-       user.save()
-
-       # close the connection
-       user.connection.close()
-
-       # remove from the list
-       user.remove()
+def command_time(user, command="", parameters=""):
+       """Show the current world time in elapsed increments."""
+       user.send(muffmisc.repr_long(muffmisc.getlong(muffvars.variable_data,
+               "time", "elapsed")) + " increments elapsed since the world was created.")
 
 def command_help(user, command="", parameters=""):
        """List available commands and provide help for commands."""
@@ -312,40 +311,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
-               # TODO: entering one period results in a double-period--oops!
-               else:
-                       action = "say"
-                       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
@@ -360,7 +361,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