1 """Command objects for the MUFF Engine"""
3 # Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
10 # - Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # - Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
37 for module in muff.__all__:
38 exec("import " + module)
41 if muffconf.config_data.get("general", "command_path"):
43 except AttributeError:
45 command_path = muffconf.config_data.get("general", "command_path")
47 for each_file in os.listdir(command_path):
48 command_files.append(command_path + "/" + each_file)
49 command_data = ConfigParser.SafeConfigParser()
50 command_data.read(command_files)
51 command_list = command_data.sections()
53 def handle_user_input(user, input):
54 if user.state == "active": handler_active(user, input)
55 elif user.state == "entering account name": handler_entering_account_name(user, input)
56 elif user.state == "checking password": handler_checking_password(user, input)
57 elif user.state == "checking new account name": handler_checking_new_account_name(user, input)
58 elif user.state == "entering new password": handler_entering_new_password(user, input)
59 elif user.state == "verifying new password": handler_verifying_new_password(user, input)
60 else: handler_fallthrough(user, input)
61 user.menu_seen = False
63 def handler_entering_account_name(user, input):
65 user.proposed_name = string.split(input)[0].lower()
68 user.state = "checking password"
70 user.name = user.proposed_name
71 user.proposed_name = None
73 user.state = "checking new account name"
77 def handler_checking_password(user, input):
78 if md5.new(user.proposed_name + input).hexdigest() == user.passhash:
79 user.name = user.proposed_name
80 user.proposed_name = None
83 elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
84 user.password_tries += 1
85 user.error = "incorrect"
87 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
90 def handler_checking_new_account_name(user, input):
92 choice = input.lower()[0]
94 choice = muffmenu.get_default(user)
98 user.state = "entering account name"
100 user.state = "entering new password"
102 user.error = "default"
104 def handler_entering_new_password(user, input):
105 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)):
106 user.passhash = md5.new(user.name + input).hexdigest()
107 user.state = "verifying new password"
108 elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
109 user.password_tries += 1
112 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
115 def handler_verifying_new_password(user, input):
116 if md5.new(user.name + input).hexdigest() == user.passhash:
117 user.state = "active"
118 elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
119 user.password_tries += 1
120 user.error = "differs"
121 user.state = "entering new password"
123 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
126 def handler_active(user, input):
127 if not user.authenticated: user.authenticated = True
129 inputlist = string.split(input, None, 1)
130 command = inputlist[0]
134 parameters = inputlist[1]
137 command = command.lower()
138 if not command: command_null(user, command, parameters)
139 elif command in command_list: exec("command_" + command + "(user, command, parameters)")
140 else: command_error(user, command, parameters)
142 def handler_fallthrough(user, input):
144 print("User \"" + user + "\" entered \"" + input + "\" while in unknown state \"" + user.state + "\".")
146 def command_halt(user, command="", parameters=""):
147 muffmisc.broadcast(user.name + " halts the world.")
148 for each_user in muffvars.userlist:
150 muffvars.terminate_world = True
152 def command_reload(user, command="", parameters=""):
153 user.send("Reloading all code modules.")
154 muffvars.reload_modules = True
156 def command_quit(user, command="", parameters=""):
158 user.connection.close()
161 def command_help(user, command="", parameters=""):
163 if parameters in command_list:
164 if command_data.has_section(parameters):
166 description = command_data.get(parameters, "description")
168 description = "(no short description provided)"
169 output = "$(grn)" + parameters + "$(nrm) - " + command_data.get(parameters, "description") + "$(eol)$(eol)"
171 help_text = command_data.get(parameters, "help")
173 help_text = "No help is provided for this command."
176 output = "There is no information on that command."
178 output = "That is not an available command."
181 output = "These are the commands available to you:$(eol)$(eol)"
182 sorted_commands = command_list
183 sorted_commands.sort()
184 for item in sorted_commands:
186 description = command_data.get(item, "description")
188 description = "(no short description provided)"
189 output += " $(grn)" + item + "$(nrm) - " + command_data.get(item, "description") + "$(eol)"
190 output += "$(eol)Enter \"help COMMAND\" for help on a command named \"COMMAND\"."
193 def command_say(user, command="", parameters=""):
194 message = parameters.strip("\"'`").capitalize()
196 if message[-1] == "!":
198 elif message[-1] in [ ",", "-", ":", ";" ]:
200 elif message[-3:] == "...":
202 elif message[-1] == "?":
207 capitalization = [ "i", "i'd", "i'll" ]
208 for word in capitalization:
209 message = message.replace(" " + word + " ", " " + word.capitalize() + " ")
210 muffmisc.broadcast(user.name + " " + action + "s, \"" + message + "\"")
212 user.send("What do you want to say?")
214 def command_null(user, command="", parameters=""):
217 def command_error(user, command="", parameters=""):
218 if random.random() > 0.1:
219 message = "I'm not sure what \"" + command
221 message += " " + parameters
222 message += "\" means..."
224 message = "Arglebargle, glop-glyf!?!"