1 /*****************************************************/
2 /* This java file is a part of the */
4 /* - Plouf's Java IRC Client - */
6 /* Copyright (C) 2002 - 2005 Philippe Detournay */
8 /* All contacts : theplouf@yahoo.com */
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 */
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. */
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 */
28 /*****************************************************/
32 import java.awt.Color;
34 import java.util.StringTokenizer;
35 import java.util.Vector;
50 * Optionnal server password.
56 * Toolkit for Configuration creation.
58 public class ConfigurationLoader {
59 private ParameterProvider _provider;
60 private URLHandler _handler;
61 private ImageLoader _loader;
62 private SoundHandler _sound;
63 private FileHandler _file;
66 * Create a new IRCConfigurationLoader.
69 * parameter provider to load the configuration from.
79 public ConfigurationLoader(ParameterProvider provider, URLHandler handler, ImageLoader loader, SoundHandler sound,
89 * Create a new IRCConfiguration object, using the given ParameterProvider.
91 * @return a new IRCConfiguration instance.
95 public IRCConfiguration loadIRCConfiguration() throws Exception {
96 return getIRCConfiguration();
100 * Create a new StartupConfiguration object, using the given
103 * @return a new StartupConfiguration instance.
105 * if a mandatory parameter is not supplied or if an error occurs.
107 public StartupConfiguration loadStartupConfiguration() throws Exception {
108 return getStartupConfiguration();
111 private String getParameter(String key) {
112 return _provider.getParameter(key);
115 private boolean getBoolean(String key, boolean def) {
116 String v = getParameter(key);
119 v = v.toLowerCase(java.util.Locale.ENGLISH).trim();
120 if (v.equals("true") || v.equals("on") || v.equals("1"))
125 private String getString(String key, String def) {
126 String v = getParameter(key);
132 private int getInt(String key, int def) {
133 String v = getParameter(key);
137 return Integer.parseInt(v);
138 } catch (Exception e) {
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++) {
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);
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);
170 private String[] getArray(String name) {
171 Vector v = new Vector();
175 cmd = getParameter(name + i);
177 v.insertElementAt(cmd, v.size());
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);
186 private void readSmileys(IRCConfiguration config) {
187 String[] arr = getArray("style:smiley");
188 for (int i = 0; i < arr.length; i++) {
190 int pos = cmd.indexOf(" ");
192 String match = cmd.substring(0, pos).trim();
193 String file = cmd.substring(pos + 1).trim();
194 config.addSmiley(match, file);
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++) {
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);
211 if (fname.startsWith("'") && fname.endsWith("'"))
212 fname = fname.substring(1, fname.length() - 1);
214 int fsize = new Integer(back[3].toLowerCase(java.util.Locale.ENGLISH)).intValue();
215 config.setFont(type, name, new Font(fname, Font.PLAIN, fsize));
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())
227 String type = (String) tok.nextElement();
228 if (!tok.hasMoreElements())
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('=');
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))
245 config.setSourceColor(type, name, c);
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++) {
259 int pos = cmd.indexOf(' ');
261 String word = cmd.substring(0, pos).trim();
262 String sound = cmd.substring(pos + 1).trim();
263 ac.setWord(word, sound);
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 + ":"));
273 config.setJoinList(getString("authorizedjoinlist", ""));
274 config.setLeaveList(getString("authorizedleavelist", ""));
275 config.setCommandList(getString("authorizedcommandlist", ""));
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));
284 config.set("style:highlightlinks", getBoolean("style:highlightlinks", false));
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));
308 config.setInitialisation(getArray("init"));
310 readBackgroundConfig(config);
312 configureFonts(config);
313 configureTextColors(config);
319 private ServerItem[] readServers(String dhost, int dport, String dpass) {
320 Vector res = new Vector();
321 ServerItem item = new ServerItem();
323 item.port = new Integer(dport).intValue();
325 res.insertElementAt(item, res.size());
327 String[] arr = getArray("alternateserver");
328 for (int i = 0; i < arr.length; i++) {
330 int pos = cmd.indexOf(" ");
332 String host = cmd.substring(0, pos).trim();
333 String port = cmd.substring(pos + 1).trim();
335 pos = port.indexOf(" ");
337 pass = port.substring(pos + 1).trim();
338 port = port.substring(0, pos).trim();
340 item = new ServerItem();
342 item.port = new Integer(port).intValue();
344 res.insertElementAt(item, res.size());
347 ServerItem[] ans = new ServerItem[res.size()];
348 for (int i = 0; i < ans.length; i++)
349 ans[i] = (ServerItem) res.elementAt(i);
353 private StartupConfiguration getStartupConfiguration() throws Exception {
354 String nick = getParameter("nick");
356 throw new Exception("Mandatory 'nick' parameter not provided");
357 String name = getParameter("name");
359 name = getParameter("fullname");
361 throw new Exception("Mandatory 'fullname' parameter not provided");
362 String host = getParameter("host");
364 throw new Exception("Mandatory 'host' parameter not provided");
365 String pass = getParameter("password");
368 String sport = getParameter("port");
371 int port = new Integer(sport).intValue();
372 String altNick = getParameter("alternatenick");
374 altNick = nick + "?";
375 String alias = getParameter("serveralias");
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;
388 return new StartupConfiguration(nick, altNick, name, passs, hosts, ports, alias, getArray("command"),