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 def handle_user_input(user):
30 """The main handler, branches to a state-specific handler."""
32 # check to make sure the state is expected, then call that handler
33 if "handler_" + user.state in globals():
34 exec("handler_" + user.state + "(user)")
36 generic_menu_handler(user)
38 # since we got input, flag that the menu/prompt needs to be redisplayed
39 user.menu_seen = False
41 # if the user's client echo is off, send a blank line for aesthetics
42 if not user.echoing: user.send("", "")
44 def generic_menu_handler(user):
45 """A generic menu choice handler."""
47 # get a lower-case representation of the next line of input
49 choice = user.input_queue.pop(0)
50 if choice: choice = choice.lower()
53 # run any script related to this choice
54 exec(muffmenu.get_choice_action(user, choice))
56 # move on to the next state or return an error
57 new_state = muffmenu.get_choice_branch(user, choice)
58 if new_state: user.state = new_state
59 else: user.error = "default"
61 def handler_entering_account_name(user):
62 """Handle the login account name."""
64 # get the next waiting line of input
65 input_data = user.input_queue.pop(0)
67 # did the user enter anything?
70 # keep only the first word and convert to lower-case
71 name = input_data.lower()
73 # fail if there are non-alphanumeric characters
74 if name != filter(lambda x: x>="0" and x<="9" or x>="a" and x<="z", name):
75 user.error = "bad_name"
77 # if that account exists, time to request a password
78 elif name in muffuniv.universe.categories["account"]:
79 user.account = muffuniv.universe.categories["account"][name]
80 user.state = "checking_password"
82 # otherwise, this could be a brand new user
84 user.account = muffuniv.Element("account:" + name, muffuniv.universe)
85 user.account.set("name", name)
86 muffmisc.log("New user: " + name)
87 user.state = "checking_new_account_name"
89 # if the user entered nothing for a name, then buhbye
91 user.state = "disconnecting"
93 def handler_checking_password(user):
94 """Handle the login account password."""
96 # get the next waiting line of input
97 input_data = user.input_queue.pop(0)
99 # does the hashed input equal the stored hash?
100 if md5.new(user.account.get("name") + input_data).hexdigest() == user.account.get("passhash"):
102 # if so, set the username and load from cold storage
103 if not user.replace_old_connections():
105 user.state = "main_utility"
107 # if at first your hashes don't match, try, try again
108 elif user.password_tries < muffuniv.universe.categories["internal"]["general"].getint("password_tries"):
109 user.password_tries += 1
110 user.error = "incorrect"
112 # we've exceeded the maximum number of password failures, so disconnect
114 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
115 user.state = "disconnecting"
117 def handler_checking_new_account_name(user):
118 """Handle input for the new user menu."""
120 # get the next waiting line of input
121 input_data = user.input_queue.pop(0)
123 # if there's input, take the first character and lowercase it
125 choice = input_data.lower()[0]
127 # if there's no input, use the default
129 choice = muffmenu.get_default_menu_choice(user.state)
131 # user selected to disconnect
133 user.account.delete()
134 user.state = "disconnecting"
136 # go back to the login screen
138 user.account.delete()
139 user.state = "entering_account_name"
141 # new user, so ask for a password
143 user.state = "entering_new_password"
145 # user entered a non-existent option
147 user.error = "default"
149 def handler_entering_new_password(user):
150 """Handle a new password entry."""
152 # get the next waiting line of input
153 input_data = user.input_queue.pop(0)
155 # make sure the password is strong--at least one upper, one lower and
156 # one digit, seven or more characters in length
157 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)):
159 # hash and store it, then move on to verification
160 user.account.set("passhash", md5.new(user.account.get("name") + input_data).hexdigest())
161 user.state = "verifying_new_password"
163 # the password was weak, try again if you haven't tried too many times
164 elif user.password_tries < muffuniv.universe.categories["internal"]["general"].getint("password_tries"):
165 user.password_tries += 1
168 # too many tries, so adios
170 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
171 user.account.delete()
172 user.state = "disconnecting"
174 def handler_verifying_new_password(user):
175 """Handle the re-entered new password for verification."""
177 # get the next waiting line of input
178 input_data = user.input_queue.pop(0)
180 # hash the input and match it to storage
181 if md5.new(user.account.get("name") + input_data).hexdigest() == user.account.get("passhash"):
184 # the hashes matched, so go active
185 if not user.replace_old_connections(): user.state = "main_utility"
187 # go back to entering the new password as long as you haven't tried
189 elif user.password_tries < muffuniv.universe.categories["internal"]["general"].getint("password_tries"):
190 user.password_tries += 1
191 user.error = "differs"
192 user.state = "entering_new_password"
194 # otherwise, sayonara
196 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
197 user.account.delete()
198 user.state = "disconnecting"
200 def handler_active(user):
201 """Handle input for active users."""
203 # get the next waiting line of input
204 input_data = user.input_queue.pop(0)
206 # split out the command (first word) and parameters (everything else)
207 if input_data.find(" ") > 0:
208 command, parameters = input_data.split(" ", 1)
213 # lowercase the command
214 command = command.lower()
216 # the command matches a command word for which we have data
217 if command in muffuniv.universe.categories["command"]:
218 exec(muffuniv.universe.categories["command"][command].get("action"))
220 # no data matching the entered command word
221 elif command: command_error(user, command, parameters)
223 def command_halt(user, command="", parameters=""):
224 """Halt the world."""
226 # see if there's a message or use a generic one
227 if parameters: message = "Halting: " + parameters
228 else: message = "User " + user.account.get("name") + " halted the world."
231 muffmisc.broadcast(message)
232 muffmisc.log(message)
234 # set a flag to terminate the world
235 muffvars.terminate_world = True
237 def command_reload(user, command="", parameters=""):
238 """Reload all code modules, configs and data."""
240 # let the user know and log
241 user.send("Reloading all code modules, configs and data.")
242 muffmisc.log("User " + user.account.get("name") + " reloaded the world.")
244 # set a flag to reload
245 muffvars.reload_modules = True
247 def command_quit(user, command="", parameters=""):
248 """Quit the world."""
249 user.state = "disconnecting"
251 def command_help(user, command="", parameters=""):
252 """List available commands and provide help for commands."""
254 # did the user ask for help on a specific command word?
257 # is the command word one for which we have data?
258 if parameters in muffuniv.universe.categories["command"]:
260 # add a description if provided
261 description = muffuniv.universe.categories["command"][parameters].get("description")
263 description = "(no short description provided)"
264 output = "$(grn)" + parameters + "$(nrm) - " + description + "$(eol)$(eol)"
266 # add the help text if provided
267 help_text = muffuniv.universe.categories["command"][parameters].get("help")
269 help_text = "No help is provided for this command."
272 # no data for the requested command word
274 output = "That is not an available command."
276 # no specific command word was indicated
279 # give a sorted list of commands with descriptions if provided
280 output = "These are the commands available to you:$(eol)$(eol)"
281 sorted_commands = muffuniv.universe.categories["command"].keys()
282 sorted_commands.sort()
283 for item in sorted_commands:
284 description = muffuniv.universe.categories["command"][item].get("description")
286 description = "(no short description provided)"
287 output += " $(grn)" + item + "$(nrm) - " + description + "$(eol)"
288 output += "$(eol)Enter \"help COMMAND\" for help on a command named \"COMMAND\"."
290 # send the accumulated output to the user
293 def command_say(user, command="", parameters=""):
294 """Speak to others in the same room."""
296 # check for replacement macros
297 if muffmisc.replace_macros(user, parameters, True) != parameters:
298 user.send("You cannot speak $_(replacement macros).")
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 # a dictionary of punctuation:action pairs
309 for facet in muffuniv.universe.categories["internal"]["language"].facets():
310 if facet.startswith("punctuation_"):
311 action = facet.split("_")[1]
312 for mark in muffuniv.universe.categories["internal"]["language"].get(facet).split():
313 actions[mark] = action
315 # match the punctuation used, if any, to an action
316 default_punctuation = muffuniv.universe.categories["internal"]["language"].get("default_punctuation")
317 action = actions[default_punctuation]
318 for mark in actions.keys():
319 if message.endswith(mark) and mark != default_punctuation:
320 action = actions[mark]
323 # if the action is default and there is no mark, add one
324 if action == actions[default_punctuation] and not message.endswith(default_punctuation):
325 message += default_punctuation
327 # capitalize a list of words within the message
328 capitalize = muffuniv.universe.categories["internal"]["language"].get("capitalize").split()
329 for word in capitalize:
330 message = message.replace(" " + word + " ", " " + word.capitalize() + " ")
333 # TODO: we won't be using broadcast once there are actual rooms
334 muffmisc.broadcast(user.account.get("name") + " " + action + "s, \"" + message + "\"")
336 # there was no message
338 user.send("What do you want to say?")
340 def command_show(user, command="", parameters=""):
341 """Show program data."""
342 if parameters == "avatars":
343 message = "These are the avatars managed by your account:$(eol)"
344 avatars = user.list_avatar_names()
346 for avatar in avatars: message += "$(eol) $(grn)" + avatar + "$(nrm)"
347 elif parameters == "files":
348 message = "These are the current files containing the universe:$(eol)"
349 keys = muffuniv.universe.files.keys()
351 for key in keys: message += "$(eol) $(grn)" + key + "$(nrm)"
352 elif parameters == "universe":
353 message = "These are the current elements in the universe:$(eol)"
354 keys = muffuniv.universe.contents.keys()
356 for key in keys: message += "$(eol) $(grn)" + key + "$(nrm)"
357 elif parameters == "time":
358 message = muffuniv.universe.categories["internal"]["counters"].get("elapsed") + " increments elapsed since the world was created."
359 elif parameters: message = "I don't know what \"" + parameters + "\" is."
360 else: message = "What do you want to show?"
363 def command_error(user, command="", parameters=""):
364 """Generic error for an unrecognized command word."""
366 # 90% of the time use a generic error
367 if random.randrange(10):
368 message = "I'm not sure what \"" + command
370 message += " " + parameters
371 message += "\" means..."
373 # 10% of the time use the classic diku error
375 message = "Arglebargle, glop-glyf!?!"
377 # send the error message