]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/RootInterpretor.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / RootInterpretor.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 - 2004 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 /**\r
33  * Root interpretor.\r
34  */\r
35 public abstract class RootInterpretor extends IRCObject implements Interpretor {\r
36         /**\r
37          * The used string parser.\r
38          */\r
39         protected StringParser _parser;\r
40 \r
41         /**\r
42          * The next interpretor to be used if the command is unknown.\r
43          */\r
44         protected Interpretor _next;\r
45 \r
46         /**\r
47          * Create a new BasicInterpretor without default interpretor.\r
48          * \r
49          * @param config\r
50          *          the configuration.\r
51          */\r
52         public RootInterpretor(IRCConfiguration config) {\r
53                 this(config, null);\r
54         }\r
55 \r
56         /**\r
57          * Create a new BasicInterpretor.\r
58          * \r
59          * @param config\r
60          *          the configuration.\r
61          * @param next\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
64          */\r
65         public RootInterpretor(IRCConfiguration config, Interpretor next) {\r
66                 super(config);\r
67                 setNextInterpretor(next);\r
68                 _parser = new StringParser();\r
69         }\r
70 \r
71         @Override\r
72         public void setNextInterpretor(Interpretor next) {\r
73                 _next = next;\r
74         }\r
75 \r
76         @Override\r
77         public Interpretor getNextInterpretor() {\r
78                 return _next;\r
79         }\r
80 \r
81         @Override\r
82         public boolean isInside(Interpretor in) {\r
83                 while (in != null) {\r
84                         if (in == this)\r
85                                 return true;\r
86                         in = in.getNextInterpretor();\r
87                 }\r
88                 return false;\r
89         }\r
90 \r
91         @Override\r
92         public void addLast(Interpretor in) {\r
93                 if (isInside(in))\r
94                         return;\r
95                 Interpretor c = this;\r
96                 while (c.getNextInterpretor() != null)\r
97                         c = c.getNextInterpretor();\r
98                 c.setNextInterpretor(in);\r
99         }\r
100 \r
101         /**\r
102          * Test the given command against the expected number of parameters.\r
103          * \r
104          * @param cmd\r
105          *          the hole command line.\r
106          * @param parts\r
107          *          the parsed command line.\r
108          * @param params\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
113          */\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
117         }\r
118 \r
119         /**\r
120          * Handle the received command.\r
121          * \r
122          * @param source\r
123          *          the source that emitted the command.\r
124          * @param cmd\r
125          *          the hole command line.\r
126          * @param parts\r
127          *          the parsed command line.\r
128          * @param cumul\r
129          *          the cumul parsed command line.\r
130          */\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
136                         else\r
137                                 source.report(getText(IRCTextProvider.SERVER_NOT_CONNECTED));\r
138                 } else\r
139                         _next.sendString(source, "/" + cumul[0]);\r
140         }\r
141 \r
142         @Override\r
143         public void sendString(Source source, String str) {\r
144                 if (str.length() == 0)\r
145                         return;\r
146 \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
152                                 cumul[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
156                         }\r
157 \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
163                         }\r
164 \r
165                         String cmd = parts[0];\r
166                         handleCommand(source, cmd.toLowerCase(java.util.Locale.ENGLISH), parts, cumul);\r
167                 } else {\r
168                         say(source, str);\r
169                 }\r
170         }\r
171 \r
172         /**\r
173          * Say the given text.\r
174          * \r
175          * @param source\r
176          *          the source.\r
177          * @param str\r
178          *          what to say.\r
179          */\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
185                 } else {\r
186                         source.report(getText(IRCTextProvider.INTERPRETOR_NOT_ON_CHANNEL));\r
187                 }\r
188         }\r
189 \r
190 }\r