da061fa30d3d616aee0bb71083c08357aefdec3f
[mudpy.git] / muff / muffcmds.py
1 """Command objects for the MUFF Engine"""
2
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.
5
6 # command data like descriptions, help text, limits, et cetera, are stored in
7 # ini-style configuration files supported by the ConfigParser module
8 import ConfigParser
9
10 # md5 hashing is used for verification of account passwords
11 import md5
12
13 # the os module is used to we can get a directory listing and build lists of
14 # multiple config files in one directory
15 import os
16
17 # the random module is useful for creating random conditional output
18 import random
19
20 # string.split is used extensively to tokenize user input (break up command
21 # names and parameters)
22 import string
23
24 # bit of a hack to load all modules in the muff package
25 import muff
26 for module in muff.__all__:
27         exec("import " + module)
28
29 def handle_user_input(user):
30         """The main handler, branches to a state-specific handler."""
31
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)")
35         else:
36                 generic_menu_handler(user)
37
38         # since we got input, flag that the menu/prompt needs to be redisplayed
39         user.menu_seen = False
40
41         # if the user's client echo is off, send a blank line for aesthetics
42         if not user.echoing: user.send("", "")
43
44 def generic_menu_handler(user):
45         """A generic menu choice handler."""
46
47         # get a lower-case representation of the next line of input
48         if user.input_queue:
49                 choice = user.input_queue.pop(0)
50                 if choice: choice = choice.lower()
51         else: choice = ""
52
53         # run any script related to this choice
54         exec(muffmenu.get_choice_action(user, choice))
55
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"
60
61 def handler_entering_account_name(user):
62         """Handle the login account name."""
63
64         # get the next waiting line of input
65         input_data = user.input_queue.pop(0)
66
67         # did the user enter anything?
68         if input_data:
69                 
70                 # keep only the first word and convert to lower-case
71                 name = input_data.lower()
72
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"
76
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"
81
82                 # otherwise, this could be a brand new user
83                 else:
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"
88
89         # if the user entered nothing for a name, then buhbye
90         else:
91                 user.state = "disconnecting"
92
93 def handler_checking_password(user):
94         """Handle the login account password."""
95
96         # get the next waiting line of input
97         input_data = user.input_queue.pop(0)
98
99         # does the hashed input equal the stored hash?
100         if md5.new(user.account.get("name") + input_data).hexdigest() == user.account.get("passhash"):
101
102                 # if so, set the username and load from cold storage
103                 if not user.replace_old_connections():
104                         user.authenticate()
105                         user.state = "main_utility"
106
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"
111
112         # we've exceeded the maximum number of password failures, so disconnect
113         else:
114                 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
115                 user.state = "disconnecting"
116
117 def handler_checking_new_account_name(user):
118         """Handle input for the new user menu."""
119
120         # get the next waiting line of input
121         input_data = user.input_queue.pop(0)
122
123         # if there's input, take the first character and lowercase it
124         if input_data:
125                 choice = input_data.lower()[0]
126
127         # if there's no input, use the default
128         else:
129                 choice = muffmenu.get_default_menu_choice(user.state)
130
131         # user selected to disconnect
132         if choice == "d":
133                 user.account.delete()
134                 user.state = "disconnecting"
135
136         # go back to the login screen
137         elif choice == "g":
138                 user.account.delete()
139                 user.state = "entering_account_name"
140
141         # new user, so ask for a password
142         elif choice == "n":
143                 user.state = "entering_new_password"
144
145         # user entered a non-existent option
146         else:
147                 user.error = "default"
148
149 def handler_entering_new_password(user):
150         """Handle a new password entry."""
151
152         # get the next waiting line of input
153         input_data = user.input_queue.pop(0)
154
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)):
158
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"
162
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
166                 user.error = "weak"
167
168         # too many tries, so adios
169         else:
170                 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
171                 user.account.delete()
172                 user.state = "disconnecting"
173
174 def handler_verifying_new_password(user):
175         """Handle the re-entered new password for verification."""
176
177         # get the next waiting line of input
178         input_data = user.input_queue.pop(0)
179
180         # hash the input and match it to storage
181         if md5.new(user.account.get("name") + input_data).hexdigest() == user.account.get("passhash"):
182                 user.authenticate()
183
184                 # the hashes matched, so go active
185                 if not user.replace_old_connections(): user.state = "main_utility"
186
187         # go back to entering the new password as long as you haven't tried
188         # too many times
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"
193
194         # otherwise, sayonara
195         else:
196                 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
197                 user.account.delete()
198                 user.state = "disconnecting"
199
200 def handler_active(user):
201         """Handle input for active users."""
202
203         # get the next waiting line of input
204         input_data = user.input_queue.pop(0)
205
206         # split out the command (first word) and parameters (everything else)
207         if input_data.find(" ") > 0:
208                 command, parameters = input_data.split(" ", 1)
209         else:
210                 command = input_data
211                 parameters = ""
212
213         # lowercase the command
214         command = command.lower()
215
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"))
219
220         # no data matching the entered command word
221         elif command: command_error(user, command, parameters)
222
223 def command_halt(user, command="", parameters=""):
224         """Halt the world."""
225
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."
229
230         # let everyone know
231         muffmisc.broadcast(message)
232         muffmisc.log(message)
233
234         # set a flag to terminate the world
235         muffvars.terminate_world = True
236
237 def command_reload(user, command="", parameters=""):
238         """Reload all code modules, configs and data."""
239
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.")
243
244         # set a flag to reload
245         muffvars.reload_modules = True
246
247 def command_quit(user, command="", parameters=""):
248         """Quit the world."""
249         user.state = "disconnecting"
250
251 def command_help(user, command="", parameters=""):
252         """List available commands and provide help for commands."""
253
254         # did the user ask for help on a specific command word?
255         if parameters:
256
257                 # is the command word one for which we have data?
258                 if parameters in muffuniv.universe.categories["command"]:
259
260                         # add a description if provided
261                         description = muffuniv.universe.categories["command"][parameters].get("description")
262                         if not description:
263                                 description = "(no short description provided)"
264                         output = "$(grn)" + parameters + "$(nrm) - " + description + "$(eol)$(eol)"
265
266                         # add the help text if provided
267                         help_text = muffuniv.universe.categories["command"][parameters].get("help")
268                         if not help_text:
269                                 help_text = "No help is provided for this command."
270                         output += help_text
271
272                 # no data for the requested command word
273                 else:
274                         output = "That is not an available command."
275
276         # no specific command word was indicated
277         else:
278
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")
285                         if not 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\"."
289
290         # send the accumulated output to the user
291         user.send(output)
292
293 def command_say(user, command="", parameters=""):
294         """Speak to others in the same room."""
295
296         # check for replacement macros
297         if muffmisc.replace_macros(user, parameters, True) != parameters:
298                 user.send("You cannot speak $_(replacement macros).")
299
300         # the user entered a message
301         elif parameters:
302
303                 # get rid of quote marks on the ends of the message and
304                 # capitalize the first letter
305                 message = parameters.strip("\"'`").capitalize()
306
307                 # a dictionary of punctuation:action pairs
308                 actions = {}
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
314
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]
321                                 break
322
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
326
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() + " ")
331
332                 # tell the room
333                 # TODO: we won't be using broadcast once there are actual rooms
334                 muffmisc.broadcast(user.account.get("name") + " " + action + "s, \"" + message + "\"")
335
336         # there was no message
337         else:
338                 user.send("What do you want to say?")
339
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()
345                 avatars.sort()
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()
350                 keys.sort()
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()
355                 keys.sort()
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?"
361         user.send(message)
362
363 def command_error(user, command="", parameters=""):
364         """Generic error for an unrecognized command word."""
365
366         # 90% of the time use a generic error
367         if random.randrange(10):
368                 message = "I'm not sure what \"" + command
369                 if parameters:
370                         message += " " + parameters
371                 message += "\" means..."
372
373         # 10% of the time use the classic diku error
374         else:
375                 message = "Arglebargle, glop-glyf!?!"
376
377         # send the error message
378         user.send(message)
379