source: conf.c @ 1ee6c18

Last change on this file since 1ee6c18 was 1ee6c18, checked in by Jelmer Vernooij <jelmer@…>, at 2005-12-08T13:41:53Z

Add abstraction layer for storage

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