Source code: org/alicebot/server/core/util/Shell.java
1 // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.geocities.com/kpdus/jad.html
3 // Decompiler options: packimports(3)
4
5 package org.alicebot.server.core.util;
6
7 import java.io.*;
8 import java.util.*;
9 import org.alicebot.server.core.*;
10 import org.alicebot.server.core.logging.Log;
11 import org.alicebot.server.core.responder.TextResponder;
12 import org.alicebot.server.core.targeting.TargetMaster;
13
14 // Referenced classes of package org.alicebot.server.core.util:
15 // ShellCommandable, Toolkit, MessagePrinter, XMLWriter
16
17 public class Shell
18 {
19 private class NoSuchCommandableException extends Exception
20 {
21
22 public NoSuchCommandableException()
23 {
24 }
25 }
26
27 private class NoCommandException extends Exception
28 {
29
30 public NoCommandException()
31 {
32 }
33 }
34
35
36 public Shell()
37 {
38 consoleIn = new BufferedReader(new InputStreamReader(System.in));
39 consoleDisplay = consolePrompt = System.out;
40 }
41
42 public Shell(InputStream inputstream, PrintStream printstream, PrintStream printstream1)
43 {
44 consoleIn = new BufferedReader(new InputStreamReader(inputstream));
45 consoleDisplay = printstream;
46 consolePrompt = printstream1;
47 }
48
49 public void run()
50 {
51 showConsole("Interactive shell: type \"/exit\" to shut down; \"/help\" for help.");
52 Bot bot = Bots.getFirstBot();
53 if(bot == null)
54 {
55 showConsole("No bot to talk to!");
56 return;
57 }
58 botid = bot.getID();
59 botName = bot.getPropertyValue(BOT_NAME_PREDICATE);
60 showConsole(botName, Toolkit.breakLines(Multiplexor.getResponse(Globals.getProperty("programd.connect-string", "CONNECT"), HOSTNAME, botid, new TextResponder())));
61 do
62 {
63 promptConsole('[' + botName + "] " + PredicateMaster.get(CLIENT_NAME_PREDICATE, HOSTNAME, botid));
64 String s = null;
65 try
66 {
67 s = consoleIn.readLine();
68 }
69 catch(IOException ioexception)
70 {
71 Log.userinfo("Cannot read from console!", Log.ERROR);
72 return;
73 }
74 InterruptedException interruptedexception;
75 if(s == null)
76 do
77 try
78 {
79 Thread.sleep(0x0L);
80 }
81 // Misplaced declaration of an exception variable
82 // catch(InterruptedException interruptedexception)
83 catch(InterruptedException interruptedexception2)
84 {
85 return;
86 }
87 while(true);
88 MessagePrinter.gotLine();
89 if(s.indexOf('/') == 0)
90 {
91 if(s.toLowerCase().equals("/exit"))
92 {
93 printExitMessage();
94 return;
95 }
96 if(s.toLowerCase().equals("/help"))
97 help();
98 else
99 if(s.toLowerCase().startsWith("/load"))
100 load(s, botid);
101 else
102 if(s.toLowerCase().startsWith("/unload"))
103 unload(s, botid);
104 else
105 if(s.toLowerCase().equals("/bots"))
106 showBotList();
107 else
108 if(s.toLowerCase().startsWith("/talkto"))
109 talkto(s);
110 else
111 if(s.toLowerCase().equals("/who"))
112 who();
113 else
114 if(s.toLowerCase().equals("/files"))
115 listBotFiles();
116 else
117 if(s.toLowerCase().startsWith("/roll chatlog"))
118 rollChatLog(botid);
119 else
120 if(s.toLowerCase().equals("/roll targets"))
121 rollTargets();
122 else
123 if(s.toLowerCase().equals("/commandables"))
124 listCommandables();
125 else
126 try
127 {
128 sendCommand(s);
129 }
130 catch(NoCommandException nocommandexception)
131 {
132 showConsole("Please specify a command following the commandable.");
133 }
134 catch(NoSuchCommandableException nosuchcommandableexception)
135 {
136 showConsole("No such commandable is loaded. Type \"/commandables\" for a list of loaded commandables.");
137 }
138 } else
139 if(s.length() > 0)
140 showConsole(botName, Toolkit.breakLines(Multiplexor.getResponse(s, HOSTNAME, botid, new TextResponder())));
141 } while(true);
142 }
143
144 public String getCurrentBotID()
145 {
146 return botid;
147 }
148
149 private void promptConsole(String s)
150 {
151 MessagePrinter.print(s + "> ", SHELL, consolePrompt, 1);
152 }
153
154 private void showConsole(String s)
155 {
156 MessagePrinter.println(s, SHELL, consoleDisplay, 1);
157 }
158
159 private void showConsole(String as[])
160 {
161 for(int i = 0; i < as.length; i++)
162 MessagePrinter.println(as[i], SHELL, consoleDisplay, 1);
163
164 }
165
166 private void showConsole(String s, String s1)
167 {
168 MessagePrinter.println(s + "> " + s1, SHELL, consoleDisplay, 1);
169 }
170
171 private void showConsole(String s, String as[])
172 {
173 for(int i = 0; i < as.length; i++)
174 MessagePrinter.println(s + "> " + as[i], SHELL, consoleDisplay, 1);
175
176 }
177
178 private void printExitMessage()
179 {
180 Log.userinfo("Exiting at user request.", Log.STARTUP);
181 }
182
183 public void help()
184 {
185 showConsole(HELP_TEXT);
186 }
187
188 public void load(String s, String s1)
189 {
190 int i = s.indexOf(' ');
191 if(i == -1)
192 {
193 showConsole("You must specify a filename.");
194 } else
195 {
196 int j = Graphmaster.getTotalCategories();
197 String s2;
198 try
199 {
200 s2 = (new File(s.substring(i + 1))).getCanonicalPath();
201 }
202 catch(IOException ioexception)
203 {
204 showConsole("I/O exception trying to locate file.");
205 return;
206 }
207 Graphmaster.load(s2, s1);
208 Log.userinfo((Graphmaster.getTotalCategories() - j) + " categories loaded from \"" + s2 + "\".", Log.LEARN);
209 }
210 }
211
212 private void unload(String s, String s1)
213 {
214 int i = s.indexOf(' ');
215 if(i == -1)
216 {
217 showConsole("You must specify a filename.");
218 } else
219 {
220 int j = Graphmaster.getTotalCategories();
221 String s2;
222 try
223 {
224 s2 = (new File(s.substring(i + 1))).getCanonicalPath();
225 }
226 catch(IOException ioexception)
227 {
228 showConsole("I/O exception trying to locate file.");
229 return;
230 }
231 Graphmaster.unload(s2, Bots.getBot(s1));
232 Log.userinfo((j - Graphmaster.getTotalCategories()) + " categories unloaded.", Log.LEARN);
233 }
234 }
235
236 public void showBotList()
237 {
238 showConsole("Active bots: " + Bots.getNiceList());
239 }
240
241 private void talkto(String s)
242 {
243 int i = s.indexOf(' ');
244 if(i == -1)
245 showConsole("You must specify a bot id.");
246 else
247 switchToBot(s.substring(i + 1));
248 }
249
250 public void switchToBot(String s)
251 {
252 if(!Bots.knowsBot(s))
253 {
254 showConsole("That bot id is not known. Check your startup files.");
255 return;
256 } else
257 {
258 botid = s;
259 botName = Bots.getBot(s).getPropertyValue(BOT_NAME_PREDICATE);
260 showConsole("Switched to bot \"" + s + "\" (name: \"" + botName + "\").");
261 showConsole(botName, Toolkit.breakLines(Multiplexor.getResponse(Globals.getProperty("programd.connect-string", "CONNECT"), HOSTNAME, botid, new TextResponder())));
262 return;
263 }
264 }
265
266 private void who()
267 {
268 showConsole("You are talking to \"" + botid + "\".");
269 }
270
271 public void listBotFiles()
272 {
273 Set set = Bots.getBot(botid).getLoadedFilesMap().keySet();
274 Iterator iterator = set.iterator();
275 int i = set.size();
276 if(i > 1)
277 showConsole(i + " files loaded by \"" + botid + "\":");
278 else
279 showConsole("1 file loaded by \"" + botid + "\":");
280 for(; iterator.hasNext(); showConsole(((File)iterator.next()).getAbsolutePath()));
281 }
282
283 public void rollChatLog(String s)
284 {
285 showConsole("Rolling over chat log for \"" + s + "\".");
286 XMLWriter.rollover(Bots.getBot(s).getChatlogSpec());
287 showConsole("Finished rolling over chat log.");
288 }
289
290 public void rollTargets()
291 {
292 TargetMaster.rollTargetData();
293 }
294
295 private void listCommandables()
296 {
297 Iterator iterator = BotProcesses.getRegistryIterator();
298 int i = 0;
299 if(iterator.hasNext())
300 {
301 showConsole("Available shell commandables:");
302 while(iterator.hasNext())
303 try
304 {
305 ShellCommandable shellcommandable = (ShellCommandable)iterator.next();
306 showConsole("/" + shellcommandable.getShellID() + " - " + shellcommandable.getShellDescription());
307 i++;
308 }
309 catch(ClassCastException classcastexception) { }
310 }
311 if(i == 0)
312 {
313 showConsole("No shell commandables are loaded.");
314 } else
315 {
316 showConsole("Commands after the shell commandable will be sent to the commandable.");
317 showConsole("Example: \"/irc /JOIN #foo\" tells the AliceIRC listener to join channel \"#foo\".");
318 }
319 }
320
321 private void sendCommand(String s)
322 throws NoCommandException, NoSuchCommandableException
323 {
324 int i = s.indexOf(' ');
325 if(i == -1)
326 throw new NoCommandException();
327 if(i == s.length())
328 throw new NoCommandException();
329 String s1 = s.substring(1, i);
330 ShellCommandable shellcommandable = null;
331 Iterator iterator = BotProcesses.getRegistryIterator();
332 if(iterator.hasNext())
333 while(iterator.hasNext())
334 try
335 {
336 ShellCommandable shellcommandable1 = (ShellCommandable)iterator.next();
337 if(s1.equals(shellcommandable1.getShellID()))
338 shellcommandable = shellcommandable1;
339 }
340 catch(ClassCastException classcastexception) { }
341 if(shellcommandable == null)
342 {
343 throw new NoSuchCommandableException();
344 } else
345 {
346 shellcommandable.processShellCommand(s.substring(i + 1));
347 return;
348 }
349 }
350
351 private static final String HOSTNAME = Globals.getHostName();
352 private static final String CLIENT_NAME_PREDICATE = Globals.getClientNamePredicate();
353 private static final String BOT_NAME_PREDICATE = Globals.getBotNamePredicate();
354 public static final String PROMPT = "> ";
355 private static final boolean SHOW_MESSAGE_FLAGS;
356 public static final String SHELL;
357 private static final String HELP = "/help";
358 private static final String EXIT = "/exit";
359 private static final String LOAD = "/load";
360 private static final String UNLOAD = "/unload";
361 private static final String BOTLIST = "/bots";
362 private static final String TALKTO = "/talkto";
363 private static final String WHO = "/who";
364 private static final String BOT_FILES = "/files";
365 private static final String ROLL_CHATLOG = "/roll chatlog";
366 private static final String ROLL_TARGETS = "/roll targets";
367 private static final String COMMANDABLES = "/commandables";
368 private static final String HELP_TEXT[] = {
369 "All shell commands are preceded by a forward slash (/).", "The commands available are:", "/help - prints this help", "/exit - shuts down the bot server", "/load filename - loads/reloads given filename for active bot", "/unload filename - unloads given filename for active bot", "/bots - lists loaded bots", "/talkto botid - switches conversation to given bot", "/who - prints the id of the current bot", "/files - lists the files loaded by the current bot",
370 "/roll chatlog - rolls over chat log", "/roll targets - rolls over saved targeting data", "/commandables - lists available \"shell commandables\" (such as listeners)"
371 };
372 private BufferedReader consoleIn;
373 private PrintStream consoleDisplay;
374 private PrintStream consolePrompt;
375 private String botid;
376 private String botName;
377
378 static
379 {
380 SHOW_MESSAGE_FLAGS = Boolean.valueOf(Globals.getProperty("programd.console.message-flags", "true")).booleanValue();
381 SHELL = SHOW_MESSAGE_FLAGS ? "s " : "";
382 }
383 }