source: conf.c @ 74c119d

Last change on this file since 74c119d was 74c119d, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-01-15T15:42:20Z

Better DIE implementation, added SO_REUSEADDR to listening socket.

  • Property mode set to 100644
File size: 8.4 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[c92e6801]4  * Copyright 2002-2005 Wilmer van der Gaast and others                *
[b7d3cc34]5  \********************************************************************/
6
7/* Configuration reading code                                           */
8
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.
14
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.
19
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
24*/
25
26#include "bitlbee.h"
27
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
35#include "protocols/proxy.h"
36
37char *CONF_FILE;
38
39static int conf_loadini( conf_t *conf, char *file );
40
41conf_t *conf_load( int argc, char *argv[] )
42{
43        conf_t *conf;
44        int opt, i;
45       
46        conf = g_new0( conf_t, 1 );
47       
[2a6ca4f]48#ifdef IPV6
49        conf->iface = "::";
50#else
[b7d3cc34]51        conf->iface = "0.0.0.0";
[2a6ca4f]52#endif
[b7d3cc34]53        conf->port = 6667;
54        conf->nofork = 0;
55        conf->verbose = 0;
[b73ac9c]56        conf->primary_storage = "text";
[b7d3cc34]57        conf->runmode = RUNMODE_INETD;
58        conf->authmode = AUTHMODE_OPEN;
[d25f6fc]59        conf->auth_pass = NULL;
60        conf->oper_pass = NULL;
[b7d3cc34]61        conf->configdir = g_strdup( CONFIG );
[4bfca70]62        conf->plugindir = g_strdup( PLUGINDIR );
[b7d3cc34]63        conf->motdfile = g_strdup( ETCDIR "/motd.txt" );
64        conf->ping_interval = 180;
65        conf->ping_timeout = 300;
66       
67        i = conf_loadini( conf, CONF_FILE );
68        if( i == 0 )
69        {
70                fprintf( stderr, "Error: Syntax error in configuration file `%s'.\n", CONF_FILE );
71                return( NULL );
72        }
73        else if( i == -1 )
74        {
75                fprintf( stderr, "Warning: Unable to read configuration file `%s'.\n", CONF_FILE );
76        }
77       
[d25f6fc]78        while( ( opt = getopt( argc, argv, "i:p:nvIDFc:d:h" ) ) >= 0 )
[b7d3cc34]79        {
80                if( opt == 'i' )
81                {
82                        conf->iface = g_strdup( optarg );
83                }
84                else if( opt == 'p' )
85                {
86                        if( ( sscanf( optarg, "%d", &i ) != 1 ) || ( i <= 0 ) || ( i > 65535 ) )
87                        {
88                                fprintf( stderr, "Invalid port number: %s\n", optarg );
89                                return( NULL );
90                        }
91                        conf->port = i;
92                }
93                else if( opt == 'n' )
[74c119d]94                        conf->nofork = 1;
[b7d3cc34]95                else if( opt == 'v' )
[74c119d]96                        conf->verbose = 1;
[b7d3cc34]97                else if( opt == 'I' )
[74c119d]98                        conf->runmode = RUNMODE_INETD;
[b7d3cc34]99                else if( opt == 'D' )
[74c119d]100                        conf->runmode = RUNMODE_DAEMON;
[d25f6fc]101                else if( opt == 'F' )
[74c119d]102                        conf->runmode = RUNMODE_FORKDAEMON;
[b7d3cc34]103                else if( opt == 'c' )
104                {
105                        if( strcmp( CONF_FILE, optarg ) != 0 )
106                        {
107                                g_free( CONF_FILE );
108                                CONF_FILE = g_strdup( optarg );
109                                g_free( conf );
[74c119d]110                                /* Re-evaluate arguments. */
111                                optind = 1;
[b7d3cc34]112                                return( conf_load( argc, argv ) );
113                        }
114                }
115                else if( opt == 'd' )
116                {
117                        g_free( conf->configdir );
118                        conf->configdir = g_strdup( optarg );
119                }
120                else if( opt == 'h' )
121                {
122                        printf( "Usage: bitlbee [-D [-i <interface>] [-p <port>] [-n] [-v]] [-I]\n"
123                                "               [-c <file>] [-d <dir>] [-h]\n"
124                                "\n"
125                                "An IRC-to-other-chat-networks gateway\n"
126                                "\n"
127                                "  -I  Classic/InetD mode. (Default)\n"
128                                "  -D  Daemon mode. (Still EXPERIMENTAL!)\n"
[d25f6fc]129                                "  -F  Forking daemon. (one process per client)\n"
[b7d3cc34]130                                "  -i  Specify the interface (by IP address) to listen on.\n"
131                                "      (Default: 0.0.0.0 (any interface))\n"
132                                "  -p  Port number to listen on. (Default: 6667)\n"
133                                "  -n  Don't fork.\n"
134                                "  -v  Be verbose (only works in combination with -n)\n"
135                                "  -c  Load alternative configuration file\n"
136                                "  -d  Specify alternative user configuration directory\n"
137                                "  -h  Show this help page.\n" );
138                        return( NULL );
139                }
140        }
141       
142        if( conf->configdir[strlen(conf->configdir)-1] != '/' )
143        {
144                char *s = g_new( char, strlen( conf->configdir ) + 2 );
145               
146                sprintf( s, "%s/", conf->configdir );
147                g_free( conf->configdir );
148                conf->configdir = s;
149        }
150       
151        return( conf );
152}
153
154static int conf_loadini( conf_t *conf, char *file )
155{
156        ini_t *ini;
157        int i;
158       
159        ini = ini_open( file );
160        if( ini == NULL ) return( -1 );
161        while( ini_read( ini ) )
162        {
163                if( g_strcasecmp( ini->section, "settings" ) == 0 )
164                {
165                        if( g_strcasecmp( ini->key, "runmode" ) == 0 )
166                        {
167                                if( g_strcasecmp( ini->value, "daemon" ) == 0 )
168                                        conf->runmode = RUNMODE_DAEMON;
[d25f6fc]169                                else if( g_strcasecmp( ini->value, "forkdaemon" ) == 0 )
170                                        conf->runmode = RUNMODE_FORKDAEMON;
[b7d3cc34]171                                else
172                                        conf->runmode = RUNMODE_INETD;
173                        }
174                        else if( g_strcasecmp( ini->key, "daemoninterface" ) == 0 )
175                        {
176                                conf->iface = g_strdup( ini->value );
177                        }
178                        else if( g_strcasecmp( ini->key, "daemonport" ) == 0 )
179                        {
180                                if( ( sscanf( ini->value, "%d", &i ) != 1 ) || ( i <= 0 ) || ( i > 65535 ) )
181                                {
182                                        fprintf( stderr, "Invalid port number: %s\n", ini->value );
183                                        return( 0 );
184                                }
185                                conf->port = i;
186                        }
187                        else if( g_strcasecmp( ini->key, "authmode" ) == 0 )
188                        {
189                                if( g_strcasecmp( ini->value, "registered" ) == 0 )
190                                        conf->authmode = AUTHMODE_REGISTERED;
191                                else if( g_strcasecmp( ini->value, "closed" ) == 0 )
192                                        conf->authmode = AUTHMODE_CLOSED;
193                                else
194                                        conf->authmode = AUTHMODE_OPEN;
195                        }
196                        else if( g_strcasecmp( ini->key, "authpassword" ) == 0 )
197                        {
[d25f6fc]198                                conf->auth_pass = g_strdup( ini->value );
199                        }
200                        else if( g_strcasecmp( ini->key, "operpassword" ) == 0 )
201                        {
202                                conf->oper_pass = g_strdup( ini->value );
[b7d3cc34]203                        }
204                        else if( g_strcasecmp( ini->key, "hostname" ) == 0 )
205                        {
206                                conf->hostname = g_strdup( ini->value );
207                        }
208                        else if( g_strcasecmp( ini->key, "configdir" ) == 0 )
209                        {
210                                g_free( conf->configdir );
211                                conf->configdir = g_strdup( ini->value );
212                        }
213                        else if( g_strcasecmp( ini->key, "motdfile" ) == 0 )
214                        {
215                                g_free( conf->motdfile );
216                                conf->motdfile = g_strdup( ini->value );
[1ee6c18]217                        }
[b73ac9c]218                        else if( g_strcasecmp( ini->key, "account_storage" ) == 0 )
[1ee6c18]219                        {
[b73ac9c]220                                g_free( conf->primary_storage );
221                                conf->primary_storage = g_strdup( ini->value );
222                        }
223                        else if( g_strcasecmp( ini->key, "account_storage_migrate" ) == 0 )
224                        {
225                                g_strfreev( conf->migrate_storage );
226                                conf->migrate_storage = g_strsplit( ini->value, " \t,;", -1 );
[b7d3cc34]227                        }
228                        else if( g_strcasecmp( ini->key, "pinginterval" ) == 0 )
229                        {
230                                if( sscanf( ini->value, "%d", &i ) != 1 )
231                                {
232                                        fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
233                                        return( 0 );
234                                }
235                                conf->ping_interval = i;
236                        }
237                        else if( g_strcasecmp( ini->key, "pingtimeout" ) == 0 )
238                        {
239                                if( sscanf( ini->value, "%d", &i ) != 1 )
240                                {
241                                        fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
242                                        return( 0 );
243                                }
244                                conf->ping_timeout = i;
245                        }
246                        else if( g_strcasecmp( ini->key, "proxy" ) == 0 )
247                        {
248                                url_t *url = g_new0( url_t, 1 );
249                               
250                                if( !url_set( url, ini->value ) )
251                                {
252                                        fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value );
253                                        g_free( url );
254                                        return( 0 );
255                                }
256                               
257                                strncpy( proxyhost, url->host, sizeof( proxyhost ) );
258                                strncpy( proxyuser, url->user, sizeof( proxyuser ) );
259                                strncpy( proxypass, url->pass, sizeof( proxypass ) );
260                                proxyport = url->port;
261                                if( url->proto == PROTO_HTTP )
262                                        proxytype = PROXY_HTTP;
263                                else if( url->proto == PROTO_SOCKS4 )
264                                        proxytype = PROXY_SOCKS4;
265                                else if( url->proto == PROTO_SOCKS5 )
266                                        proxytype = PROXY_SOCKS5;
267                               
268                                g_free( url );
269                        }
270                        else
271                        {
272                                fprintf( stderr, "Error: Unknown setting `%s` in configuration file.\n", ini->key );
273                                return( 0 );
274                                /* For now just ignore unknown keys... */
275                        }
276                }
277                else if( g_strcasecmp( ini->section, "defaults" ) != 0 )
278                {
279                        fprintf( stderr, "Error: Unknown section [%s] in configuration file. "
280                                         "BitlBee configuration must be put in a [settings] section!\n", ini->section );
281                        return( 0 );
282                }
283        }
284        ini_close( ini );
285       
286        return( 1 );
287}
288
289void conf_loaddefaults( irc_t *irc )
290{
291        ini_t *ini;
292       
293        ini = ini_open( CONF_FILE );
294        if( ini == NULL ) return;
295        while( ini_read( ini ) )
296        {
297                if( g_strcasecmp( ini->section, "defaults" ) == 0 )
298                {
299                        set_t *s = set_find( irc, ini->key );
300                       
301                        if( s )
302                        {
303                                if( s->def ) g_free( s->def );
304                                s->def = g_strdup( ini->value );
305                        }
306                }
307        }
308        ini_close( ini );
309}
Note: See TracBrowser for help on using the repository browser.