1 """Command objects for the MUFF Engine"""
3 # Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>, all rights reserved.
4 # Licensed per terms in the LICENSE file distributed with this software.
6 # command data like descriptions, help text, limits, et cetera, are stored in
7 # ini-style configuration files supported by the ConfigParser module
10 # md5 hashing is used for verification of account passwords
13 # the os module is used to we can get a directory listing and build lists of
14 # multiple config files in one directory
17 # the random module is useful for creating random conditional output
20 # string.split is used extensively to tokenize user input (break up command
21 # names and parameters)
24 # bit of a hack to load all modules in the muff package
26 for module in muff.__all__:
27 exec("import " + module)
29 # does the files:commands setting exist yet?
31 if muffconf.config_data.get("files", "commands"): pass
33 # if not, reload the muffconf module
34 except AttributeError:
37 # now we can safely nab the command path setting and build a list of data files
38 command_path = muffconf.config_data.get("files", "commands")
40 for each_file in os.listdir(command_path):
41 command_files.append(command_path + "/" + each_file)
43 # read the command data files
44 command_data = ConfigParser.SafeConfigParser()
45 command_data.read(command_files)
47 # this creates a list of commands mentioned in the data files
48 command_list = command_data.sections()
50 def handle_user_input(user, input_data):
51 """The main handler, branches to a state-specific handler."""
53 # the pairings of user state and command to run
54 handler_dictionary = {
55 "active": handler_active,
56 "entering account name": handler_entering_account_name,
57 "checking password": handler_checking_password,
58 "checking new account name": handler_checking_new_account_name,
59 "entering new password": handler_entering_new_password,
60 "verifying new password": handler_verifying_new_password
62 # check to make sure the state is expected, then call that handler
63 if user.state in handler_dictionary.keys():
64 handler_dictionary[user.state](user, input_data)
66 # if there's input with an unknown user state, something is wrong
67 else: handler_fallthrough(user, input_data)
69 # since we got input, flag that the menu/prompt needs to be redisplayed
70 user.menu_seen = False
72 def handler_entering_account_name(user, input_data):
73 """Handle the login account name."""
75 # did the user enter anything?
78 # keep only the first word and convert to lower-case
79 user.proposed_name = string.split(input_data)[0].lower()
81 # if we have a password hash, time to request a password
82 if user.get_passhash():
83 user.state = "checking password"
85 # otherwise, this could be a brand new user
87 user.name = user.proposed_name
88 user.proposed_name = None
90 user.state = "checking new account name"
92 # if the user entered nothing for a name, then buhbye
94 user.state = "disconnecting"
96 def handler_checking_password(user, input_data):
97 """Handle the login account password."""
99 # does the hashed input equal the stored hash?
100 if md5.new(user.proposed_name + input_data).hexdigest() == user.passhash:
102 # if so, set the username and load from cold storage
103 user.name = user.proposed_name
104 del(user.proposed_name)
108 # TODO: branch to character creation and selection menus
109 user.state = "active"
111 # if at first your hashes don't match, try, try again
112 elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
113 user.password_tries += 1
114 user.error = "incorrect"
116 # we've exceeded the maximum number of password failures, so disconnect
118 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
119 user.state = "disconnecting"
121 def handler_checking_new_account_name(user, input_data):
122 """Handle input for the new user menu."""
124 # if there's input, take the first character and lowercase it
126 choice = input_data.lower()[0]
128 # if there's no input, use the default
130 choice = muffmenu.get_default(user)
132 # user selected to disconnect
134 user.state == "disconnecting"
136 # go back to the login screen
138 user.state = "entering account name"
140 # new user, so ask for a password
142 user.state = "entering new password"
144 # user entered a non-existent option
146 user.error = "default"
148 def handler_entering_new_password(user, input_data):
149 """Handle a new password entry."""
151 # make sure the password is strong--at least one upper, one lower and
152 # one digit, seven or more characters in length
153 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)):
155 # hash and store it, then move on to verification
156 user.passhash = md5.new(user.name + input_data).hexdigest()
157 user.state = "verifying new password"
159 # the password was weak, try again if you haven't tried too many times
160 elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
161 user.password_tries += 1
164 # too many tries, so adios
166 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
167 user.state = "disconnecting"
169 def handler_verifying_new_password(user, input_data):
170 """Handle the re-entered new password for verification."""
172 # hash the input and match it to storage
173 if md5.new(user.name + input_data).hexdigest() == user.passhash:
175 # the hashes matched, so go active
176 # TODO: branch to character creation and selection menus
177 user.state = "active"
179 # go back to entering the new password as long as you haven't tried
181 elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
182 user.password_tries += 1
183 user.error = "differs"
184 user.state = "entering new password"
186 # otherwise, sayonara
188 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
189 user.state = "disconnecting"
191 def handler_active(user, input_data):
192 """Handle input for active users."""
194 # users reaching this stage should be considered authenticated
195 # TODO: this should actually happen before or in load() instead
196 if not user.authenticated: user.authenticated = True
198 # split out the command (first word) and parameters (everything else)
200 inputlist = string.split(input_data, None, 1)
201 command = inputlist[0]
205 parameters = inputlist[1]
210 # lowercase the command
211 command = command.lower()
213 # the command matches a command word for which we have data
214 if command in command_list: exec("command_" + command + "(user, command, parameters)")
216 # no data matching the entered command word
217 elif command: command_error(user, command, parameters)
219 def handler_fallthrough(user, input_data):
220 """Input received in an unknown user state."""
222 muffmisc.log("User \"" + user + "\" entered \"" + input_data + "\" while in unknown state \"" + user.state + "\".")
224 def command_halt(user, command="", parameters=""):
225 """Halt the world."""
228 # TODO: optionally take input for the message
229 muffmisc.broadcast(user.name + " halts the world.")
232 # TODO: probably want a misc function for this
233 for each_user in muffvars.userlist:
236 # set a flag to terminate the world
237 muffvars.terminate_world = True
239 def command_reload(user, command="", parameters=""):
240 """Reload all code modules, configs and data."""
243 user.send("Reloading all code modules, configs and data.")
245 # set a flag to reload
246 muffvars.reload_modules = True
248 def command_quit(user, command="", parameters=""):
249 """Quit the world."""
250 user.state = "disconnecting"
252 def command_help(user, command="", parameters=""):
253 """List available commands and provide help for commands."""
255 # did the user ask for help on a specific command word?
258 # is the command word one for which we have data?
259 if parameters in command_list:
261 # add a description if provided
263 description = command_data.get(parameters, "description")
265 description = "(no short description provided)"
266 output = "$(grn)" + parameters + "$(nrm) - " + command_data.get(parameters, "description") + "$(eol)$(eol)"
268 # add the help text if provided
270 help_text = command_data.get(parameters, "help")
272 help_text = "No help is provided for this command."
275 # no data for the requested command word
277 output = "That is not an available command."
279 # no specific command word was indicated
282 # give a sorted list of commands with descriptions if provided
283 output = "These are the commands available to you:$(eol)$(eol)"
284 sorted_commands = command_list
285 sorted_commands.sort()
286 for item in sorted_commands:
288 description = command_data.get(item, "description")
290 description = "(no short description provided)"
291 output += " $(grn)" + item + "$(nrm) - " + command_data.get(item, "description") + "$(eol)"
292 output += "$(eol)Enter \"help COMMAND\" for help on a command named \"COMMAND\"."
294 # send the accumulated output to the user
297 def command_say(user, command="", parameters=""):
298 """Speak to others in the same room."""
300 # the user entered a message
303 # get rid of quote marks on the ends of the message and
304 # capitalize the first letter
305 message = parameters.strip("\"'`").capitalize()
307 # exclaim because the message ended in an exclamation mark
308 # TODO: use the ends() function instead of an index throughout
309 if message[-1] == "!":
312 # begin because the message ended in miscellaneous punctuation
313 elif message[-1] in [ ",", "-", ":", ";" ]:
316 # muse because the message ended in an ellipsis
317 elif message[-3:] == "...":
320 # ask because the message ended in a question mark
321 elif message[-1] == "?":
324 # say because the message ended in a singular period
327 if message.endswith("."):
330 # capitalize a list of words within the message
331 # TODO: move this list to the config
332 capitalization = [ "i", "i'd", "i'll" ]
333 for word in capitalization:
334 message = message.replace(" " + word + " ", " " + word.capitalize() + " ")
337 # TODO: we won't be using broadcast once there are actual rooms
338 muffmisc.broadcast(user.name + " " + action + "s, \"" + message + "\"")
340 # there was no message
342 user.send("What do you want to say?")
344 def command_error(user, command="", parameters=""):
345 """Generic error for an unrecognized command word."""
347 # 90% of the time use a generic error
348 if random.randrange(10):
349 message = "I'm not sure what \"" + command
351 message += " " + parameters
352 message += "\" means..."
354 # 10% of the time use the classic diku error
356 message = "Arglebargle, glop-glyf!?!"
358 # send the error message