Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/alicebot/server/net/listener/AliceIRC.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.*;
15  
16  // Referenced classes of package org.alicebot.server.net.listener:
17  //            AliceChatListener
18  
19  public class AliceIRC extends AliceChatListener
20      implements ShellCommandable
21  {
22  
23      public AliceIRC(Bot bot)
24      {
25          super(bot, "AliceIRC", new String[][] {
26              new String[] {
27                  "host", ""
28              }, new String[] {
29                  "port", "6667"
30              }, new String[] {
31                  "nick", ""
32              }, new String[] {
33                  "channel", ""
34              }
35          });
36          clientStatus = 0;
37      }
38  
39      public boolean checkParameters()
40      {
41          host = (String)parameters.get("host");
42          try
43          {
44              port = Integer.parseInt((String)parameters.get("port"));
45          }
46          catch(NumberFormatException numberformatexception)
47          {
48              logMessage("Invalid port specification (try a number!); aborting.");
49              return false;
50          }
51          nick = (String)parameters.get("nick");
52          channel = (String)parameters.get("channel");
53          if(host.length() == 0)
54          {
55              logMessage("No host specified; aborting.");
56              return false;
57          }
58          if(port <= 0)
59              logMessage("Invalid port; aborting.");
60          if(nick.length() == 0)
61          {
62              logMessage("No nick specified; aborting.");
63              return false;
64          }
65          if(channel.length() == 0)
66          {
67              logMessage("No channel specified; aborting.");
68              return false;
69          } else
70          {
71              return true;
72          }
73      }
74  
75      public void shutdown()
76      {
77          disconnect();
78      }
79  
80      public void run()
81      {
82          logMessage("Starting for \"" + botID + "\".");
83          processMessageCommandClient("CONNECT", host + " " + port);
84          processMessage("/NICK " + nick);
85          processMessage("/JOIN " + channel);
86          listen();
87      }
88  
89      public String getShellID()
90      {
91          return "irc";
92      }
93  
94      public String getShellDescription()
95      {
96          return "Alice IRC chat listener";
97      }
98  
99      public String getShellCommands()
100     {
101         return "Not yet implemented.";
102     }
103 
104     public void processShellCommand(String s)
105     {
106         int i = s.indexOf('/');
107         if(i != 0)
108         {
109             logMessage("Invalid command.");
110             return;
111         }
112         int j = s.indexOf(' ');
113         if(j == -1)
114         {
115             processMessageCommand(s.substring(1), "");
116             return;
117         } else
118         {
119             processMessageCommand(s.substring(1, j), s.substring(j + 1));
120             return;
121         }
122     }
123 
124     public String getVersion()
125     {
126         return "0.86.0b 1999.04.07";
127     }
128 
129     private void connect()
130     {
131         if(clientStatus == 0)
132         {
133             clientStatus = 1;
134             logMessage("Contacting " + host + ":" + port);
135             try
136             {
137                 socket = new Socket(host, port);
138                 logMessage("Connected to " + host + ":" + port);
139             }
140             catch(UnknownHostException unknownhostexception)
141             {
142                 logMessage("Cannot connect; unknown server.");
143                 clientStatus = 0;
144             }
145             catch(IOException ioexception1)
146             {
147                 logMessage("Cannot connect; the server is down or not responding.");
148                 clientStatus = 0;
149             }
150             if(clientStatus == 1)
151                 try
152                 {
153                     reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
154                     writer = new PrintWriter(socket.getOutputStream(), true);
155                     clientStatus = 2;
156                 }
157                 catch(IOException ioexception)
158                 {
159                     logMessage("Cannot connect: I/O error.");
160                     clientStatus = 3;
161                     try
162                     {
163                         socket.close();
164                     }
165                     catch(IOException ioexception2) { }
166                     finally
167                     {
168                         socket = null;
169                         clientStatus = 0;
170                     }
171                 }
172         } else
173         {
174             switch(clientStatus)
175             {
176             case 2: // '\002'
177                 logMessage("Cannot connect again; already connected.");
178                 break;
179 
180             case 1: // '\001'
181                 logMessage("Cannot connect again; already in the process of connecting.");
182                 break;
183 
184             case 3: // '\003'
185                 logMessage("Cannot connect now; still trying to disconnect.");
186                 break;
187 
188             default:
189                 logMessage("Got unknown clientStatusCode: " + clientStatus);
190                 break;
191             }
192         }
193     }
194 
195     private void disconnect()
196     {
197         if(clientStatus == 2)
198         {
199             clientStatus = 3;
200             try
201             {
202                 socket.close();
203             }
204             catch(IOException ioexception)
205             {
206                 logMessage("IO exception trying to disconnect.");
207             }
208             finally
209             {
210                 reader = null;
211                 writer = null;
212                 logMessage("Connection closed.");
213                 clientStatus = 0;
214             }
215         } else
216         {
217             switch(clientStatus)
218             {
219             case 0: // '\0'
220                 logMessage("Cannot close connection; not connected.");
221                 break;
222 
223             case 1: // '\001'
224                 logMessage("Cannot close connection; currently trying to connect.");
225                 break;
226 
227             case 3: // '\003'
228                 logMessage("Cannot close connection; currently trying to close it.");
229                 break;
230 
231             case 2: // '\002'
232             default:
233                 logMessage("Got unknown clientStatusCode: " + clientStatus);
234                 break;
235             }
236         }
237     }
238 
239     protected void processMessage(String s)
240     {
241         if(s.length() > 0)
242             if(s.charAt(0) == '/')
243             {
244                 String s1;
245                 try
246                 {
247                     s1 = s.substring(1, s.indexOf(' '));
248                 }
249                 catch(StringIndexOutOfBoundsException stringindexoutofboundsexception)
250                 {
251                     s1 = s.substring(1);
252                 }
253                 if(!processMessageCommand(s1, s) && !processMessageCommandClient(s1, s))
254                 {
255                     if(!processMessageCommandDebug(s1, s));
256                     sendMessage("[irc]", "Unknown Command: " + s1);
257                 }
258             } else
259             if(clientStatus == 2 && channel.length() > 0)
260             {
261                 sendMessage("", "[" + nick + "] " + s);
262                 logMessage("Got a message from [" + nick + "]: " + s);
263                 sendServerMessage("/MSG  " + channel + " :" + s);
264                 String as[] = Toolkit.breakLines(Multiplexor.getResponse(s, nick + "_IRC", botID, new TextResponder()));
265                 if(as.length > 0)
266                 {
267                     for(int i = 0; i < as.length; i++)
268                         processMessage("/PRVMSG " + nick + " " + as[i]);
269 
270                 }
271             }
272     }
273 
274     private boolean processMessageCommand(String s, String s1)
275     {
276         boolean flag = false;
277         String s2 = s1.substring(s1.indexOf(' ') + 1);
278         if(s2.equals("/" + s))
279             s2 = "";
280         if(clientStatus == 2)
281         {
282             if(s.equalsIgnoreCase("AWAY"))
283             {
284                 sendServerMessage("AWAY :" + s2);
285                 flag = true;
286             } else
287             if(s.equalsIgnoreCase("INVITE"))
288             {
289                 if(s2.length() > 0)
290                     sendServerMessage("INVITE " + s2 + " " + channel);
291                 flag = true;
292             } else
293             if(s.equalsIgnoreCase("KICK"))
294             {
295                 if(s2.length() > 0)
296                 {
297                     int i = s2.indexOf(' ');
298                     int k = s2.indexOf(' ', i + 1);
299                     try
300                     {
301                         sendServerMessage("KICK " + channel + " " + s2.substring(0, k) + " :" + s2.substring(k + 1));
302                     }
303                     catch(StringIndexOutOfBoundsException stringindexoutofboundsexception1)
304                     {
305                         sendServerMessage("KICK " + channel + " " + s2);
306                     }
307                 }
308                 flag = true;
309             } else
310             if(s.equalsIgnoreCase("LIST"))
311             {
312                 if(s2.length() == 0)
313                     sendServerMessage("LIST " + channel);
314                 else
315                     sendServerMessage("LIST " + s2);
316                 flag = true;
317             } else
318             if(s.equalsIgnoreCase("JOIN"))
319             {
320                 if(s2.length() == 0)
321                 {
322                     if(channel.length() == 0)
323                         sendMessage("[irc]", "You're not in a channel.");
324                     else
325                         sendMessage("[irc]", "You're currently in: " + channel + ".");
326                 } else
327                 if(channel.length() == 0)
328                     sendServerMessage("JOIN " + s2);
329                 else
330                 if(s2.equals("0"))
331                 {
332                     sendServerMessage("PART " + channel);
333                 } else
334                 {
335                     sendServerMessage("PART " + channel);
336                     sendServerMessage("JOIN " + s2);
337                 }
338                 flag = true;
339             } else
340             if(s.equalsIgnoreCase("MODE"))
341             {
342                 if(s2.length() > 0)
343                     sendServerMessage("MODE " + channel + " " + s2);
344                 flag = true;
345             } else
346             if(s.equalsIgnoreCase("MSG"))
347             {
348                 if(s2.length() > 0)
349                 {
350                     try
351                     {
352                         int j = s2.indexOf(' ');
353                         sendServerMessage("PRIVMSG " + s2.substring(0, j) + " :" + s2.substring(j + 1));
354                     }
355                     catch(StringIndexOutOfBoundsException stringindexoutofboundsexception)
356                     {
357                         sendServerMessage("PRIVMSG " + s2);
358                     }
359                     sendMessage("", "*" + nick + "* " + s2);
360                 }
361                 flag = true;
362             } else
363             if(s.equalsIgnoreCase("NAMES"))
364             {
365                 if(s2.length() == 0)
366                     sendServerMessage("NAMES " + channel);
367                 else
368                     sendServerMessage("NAMES " + s2);
369                 flag = true;
370             } else
371             if(s.equalsIgnoreCase("NICK"))
372             {
373                 if(s2.length() == 0)
374                     sendMessage("[irc]", "You're currently known as " + nick);
375                 else
376                     sendServerMessage("NICK " + s2);
377                 flag = true;
378             } else
379             if(s.equalsIgnoreCase("QUIT"))
380             {
381                 if(s2.length() == 0)
382                     sendServerMessage("QUIT");
383                 else
384                     sendServerMessage("QUIT :" + s2);
385                 channel = "";
386                 disconnect();
387                 flag = true;
388             } else
389             if(s.equalsIgnoreCase("TOPIC"))
390             {
391                 if(channel.length() == 0)
392                     sendMessage("[irc]", "You must be in a channel to set the topic!");
393                 else
394                 if(s2.length() == 0)
395                     sendServerMessage("TOPIC " + channel);
396                 else
397                     sendServerMessage("TOPIC " + channel + " :" + s2);
398                 flag = true;
399             }
400         } else
401         if(s.equalsIgnoreCase("NICK"))
402         {
403             if(s2.length() == 0)
404             {
405                 sendMessage("[irc]", "You're currently known as " + nick);
406             } else
407             {
408                 nick = s2;
409                 sendMessage("[irc]", "You're now known as " + nick);
410             }
411             flag = true;
412         }
413         return flag;
414     }
415 
416     private boolean processMessageCommandClient(String s, String s1)
417     {
418         boolean flag = false;
419         int i = -1;
420         int ai[] = new int[17];
421         do
422             if(++i == 0)
423                 ai[i] = s1.indexOf(" ");
424             else
425                 ai[i] = s1.indexOf(" ", ai[i - 1] + 1);
426         while(ai[i] != -1);
427         String as[] = new String[16];
428         for(int j = 0; j < i; j++)
429             if(j + 1 >= i)
430                 as[j] = s1.substring(ai[j] + 1);
431             else
432                 as[j] = s1.substring(ai[j] + 1, ai[j + 1]);
433 
434         if(s.equalsIgnoreCase("SERVER") || s.equalsIgnoreCase("CONNECT"))
435         {
436             if(nick.length() == 0)
437                 sendMessage("[irc]", "You cannot connect to a server unless your NICK is set.");
438             else
439             if(i == 0)
440                 sendMessage("[irc]", "Please specify a server to connect to.");
441             else
442             if(i == 1)
443             {
444                 connect();
445                 if(clientStatus == 2)
446                 {
447                     sendServerMessage("USER " + nick + " " + socket.getInetAddress().getHostName() + " server :" + nick);
448                     sendServerMessage("NICK " + nick);
449                 }
450             } else
451             {
452                 try
453                 {
454                     connect();
455                     if(clientStatus == 2)
456                     {
457                         sendServerMessage("USER " + nick + " " + socket.getInetAddress().getHostName() + " server :" + nick);
458                         sendServerMessage("NICK " + nick);
459                     }
460                 }
461                 catch(NumberFormatException numberformatexception)
462                 {
463                     sendMessage("[irc]", "The Port you specified is invalid.");
464                 }
465             }
466             flag = true;
467         } else
468         if(s.equalsIgnoreCase("EXIT"))
469         {
470             if(clientStatus == 2)
471                 disconnect();
472             flag = true;
473         } else
474         if(s.equalsIgnoreCase("COMMANDS") || s.equalsIgnoreCase("HELP"))
475         {
476             sendMessage("", "");
477             sendMessage("[irc]", "sIRC Commands:");
478             sendMessage("[irc]", "  /away [<message>]");
479             sendMessage("[irc]", "  /commands");
480             sendMessage("[irc]", "  /connect <server> [<this.port>]");
481             sendMessage("[irc]", "  /exit");
482             sendMessage("[irc]", "  /invite <this.nickname>");
483             sendMessage("[irc]", "  /join [<channel> | '0']");
484             sendMessage("[irc]", "  /kick <this.nickname> [<message>]");
485             sendMessage("[irc]", "  /list [<channel>]");
486             sendMessage("[irc]", "  /mode <(+|-)mode> [<this.nickname>]");
487             sendMessage("[irc]", "  /msg <this.nickname> <message>");
488             sendMessage("[irc]", "  /names [<channel>]");
489             sendMessage("[irc]", "  /this.nick [<this.nickname>]");
490             sendMessage("[irc]", "  /quit [<message>]");
491             sendMessage("[irc]", "  /server <server> [<this.port>]");
492             sendMessage("[irc]", "  /topic [<topic>]");
493             sendMessage("[irc]", "  /version");
494             sendMessage("", "");
495             flag = true;
496         }
497         return flag;
498     }
499 
500     private boolean processMessageCommandDebug(String s, String s1)
501     {
502         boolean flag = false;
503         int i = -1;
504         int ai[] = new int[17];
505         do
506             if(++i == 0)
507                 ai[i] = s1.indexOf(" ");
508             else
509                 ai[i] = s1.indexOf(" ", ai[i - 1] + 1);
510         while(ai[i] != -1);
511         String as[] = new String[16];
512         for(int j = 0; j < i; j++)
513             if(j + 1 >= i)
514                 as[j] = s1.substring(ai[j] + 1);
515             else
516                 as[j] = s1.substring(ai[j] + 1, ai[j + 1]);
517 
518         if(s.equals("testargs"))
519         {
520             StringBuffer stringbuffer = new StringBuffer("Test Arguments; argc=" + i + " command=" + s);
521             for(int k = 0; k < i; k++)
522                 stringbuffer.append(" [" + (k + 1) + ";" + as[k] + "]");
523 
524             Trace.devinfo(stringbuffer.toString());
525             flag = true;
526         } else
527         if(s.equals("debug"))
528         {
529             Trace.devinfo("- - - - - - - - - - - - - - - - - - - - ");
530             Trace.devinfo("clientStatus=" + clientStatus);
531             Trace.devinfo("socket=" + socket);
532             Trace.devinfo("reader=" + reader);
533             Trace.devinfo("writer=" + writer);
534             Trace.devinfo("- - - - - - - - - - - - - - - - - - - - ");
535             flag = true;
536         } else
537         if(s.equals("raw"))
538         {
539             String s2 = "";
540             for(int l = 0; l < i; l++)
541                 s2 = s2 + as[l] + " ";
542 
543             sendServerMessage(s2);
544             flag = true;
545         }
546         return flag;
547     }
548 
549     private void processServerMessage(String s)
550     {
551         String s1 = "";
552         String s2 = "";
553         String s3 = "";
554         String s4 = "";
555         if(s == null)
556         {
557             disconnect();
558             return;
559         }
560         if(s.length() <= 0);
561         if(s.charAt(0) == ':')
562         {
563             int i = s.indexOf(' ');
564             int i1 = s.indexOf(' ', i + 1);
565             s1 = s.substring(0, i);
566             s2 = s.substring(i + 1, i1);
567             s3 = s.substring(i1 + 1);
568         } else
569         {
570             int j = s.indexOf(' ');
571             s2 = s.substring(0, j);
572             s3 = s.substring(j + 1);
573         }
574         if(s1.length() > 0)
575             try
576             {
577                 s4 = s1.substring(1, s1.indexOf('!'));
578             }
579             catch(StringIndexOutOfBoundsException stringindexoutofboundsexception)
580             {
581                 s4 = s1.substring(1);
582             }
583         try
584         {
585             switch(Integer.parseInt(s2))
586             {
587             case 322: 
588                 int k = s3.indexOf(' ');
589                 int j1 = s3.indexOf(' ', k + 1);
590                 int i2 = s3.indexOf(':');
591                 sendMessage("[server]", s3.substring(k + 1, j1) + ": " + s3.substring(j1 + 1, i2 - 1) + " " + s3.substring(i2 + 1));
592                 break;
593 
594             case 353: 
595                 int l = s3.indexOf(':');
596                 int k1 = s3.indexOf('=');
597                 sendMessage("[server]", "Users on " + s3.substring(k1 + 2, l - 1) + ": " + s3.substring(l + 1));
598                 break;
599 
600             case 372: 
601                 sendMessage("[server]", s3.substring(s3.indexOf(':') + 1));
602                 break;
603 
604             default:
605                 sendMessage("[server]", "(" + s2 + ") " + s3.substring(s3.indexOf(':') + 1));
606                 break;
607 
608             case 1: // '\001'
609             case 321: 
610                 break;
611             }
612         }
613         catch(NumberFormatException numberformatexception)
614         {
615             if(s2.equals("INVITE"))
616             {
617                 int l1 = s3.indexOf(' ');
618                 sendMessage("[server]", s4 + " has invited you to " + helpExtractIRCString(s3.substring(l1 + 1)) + ".");
619             } else
620             if(s2.equals("JOIN"))
621             {
622                 String s5 = helpExtractIRCString(s3);
623                 if(s4.equals(nick))
624                 {
625                     sendMessage("[server]", "You're now on " + s5 + ".");
626                     channel = s5;
627                 } else
628                 {
629                     sendMessage("[server]", s4 + " has joined the channel.");
630                 }
631             } else
632             if(s2.equals("KICK"))
633             {
634                 String s6 = "";
635                 String s11 = "";
636                 String s15 = "";
637                 int j2 = s3.indexOf(' ');
638                 int l2 = s3.indexOf(' ', j2 + 1);
639                 try
640                 {
641                     String s7 = s3.substring(0, j2);
642                     String s12 = s3.substring(j2 + 1, l2);
643                     String s16 = helpExtractIRCString(s3.substring(l2 + 1));
644                     if(s12.equals(nick))
645                     {
646                         sendMessage("[server]", "You've just been kicked off " + s7 + " by " + s4 + " (" + s16 + ").");
647                         channel = "";
648                     } else
649                     {
650                         sendMessage("[server]", s12 + " has been kicked off " + s7 + " by " + s4 + " (" + s16 + ").");
651                     }
652                 }
653                 catch(StringIndexOutOfBoundsException stringindexoutofboundsexception1)
654                 {
655                     String s8 = s3.substring(0, j2);
656                     String s13 = s3.substring(j2 + 1);
657                     if(s13.equals(nick))
658                     {
659                         sendMessage("[server]", "You've just been kicked off " + s8 + " by " + s4 + ".");
660                         channel = "";
661                     } else
662                     {
663                         sendMessage("[server]", s4 + " has been kicked off " + s8 + " by " + s4 + ".");
664                     }
665                 }
666             } else
667             if(s2.equals("NICK"))
668             {
669                 if(s4.equals(nick))
670                 {
671                     String s9 = helpExtractIRCString(s3);
672                     sendMessage("[server]", "You're now known as " + s9 + ".");
673                     nick = s9;
674                 } else
675                 {
676                     sendMessage("[server]", s4 + " is now known as " + s3.substring(1) + ".");
677                 }
678             } else
679             if(s2.equals("PART"))
680             {
681                 if(s4.equals(nick))
682                 {
683                     sendMessage("[server]", "You've just left " + s3 + ".");
684                     channel = "";
685                 } else
686                 {
687                     sendMessage("[server]", s4 + " has left the channel.");
688                 }
689             } else
690             if(s2.equals("PING"))
691                 sendServerMessage("PONG " + s3);
692             else
693             if(s2.equals("PRIVMSG"))
694             {
695                 String s10 = s3.substring(0, s3.indexOf(' '));
696                 String s14 = s3.substring(s3.indexOf(':') + 1);
697                 if(s10.equals(nick))
698                 {
699                     sendMessage("", "*" + s4 + "* " + s14);
700                     logMessage("Request: [" + s4 + "]: " + s14);
701                     String as[] = Toolkit.breakLines(Multiplexor.getResponse(s14, s4 + "_IRC", botID, new TextResponder()));
702                     if(as.length > 0)
703                     {
704                         for(int k2 = 0; k2 < as.length; k2++)
705                             processMessage("/MSG " + s4 + " " + as[k2]);
706 
707                     }
708                 } else
709                 {
710                     sendMessage("", "[" + s4 + "] " + s14);
711                 }
712             } else
713             if(s2.equals("QUIT"))
714             {
715                 if(s3.length() == 0)
716                     sendMessage("[server]", s4 + " has quit.");
717                 else
718                     sendMessage("[server]", s4 + " has quit (" + helpExtractIRCString(s3) + ").");
719             } else
720             if(s2.equals("TOPIC"))
721                 if(s4.equals(nick))
722                     sendMessage("[server]", "The topic is now: " + s3.substring(s3.indexOf(':') + 1));
723                 else
724                     sendMessage("[server]", s4 + " has set the topic to: " + helpExtractIRCString(s3.substring(s3.indexOf(' ') + 1)));
725         }
726     }
727 
728     private String helpExtractIRCString(String s)
729     {
730         try
731         {
732             if(s.charAt(0) == ':')
733                 return s.substring(1);
734             else
735                 return s;
736         }
737         catch(StringIndexOutOfBoundsException stringindexoutofboundsexception)
738         {
739             return "";
740         }
741     }
742 
743     private void logMessage(String s)
744     {
745         Log.userinfo("AliceIRC: " + s, Log.LISTENERS);
746     }
747 
748     private void sendMessage(String s, String s1)
749     {
750         Log.userinfo("AliceIRC: " + s + " " + s1, Log.LISTENERS);
751     }
752 
753     private void sendServerMessage(String s)
754     {
755         if(clientStatus == 2)
756             writer.println(s);
757     }
758 
759     private void listen()
760     {
761         while(clientStatus == 2) 
762             try
763             {
764                 String s = reader.readLine();
765                 processServerMessage(s);
766             }
767             catch(IOException ioexception) { }
768     }
769 
770     private static final String VERSION = "0.86.0b";
771     private static final String VERDATE = "1999.04.07";
772     private static final boolean DEBUG = false;
773     private static final int MAXARGC = 16;
774     private static final String SERVERPREFIX = "[server]";
775     private static final String SIRCMESSAGE = "[irc]";
776     private static final String DEBUGPREFIX = "[debug]";
777     private static final String NONE = "";
778     private static final byte NOTCONNECTED = 0;
779     private static final byte CONNECTING = 1;
780     private static final byte CONNECTED = 2;
781     private static final byte DISCONNECTING = 3;
782     private byte clientStatus;
783     private Socket socket;
784     private BufferedReader reader;
785     private PrintWriter writer;
786     private String host;
787     private String nick;
788     private String channel;
789     private int port;
790     private static final String MSG = "AliceIRC: ";
791     public static final String label = "AliceIRC";
792 }