1 /*****************************************************/
\r
2 /* This java file is a part of the */
\r
4 /* - Plouf's Java IRC Client - */
\r
6 /* Copyright (C) 2002 - 2004 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
35 public abstract class RootInterpretor extends IRCObject implements Interpretor {
\r
37 * The used string parser.
\r
39 protected StringParser _parser;
\r
42 * The next interpretor to be used if the command is unknown.
\r
44 protected Interpretor _next;
\r
47 * Create a new BasicInterpretor without default interpretor.
\r
50 * the configuration.
\r
52 public RootInterpretor(IRCConfiguration config) {
\r
57 * Create a new BasicInterpretor.
\r
60 * the configuration.
\r
62 * next interpretor to be used if the command is unknown. If null,
\r
63 * the command will be sent as it to the server.
\r
65 public RootInterpretor(IRCConfiguration config, Interpretor next) {
\r
67 setNextInterpretor(next);
\r
68 _parser = new StringParser();
\r
72 public void setNextInterpretor(Interpretor next) {
\r
77 public Interpretor getNextInterpretor() {
\r
82 public boolean isInside(Interpretor in) {
\r
83 while (in != null) {
\r
86 in = in.getNextInterpretor();
\r
92 public void addLast(Interpretor in) {
\r
95 Interpretor c = this;
\r
96 while (c.getNextInterpretor() != null)
\r
97 c = c.getNextInterpretor();
\r
98 c.setNextInterpretor(in);
\r
102 * Test the given command against the expected number of parameters.
\r
105 * the hole command line.
\r
107 * the parsed command line.
\r
109 * expected amount of parameters.
\r
110 * @throws NotEnoughParametersException
\r
111 * if there is not enough parameters. That's it : if
\r
112 * parts.length<=params.
\r
114 protected void test(String cmd, String[] parts, int params) throws NotEnoughParametersException {
\r
115 if (parts.length <= params)
\r
116 throw new NotEnoughParametersException(cmd);
\r
120 * Handle the received command.
\r
123 * the source that emitted the command.
\r
125 * the hole command line.
\r
127 * the parsed command line.
\r
129 * the cumul parsed command line.
\r
131 protected void handleCommand(Source source, String cmd, String[] parts, String[] cumul) {
\r
132 if (_next == null) {
\r
133 Server server = source.getServer();
\r
134 if (server.isConnected())
\r
135 server.execute(cumul[0]);
\r
137 source.report(getText(IRCTextProvider.SERVER_NOT_CONNECTED));
\r
139 _next.sendString(source, "/" + cumul[0]);
\r
143 public void sendString(Source source, String str) {
\r
144 if (str.length() == 0)
\r
147 if (str.startsWith("/")) {
\r
148 str = str.substring(1);
\r
149 String[] parts = _parser.parseString(str);
\r
150 String[] cumul = new String[parts.length];
\r
151 for (int i = 0; i < cumul.length; i++) {
\r
153 for (int j = i; j < parts.length; j++)
\r
154 cumul[i] += parts[j] + " ";
\r
155 cumul[i] = StringParser.trim(cumul[i]);
\r
158 for (int i = 0; i < parts.length; i++) {
\r
159 if (parts[i].startsWith("" + '"') && parts[i].endsWith("" + '"'))
\r
160 parts[i] = parts[i].substring(1, parts[i].length() - 1);
\r
161 else if (parts[i].startsWith("'") && parts[i].endsWith("'"))
\r
162 parts[i] = parts[i].substring(1, parts[i].length() - 1);
\r
165 String cmd = parts[0];
\r
166 handleCommand(source, cmd.toLowerCase(java.util.Locale.ENGLISH), parts, cumul);
\r
173 * Say the given text.
\r
180 protected void say(Source source, String str) {
\r
181 Server server = source.getServer();
\r
182 if (source.talkable()) {
\r
183 source.messageReceived(server.getNick(), str);
\r
184 server.say(source.getName(), str);
\r
186 source.report(getText(IRCTextProvider.INTERPRETOR_NOT_ON_CHANNEL));
\r