Imported from archive.
[mudpy.git] / lib / 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 # does the files:commands setting exist yet?
30 try:
31         if muffconf.get("files", "commands"): pass
32
33 # if not, reload the muffconf module
34 except AttributeError:
35         reload(muffconf)
36
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")
41 command_files = []
42 for each_file in command_files_index.get("index", "files").split():
43         command_files.append(command_path + "/" + each_file)
44
45 # read the command data files
46 command_data = ConfigParser.SafeConfigParser()
47 command_data.read(command_files)
48
49 # this creates a list of commands mentioned in the data files
50 command_list = command_data.sections()
51
52 def handle_user_input(user):
53         """The main handler, branches to a state-specific handler."""
54
55         # check to make sure the state is expected, then call that handler
56         exec("handler_" + user.state.replace(" ", "_") + "(user)")
57
58         # since we got input, flag that the menu/prompt needs to be redisplayed
59         user.menu_seen = False
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                 user.proposed_name = string.split(input_data)[0].lower()
72
73                 # if we have a password hash, time to request a password
74                 if user.get_passhash():
75                         user.state = "checking password"
76
77                 # otherwise, this could be a brand new user
78                 else:
79                         user.name = user.proposed_name
80                         user.proposed_name = None
81                         user.load()
82                         muffmisc.log("New user: " + user.name)
83                         user.state = "checking new account name"
84
85         # if the user entered nothing for a name, then buhbye
86         else:
87                 user.state = "disconnecting"
88
89 def handler_checking_password(user):
90         """Handle the login account password."""
91
92         # get the next waiting line of input
93         input_data = user.input_queue.pop(0)
94
95         # does the hashed input equal the stored hash?
96         if md5.new(user.proposed_name + input_data).hexdigest() == user.passhash:
97
98                 # if so, set the username and load from cold storage
99                 # TODO: branch to character creation and selection menus
100                 user.name = user.proposed_name
101                 del(user.proposed_name)
102                 if not user.replace_old_connections():
103                         user.load()
104                         user.authenticate()
105                         user.state = "active"
106
107         # if at first your hashes don't match, try, try again
108         elif user.password_tries < muffconf.getint("general", "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_disconnecting(user):
118         """Waiting for the user's connection to close."""
119         pass
120
121 def handler_disconnecting_duplicates(user):
122         """Waiting for duplicate connections to close."""
123         pass
124
125 def handler_checking_new_account_name(user):
126         """Handle input for the new user menu."""
127
128         # get the next waiting line of input
129         input_data = user.input_queue.pop(0)
130
131         # if there's input, take the first character and lowercase it
132         if input_data:
133                 choice = input_data.lower()[0]
134
135         # if there's no input, use the default
136         else:
137                 choice = muffmenu.get_default(user)
138
139         # user selected to disconnect
140         if choice == "d":
141                 user.state = "disconnecting"
142
143         # go back to the login screen
144         elif choice == "g":
145                 user.state = "entering account name"
146
147         # new user, so ask for a password
148         elif choice == "n":
149                 user.state = "entering new password"
150
151         # user entered a non-existent option
152         else:
153                 user.error = "default"
154
155 def handler_entering_new_password(user):
156         """Handle a new password entry."""
157
158         # get the next waiting line of input
159         input_data = user.input_queue.pop(0)
160
161         # make sure the password is strong--at least one upper, one lower and
162         # one digit, seven or more characters in length
163         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)):
164
165                 # hash and store it, then move on to verification
166                 user.passhash = md5.new(user.name + input_data).hexdigest()
167                 user.state = "verifying new password"
168
169         # the password was weak, try again if you haven't tried too many times
170         elif user.password_tries < muffconf.getint("general", "password_tries"):
171                 user.password_tries += 1
172                 user.error = "weak"
173
174         # too many tries, so adios
175         else:
176                 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
177                 user.state = "disconnecting"
178
179 def handler_verifying_new_password(user):
180         """Handle the re-entered new password for verification."""
181
182         # get the next waiting line of input
183         input_data = user.input_queue.pop(0)
184
185         # hash the input and match it to storage
186         if md5.new(user.name + input_data).hexdigest() == user.passhash:
187                 user.authenticate()
188                 user.save()
189
190                 # the hashes matched, so go active
191                 # TODO: branch to character creation and selection menus
192                 if not user.replace_old_connections(): user.state = "active"
193
194         # go back to entering the new password as long as you haven't tried
195         # too many times
196         elif user.password_tries < muffconf.getint("general", "password_tries"):
197                 user.password_tries += 1
198                 user.error = "differs"
199                 user.state = "entering new password"
200
201         # otherwise, sayonara
202         else:
203                 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
204                 user.state = "disconnecting"
205
206 def handler_active(user):
207         """Handle input for active users."""
208
209         # get the next waiting line of input
210         input_data = user.input_queue.pop(0)
211
212         # split out the command (first word) and parameters (everything else)
213         try:
214                 inputlist = string.split(input_data, None, 1)
215                 command = inputlist[0]
216         except:
217                 command = input_data
218         try:
219                 parameters = inputlist[1]
220         except:
221                 parameters = ""
222         del(inputlist)
223
224         # lowercase the command
225         command = command.lower()
226
227         # the command matches a command word for which we have data
228         if command in command_list: exec("command_" + command + "(user, command, parameters)")
229
230         # no data matching the entered command word
231         elif command: command_error(user, command, parameters)
232
233 def command_halt(user, command="", parameters=""):
234         """Halt the world."""
235
236         # see if there's a message or use a generic one
237         if parameters: message = "Halting: " + parameters
238         else: message = "User " + user.name + " halted the world."
239
240         # let everyone know
241         muffmisc.broadcast(message)
242         muffmisc.log(message)
243
244         # set a flag to terminate the world
245         muffvars.terminate_world = True
246
247 def command_reload(user, command="", parameters=""):
248         """Reload all code modules, configs and data."""
249
250         # let the user know and log
251         user.send("Reloading all code modules, configs and data.")
252         muffmisc.log("User " + user.name + " reloaded the world.")
253
254         # set a flag to reload
255         muffvars.reload_modules = True
256
257 def command_quit(user, command="", parameters=""):
258         """Quit the world."""
259         user.state = "disconnecting"
260
261 def command_time(user, command="", parameters=""):
262         """Show the current world time in elapsed increments."""
263         user.send(muffmisc.repr_long(muffmisc.getlong(muffvars.variable_data,
264                 "time", "elapsed")) + " increments elapsed since the world was created.")
265
266 def command_help(user, command="", parameters=""):
267         """List available commands and provide help for commands."""
268
269         # did the user ask for help on a specific command word?
270         if parameters:
271
272                 # is the command word one for which we have data?
273                 if parameters in command_list:
274
275                         # add a description if provided
276                         try:
277                                 description = command_data.get(parameters, "description")
278                         except:
279                                 description = "(no short description provided)"
280                         output = "$(grn)" + parameters + "$(nrm) - " + command_data.get(parameters, "description") + "$(eol)$(eol)"
281
282                         # add the help text if provided
283                         try:
284                                 help_text = command_data.get(parameters, "help")
285                         except:
286                                 help_text = "No help is provided for this command."
287                         output += help_text
288
289                 # no data for the requested command word
290                 else:
291                         output = "That is not an available command."
292
293         # no specific command word was indicated
294         else:
295
296                 # give a sorted list of commands with descriptions if provided
297                 output = "These are the commands available to you:$(eol)$(eol)"
298                 sorted_commands = command_list
299                 sorted_commands.sort()
300                 for item in sorted_commands:
301                         try:
302                                 description = command_data.get(item, "description")
303                         except:
304                                 description = "(no short description provided)"
305                         output += "   $(grn)" + item + "$(nrm) - " + command_data.get(item, "description") + "$(eol)"
306                 output += "$(eol)Enter \"help COMMAND\" for help on a command named \"COMMAND\"."
307
308         # send the accumulated output to the user
309         user.send(output)
310
311 def command_say(user, command="", parameters=""):
312         """Speak to others in the same room."""
313
314         # check for replacement macros
315         if muffmisc.replace_macros(user, parameters, True) != parameters:
316                 user.send("You cannot speak $_(replacement macros).")
317
318         # the user entered a message
319         elif parameters:
320
321                 # get rid of quote marks on the ends of the message and
322                 # capitalize the first letter
323                 message = parameters.strip("\"'`").capitalize()
324
325                 # a dictionary of punctuation:action pairs
326                 actions = {}
327                 for option in muffconf.config_data.options("language"):
328                         if option.startswith("punctuation_"):
329                                 action = option.split("_")[1]
330                                 for mark in muffconf.config_data.get("language", option).split():
331                                                 actions[mark] = action
332
333                 # set the default action
334                 action = actions[muffconf.config_data.get("language", "default_punctuation")]
335
336                 # match the punctuation used, if any, to an action
337                 default_punctuation = muffconf.config_data.get("language", "default_punctuation")
338                 for mark in actions.keys():
339                         if message.endswith(mark) and mark != default_punctuation:
340                                 action = actions[mark]
341                                 break
342
343                 # if the action is default and there is no mark, add one
344                 if action == actions[default_punctuation] and not message.endswith(default_punctuation):
345                         message += default_punctuation
346
347                 # capitalize a list of words within the message
348                 capitalize = muffconf.get("language", "capitalize").split()
349                 for word in capitalize:
350                         message = message.replace(" " + word + " ", " " + word.capitalize() + " ")
351
352                 # tell the room
353                 # TODO: we won't be using broadcast once there are actual rooms
354                 muffmisc.broadcast(user.name + " " + action + "s, \"" + message + "\"")
355
356         # there was no message
357         else:
358                 user.send("What do you want to say?")
359
360 def command_error(user, command="", parameters=""):
361         """Generic error for an unrecognized command word."""
362
363         # 90% of the time use a generic error
364         if random.randrange(10):
365                 message = "I'm not sure what \"" + command
366                 if parameters:
367                         message += " " + parameters
368                 message += "\" means..."
369
370         # 10% of the time use the classic diku error
371         else:
372                 message = "Arglebargle, glop-glyf!?!"
373
374         # send the error message
375         user.send(message)
376