Set PACKAGE to BitlBee-LIBPURPLE for the libpurple variant, because in many
[bitlbee.git] / conf.c
blob3592f7693c8cfe73b6b8f2d46346eaaa20765cf6
1 /********************************************************************\
2 * BitlBee -- An IRC to other IM-networks gateway *
3 * *
4 * Copyright 2002-2005 Wilmer van der Gaast and others *
5 \********************************************************************/
7 /* Configuration reading code */
9 /*
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License with
21 the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22 if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 Suite 330, Boston, MA 02111-1307 USA
26 #include "bitlbee.h"
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include "conf.h"
32 #include "ini.h"
33 #include "url.h"
34 #include "ipc.h"
36 #include "proxy.h"
38 static int conf_loadini( conf_t *conf, char *file );
40 conf_t *conf_load( int argc, char *argv[] )
42 conf_t *conf;
43 int opt, i, config_missing = 0;
45 conf = g_new0( conf_t, 1 );
47 conf->iface_in = NULL;
48 conf->iface_out = NULL;
49 conf->port = g_strdup( "6667" );
50 conf->nofork = 0;
51 conf->verbose = 0;
52 conf->primary_storage = g_strdup( "xml" );
53 conf->migrate_storage = g_strsplit( "text", ",", -1 );
54 conf->runmode = RUNMODE_INETD;
55 conf->authmode = AUTHMODE_OPEN;
56 conf->auth_pass = NULL;
57 conf->oper_pass = NULL;
58 conf->configdir = g_strdup( CONFIG );
59 conf->plugindir = g_strdup( PLUGINDIR );
60 conf->pidfile = g_strdup( PIDFILE );
61 conf->motdfile = g_strdup( ETCDIR "/motd.txt" );
62 conf->ping_interval = 180;
63 conf->ping_timeout = 300;
64 conf->user = NULL;
65 conf->ft_max_size = SIZE_MAX;
66 conf->ft_max_kbps = G_MAXUINT;
67 conf->ft_listen = NULL;
68 conf->protocols = NULL;
69 conf->cafile = NULL;
70 proxytype = 0;
72 i = conf_loadini( conf, global.conf_file );
73 if( i == 0 )
75 fprintf( stderr, "Error: Syntax error in configuration file `%s'.\n", global.conf_file );
76 return NULL;
78 else if( i == -1 )
80 config_missing ++;
81 /* Whine after parsing the options if there was no -c pointing
82 at a *valid* configuration file. */
85 while( argc > 0 && ( opt = getopt( argc, argv, "i:p:P:nvIDFc:d:hu:V" ) ) >= 0 )
86 /* ^^^^ Just to make sure we skip this step from the REHASH handler. */
88 if( opt == 'i' )
90 conf->iface_in = g_strdup( optarg );
92 else if( opt == 'p' )
94 g_free( conf->port );
95 conf->port = g_strdup( optarg );
97 else if( opt == 'P' )
99 g_free( conf->pidfile );
100 conf->pidfile = g_strdup( optarg );
102 else if( opt == 'n' )
103 conf->nofork = 1;
104 else if( opt == 'v' )
105 conf->verbose = 1;
106 else if( opt == 'I' )
107 conf->runmode = RUNMODE_INETD;
108 else if( opt == 'D' )
109 conf->runmode = RUNMODE_DAEMON;
110 else if( opt == 'F' )
111 conf->runmode = RUNMODE_FORKDAEMON;
112 else if( opt == 'c' )
114 if( strcmp( global.conf_file, optarg ) != 0 )
116 g_free( global.conf_file );
117 global.conf_file = g_strdup( optarg );
118 g_free( conf );
119 /* Re-evaluate arguments. Don't use this option twice,
120 you'll end up in an infinite loop! Hope this trick
121 works with all libcs BTW.. */
122 optind = 1;
123 return conf_load( argc, argv );
126 else if( opt == 'd' )
128 g_free( conf->configdir );
129 conf->configdir = g_strdup( optarg );
131 else if( opt == 'h' )
133 printf( "Usage: bitlbee [-D/-F [-i <interface>] [-p <port>] [-n] [-v]] [-I]\n"
134 " [-c <file>] [-d <dir>] [-x] [-h]\n"
135 "\n"
136 "An IRC-to-other-chat-networks gateway\n"
137 "\n"
138 " -I Classic/InetD mode. (Default)\n"
139 " -D Daemon mode. (one process serves all)\n"
140 " -F Forking daemon. (one process per client)\n"
141 " -u Run daemon as specified user.\n"
142 " -P Specify PID-file (not for inetd mode)\n"
143 " -i Specify the interface (by IP address) to listen on.\n"
144 " (Default: 0.0.0.0 (any interface))\n"
145 " -p Port number to listen on. (Default: 6667)\n"
146 " -n Don't fork.\n"
147 " -v Be verbose (only works in combination with -n)\n"
148 " -c Load alternative configuration file\n"
149 " -d Specify alternative user configuration directory\n"
150 " -x Command-line interface to password encryption/hashing\n"
151 " -h Show this help page.\n"
152 " -V Show version info.\n" );
153 return NULL;
155 else if( opt == 'V' )
157 printf( "BitlBee %s\nAPI version %06x\n",
158 BITLBEE_VERSION, BITLBEE_VERSION_CODE );
159 return NULL;
161 else if( opt == 'u' )
163 g_free( conf->user );
164 conf->user = g_strdup( optarg );
168 if( conf->configdir[strlen(conf->configdir)-1] != '/' )
170 char *s = g_new( char, strlen( conf->configdir ) + 2 );
172 sprintf( s, "%s/", conf->configdir );
173 g_free( conf->configdir );
174 conf->configdir = s;
177 if( config_missing )
178 fprintf( stderr, "Warning: Unable to read configuration file `%s'.\n", global.conf_file );
180 if( conf->cafile && access( conf->cafile, R_OK ) != 0 )
182 /* Let's treat this as a serious problem so people won't think
183 they're secure when in fact they're not. */
184 fprintf( stderr, "Error: Could not read CA file %s: %s\n", conf->cafile, strerror( errno ) );
185 return NULL;
188 return conf;
191 static int conf_loadini( conf_t *conf, char *file )
193 ini_t *ini;
194 int i;
196 ini = ini_open( file );
197 if( ini == NULL ) return -1;
198 while( ini_read( ini ) )
200 if( g_strcasecmp( ini->section, "settings" ) == 0 )
202 if( g_strcasecmp( ini->key, "runmode" ) == 0 )
204 if( g_strcasecmp( ini->value, "daemon" ) == 0 )
205 conf->runmode = RUNMODE_DAEMON;
206 else if( g_strcasecmp( ini->value, "forkdaemon" ) == 0 )
207 conf->runmode = RUNMODE_FORKDAEMON;
208 else
209 conf->runmode = RUNMODE_INETD;
211 else if( g_strcasecmp( ini->key, "pidfile" ) == 0 )
213 g_free( conf->pidfile );
214 conf->pidfile = g_strdup( ini->value );
216 else if( g_strcasecmp( ini->key, "daemoninterface" ) == 0 )
218 g_free( conf->iface_in );
219 conf->iface_in = g_strdup( ini->value );
221 else if( g_strcasecmp( ini->key, "daemonport" ) == 0 )
223 g_free( conf->port );
224 conf->port = g_strdup( ini->value );
226 else if( g_strcasecmp( ini->key, "clientinterface" ) == 0 )
228 g_free( conf->iface_out );
229 conf->iface_out = g_strdup( ini->value );
231 else if( g_strcasecmp( ini->key, "authmode" ) == 0 )
233 if( g_strcasecmp( ini->value, "registered" ) == 0 )
234 conf->authmode = AUTHMODE_REGISTERED;
235 else if( g_strcasecmp( ini->value, "closed" ) == 0 )
236 conf->authmode = AUTHMODE_CLOSED;
237 else
238 conf->authmode = AUTHMODE_OPEN;
240 else if( g_strcasecmp( ini->key, "authpassword" ) == 0 )
242 g_free( conf->auth_pass );
243 conf->auth_pass = g_strdup( ini->value );
245 else if( g_strcasecmp( ini->key, "operpassword" ) == 0 )
247 g_free( conf->oper_pass );
248 conf->oper_pass = g_strdup( ini->value );
250 else if( g_strcasecmp( ini->key, "hostname" ) == 0 )
252 g_free( conf->hostname );
253 conf->hostname = g_strdup( ini->value );
255 else if( g_strcasecmp( ini->key, "configdir" ) == 0 )
257 g_free( conf->configdir );
258 conf->configdir = g_strdup( ini->value );
260 else if( g_strcasecmp( ini->key, "motdfile" ) == 0 )
262 g_free( conf->motdfile );
263 conf->motdfile = g_strdup( ini->value );
265 else if( g_strcasecmp( ini->key, "account_storage" ) == 0 )
267 g_free( conf->primary_storage );
268 conf->primary_storage = g_strdup( ini->value );
270 else if( g_strcasecmp( ini->key, "account_storage_migrate" ) == 0 )
272 g_strfreev( conf->migrate_storage );
273 conf->migrate_storage = g_strsplit_set( ini->value, " \t,;", -1 );
275 else if( g_strcasecmp( ini->key, "pinginterval" ) == 0 )
277 if( sscanf( ini->value, "%d", &i ) != 1 )
279 fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
280 return 0;
282 conf->ping_interval = i;
284 else if( g_strcasecmp( ini->key, "pingtimeout" ) == 0 )
286 if( sscanf( ini->value, "%d", &i ) != 1 )
288 fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
289 return 0;
291 conf->ping_timeout = i;
293 else if( g_strcasecmp( ini->key, "proxy" ) == 0 )
295 url_t *url = g_new0( url_t, 1 );
297 if( !url_set( url, ini->value ) )
299 fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
300 g_free( url );
301 return 0;
304 strncpy( proxyhost, url->host, sizeof( proxyhost ) );
305 strncpy( proxyuser, url->user, sizeof( proxyuser ) );
306 strncpy( proxypass, url->pass, sizeof( proxypass ) );
307 proxyport = url->port;
308 if( url->proto == PROTO_HTTP )
309 proxytype = PROXY_HTTP;
310 else if( url->proto == PROTO_SOCKS4 )
311 proxytype = PROXY_SOCKS4;
312 else if( url->proto == PROTO_SOCKS5 )
313 proxytype = PROXY_SOCKS5;
315 g_free( url );
317 else if( g_strcasecmp( ini->key, "user" ) == 0 )
319 g_free( conf->user );
320 conf->user = g_strdup( ini->value );
322 else if( g_strcasecmp( ini->key, "ft_max_size" ) == 0 )
324 size_t ft_max_size;
325 if( sscanf( ini->value, "%zu", &ft_max_size ) != 1 )
327 fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
328 return 0;
330 conf->ft_max_size = ft_max_size;
332 else if( g_strcasecmp( ini->key, "ft_max_kbps" ) == 0 )
334 if( sscanf( ini->value, "%d", &i ) != 1 )
336 fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
337 return 0;
339 conf->ft_max_kbps = i;
341 else if( g_strcasecmp( ini->key, "ft_listen" ) == 0 )
343 g_free( conf->ft_listen );
344 conf->ft_listen = g_strdup( ini->value );
346 else if( g_strcasecmp( ini->key, "protocols" ) == 0 )
348 g_strfreev( conf->protocols );
349 conf->protocols = g_strsplit_set( ini->value, " \t,;", -1 );
351 else if( g_strcasecmp( ini->key, "cafile" ) == 0 )
353 g_free( conf->cafile );
354 conf->cafile = g_strdup( ini->value );
356 else
358 fprintf( stderr, "Error: Unknown setting `%s` in configuration file (line %d).\n", ini->key, ini->line );
359 return 0;
360 /* For now just ignore unknown keys... */
363 else if( g_strcasecmp( ini->section, "defaults" ) != 0 )
365 fprintf( stderr, "Error: Unknown section [%s] in configuration file (line %d). "
366 "BitlBee configuration must be put in a [settings] section!\n", ini->section, ini->line );
367 return 0;
370 ini_close( ini );
372 return 1;
375 void conf_loaddefaults( irc_t *irc )
377 ini_t *ini;
379 ini = ini_open( global.conf_file );
380 if( ini == NULL ) return;
381 while( ini_read( ini ) )
383 if( g_strcasecmp( ini->section, "defaults" ) == 0 )
385 set_t *s = set_find( &irc->b->set, ini->key );
387 if( s )
389 if( s->def ) g_free( s->def );
390 s->def = g_strdup( ini->value );
394 ini_close( ini );