]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/BasicInterpretor.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / BasicInterpretor.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2005 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc;\r
31 \r
32 import java.util.Enumeration;\r
33 \r
34 /**\r
35  * Basic interpretor.\r
36  */\r
37 public class BasicInterpretor extends RootInterpretor implements Interpretor {\r
38         /**\r
39          * Create a new BasicInterpretor without default interpretor.\r
40          * \r
41          * @param config\r
42          *          the configuration.\r
43          */\r
44         public BasicInterpretor(IRCConfiguration config) {\r
45                 this(config, null);\r
46         }\r
47 \r
48         /**\r
49          * Create a new BasicInterpretor.\r
50          * \r
51          * @param config\r
52          *          the configuration.\r
53          * @param next\r
54          *          next interpretor to be used if the command is unknown. If null,\r
55          *          the command will be sent as it to the server.\r
56          */\r
57         public BasicInterpretor(IRCConfiguration config, Interpretor next) {\r
58                 super(config, next);\r
59         }\r
60 \r
61         /**\r
62          * Handle the received command.\r
63          * \r
64          * @param source\r
65          *          the source that emitted the command.\r
66          * @param cmd\r
67          *          the hole command line.\r
68          * @param parts\r
69          *          the parsed command line.\r
70          * @param cumul\r
71          *          the cumul parsed command line.\r
72          */\r
73         @Override\r
74         protected void handleCommand(Source source, String cmd, String[] parts, String[] cumul) {\r
75                 try {\r
76                         Server server = source.getServer();\r
77                         if (cmd.equals("echo")) {\r
78                                 test(cmd, parts, 1);\r
79                                 source.report(cumul[1]);\r
80                         } else if (cmd.equals("sleep")) {\r
81                                 test(cmd, parts, 1);\r
82                                 try {\r
83                                         int ms = (new Integer(parts[1])).intValue();\r
84                                         Thread.sleep(ms);\r
85                                 } catch (Exception ex) {\r
86                                         // Invalid integer or interrupted, ignore it...\r
87                                 }\r
88                         } else if (cmd.equals("me")) {\r
89                                 test(cmd, parts, 1);\r
90                                 sendString(source, "/ctcp action " + cumul[1]);\r
91                         } else if (cmd.equals("beep")) {\r
92                                 _ircConfiguration.getAudioConfiguration().beep();\r
93                         } else if (cmd.equals("play")) {\r
94                                 test(cmd, parts, 1);\r
95                                 _ircConfiguration.getAudioConfiguration().play(parts[1]);\r
96                         } else if (cmd.equals("sound")) {\r
97                                 test(cmd, parts, 1);\r
98                                 if (source.talkable()) {\r
99                                         sendString(source, "/ctcp sound " + cumul[1]);\r
100                                         sendString(source, "/play " + cumul[1]);\r
101                                 } else {\r
102                                         source.report(getText(IRCTextProvider.INTERPRETOR_NOT_ON_CHANNEL));\r
103                                 }\r
104                         } else if (cmd.equals("url")) {\r
105                                 test(cmd, parts, 1);\r
106                                 if (parts.length >= 3)\r
107                                         _ircConfiguration.getURLHandler().openURL(parts[1], parts[2]);\r
108                                 else\r
109                                         _ircConfiguration.getURLHandler().openURL(parts[1]);\r
110                         } else if (cmd.equals("clear")) {\r
111                                 source.clear();\r
112                         } else if (cmd.equals("leave")) {\r
113                                 source.leave();\r
114                         } else if (cmd.equals("msg")) {\r
115                                 test(cmd, parts, 2);\r
116                                 boolean said = false;\r
117                                 Enumeration e = server.getSources();\r
118                                 while (e.hasMoreElements()) {\r
119                                         Source s = (Source) e.nextElement();\r
120                                         if (s.getName().equals(parts[1])) {\r
121                                                 say(s, cumul[2]);\r
122                                                 said = true;\r
123                                         }\r
124                                 }\r
125                                 if (!said)\r
126                                         server.say(parts[1], cumul[2]);\r
127                         } else if (cmd.equals("ping")) {\r
128                                 test(cmd, parts, 1);\r
129                                 sendString(source, "/ctcp ping " + cumul[1]);\r
130                         } else if (cmd.equals("dcc")) {\r
131                                 test(cmd, parts, 1);\r
132                                 sendString(source, "/ctcp dcc " + cumul[1]);\r
133                         } else if (cmd.equals("raw")) {\r
134                                 server.execute(cumul[1]);\r
135                         } else if (cmd.equals("version")) {\r
136                                 source.report("PJIRC v" + _ircConfiguration.getVersion());\r
137                         } else if (cmd.equals("gc")) {\r
138                                 System.gc();\r
139                                 source.report(Runtime.getRuntime().freeMemory() + " " + Runtime.getRuntime().totalMemory());\r
140                         } else if (cmd.equals("onserver")) {\r
141                                 test(cmd, parts, 2);\r
142                                 String s = parts[1];\r
143                                 if (s.equals(server.getServerName())) {\r
144                                         if ((cumul[2].startsWith("/")) && (server instanceof IRCServer))\r
145                                                 ((IRCServer) server).getStatus().sendString(cumul[2]);\r
146                                         else\r
147                                                 server.execute(cumul[2]);\r
148                                 }\r
149                         } else {\r
150                                 super.handleCommand(source, cmd, parts, cumul);\r
151                         }\r
152                 } catch (NotEnoughParametersException ex) {\r
153                         source.report(getText(IRCTextProvider.INTERPRETOR_INSUFFICIENT_PARAMETERS, ex.getMessage()));\r
154                 }\r
155         }\r
156 }\r