Source code: org/alicebot/server/net/listener/AliceICQ.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.*;
9 import java.util.HashMap;
10 import org.alicebot.server.core.Bot;
11 import org.alicebot.server.core.Multiplexor;
12 import org.alicebot.server.core.logging.Log;
13 import org.alicebot.server.core.responder.TextResponder;
14 import org.alicebot.server.core.util.Trace;
15
16 // Referenced classes of package org.alicebot.server.net.listener:
17 // AliceChatListener, AliceICQKeepAlive
18
19 public class AliceICQ extends AliceChatListener
20 {
21
22 public AliceICQ(Bot bot)
23 {
24 super(bot, "AliceICQ", new String[][] {
25 new String[] {
26 "number", ""
27 }, new String[] {
28 "password", ""
29 }
30 });
31 seqNo = 1;
32 online = false;
33 }
34
35 public boolean checkParameters()
36 {
37 try
38 {
39 uin = Integer.parseInt((String)parameters.get("number"));
40 }
41 catch(NumberFormatException numberformatexception)
42 {
43 logMessage("Invalid user number (try a number!); aborting.");
44 return false;
45 }
46 pass = (String)parameters.get("password");
47 if(uin <= 0)
48 logMessage("Invalid user number; aborting.");
49 if(pass.length() == 0)
50 {
51 logMessage("Invalid empty password; aborting.");
52 return false;
53 } else
54 {
55 clientport = 0;
56 return true;
57 }
58 }
59
60 public void run()
61 {
62 try
63 {
64 socket = new DatagramSocket();
65 serverAddy = InetAddress.getByName("icq.mirabilis.com");
66 logMessage("logging in to " + serverAddy);
67 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
68 bytearrayoutputstream.write(header((short)1000));
69 bytearrayoutputstream.write(toBytes(clientport));
70 bytearrayoutputstream.write(toBytes((short)(pass.length() + 1)));
71 bytearrayoutputstream.write((new String(pass + "\0")).getBytes());
72 bytearrayoutputstream.write(toBytes(120));
73 bytearrayoutputstream.write(InetAddress.getByName("localhost").getAddress());
74 bytearrayoutputstream.write(4);
75 bytearrayoutputstream.write(toBytes(0));
76 bytearrayoutputstream.write(toBytes(2));
77 bytearrayoutputstream.write(toBytes((short)62));
78 bytearrayoutputstream.write(toBytes(0));
79 bytearrayoutputstream.write(toBytes(0x780000));
80 socket.send(new DatagramPacket(bytearrayoutputstream.toByteArray(), bytearrayoutputstream.size(), serverAddy, 4000));
81 bytearrayoutputstream.close();
82 buffer = new byte[10];
83 packet = new DatagramPacket(buffer, buffer.length);
84 socket.receive(packet);
85 buffer = packet.getData();
86 if(buffer[2] != 10 || buffer[3] != 0)
87 if(buffer[2] == 90 && buffer[3] == 0)
88 {
89 ack(buffer[4], buffer[5]);
90 } else
91 {
92 logMessage("No acknowledgement from server; aborting.");
93 return;
94 }
95 socket.receive(packet);
96 buffer = packet.getData();
97 if(buffer[2] == 90 && buffer[3] == 0)
98 ack(buffer[4], buffer[5]);
99 else
100 if(buffer[2] != 10 || buffer[3] != 0)
101 {
102 logMessage("No Login Reply: " + buffer[2] + " " + buffer[3]);
103 return;
104 }
105 online = true;
106 logMessage("Successfully logged on.");
107 toICQ(new byte[] {
108 76, 4
109 });
110 AliceICQKeepAlive aliceicqkeepalive = new AliceICQKeepAlive(this);
111 aliceicqkeepalive.setDaemon(true);
112 logMessage("Starting for \"" + botID + "\".");
113 aliceicqkeepalive.start();
114 socket.setSoTimeout(2000);
115 do
116 try
117 {
118 do
119 {
120 buffer = new byte[512];
121 packet = new DatagramPacket(buffer, buffer.length);
122 socket.receive(packet);
123 buffer = packet.getData();
124 } while(buffer[0] != 2 || buffer[1] != 0 || buffer[2] == 10 && buffer[3] == 0);
125 fromICQ(buffer);
126 ack(buffer[4], buffer[5]);
127 Log.devinfo("AliceICQ: ICQ Command in: " + Integer.toHexString(buffer[2]) + " " + Integer.toHexString(buffer[3]), Log.LISTENERS);
128 }
129 catch(InterruptedIOException interruptedioexception) { }
130 while(true);
131 }
132 catch(UnknownHostException unknownhostexception)
133 {
134 logMessage("Unknown host!");
135 }
136 catch(SocketException socketexception)
137 {
138 logMessage("Socket exception!");
139 }
140 catch(IOException ioexception)
141 {
142 logMessage("IO Exception!");
143 }
144 signoff();
145 }
146
147 public void shutdown()
148 {
149 signoff();
150 }
151
152 public void toICQ(byte abyte0[])
153 throws IOException
154 {
155 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
156 bytearrayoutputstream.write(toBytes((short)2));
157 bytearrayoutputstream.write(abyte0[0]);
158 bytearrayoutputstream.write(abyte0[1]);
159 bytearrayoutputstream.write(toBytes(seqNo));
160 bytearrayoutputstream.write(toBytes(uin));
161 seqNo = (short)(seqNo + 1 & 0xffff);
162 Log.devinfo("AliceICQ: ICQ Command out: " + Integer.toHexString(abyte0[0]) + " " + Integer.toHexString(abyte0[1]), Log.LISTENERS);
163 Trace.devinfo("Buffer length: " + abyte0.length);
164 if(abyte0.length > 2)
165 bytearrayoutputstream.write(abyte0, 2, abyte0.length - 2);
166 socket.send(new DatagramPacket(bytearrayoutputstream.toByteArray(), bytearrayoutputstream.size(), serverAddy, 4000));
167 }
168
169 private void fromICQ(byte abyte0[])
170 {
171 if(abyte0[2] == 110 && abyte0[3] == 0)
172 {
173 int i = (abyte0[6] & 0xff) + (abyte0[7] << 8 & 0xff00) + (abyte0[8] << 16 & 0xff0000) + (abyte0[9] << 24 & 0xff000000);
174 return;
175 }
176 if(abyte0[2] == 120 && abyte0[3] == 0)
177 {
178 int j = (abyte0[6] & 0xff) + (abyte0[7] << 8 & 0xff00) + (abyte0[8] << 16 & 0xff0000) + (abyte0[9] << 24 & 0xff000000);
179 return;
180 }
181 if(abyte0[2] == -26 && abyte0[3] == 0)
182 {
183 try
184 {
185 toICQ(new byte[] {
186 66, 4
187 });
188 }
189 catch(IOException ioexception)
190 {
191 logMessage("IO Exception: " + ioexception.getMessage());
192 }
193 return;
194 }
195 if(abyte0[2] == -36 && abyte0[3] == 0)
196 {
197 int k = (abyte0[6] & 0xff) + (abyte0[7] << 8 & 0xff00) + (abyte0[8] << 16 & 0xff0000) + (abyte0[9] << 24 & 0xff000000);
198 short word0 = (short)((abyte0[18] & 0xff) + (abyte0[19] << 8 & 0xff00));
199 String s = new String(abyte0, 20, word0 - 1);
200 logMessage("Message from [" + k + "]: " + s);
201 return;
202 }
203 if(abyte0[2] == 4 && abyte0[3] == 1)
204 {
205 int l = (abyte0[6] & 0xff) + (abyte0[7] << 8 & 0xff00) + (abyte0[8] << 16 & 0xff0000) + (abyte0[9] << 24 & 0xff000000);
206 short word1 = (short)((abyte0[12] & 0xff) + (abyte0[13] << 8 & 0xff00));
207 String s1 = new String(abyte0, 14, word1 - 1);
208 logMessage("Message from [" + l + "]: " + s1);
209 if(s1 != null)
210 {
211 String s2 = Multiplexor.getResponse(s1, l + "_ICQ", botID, new TextResponder());
212 sendMesg(l, s2);
213 }
214 return;
215 } else
216 {
217 return;
218 }
219 }
220
221 public static byte[] toBytes(short word0)
222 {
223 byte abyte0[] = new byte[2];
224 abyte0[0] = (byte)(word0 & 0xff);
225 abyte0[1] = (byte)(word0 >> 8 & 0xff);
226 return abyte0;
227 }
228
229 public static byte[] toBytes(int i)
230 {
231 byte abyte0[] = new byte[4];
232 abyte0[0] = (byte)(i & 0xff);
233 abyte0[1] = (byte)(i >> 8 & 0xff);
234 abyte0[2] = (byte)(i >> 16 & 0xff);
235 abyte0[3] = (byte)(i >> 24 & 0xff);
236 return abyte0;
237 }
238
239 public byte[] header(short word0)
240 throws IOException
241 {
242 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
243 bytearrayoutputstream.write(toBytes((short)2));
244 bytearrayoutputstream.write(toBytes(word0));
245 bytearrayoutputstream.write(toBytes(seqNo));
246 bytearrayoutputstream.write(toBytes(uin));
247 seqNo = (short)(seqNo + 1 & 0xffff);
248 return bytearrayoutputstream.toByteArray();
249 }
250
251 public void ack(byte byte0, byte byte1)
252 throws IOException
253 {
254 Log.devinfo("AliceICQ: Acknowledgement from server!", Log.LISTENERS);
255 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
256 bytearrayoutputstream.write(toBytes((short)2));
257 bytearrayoutputstream.write(toBytes((short)10));
258 bytearrayoutputstream.write(byte0);
259 bytearrayoutputstream.write(byte1);
260 bytearrayoutputstream.write(toBytes(uin));
261 socket.send(new DatagramPacket(bytearrayoutputstream.toByteArray(), bytearrayoutputstream.size(), serverAddy, 4000));
262 }
263
264 public void send(byte abyte0[])
265 throws IOException
266 {
267 socket.send(new DatagramPacket(abyte0, abyte0.length, serverAddy, 4000));
268 }
269
270 public void sendMesg(int i, String s)
271 {
272 logMessage("Response to [" + i + "]: " + s);
273 try
274 {
275 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
276 bytearrayoutputstream.write(14);
277 bytearrayoutputstream.write(1);
278 bytearrayoutputstream.write(toBytes(i));
279 bytearrayoutputstream.write(1);
280 bytearrayoutputstream.write(0);
281 bytearrayoutputstream.write(toBytes((short)s.length()));
282 bytearrayoutputstream.write(s.getBytes());
283 bytearrayoutputstream.write(0);
284 toICQ(bytearrayoutputstream.toByteArray());
285 }
286 catch(IOException ioexception)
287 {
288 logMessage("IO exception!");
289 }
290 }
291
292 public void signoff()
293 {
294 online = false;
295 logMessage("Signing off.");
296 try
297 {
298 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
299 bytearrayoutputstream.write(header((short)1080));
300 bytearrayoutputstream.write(toBytes((short)32));
301 bytearrayoutputstream.write((new String("B_USER_DISCONNECTED\0")).getBytes());
302 bytearrayoutputstream.write(toBytes((short)5));
303 socket.send(new DatagramPacket(bytearrayoutputstream.toByteArray(), bytearrayoutputstream.size(), serverAddy, 4000));
304 bytearrayoutputstream.close();
305 }
306 catch(IOException ioexception)
307 {
308 logMessage("IO exception while trying to sign off!");
309 }
310 socket.close();
311 logMessage("Signed off.");
312 }
313
314 private void logMessage(String s)
315 {
316 Log.userinfo("AliceICQ: " + s, Log.LISTENERS);
317 }
318
319 private static final int SERVERPORT = 4000;
320 private static final String SERVER = "icq.mirabilis.com";
321 private static final String _ICQ = "_ICQ";
322 private String pass;
323 private int uin;
324 protected DatagramSocket socket;
325 private DatagramPacket packet;
326 private byte buffer[];
327 private short seqNo;
328 private static final short VERSION = 2;
329 private static InetAddress serverAddy;
330 private boolean online;
331 private int clientport;
332 private static final String MSG = "AliceICQ: ";
333 public static final String label = "AliceICQ";
334 }