1 /*****************************************************/
\r
2 /* This java file is a part of the */
\r
4 /* - Plouf's Java IRC Client - */
\r
6 /* Copyright (C) 2002 - 2005 Philippe Detournay */
\r
8 /* All contacts : theplouf@yahoo.com */
\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
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
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
28 /*****************************************************/
\r
32 import java.util.Enumeration;
\r
35 * Basic interpretor.
\r
37 public class BasicInterpretor extends RootInterpretor implements Interpretor {
\r
39 * Create a new BasicInterpretor without default interpretor.
\r
42 * the configuration.
\r
44 public BasicInterpretor(IRCConfiguration config) {
\r
49 * Create a new BasicInterpretor.
\r
52 * the configuration.
\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
57 public BasicInterpretor(IRCConfiguration config, Interpretor next) {
\r
58 super(config, next);
\r
62 * Handle the received command.
\r
65 * the source that emitted the command.
\r
67 * the hole command line.
\r
69 * the parsed command line.
\r
71 * the cumul parsed command line.
\r
74 protected void handleCommand(Source source, String cmd, String[] parts, String[] cumul) {
\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
83 int ms = (new Integer(parts[1])).intValue();
\r
85 } catch (Exception ex) {
\r
86 // Invalid integer or interrupted, ignore it...
\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
102 source.report(getText(IRCTextProvider.INTERPRETOR_NOT_ON_CHANNEL));
\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
109 _ircConfiguration.getURLHandler().openURL(parts[1]);
\r
110 } else if (cmd.equals("clear")) {
\r
112 } else if (cmd.equals("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
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
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
147 server.execute(cumul[2]);
\r
150 super.handleCommand(source, cmd, parts, cumul);
\r
152 } catch (NotEnoughParametersException ex) {
\r
153 source.report(getText(IRCTextProvider.INTERPRETOR_INSUFFICIENT_PARAMETERS, ex.getMessage()));
\r