a9df0327302b26894aefcbba67a4546d5abf5302
[mudpy.git] / lib / muff / muffcmds.py
1 """Command objects for the MUFF Engine"""
2
3 # Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 #    - Redistributions of source code must retain the above copyright
11 #      notice, this list of conditions and the following disclaimer.
12 #    - Redistributions in binary form must reproduce the above
13 #      copyright notice, this list of conditions and the following
14 #      disclaimer in the documentation and/or other materials provided
15 #      with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 # COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29
30 import ConfigParser
31 import md5
32 import os
33 import random
34 import string
35
36 import muff
37 for module in muff.__all__:
38         exec("import " + module)
39
40 try:
41         if muffconf.config_data.get("general", "command_path"):
42                 pass
43 except AttributeError:
44         reload(muffconf)
45 command_path = muffconf.config_data.get("general", "command_path")
46 command_files = []
47 for each_file in os.listdir(command_path):
48         command_files.append(command_path + "/" + each_file)
49 command_data = ConfigParser.SafeConfigParser()
50 command_data.read(command_files)
51 command_list = command_data.sections()
52
53 def handle_user_input(user, input):
54         if user.state == "active": handler_active(user, input)
55         elif user.state == "entering account name": handler_entering_account_name(user, input)
56         elif user.state == "checking password": handler_checking_password(user, input)
57         elif user.state == "checking new account name": handler_checking_new_account_name(user, input)
58         elif user.state == "entering new password": handler_entering_new_password(user, input)
59         elif user.state == "verifying new password": handler_verifying_new_password(user, input)
60         else: handler_fallthrough(user, input)
61         user.menu_seen = False
62
63 def handler_entering_account_name(user, input):
64         if input:
65                 user.proposed_name = string.split(input)[0].lower()
66                 user.get_passhash()
67                 if user.passhash:
68                         user.state = "checking password"
69                 else:
70                         user.name = user.proposed_name
71                         user.proposed_name = None
72                         user.load()
73                         user.state = "checking new account name"
74         else:
75                 command_quit(user)
76
77 def handler_checking_password(user, input):
78         if md5.new(user.proposed_name + input).hexdigest() == user.passhash:
79                 user.name = user.proposed_name
80                 user.proposed_name = None
81                 user.load()
82                 user.state = "active"
83         elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
84                 user.password_tries += 1
85                 user.error = "incorrect"
86         else:
87                 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
88                 command_quit(user)
89
90 def handler_checking_new_account_name(user, input):
91         if input:
92                 choice = input.lower()[0]
93         else:
94                 choice = muffmenu.get_default(user)
95         if choice == "d":
96                 command_quit(user)
97         elif choice == "g":
98                 user.state = "entering account name"
99         elif choice == "n":
100                 user.state = "entering new password"
101         else:
102                 user.error = "default"
103
104 def handler_entering_new_password(user, input):
105         if len(input) > 6 and len(filter(lambda x: x>="0" and x<="9", input)) and len(filter(lambda x: x>="A" and x<="Z", input)) and len(filter(lambda x: x>="a" and x<="z", input)):
106                 user.passhash = md5.new(user.name + input).hexdigest()
107                 user.state = "verifying new password"
108         elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
109                 user.password_tries += 1
110                 user.error = "weak"
111         else:
112                 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
113                 command_quit(user)
114
115 def handler_verifying_new_password(user, input):
116         if md5.new(user.name + input).hexdigest() == user.passhash:
117                 user.state = "active"
118         elif user.password_tries < muffconf.config_data.getint("general", "password_tries"):
119                 user.password_tries += 1
120                 user.error = "differs"
121                 user.state = "entering new password"
122         else:
123                 user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
124                 command_quit(user)
125
126 def handler_active(user, input):
127         if not user.authenticated: user.authenticated = True
128         try:
129                 inputlist = string.split(input, None, 1)
130                 command = inputlist[0]
131         except:
132                 command = input
133         try:
134                 parameters = inputlist[1]
135         except:
136                 parameters = ""
137         command = command.lower()
138         if not command: command_null(user, command, parameters)
139         elif command in command_list: exec("command_" + command + "(user, command, parameters)")
140         else: command_error(user, command, parameters)
141
142 def handler_fallthrough(user, input):
143         if input:
144                 print("User \"" + user + "\" entered \"" + input + "\" while in unknown state \"" + user.state + "\".")
145
146 def command_halt(user, command="", parameters=""):
147         muffmisc.broadcast(user.name + " halts the world.")
148         for each_user in muffvars.userlist:
149                 each_user.save()
150         muffvars.terminate_world = True
151
152 def command_reload(user, command="", parameters=""):
153         user.send("Reloading all code modules.")
154         muffvars.reload_modules = True
155
156 def command_quit(user, command="", parameters=""):
157         user.save()
158         user.connection.close()
159         user.remove()
160
161 def command_help(user, command="", parameters=""):
162         if parameters:
163                 if parameters in command_list:
164                         if command_data.has_section(parameters):
165                                 try:
166                                         description = command_data.get(parameters, "description")
167                                 except:
168                                         description = "(no short description provided)"
169                                 output = "$(grn)" + parameters + "$(nrm) - " + command_data.get(parameters, "description") + "$(eol)$(eol)"
170                                 try:
171                                         help_text = command_data.get(parameters, "help")
172                                 except:
173                                         help_text = "No help is provided for this command."
174                                 output += help_text
175                         else:
176                                 output = "There is no information on that command."
177                 else:
178                         output = "That is not an available command."
179                         
180         else:
181                 output = "These are the commands available to you:$(eol)$(eol)"
182                 sorted_commands = command_list
183                 sorted_commands.sort()
184                 for item in sorted_commands:
185                         try:
186                                 description = command_data.get(item, "description")
187                         except:
188                                 description = "(no short description provided)"
189                         output += "   $(grn)" + item + "$(nrm) - " + command_data.get(item, "description") + "$(eol)"
190                 output += "$(eol)Enter \"help COMMAND\" for help on a command named \"COMMAND\"."
191         user.send(output)
192
193 def command_say(user, command="", parameters=""):
194         message = parameters.strip("\"'`").capitalize()
195         if parameters:
196                 if message[-1] == "!":
197                         action = "exclaim"
198                 elif message[-1] in [ ",", "-", ":", ";" ]:
199                         action = "begin"
200                 elif message[-3:] == "...":
201                         action = "muse"
202                 elif message[-1] == "?":
203                         action = "ask"
204                 else:
205                         action = "say"
206                         message += "."
207                 capitalization = [ "i", "i'd", "i'll" ]
208                 for word in capitalization:
209                         message = message.replace(" " + word + " ", " " + word.capitalize() + " ")
210                 muffmisc.broadcast(user.name + " " + action + "s, \"" + message + "\"")
211         else:
212                 user.send("What do you want to say?")
213
214 def command_null(user, command="", parameters=""):
215         pass
216
217 def command_error(user, command="", parameters=""):
218         if random.random() > 0.1:
219                 message = "I'm not sure what \"" + command
220                 if parameters:
221                         message += " " + parameters
222                 message += "\" means..."
223         else:
224                 message = "Arglebargle, glop-glyf!?!"
225         user.send(message)
226