Imported from archive.
[mudpy.git] / lib / muff / muffcmds.py
index a9df032..033c74c 100644 (file)
 """Command objects for the MUFF Engine"""
 
-# Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-#    - Redistributions of source code must retain the above copyright
-#      notice, this list of conditions and the following disclaimer.
-#    - Redistributions in binary form must reproduce the above
-#      copyright notice, this list of conditions and the following
-#      disclaimer in the documentation and/or other materials provided
-#      with the distribution.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
+# Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>, all rights reserved.
+# Licensed per terms in the LICENSE file distributed with this software.
 
+# command data like descriptions, help text, limits, et cetera, are stored in
+# ini-style configuration files supported by the ConfigParser module
 import ConfigParser
+
+# md5 hashing is used for verification of account passwords
 import md5
+
+# the os module is used to we can get a directory listing and build lists of
+# multiple config files in one directory
 import os
+
+# the random module is useful for creating random conditional output
 import random
+
+# string.split is used extensively to tokenize user input (break up command
+# names and parameters)
 import string
 
+# bit of a hack to load all modules in the muff package
 import muff
 for module in muff.__all__:
        exec("import " + module)
 
+# does the files:commands setting exist yet?
 try:
-       if muffconf.config_data.get("general", "command_path"):
-               pass
+       if muffconf.config_data.get("files", "commands"): pass
+
+# if not, reload the muffconf module
 except AttributeError:
        reload(muffconf)
-command_path = muffconf.config_data.get("general", "command_path")
+
+# 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)
+
+# read the command data files
 command_data = ConfigParser.SafeConfigParser()
 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):
-       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)
-       else: handler_fallthrough(user, input)
+def handle_user_input(user, input_data):
+       """The main handler, branches to a state-specific handler."""
+
+       # 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_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):
-       if input:
-               user.proposed_name = string.split(input)[0].lower()
-               user.get_passhash()
-               if user.passhash:
+def handler_entering_account_name(user, input_data):
+       """Handle the login account name."""
+
+       # did the user enter anything?
+       if input_data:
+               
+               # keep only the first word and convert to lower-case
+               user.proposed_name = string.split(input_data)[0].lower()
+
+               # if we have a password hash, time to request a password
+               if user.get_passhash():
                        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"
+
+       # if the user entered nothing for a name, then buhbye
        else:
-               command_quit(user)
+               user.state = "disconnecting"
+
+def handler_checking_password(user, input_data):
+       """Handle the login account password."""
 
-def handler_checking_password(user, input):
-       if md5.new(user.proposed_name + input).hexdigest() == user.passhash:
+       # 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
-               user.proposed_name = None
+               del(user.proposed_name)
                user.load()
+
+               # now go active
+               # TODO: branch to character creation and selection menus
                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"):
                user.password_tries += 1
                user.error = "incorrect"
+
+       # we've exceeded the maximum number of password failures, so disconnect
        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_data):
+       """Handle input for the new user menu."""
 
-def handler_checking_new_account_name(user, input):
-       if input:
-               choice = input.lower()[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)
+
+       # user selected to disconnect
        if choice == "d":
-               command_quit(user)
+               user.state == "disconnecting"
+
+       # go back to the login screen
        elif choice == "g":
                user.state = "entering account name"
+
+       # new user, so ask for a password
        elif choice == "n":
                user.state = "entering new password"
+
+       # user entered a non-existent option
        else:
                user.error = "default"
 
-def handler_entering_new_password(user, input):
-       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)):
-               user.passhash = md5.new(user.name + input).hexdigest()
+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_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"
+
+       # 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"):
                user.password_tries += 1
                user.error = "weak"
+
+       # too many tries, so adios
        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_data):
+       """Handle the re-entered new password for verification."""
+
+       # hash the input and match it to storage
+       if md5.new(user.name + input_data).hexdigest() == user.passhash:
 
-def handler_verifying_new_password(user, input):
-       if md5.new(user.name + input).hexdigest() == user.passhash:
+               # the hashes matched, so go active
+               # TODO: branch to character creation and selection menus
                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"):
                user.password_tries += 1
                user.error = "differs"
                user.state = "entering new password"
+
+       # otherwise, sayonara
        else:
                user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
-               command_quit(user)
+               user.state = "disconnecting"
 
