1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package com.levelonelabs.aimbot.modules;
34
35 import megahal.MegaDictionary;
36 import megahal.MegaHalInterface;
37 import megahal.MegaModel;
38
39 import java.io.File;
40
41 import java.util.ArrayList;
42 import java.util.StringTokenizer;
43
44 import com.levelonelabs.aim.AIMBuddy;
45 import com.levelonelabs.aimbot.AIMBot;
46 import com.levelonelabs.aimbot.BotModule;
47
48
49 /***
50 * Creates Babel poetry from user input
51 *
52 * @author Will Gorman
53 *
54 * @created February 3, 2002
55 */
56 public class MegaHalModule extends BotModule implements MegaHalInterface {
57 private static ArrayList services;
58
59 /***
60 * Initialize the service commands.
61 */
62 static {
63
64 services = new ArrayList();
65 services.add("megahaladmin");
66 }
67
68 /*** tells MegaModel which order (how many contexts) it should be. */
69 protected int order = 5;
70 /*** used for specifing which brain directory to use */
71 protected String directory = System.getProperty("user.dir") + File.separator + "conf" + File.separator + "megahal";
72 /*** remembers previous brain directory */
73 protected String last = null;
74 MegaModel model = null;
75
76
77 /***
78 * Constructor for the BabelModule object
79 *
80 * @param bot
81 */
82 public MegaHalModule(AIMBot bot) {
83 super(bot);
84 model = new MegaModel(this);
85
86
87 MegaModel.timeout = 3;
88 model.load_personality(order, directory, "");
89
90 }
91
92
93 /***
94 * warn is used for errors that occur.
95 *
96 * @param title
97 * error title.
98 * @param error_msg
99 * error message.
100 * @return boolean whether printing of error was successful.
101 */
102 public boolean warn(String title, String error_msg) {
103 return true;
104 }
105
106
107 /***
108 * allow for progress tracking of certain activities in MegaModel.
109 *
110 * @param message
111 * message to display
112 * @param done
113 * amount completed.
114 * @param total
115 * total amount to complete.
116 * @return whether printing of progress was complete or not.
117 */
118 public boolean progress(String message, int done, int total) {
119 return true;
120 }
121
122
123 /***
124 * allows for printing of debug information.
125 *
126 * @param str
127 * debug string
128 */
129 public void debug(String str) {
130 }
131
132
133 /***
134 * Gets the services attribute of the BabelModule object
135 *
136 * @return The services value
137 */
138 public ArrayList getServices() {
139 return services;
140 }
141
142
143 /***
144 * Gets the name attribute of the BabelModule object
145 *
146 * @return The name value
147 */
148 public String getName() {
149 return "MegaHal Module";
150 }
151
152
153 /***
154 * Describes the usage of the module
155 *
156 * @return the usage of the module
157 */
158 public String help() {
159 StringBuffer sb = new StringBuffer();
160 sb.append("<B>MEGAHAL</B> commands:\n");
161 sb.append("<b>megahaladmin save</b> (saves the megahal brain *ADMIN ONLY*)\n");
162 sb.append("<b>megahaladmin think <i>N</i></b> (sets the think time to <N> seconds *ADMIN ONLY*)\n");
163 return sb.toString();
164 }
165
166
167 /***
168 * Create poetry
169 *
170 * @param buddy
171 * @param query
172 */
173 public void performService(AIMBuddy buddy, String query) {
174 if (query.toLowerCase().startsWith("megahaladmin")) {
175 if (!buddy.hasRole(AIMBot.ROLE_ADMINISTRATOR)) {
176 super.sendMessage(buddy, "Sorry, you are not an " + AIMBot.ROLE_ADMINISTRATOR);
177 return;
178 }
179 StringTokenizer st = new StringTokenizer(query, " ");
180
181
182 if (st.countTokens() < 2) {
183 super.sendMessage(buddy, "ERROR:\n" + help());
184 return;
185 }
186 st.nextToken();
187 String imcommand = st.nextToken();
188 if (imcommand.toLowerCase().equals("save")) {
189 synchronized (model) {
190 model.save_model(directory + File.separator + MegaModel.MEGA_BRAIN);
191 }
192 super.sendMessage(buddy, "Saved Model");
193 return;
194 } else if (imcommand.toLowerCase().equals("think") && (st.countTokens() == 1)) {
195 int delay = MegaModel.timeout;
196 String newDelay = st.nextToken();
197 try {
198 delay = Integer.parseInt(newDelay);
199 } catch (Exception e) {
200 super.sendMessage(buddy, "Error! " + newDelay + " did not parse.");
201 }
202 synchronized (model) {
203 MegaModel.timeout = delay;
204 }
205 super.sendMessage(buddy, "Changing thinking time to " + delay + " seconds.");
206 return;
207 } else {
208 super.sendMessage(buddy, "I don't know about " + imcommand);
209 }
210
211 } else {
212 if (query.toLowerCase().startsWith("you should talk to")) {
213 AIMBuddy to = getBuddy(query.substring(18).trim());
214 if (to != null) {
215 if (to.getName().toLowerCase().equals(super.bot.getUsername().toLowerCase())) {
216 super.sendMessage(buddy, "Sorry, I can't talk to myself");
217 } else {
218 if (!to.isOnline()) {
219 to.addMessage(buddy.getName() + " said I should talk to you!");
220 super.sendMessage(buddy, "I'll talk to them once they're online.");
221 } else {
222 super.sendMessage(to, buddy.getName() + " said I should talk to you!");
223 super.sendMessage(buddy, "I've started a conversation with " + to.getName());
224 }
225 }
226 } else {
227 super.sendMessage(buddy, "I don't know a " + query.substring(18).trim());
228 }
229 return;
230 }
231 MegaDictionary words = new MegaDictionary();
232 words.make_words(query.toUpperCase());
233 String output = "";
234 synchronized (model) {
235 model.learn(words);
236 output = MegaModel.capitalize(model.generate_reply(words));
237 }
238
239 super.sendMessage(buddy, output);
240 }
241 }
242 }