]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/ConfigurationLoader.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / ConfigurationLoader.java
1 /*****************************************************/
2 /*          This java file is a part of the          */
3 /*                                                   */
4 /*           -  Plouf's Java IRC Client  -           */
5 /*                                                   */
6 /*   Copyright (C)  2002 - 2005 Philippe Detournay   */
7 /*                                                   */
8 /*         All contacts : theplouf@yahoo.com         */
9 /*                                                   */
10 /*  PJIRC is free software; you can redistribute     */
11 /*  it and/or modify it under the terms of the GNU   */
12 /*  General Public License as published by the       */
13 /*  Free Software Foundation; version 2 or later of  */
14 /*  the License.                                     */
15 /*                                                   */
16 /*  PJIRC is distributed in the hope that it will    */
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */
18 /*  even the implied warranty of MERCHANTABILITY or  */
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */
20 /*  General Public License for more details.         */
21 /*                                                   */
22 /*  You should have received a copy of the GNU       */
23 /*  General Public License along with PJIRC; if      */
24 /*  not, write to the Free Software Foundation,      */
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */
26 /*  MA  02111-1307  USA                              */
27 /*                                                   */
28 /*****************************************************/
29
30 package irc;
31
32 import java.awt.Color;
33 import java.awt.Font;
34 import java.util.StringTokenizer;
35 import java.util.Vector;
36
37 /**
38  * A single server.
39  */
40 class ServerItem {
41         /**
42          * Hostname.
43          */
44         public String host;
45         /**
46          * Server post.
47          */
48         public int port;
49         /**
50          * Optionnal server password.
51          */
52         public String pass;
53 }
54
55 /**
56  * Toolkit for Configuration creation.
57  */
58 public class ConfigurationLoader {
59         private ParameterProvider _provider;
60         private URLHandler _handler;
61         private ImageLoader _loader;
62         private SoundHandler _sound;
63         private FileHandler _file;
64
65         /**
66          * Create a new IRCConfigurationLoader.
67          * 
68          * @param provider
69          *          parameter provider to load the configuration from.
70          * @param handler
71          *          URL handler.
72          * @param loader
73          *          Image loader.
74          * @param sound
75          *          Sound handler.
76          * @param file
77          *          File handler.
78          */
79         public ConfigurationLoader(ParameterProvider provider, URLHandler handler, ImageLoader loader, SoundHandler sound,
80                         FileHandler file) {
81                 _provider = provider;
82                 _handler = handler;
83                 _loader = loader;
84                 _sound = sound;
85                 _file = file;
86         }
87
88         /**
89          * Create a new IRCConfiguration object, using the given ParameterProvider.
90          * 
91          * @return a new IRCConfiguration instance.
92          * @throws Exception
93          *           if an error occurs.
94          */
95         public IRCConfiguration loadIRCConfiguration() throws Exception {
96                 return getIRCConfiguration();
97         }
98
99         /**
100          * Create a new StartupConfiguration object, using the given
101          * ParameterProvider.
102          * 
103          * @return a new StartupConfiguration instance.
104          * @throws Exception
105          *           if a mandatory parameter is not supplied or if an error occurs.
106          */
107         public StartupConfiguration loadStartupConfiguration() throws Exception {
108                 return getStartupConfiguration();
109         }
110
111         private String getParameter(String key) {
112                 return _provider.getParameter(key);
113         }
114
115         private boolean getBoolean(String key, boolean def) {
116                 String v = getParameter(key);
117                 if (v == null)
118                         return def;
119                 v = v.toLowerCase(java.util.Locale.ENGLISH).trim();
120                 if (v.equals("true") || v.equals("on") || v.equals("1"))
121                         return true;
122                 return false;
123         }
124
125         private String getString(String key, String def) {
126                 String v = getParameter(key);
127                 if (v == null)
128                         return def;
129                 return v;
130         }
131
132         private int getInt(String key, int def) {
133                 String v = getParameter(key);
134                 if (v == null)
135                         return def;
136                 try {
137                         return Integer.parseInt(v);
138                 } catch (Exception e) {
139                         return def;
140                 }
141
142         }
143
144         private void readBackgroundConfig(IRCConfiguration config) {
145                 StringParser parser = new StringParser();
146                 String[] arr = getArray("style:backgroundimage");
147                 for (int i = 0; i < arr.length; i++) {
148                         String cmd = arr[i];
149                         String back[] = parser.parseString(cmd);
150                         if (back.length >= 4) {
151                                 String type = back[0];
152                                 String name = back[1];
153                                 int tiling = new Integer(back[2]).intValue();
154                                 String image = back[3];
155                                 config.setBackgroundImage(type, name, image);
156                                 config.setBackgroundTiling(type, name, tiling);
157                         }
158                 }
159         }
160
161         private TextProvider getTextProvider() {
162                 String lang = getString("language", "english");
163                 String encoding = getString("languageencoding", "");
164                 String extension = getString("lngextension", "lng");
165                 String backlang = getString("backuplanguage", "english");
166                 String backencoding = getString("backuplanguageencoding", "");
167                 return new FileTextProvider(lang + "." + extension, encoding, backlang + "." + extension, backencoding, _file);
168         }
169
170         private String[] getArray(String name) {
171                 Vector v = new Vector();
172                 String cmd;
173                 int i = 1;
174                 do {
175                         cmd = getParameter(name + i);
176                         if (cmd != null)
177                                 v.insertElementAt(cmd, v.size());
178                         i++;
179                 } while (cmd != null);
180                 String[] ans = new String[v.size()];
181                 for (i = 0; i < v.size(); i++)
182                         ans[i] = (String) v.elementAt(i);
183                 return ans;
184         }
185
186         private void readSmileys(IRCConfiguration config) {
187                 String[] arr = getArray("style:smiley");
188                 for (int i = 0; i < arr.length; i++) {
189                         String cmd = arr[i];
190                         int pos = cmd.indexOf(" ");
191                         if (pos != -1) {
192                                 String match = cmd.substring(0, pos).trim();
193                                 String file = cmd.substring(pos + 1).trim();
194                                 config.addSmiley(match, file);
195                         }
196                 }
197         }
198
199         private void configureFonts(IRCConfiguration config) {
200                 // type name fname fsize
201                 StringParser parser = new StringParser();
202                 String[] arr = getArray("style:sourcefontrule");
203                 for (int i = 0; i < arr.length; i++) {
204                         String cmd = arr[i];
205                         String back[] = parser.parseString(cmd);
206                         if (back.length >= 4) {
207                                 String type = back[0].toLowerCase(java.util.Locale.ENGLISH);
208                                 String name = back[1].toLowerCase(java.util.Locale.ENGLISH);
209                                 String fname = back[2].toLowerCase(java.util.Locale.ENGLISH);
210
211                                 if (fname.startsWith("'") && fname.endsWith("'"))
212                                         fname = fname.substring(1, fname.length() - 1);
213
214                                 int fsize = new Integer(back[3].toLowerCase(java.util.Locale.ENGLISH)).intValue();
215                                 config.setFont(type, name, new Font(fname, Font.PLAIN, fsize));
216                         }
217                 }
218         }
219
220         private void configureTextColors(IRCConfiguration config) {
221                 // type name {index=value}*
222                 String[] arr = getArray("style:sourcecolorrule");
223                 for (int i = 0; i < arr.length; i++) {
224                         StringTokenizer tok = new StringTokenizer(arr[i]);
225                         if (!tok.hasMoreElements())
226                                 continue;
227                         String type = (String) tok.nextElement();
228                         if (!tok.hasMoreElements())
229                                 continue;
230                         String name = (String) tok.nextElement();
231                         Color[] c = new Color[16];
232                         config.loadDefaultColors(c);
233                         while (tok.hasMoreElements()) {
234                                 String s = (String) tok.nextElement();
235                                 int pos = s.indexOf('=');
236                                 if (pos < 0)
237                                         continue;
238                                 String before = s.substring(0, pos).trim();
239                                 String after = s.substring(pos + 1).trim();
240                                 int index = Integer.parseInt(before);
241                                 Color col = new Color(Integer.parseInt(after, 16));
242                                 if ((index >= 0) && (index <= 15))
243                                         c[index] = col;
244                         }
245                         config.setSourceColor(type, name, c);
246                 }
247         }
248
249         private void readSound(IRCConfiguration config) {
250                 AudioConfiguration ac = config.getAudioConfiguration();
251                 if (getParameter("soundbeep") != null)
252                         ac.setBeep(getParameter("soundbeep"));
253                 if (getParameter("soundquery") != null)
254                         ac.setQuery(getParameter("soundquery"));
255                 String[] arr = getArray("soundword");
256                 for (int i = 0; i < arr.length; i++) {
257                         String cmd = arr[i];
258                         cmd = cmd.trim();
259                         int pos = cmd.indexOf(' ');
260                         if (pos != -1) {
261                                 String word = cmd.substring(0, pos).trim();
262                                 String sound = cmd.substring(pos + 1).trim();
263                                 ac.setWord(word, sound);
264                         }
265                 }
266         }
267
268         private IRCConfiguration getIRCConfiguration() throws Exception {
269                 String gui = getString("gui", "notprovided");
270                 IRCConfiguration config = new IRCConfiguration(getTextProvider(), _handler, _loader, _sound, _file, _provider,
271                                 new PrefixedParameterProvider(_provider, gui + ":"));
272
273                 config.setJoinList(getString("authorizedjoinlist", ""));
274                 config.setLeaveList(getString("authorizedleavelist", ""));
275                 config.setCommandList(getString("authorizedcommandlist", ""));
276
277                 config.set("style:floatingasl", getBoolean("style:floatingasl", false));
278                 config.set("style:floatingaslalpha", getInt("style:floatingaslalpha", 170));
279                 config.set("style:backgroundimage", getBoolean("style:backgroundimage", false));
280                 config.set("style:bitmapsmileys", getBoolean("style:bitmapsmileys", false));
281                 config.set("style:linespacing", getInt("style:linespacing", 0));
282                 config.set("style:maximumlinecount", getInt("style:maximumlinecount", 1024));
283
284                 config.set("style:highlightlinks", getBoolean("style:highlightlinks", false));
285
286                 config.set("aslseparatorstring", getString("aslseparatorstring", ""));
287                 config.set("noasldisplayprefix", getString("noasldisplayprefix", ""));
288                 config.set("quitmessage", getString("quitmessage", ""));
289                 config.set("asl", getBoolean("asl", false));
290                 config.set("aslmale", getString("aslmale", "m"));
291                 config.set("aslfemale", getString("aslfemale", "f"));
292                 config.set("useinfo", getBoolean("useinfo", false));
293                 config.set("coding", getInt("coding", 1));
294                 config.set("userid", getString("userid", ""));
295                 config.set("style:righttoleft", getBoolean("style:righttoleft", false));
296                 config.set("autoconnection", getBoolean("autoconnection", true));
297                 config.set("useidentserver", getBoolean("useidentserver", true));
298                 config.set("multiserver", getBoolean("multiserver", false));
299                 config.set("aslunknown", getString("aslunknown", "x"));
300                 config.set("gui", getString("gui", null));
301                 config.set("fingerreply", getString("fingerreply", "A lucky Plouf's IRC user"));
302                 config.set("userinforeply", getString("userinforeply", "A lucky Plouf's IRC user"));
303                 config.set("allowdccchat", getBoolean("allowdccchat", true));
304                 config.set("allowdccfile", getBoolean("allowdccfile", true));
305                 config.set("disablequeries", getBoolean("disablequeries", false));
306                 config.set("autorejoin", getBoolean("autorejoin", false));
307
308                 config.setInitialisation(getArray("init"));
309
310                 readBackgroundConfig(config);
311                 readSmileys(config);
312                 configureFonts(config);
313                 configureTextColors(config);
314                 readSound(config);
315
316                 return config;
317         }
318
319         private ServerItem[] readServers(String dhost, int dport, String dpass) {
320                 Vector res = new Vector();
321                 ServerItem item = new ServerItem();
322                 item.host = dhost;
323                 item.port = new Integer(dport).intValue();
324                 item.pass = dpass;
325                 res.insertElementAt(item, res.size());
326
327                 String[] arr = getArray("alternateserver");
328                 for (int i = 0; i < arr.length; i++) {
329                         String cmd = arr[i];
330                         int pos = cmd.indexOf(" ");
331                         if (pos >= 0) {
332                                 String host = cmd.substring(0, pos).trim();
333                                 String port = cmd.substring(pos + 1).trim();
334                                 String pass = "";
335                                 pos = port.indexOf(" ");
336                                 if (pos >= 0) {
337                                         pass = port.substring(pos + 1).trim();
338                                         port = port.substring(0, pos).trim();
339                                 }
340                                 item = new ServerItem();
341                                 item.host = host;
342                                 item.port = new Integer(port).intValue();
343                                 item.pass = pass;
344                                 res.insertElementAt(item, res.size());
345                         }
346                 }
347                 ServerItem[] ans = new ServerItem[res.size()];
348                 for (int i = 0; i < ans.length; i++)
349                         ans[i] = (ServerItem) res.elementAt(i);
350                 return ans;
351         }
352
353         private StartupConfiguration getStartupConfiguration() throws Exception {
354                 String nick = getParameter("nick");
355                 if (nick == null)
356                         throw new Exception("Mandatory 'nick' parameter not provided");
357                 String name = getParameter("name");
358                 if (name == null)
359                         name = getParameter("fullname");
360                 if (name == null)
361                         throw new Exception("Mandatory 'fullname' parameter not provided");
362                 String host = getParameter("host");
363                 if (host == null)
364                         throw new Exception("Mandatory 'host' parameter not provided");
365                 String pass = getParameter("password");
366                 if (pass == null)
367                         pass = "";
368                 String sport = getParameter("port");
369                 if (sport == null)
370                         sport = "6667";
371                 int port = new Integer(sport).intValue();
372                 String altNick = getParameter("alternatenick");
373                 if (altNick == null)
374                         altNick = nick + "?";
375                 String alias = getParameter("serveralias");
376                 if (alias == null)
377                         alias = "";
378
379                 ServerItem[] items = readServers(host, port, pass);
380                 String[] hosts = new String[items.length];
381                 int[] ports = new int[items.length];
382                 String[] passs = new String[items.length];
383                 for (int i = 0; i < items.length; i++) {
384                         hosts[i] = items[i].host;
385                         ports[i] = items[i].port;
386                         passs[i] = items[i].pass;
387                 }
388                 return new StartupConfiguration(nick, altNick, name, passs, hosts, ports, alias, getArray("command"),
389                                 getArray("plugin"));
390         }
391
392 }