+"""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.
+
+import ConfigParser
+import md5
+import os
+import random
+import string
+
+import muff
+for module in muff.__all__:
+ exec("import " + module)
+
+try:
+ if muffconf.config_data.get("general", "command_path"):
+ pass
+except AttributeError:
+ reload(muffconf)
+command_path = muffconf.config_data.get("general", "command_path")
+command_files = []
+for each_file in os.listdir(command_path):
+ command_files.append(command_path + "/" + each_file)
+command_data = ConfigParser.SafeConfigParser()
+command_data.read(command_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)
+ 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:
+ user.state = "checking password"
+ else:
+ user.name = user.proposed_name
+ user.proposed_name = None
+ user.load()
+ user.state = "checking new account name"
+ else:
+ command_quit(user)
+
+def handler_checking_password(user, input):
+ if md5.new(user.proposed_name + input).hexdigest() == user.passhash:
+ user.name = user.proposed_name
+ user.proposed_name = None
+ user.load()
+ user.state = "active"
+ elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
+ user.password_tries += 1
+ user.error = "incorrect"
+ else:
+ user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
+ command_quit(user)
+
+def handler_checking_new_account_name(user, input):
+ if input:
+ choice = input.lower()[0]
+ else:
+ choice = muffmenu.get_default(user)
+ if choice == "d":
+ command_quit(user)
+ elif choice == "g":
+ user.state = "entering account name"
+ elif choice == "n":
+ user.state = "entering new password"
+ 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()
+ user.state = "verifying new password"
+ elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
+ user.password_tries += 1
+ user.error = "weak"
+ else:
+ user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
+ command_quit(user)
+
+def handler_verifying_new_password(user, input):
+ if md5.new(user.name + input).hexdigest() == user.passhash:
+ user.state = "active"
+ elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
+ user.password_tries += 1
+ user.error = "differs"
+ user.state = "entering new password"
+ else:
+ user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
+ command_quit(user)
+
+def handler_active(user, input):
+ if not user.authenticated: user.authenticated = True
+ try:
+ inputlist = string.split(input, None, 1)
+ command = inputlist[0]
+ except:
+ command = input
+ try:
+ parameters = inputlist[1]
+ except:
+ parameters = ""
+ 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 + "\".")
+
+def command_halt(user, command="", parameters=""):
+ muffmisc.broadcast(user.name + " halts the world.")
+ for each_user in muffvars.userlist:
+ each_user.save()
+ muffvars.terminate_world = True
+
+def command_reload(user, command="", parameters=""):
+ user.send("Reloading all code modules.")
+ muffvars.reload_modules = True
+
+def command_quit(user, command="", parameters=""):
+ user.save()
+ user.connection.close()
+ user.remove()
+
+def command_help(user, command="", parameters=""):
+ if parameters:
+ 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."
+ else:
+ output = "That is not an available command."
+
+ else:
+ output = "These are the commands available to you:$(eol)$(eol)"
+ sorted_commands = command_list
+ sorted_commands.sort()
+ for item in sorted_commands:
+ try:
+ description = command_data.get(item, "description")
+ except:
+ 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\"."
+ user.send(output)
+
+def command_say(user, command="", parameters=""):
+ message = parameters.strip("\"'`").capitalize()
+ if parameters:
+ if message[-1] == "!":
+ action = "exclaim"
+ elif message[-1] in [ ",", "-", ":", ";" ]:
+ action = "begin"
+ elif message[-3:] == "...":
+ action = "muse"
+ elif message[-1] == "?":
+ action = "ask"
+ else:
+ action = "say"
+ message += "."
+ capitalization = [ "i", "i'd", "i'll" ]
+ for word in capitalization:
+ message = message.replace(" " + word + " ", " " + word.capitalize() + " ")
+ muffmisc.broadcast(user.name + " " + action + "s, \"" + 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:
+ message = "I'm not sure what \"" + command
+ if parameters:
+ message += " " + parameters
+ message += "\" means..."
+ else:
+ message = "Arglebargle, glop-glyf!?!"
+ user.send(message)
+