Source code: org/alicebot/server/net/listener/AliceAIM.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.net.listener;
6
7 import java.io.*;
8 import java.net.Socket;
9 import java.text.StringCharacterIterator;
10 import java.util.HashMap;
11 import java.util.StringTokenizer;
12 import org.alicebot.server.core.Bot;
13 import org.alicebot.server.core.Multiplexor;
14 import org.alicebot.server.core.logging.Log;
15 import org.alicebot.server.core.responder.AIMResponder;
16 import org.alicebot.server.core.util.Toolkit;
17
18 // Referenced classes of package org.alicebot.server.net.listener:
19 // AliceChatListener
20
21 public class AliceAIM extends AliceChatListener
22 {
23
24 public AliceAIM(Bot bot)
25 {
26 super(bot, "AliceAIM", new String[][] {
27 new String[] {
28 "owner", ""
29 }, new String[] {
30 "screenname", ""
31 }, new String[] {
32 "password", ""
33 }, new String[] {
34 "bgcolor", "White"
35 }, new String[] {
36 "fontface", "Verdana,Arial"
37 }, new String[] {
38 "fontsize", "2"
39 }, new String[] {
40 "fontcolor", "Black"
41 }, new String[] {
42 "buddies", ""
43 }
44 });
45 }
46
47 public boolean checkParameters()
48 {
49 owner = (String)parameters.get("owner");
50 name = (String)parameters.get("screenname");
51 pass = (String)parameters.get("password");
52 bgcolor = (String)parameters.get("bgcolor");
53 fontface = (String)parameters.get("fontface");
54 fontsize = (String)parameters.get("fontsize");
55 fontcolor = (String)parameters.get("fontcolor");
56 buddies = (String)parameters.get("buddies");
57 if(owner.length() == 0)
58 {
59 logMessage("No owner specified; aborting.");
60 return false;
61 }
62 if(name.length() == 0)
63 {
64 logMessage("No screen name specified; aborting.");
65 return false;
66 }
67 if(pass.length() == 0)
68 {
69 logMessage("No password specified; aborting.");
70 return false;
71 } else
72 {
73 return true;
74 }
75 }
76
77 public void run()
78 {
79 seqNo = (int)Math.floor(Math.random() * 65535D);
80 logMessage("Starting for \"" + botID + "\".");
81 try
82 {
83 connection = new Socket("toc.oscar.aol.com", 21);
84 connection.setSoTimeout(10000);
85 in = new DataInputStream(connection.getInputStream());
86 out = new DataOutputStream(new BufferedOutputStream(connection.getOutputStream()));
87 }
88 catch(Exception exception)
89 {
90 signoff("1");
91 return;
92 }
93 try
94 {
95 out.writeBytes("FLAPON\r\n\r\n");
96 out.flush();
97 byte abyte0[] = new byte[10];
98 in.readFully(abyte0);
99 out.writeByte(42);
100 out.writeByte(1);
101 out.writeShort(seqNo);
102 seqNo = seqNo + 1 & 0xffff;
103 out.writeShort(name.length() + 8);
104 out.writeInt(1);
105 out.writeShort(1);
106 out.writeShort(name.length());
107 out.writeBytes(name);
108 out.flush();
109 frameSend("toc_signon login.oscar.aol.com 5190 " + name + " " + imRoast(pass) + " english alicebot\0");
110 in.skip(0L);
111 short word0 = in.readShort();
112 abyte0 = new byte[word0];
113 in.readFully(abyte0);
114 if(String.valueOf(abyte0).startsWith("ERROR"))
115 {
116 logMessage("Signon error.");
117 signoff("2");
118 return;
119 }
120 in.skip(0L);
121 word0 = in.readShort();
122 abyte0 = new byte[word0];
123 in.readFully(abyte0);
124 online = true;
125 sendBuddies();
126 frameSend("toc_init_done\0");
127 logMessage("Logon complete.");
128 connection.setSoTimeout(3000);
129 }
130 catch(InterruptedIOException interruptedioexception)
131 {
132 online = false;
133 signoff("2.5");
134 }
135 catch(IOException ioexception)
136 {
137 online = false;
138 signoff("3");
139 }
140 do
141 try
142 {
143 do
144 {
145 in.skip(0L);
146 short word1 = in.readShort();
147 byte abyte1[] = new byte[word1];
148 in.readFully(abyte1);
149 fromAIM(abyte1);
150 } while(true);
151 }
152 catch(InterruptedIOException interruptedioexception1)
153 {
154 Log.devinfo("AliceAIM: Error*: " + interruptedioexception1, Log.LISTENERS);
155 }
156 catch(IOException ioexception1)
157 {
158 Log.devinfo("AliceAIM: Error**: " + ioexception1, Log.LISTENERS);
159 signoff("4");
160 return;
161 }
162 while(true);
163 }
164
165 public void shutdown()
166 {
167 signoff("4");
168 }
169
170 public void frameSend(String s)
171 throws IOException
172 {
173 out.writeByte(42);
174 out.writeByte(2);
175 out.writeShort(seqNo);
176 seqNo = seqNo + 1 & 0xffff;
177 out.writeShort(s.length());
178 out.writeBytes(s);
179 out.flush();
180 }
181
182 public void fromAIM(byte abyte0[])
183 {
184 String s = new String(abyte0);
185 Log.devinfo("AliceAIM: Got: " + s, Log.LISTENERS);
186 StringTokenizer stringtokenizer = new StringTokenizer(s, ":");
187 String s1 = stringtokenizer.nextToken();
188 if(s1.equals("IM_IN"))
189 {
190 String s2 = imNormalize(stringtokenizer.nextToken());
191 stringtokenizer.nextToken();
192 StringBuffer stringbuffer = new StringBuffer(stringtokenizer.nextToken());
193 for(; stringtokenizer.hasMoreTokens(); stringbuffer.append(stringtokenizer.nextToken()))
194 stringbuffer.append(':');
195
196 String s6 = Toolkit.removeMarkup(stringbuffer.toString());
197 logMessage("Message from [" + s2 + "]: " + s6);
198 if(s6.startsWith("$SENDIM") && owner.equals(s2))
199 {
200 StringTokenizer stringtokenizer1 = new StringTokenizer(s6);
201 stringtokenizer1.nextToken();
202 String s8 = stringtokenizer1.nextToken();
203 String s9 = stringtokenizer1.nextToken();
204 sendMesg(s8, s9);
205 } else
206 {
207 String as[] = Toolkit.breakLines(Multiplexor.getResponse(s6, s2 + "_AIM", botID, new AIMResponder()));
208 if(as.length > 0)
209 {
210 for(int i = 0; i < as.length; i++)
211 sendMesg(s2, as[i]);
212
213 }
214 }
215 return;
216 }
217 if(s1.equals("CHAT_IN"))
218 {
219 String s3 = imNormalize(stringtokenizer.nextToken());
220 String s5 = imNormalize(stringtokenizer.nextToken());
221 StringBuffer stringbuffer1 = new StringBuffer(stringtokenizer.nextToken());
222 for(; stringtokenizer.hasMoreTokens(); stringbuffer1.append(stringtokenizer.nextToken()))
223 stringbuffer1.append(':');
224
225 String s7 = Toolkit.removeMarkup(stringbuffer1.toString());
226 if(s7.indexOf(name) > 0)
227 {
228 String as1[] = Toolkit.breakLines(Multiplexor.getResponse(s7, s5 + "_AIM", botID, new AIMResponder()));
229 if(as1.length > 0)
230 {
231 for(int j = 0; j < as1.length; j++)
232 sendChatRoomMesg(s3, as1[j]);
233
234 }
235 }
236 return;
237 }
238 if(s1.equals("ERROR"))
239 {
240 String s4 = stringtokenizer.nextToken();
241 logMessage("Error: " + s4);
242 if(s4.equals("901"))
243 {
244 logMessage("Not currently available.");
245 return;
246 }
247 if(s4.equals("903"))
248 {
249 logMessage("Message dropped; sending too fast.");
250 return;
251 }
252 if(s4.equals("960"))
253 {
254 logMessage("Sending messages too fast to " + stringtokenizer.nextToken());
255 return;
256 }
257 if(s4.equals("961"))
258 {
259 logMessage(stringtokenizer.nextToken() + " sent you too big a message.");
260 return;
261 }
262 if(s4.equals("962"))
263 {
264 logMessage(stringtokenizer.nextToken() + " sent you a message too fast.");
265 return;
266 }
267 if(s4.equals("Signon err"))
268 {
269 logMessage("AIM signon failure: " + stringtokenizer.nextToken());
270 signoff("5");
271 }
272 return;
273 } else
274 {
275 return;
276 }
277 }
278
279 public void sendMesg(String s, String s1)
280 {
281 String s2 = "<BODY BGCOLOR=\"" + bgcolor + "\"><FONT SIZE=\"" + fontsize + " FACE=\"" + fontface + " COLOR=\"" + fontcolor + "\">" + s1 + "</FONT>";
282 String s3 = "toc_send_im " + s + " \"" + imEscape(s2);
283 logMessage(s1);
284 try
285 {
286 frameSend(s3);
287 }
288 catch(IOException ioexception)
289 {
290 signoff("9");
291 }
292 }
293
294 public void sendChatRoomMesg(String s, String s1)
295 {
296 String s2 = "toc_chat_send " + s + " \"" + imEscape(s1);
297 logMessage(s1);
298 try
299 {
300 frameSend(s2);
301 }
302 catch(IOException ioexception)
303 {
304 signoff("9");
305 }
306 }
307
308 private StringBuffer imEscape(String s)
309 {
310 StringBuffer stringbuffer = new StringBuffer();
311 StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
312 for(char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next())
313 {
314 switch(c)
315 {
316 case 34: // '"'
317 case 36: // '$'
318 case 40: // '('
319 case 41: // ')'
320 case 91: // '['
321 case 92: // '\\'
322 case 93: // ']'
323 case 123: // '{'
324 case 125: // '}'
325 stringbuffer.append("\\");
326 break;
327 }
328 stringbuffer.append(c);
329 }
330
331 stringbuffer.append("\"\0");
332 return stringbuffer;
333 }
334
335 public void toAIM(byte abyte0[])
336 {
337 if(abyte0.length < 2030)
338 {
339 logMessage("Got a message longer than 2030 bytes.");
340 return;
341 }
342 try
343 {
344 out.writeByte(42);
345 out.writeByte(2);
346 out.writeShort(seqNo);
347 seqNo = seqNo + 1 & 0xffff;
348 out.writeShort(abyte0.length + 1);
349 out.write(abyte0);
350 out.writeByte(0);
351 out.flush();
352 }
353 catch(IOException ioexception)
354 {
355 logMessage("Exception: " + ioexception);
356 signoff("6");
357 }
358 }
359
360 public void sendBuddies()
361 {
362 String s = "toc_add_buddy ";
363 Log.devinfo("AliceAIM: " + s, Log.LISTENERS);
364 try
365 {
366 frameSend(s + ' ' + name + "\0");
367 for(StringTokenizer stringtokenizer = new StringTokenizer(buddies, ","); stringtokenizer.hasMoreTokens(); frameSend(s + ' ' + stringtokenizer.nextToken() + "\0"));
368 }
369 catch(IOException ioexception)
370 {
371 logMessage("Exception: " + ioexception);
372 signoff("7");
373 }
374 }
375
376 public void signoff(String s)
377 {
378 online = false;
379 logMessage("Trying to close IM (" + s + ").....");
380 try
381 {
382 out.close();
383 in.close();
384 connection.close();
385 }
386 catch(IOException ioexception)
387 {
388 logMessage("Exception: " + ioexception);
389 }
390 logMessage("Done.");
391 }
392
393 public static String imRoast(String s)
394 {
395 Object obj = null;
396 StringBuffer stringbuffer = new StringBuffer("0x");
397 int i = s.length();
398 for(int j = 0; j < i; j++)
399 {
400 String s1 = Long.toHexString(s.charAt(j) ^ "Tic/Toc".charAt(j % 7));
401 if(s1.length() < 2)
402 stringbuffer.append("0");
403 stringbuffer.append(s1);
404 }
405
406 return stringbuffer.toString();
407 }
408
409 public static String imNormalize(String s)
410 {
411 StringBuffer stringbuffer;
412 int i;
413 for(stringbuffer = new StringBuffer(s); (i = stringbuffer.toString().indexOf(" ")) >= 0 && stringbuffer.length() > 0; stringbuffer.delete(i, i + 1));
414 return stringbuffer.toString();
415 }
416
417 private void logMessage(String s)
418 {
419 Log.userinfo("AliceAIM: " + s, Log.LISTENERS);
420 }
421
422 private String name;
423 private String pass;
424 private String bgcolor;
425 private String fontface;
426 private String fontsize;
427 private String fontcolor;
428 private String owner;
429 private String buddies;
430 private String message;
431 private int seqNo;
432 private Socket connection;
433 private DataInputStream in;
434 private DataOutputStream out;
435 private boolean online;
436 private static final int MAX_SEQ = 65535;
437 private static final String HOST = "toc.oscar.aol.com";
438 private static final int PORT = 21;
439 private static final String ROAST = "Tic/Toc";
440 private static final String ZERO = "0";
441 private static final String ZERO_X = "0x";
442 private static final String ONE = "1";
443 private static final String TWO = "2";
444 private static final String TWO_POINT_FIVE = "2.5";
445 private static final String THREE = "3";
446 private static final String FOUR = "4";
447 private static final String FIVE = "5";
448 private static final String SIX = "6";
449 private static final String SEVEN = "7";
450 private static final String EIGHT = "8";
451 private static final String NINE = "9";
452 private static final String ERROR = "ERROR";
453 private static final String IM_IN = "IM_IN";
454 private static final String MSG_FROM = "Message from [";
455 private static final String RB_COLON = "]: ";
456 private static final String SENDIM = "$SENDIM";
457 private static final String _AIM = "_AIM";
458 private static final String CHAT_IN = "CHAT_IN";
459 private static final String _901 = "901";
460 private static final String _903 = "903";
461 private static final String _960 = "960";
462 private static final String _961 = "961";
463 private static final String _962 = "962";
464 private static final String SIGNON_ERR = "Signon err";
465 private static final String TOC_SEND_IM = "toc_send_im ";
466 private static final String TOC_CHAT_SEND = "toc_chat_send ";
467 private static final String TOC_ADD_BUDDY = "toc_add_buddy ";
468 private static final String MSG_FMT_0 = "<BODY BGCOLOR=\"";
469 private static final String MSG_FMT_1 = "\"><FONT SIZE=\"";
470 private static final String MSG_FMT_2 = " FACE=\"";
471 private static final String MSG_FMT_3 = " COLOR=\"";
472 private static final String MSG_FMT_4 = "\">";
473 private static final String MSG_FMT_5 = "</FONT>";
474 private static final String SPACE_QUOTE = " \"";
475 private static final String SPACE = " ";
476 private static final String BACKSLASH = "\\";
477 private static final String NULL = "\0";
478 private static final String QUOTE_NULL = "\"\0";
479 private static final String EMPTY_STRING = "";
480 public static final String label = "AliceAIM";
481 private static final String MSG = "AliceAIM: ";
482 }