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.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.get("files", "commands")
39 command_files_index = ConfigParser.SafeConfigParser()
40 command_files_index.read(command_path + "/index")
42 for each_file in command_files_index.get("index", "files").split():
43 command_files.append(command_path + "/" + each_file)
45 # read the command data files
46 command_data = ConfigParser.SafeConfigParser()
47 command_data.read(command_files)
49 # this creates a list of commands mentioned in the data files
50 command_list = command_data.sections()
52 def handle_user_input(user):
53 """The main handler, branches to a state-specific handler."""
55 # check to make sure the state is expected, then call that handler
57 exec("handler_" + user.state + "(user)")
59 generic_menu_handler(user)
61 # since we got input, flag that the menu/prompt needs to be redisplayed
62 user.menu_seen = False
64 # if the user's client echo is off, send a blank line for aesthetics
65 if not user.echoing: user.send("", "")
67 def generic_menu_handler(user):
68 """A generic menu choice handler."""
70 # get a lower-case representation of the next line of input
71 choice = user.input_queue.pop(0)
72 if choice: choice = choice.lower()
74 # run any script related to this choice
75 exec(muffmenu.get_choice_action(user, choice))
77 # move on to the next state or return an error
78 new_state = muffmenu.get_choice_branch(user, choice)
79 if new_state: user.state = new_state
80 else: user.error = "default"
82 def handler_entering_account_name(user):
83 """Handle the login account name."""
85 # get the next waiting line of input
86 input_data = user.input_queue.pop(0)
88 # did the user enter anything?
91 # keep only the first word and convert to lower-case
92 user.proposed_name = string.split(input_data)[0].lower()
94 # if we have a password hash, time to request a password
95 if user.get_passhash():
96 user.state = "checking_password"
98 # otherwise, this could be a brand new user
100 user.name = user.proposed_name
101 user.proposed_name = None
103 muffmisc.log("New user: " + user.name)
104 user.state = "checking_new_account_name"
106 # if the user entered nothing for a name, then buhbye
108 user.state = "disconnecting"
110 def handler_checking_password(user):
111 """Handle the login account password."""
113 # get the next waiting line of input
114 input_data = user.input_queue.pop(0)
116 # does the hashed input equal the stored hash?
117 if md5.new(user.proposed_name + input_data).hexdigest() == user.passhash:
119 # if so, set the username and load from cold storage
120 user.name = user.proposed_name
121 del(user.proposed_name)
122 if not user.replace_old_connections():
125 user.state = "main_utility"
127 # if at first your hashes don't match, try, try again
128 elif user.password_tries < muffconf.getint("general", "password_tries"):
129 user.password_tries += 1
130 user.error = "incorrect"
132 # we've exceeded the maximum number of password failures, so disconnect
134 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
135 user.state = "disconnecting"
137 def handler_checking_new_account_name(user):
138 """Handle input for the new user menu."""
140 # get the next waiting line of input
141 input_data = user.input_queue.pop(0)
143 # if there's input, take the first character and lowercase it
145 choice = input_data.lower()[0]
147 # if there's no input, use the default
149 choice = muffmenu.get_default_menu_choice(user.state)
151 # user selected to disconnect
153 user.state = "disconnecting"
155 # go back to the login screen
157 user.state = "entering_account_name"
159 # new user, so ask for a password
161 user.state = "entering_new_password"
163 # user entered a non-existent option
165 user.error = "default"
167 def handler_entering_new_password(user):
168 """Handle a new password entry."""
170 # get the next waiting line of input
171 input_data = user.input_queue.pop(0)
173 # make sure the password is strong--at least one upper, one lower and
174 # one digit, seven or more characters in length
175 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)):
177 # hash and store it, then move on to verification
178 user.passhash = md5.new(user.name + input_data).hexdigest()
179 user.state = "verifying_new_password"
181 # the password was weak, try again if you haven't tried too many times
182 elif user.password_tries < muffconf.getint("general", "password_tries"):
183 user.password_tries += 1
186 # too many tries, so adios
188 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
189 user.state = "disconnecting"
191 def handler_verifying_new_password(user):
192 """Handle the re-entered new password for verification."""
194 # get the next waiting line of input
195 input_data = user.input_queue.pop(0)
197 # hash the input and match it to storage
198 if md5.new(user.name + input_data).hexdigest() == user.passhash:
202 # the hashes matched, so go active
203 if not user.replace_old_connections(): user.state = "main_utility"
205 # go back to entering the new password as long as you haven't tried
207 elif user.password_tries < muffconf.getint("general", "password_tries"):
208 user.password_tries += 1
209 user.error = "differs"
210 user.state = "entering_new_password"
212 # otherwise, sayonara
214 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
215 user.state = "disconnecting"
217 def handler_active(user):
218 """Handle input for active users."""
220 # get the next waiting line of input
221 input_data = user.input_queue.pop(0)
223 # split out the command (first word) and parameters (everything else)
225 inputlist = string.split(input_data, None, 1)
226 command = inputlist[0]
230 parameters = inputlist[1]
235 # lowercase the command
236 command = command.lower()
238 # the command matches a command word for which we have data
239 if command in command_list: exec("command_" + command + "(user, command, parameters)")
241 # no data matching the entered command word
242 elif command: command_error(user, command, parameters)
244 def command_halt(user, command="", parameters=""):
245 """Halt the world."""
247 # see if there's a message or use a generic one
248 if parameters: message = "Halting: " + parameters
249 else: message = "User " + user.name + " halted the world."
252 muffmisc.broadcast(message)
253 muffmisc.log(message)
255 # set a flag to terminate the world
256 muffvars.terminate_world = True
258 def command_reload(user, command="", parameters=""):
259 """Reload all code modules, configs and data."""
261 # let the user know and log
262 user.send("Reloading all code modules, configs and data.")
263 muffmisc.log("User " + user.name + " reloaded the world.")
265 # set a flag to reload
266 muffvars.reload_modules = True
268 def command_quit(user, command="", parameters=""):
269 """Quit the world."""
270 user.state = "disconnecting"
272 def command_time(user, command="", parameters=""):
273 """Show the current world time in elapsed increments."""
274 user.send(muffmisc.repr_long(muffmisc.getlong(muffvars.variable_data,
275 "time", "elapsed")) + " increments elapsed since the world was created.")
277 def command_help(user, command="", parameters=""):
278 """List available commands and provide help for commands."""
280 # did the user ask for help on a specific command word?
283 # is the command word one for which we have data?
284 if parameters in command_list:
286 # add a description if provided
288 description = command_data.get(parameters, "description")
290 description = "(no short description provided)"
291 output = "$(grn)" + parameters + "$(nrm) - " + command_data.get(parameters, "description") + "$(eol)$(eol)"
293 # add the help text if provided
295 help_text = command_data.get(parameters, "help")
297 help_text = "No help is provided for this command."
300 # no data for the requested command word
302 output = "That is not an available command."
304 # no specific command word was indicated
307 # give a sorted list of commands with descriptions if provided
308 output = "These are the commands available to you:$(eol)$(eol)"
309 sorted_commands = command_list
310 sorted_commands.sort()
311 for item in sorted_commands:
313 description = command_data.get(item, "description")
315 description = "(no short description provided)"
316 output += " $(grn)" + item + "$(nrm) - " + command_data.get(item, "description") + "$(eol)"
317 output += "$(eol)Enter \"help COMMAND\" for help on a command named \"COMMAND\"."
319 # send the accumulated output to the user
322 def command_say(user, command="", parameters=""):
323 """Speak to others in the same room."""
325 # check for replacement macros
326 if muffmisc.replace_macros(user, parameters, True) != parameters:
327 user.send("You cannot speak $_(replacement macros).")
329 # the user entered a message
332 # get rid of quote marks on the ends of the message and
333 # capitalize the first letter
334 message = parameters.strip("\"'`").capitalize()
336 # a dictionary of punctuation:action pairs
338 for option in muffconf.config_data.options("language"):
339 if option.startswith("punctuation_"):
340 action = option.split("_")[1]
341 for mark in muffconf.config_data.get("language", option).split():
342 actions[mark] = action
344 # set the default action
345 action = actions[muffconf.config_data.get("language", "default_punctuation")]
347 # match the punctuation used, if any, to an action
348 default_punctuation = muffconf.config_data.get("language", "default_punctuation")
349 for mark in actions.keys():
350 if message.endswith(mark) and mark != default_punctuation:
351 action = actions[mark]
354 # if the action is default and there is no mark, add one
355 if action == actions[default_punctuation] and not message.endswith(default_punctuation):
356 message += default_punctuation
358 # capitalize a list of words within the message
359 capitalize = muffconf.get("language", "capitalize").split()
360 for word in capitalize:
361 message = message.replace(" " + word + " ", " " + word.capitalize() + " ")
364 # TODO: we won't be using broadcast once there are actual rooms
365 muffmisc.broadcast(user.name + " " + action + "s, \"" + message + "\"")
367 # there was no message
369 user.send("What do you want to say?")
371 def command_show(user, command="", parameters=""):
372 """Show program data."""
373 if parameters == "universe":
374 message = "These are the current elements in the universe:$(eol)"
375 keys = muffuniv.universe.contents.keys()
377 for key in keys: message += "$(eol) $(grn)" + key + "$(nrm)"
378 elif parameters == "avatars":
379 message = "These are the avatars managed by your account:$(eol)"
380 avatars = user.list_avatar_names()
382 for avatar in avatars: message += "$(eol) $(grn)" + avatar + "$(nrm)"
383 elif parameters: message = "I don't know what \"" + parameters + "\" is."
384 else: message = "What do you want to show?"
387 def command_error(user, command="", parameters=""):
388 """Generic error for an unrecognized command word."""
390 # 90% of the time use a generic error
391 if random.randrange(10):
392 message = "I'm not sure what \"" + command
394 message += " " + parameters
395 message += "\" means..."
397 # 10% of the time use the classic diku error
399 message = "Arglebargle, glop-glyf!?!"
401 # send the error message