c640c969d29ddb75f1b29ef651cff9fb97189892
[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         try:
57                 exec("handler_" + user.state + "(user)")
58         except NameError:
59                 generic_menu_handler(user)
60
61         # since we got input, flag that the menu/prompt needs to be redisplayed
62         user.menu_seen = False
63
64         # if the user's client echo is off, send a blank line for aesthetics
65         if not user.echoing: user.send("", "")
66
67 def generic_menu_handler(user):
68         """A generic menu choice handler."""
69
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()
73
74         # run any script related to this choice
75         exec(muffmenu.get_choice_action(user, choice))
76
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"
81
82 def handler_entering_account_name(user):
83         """Handle the login account name."""
84
85         # get the next waiting line of input
86         input_data = user.input_queue.pop(0)
87
88         # did the user enter anything?
89         if input_data:
90                 
91                 # keep only the first word and convert to lower-case
92                 user.proposed_name = string.split(input_data)[0].lower()
93
94                 # if we have a password hash, time to request a password
95                 if user.get_passhash():
96                         user.state = "checking_password"
97
98                 # otherwise, this could be a brand new user
99                 else:
100                         user.name = user.proposed_name
101                         user.proposed_name = None
102                         user.load()
103                         muffmisc.log("New user: " + user.name)
104                         user.state = "checking_new_account_name"
105
106         # if the user entered nothing for a name, then buhbye
107         else:
108                 user.state = "disconnecting"
109
110 def handler_checking_password(user):
111         """Handle the login account password."""
112
113         # get the next waiting line of input
114         input_data = user.input_queue.pop(0)
115
116         # does the hashed input equal the stored hash?
117         if md5.new(user.proposed_name + input_data).hexdigest() == user.passhash:
118
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():
123                         user.load()
124                         user.authenticate()
125                         user.state = "main_utility"
126
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"
131
132         # we've exceeded the maximum number of password failures, so disconnect
133         else:
134                 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
135                 user.state = "disconnecting"
136
137 def handler_checking_new_account_name(user):
138         """Handle input for the new user menu."""
139
140         # get the next waiting line of input
141         input_data = user.input_queue.pop(0)
142
143         # if there's input, take the first character and lowercase it
144         if input_data:
145                 choice = input_data.lower()[0]
146
147         # if there's no input, use the default
148         else:
149                 choice = muffmenu.get_default_menu_choice(user.state)
150
151         # user selected to disconnect
152         if choice == "d":
153                 user.state = "disconnecting"
154
155         # go back to the login screen
156         elif choice == "g":
157                 user.state = "entering_account_name"
158
159         # new user, so ask for a password
160         elif choice == "n":
161                 user.state = "entering_new_password"
162
163         # user entered a non-existent option
164         else:
165                 user.error = "default"
166
167 def handler_entering_new_password(user):
168         """Handle a new password entry."""
169
170         # get the next waiting line of input
171         input_data = user.input_queue.pop(0)
172
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)):
176
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"
180
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
184                 user.error = "weak"
185
186         # too many tries, so adios
187         else:
188                 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
189                 user.state = "disconnecting"
190
191 def handler_verifying_new_password(user):
192         """Handle the re-entered new password for verification."""
193
194         # get the next waiting line of input
195         input_data = user.input_queue.pop(0)
196
197         # hash the input and match it to storage
198         if md5.new(user.name + input_data).hexdigest() == user.passhash:
199                 user.authenticate()
200                 user.save()
201
202                 # the hashes matched, so go active
203                 if not user.replace_old_connections(): user.state = "main_utility"
204
205         # go back to entering the new password as long as you haven't tried
206         # too many times
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"
211
212         # otherwise, sayonara
213         else:
214                 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
215                 user.state = "disconnecting"
216
217 def handler_active(user):
218         """Handle input for active users."""
219
220         # get the next waiting line of input
221         input_data = user.input_queue.pop(0)
222
223         # split out the command (first word) and parameters (everything else)
224         try:
225                 inputlist = string.split(input_data, None, 1)
226                 command = inputlist[0]
227         except:
228                 command = input_data
229         try:
230                 parameters = inputlist[1]
231         except:
232                 parameters = ""
233         del(inputlist)
234
235         # lowercase the command
236         command = command.lower()
237
238         # the command matches a command word for which we have data
239         if command in command_list: exec("command_" + command + "(user, command, parameters)")
240
241         # no data matching the entered command word
242         elif command: command_error(user, command, parameters)
243
244 def command_halt(user, command="", parameters=""):
245         """Halt the world."""
246
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."
250
251         # let everyone know
252         muffmisc.broadcast(message)
253         muffmisc.log(message)
254
255         # set a flag to terminate the world
256         muffvars.terminate_world = True
257
258 def command_reload(user, command="", parameters=""):
259         """Reload all code modules, configs and data."""
260
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.")
264
265         # set a flag to reload
266         muffvars.reload_modules = True
267
268 def command_quit(user, command="", parameters=""):
269         """Quit the world."""
270         user.state = "disconnecting"
271
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.")
276
277 def command_help(user, command="", parameters=""):
278         """List available commands and provide help for commands."""
279
280         # did the user ask for help on a specific command word?
281         if parameters:
282
283                 # is the command word one for which we have data?
284                 if parameters in command_list:
285
286                         # add a description if provided
287                         try:
288                                 description = command_data.get(parameters, "description")
289                         except:
290                                 description = "(no short description provided)"
291                         output = "$(grn)" + parameters + "$(nrm) - " + command_data.get(parameters, "description") + "$(eol)$(eol)"
292
293                         # add the help text if provided
294                         try:
295                                 help_text = command_data.get(parameters, "help")
296                         except:
297                                 help_text = "No help is provided for this command."
298                         output += help_text
299
300                 # no data for the requested command word
301                 else:
302                         output = "That is not an available command."
303
304         # no specific command word was indicated
305         else:
306
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:
312                         try:
313                                 description = command_data.get(item, "description")
314                         except:
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\"."
318
319         # send the accumulated output to the user
320         user.send(output)
321
322 def command_say(user, command="", parameters=""):
323         """Speak to others in the same room."""
324
325         # check for replacement macros
326         if muffmisc.replace_macros(user, parameters, True) != parameters:
327                 user.send("You cannot speak $_(replacement macros).")
328
329         # the user entered a message
330         elif parameters:
331
332                 # get rid of quote marks on the ends of the message and
333                 # capitalize the first letter
334                 message = parameters.strip("\"'`").capitalize()
335
336                 # a dictionary of punctuation:action pairs
337                 actions = {}
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
343
344                 # set the default action
345                 action = actions[muffconf.config_data.get("language", "default_punctuation")]
346
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]
352                                 break
353
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
357
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() + " ")
362
363                 # tell the room
364                 # TODO: we won't be using broadcast once there are actual rooms
365                 muffmisc.broadcast(user.name + " " + action + "s, \"" + message + "\"")
366
367         # there was no message
368         else:
369                 user.send("What do you want to say?")
370
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()
376                 keys.sort()
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()
381                 avatars.sort()
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?"
385         user.send(message)
386
387 def command_error(user, command="", parameters=""):
388         """Generic error for an unrecognized command word."""
389
390         # 90% of the time use a generic error
391         if random.randrange(10):
392                 message = "I'm not sure what \"" + command
393                 if parameters:
394                         message += " " + parameters
395                 message += "\" means..."
396
397         # 10% of the time use the classic diku error
398         else:
399                 message = "Arglebargle, glop-glyf!?!"
400
401         # send the error message
402         user.send(message)
403