-def handler_active(user, input):
+def handler_active(user, input_data):
+       """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
+
+       # 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:
                parameters = ""
+       del(inputlist)
+
+       # lowercase the command
        command = command.lower()
-       if not command: command_null(user, command, parameters)
-       elif command in command_list: exec("command_" + command + "(user, command, parameters)")
-       else: command_error(user, command, parameters)
 
-def handler_fallthrough(user, input):
-       if input:
-               print("User \"" + user + "\" entered \"" + input + "\" while in unknown state \"" + user.state + "\".")
+       # the command matches a command word for which we have data
+       if command in command_list: exec("command_" + command + "(user, command, parameters)")
+
+       # 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.")
+
+       # save everyone
+       # TODO: probably want a misc function for this
        for each_user in muffvars.userlist:
                each_user.save()
+
+       # set a flag to terminate the world
        muffvars.terminate_world = True
 
 def command_reload(user, command="", parameters=""):
-       user.send("Reloading all code modules.")
+       """Reload all code modules, configs and data."""
+
+       # let the user know
+       user.send("Reloading all code modules, configs and data.")
+
+       # set a flag to reload
        muffvars.reload_modules = True
 
 def command_quit(user, command="", parameters=""):
-       user.save()
-       user.connection.close()
-       user.remove()
+       """Quit the world."""
+       user.state = "disconnecting"
 
 def command_help(user, command="", parameters=""):
+       """List available commands and provide help for commands."""
+
+       # did the user ask for help on a specific command word?
        if parameters:
+
+               # is the command word one for which we have data?
                if parameters in command_list:
-                       if command_data.has_section(parameters):
-                               try:
-                                       description = command_data.get(parameters, "description")
-                               except:
-                                       description = "(no short description provided)"
-                               output = "$(grn)" + parameters + "$(nrm) - " + command_data.get(parameters, "description") + "$(eol)$(eol)"
-                               try:
-                                       help_text = command_data.get(parameters, "help")
-                               except:
-                                       help_text = "No help is provided for this command."
-                               output += help_text
-                       else:
-                               output = "There is no information on that command."
+
+                       # add a description if provided
+                       try:
+                               description = command_data.get(parameters, "description")
+                       except:
+                               description = "(no short description provided)"
+                       output = "$(grn)" + parameters + "$(nrm) - " + command_data.get(parameters, "description") + "$(eol)$(eol)"
+
+                       # add the help text if provided
+                       try:
+                               help_text = command_data.get(parameters, "help")
+                       except:
+                               help_text = "No help is provided for this command."
+                       output += help_text
+
+               # no data for the requested command word
                else:
                        output = "That is not an available command."
-                       
+
+       # no specific command word was indicated
        else:
+
+               # 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.sort()
@@ -188,39 +290,71 @@ def command_help(user, command="", parameters=""):
                                description = "(no short description provided)"
                        output += "   $(grn)" + item + "$(nrm) - " + command_data.get(item, "description") + "$(eol)"
                output += "$(eol)Enter \"help COMMAND\" for help on a command named \"COMMAND\"."
+
+       # send the accumulated output to the user
        user.send(output)
 
 def command_say(user, command="", parameters=""):
-       message = parameters.strip("\"'`").capitalize()
+       """Speak to others in the same room."""
+
+       # the user entered a message
        if 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"
+
+               # begin because the message ended in miscellaneous punctuation
                elif message[-1] in [ ",", "-", ":", ";" ]:
                        action = "begin"
+
+               # muse because the message ended in an ellipsis
                elif message[-3:] == "...":
                        action = "muse"
+
+               # 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"
-                       message += "."
+                       if message.endswith("."):
+                               message += "."
+
+               # 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:
                        message = message.replace(" " + word + " ", " " + word.capitalize() + " ")
+
+               # tell the room
+               # TODO: we won't be using broadcast once there are actual rooms
                muffmisc.broadcast(user.name + " " + action + "s, \"" + message + "\"")
+
+       # there was no message
        else:
                user.send("What do you want to say?")
 
-def command_null(user, command="", parameters=""):
-       pass
-
 def command_error(user, command="", parameters=""):
-       if random.random() > 0.1:
+       """Generic error for an unrecognized command word."""
+
+       # 90% of the time use a generic error
+       if random.randrange(10):
                message = "I'm not sure what \"" + command
                if parameters:
                        message += " " + parameters
                message += "\" means..."
+
+       # 10% of the time use the classic diku error
        else:
                message = "Arglebargle, glop-glyf!?!"
+
+       # send the error message
        user.send(message)