[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 | |
---|
| 37 | char *CONF_FILE; |
---|
| 38 | |
---|
| 39 | static int conf_loadini( conf_t *conf, char *file ); |
---|
| 40 | |
---|
| 41 | conf_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 ); |
---|
[34b17d9] | 63 | conf->pidfile = g_strdup( "/var/run/bitlbee.pid" ); |
---|
[b7d3cc34] | 64 | conf->motdfile = g_strdup( ETCDIR "/motd.txt" ); |
---|
| 65 | conf->ping_interval = 180; |
---|
| 66 | conf->ping_timeout = 300; |
---|
[f4a5940] | 67 | proxytype = 0; |
---|
[b7d3cc34] | 68 | |
---|
| 69 | i = conf_loadini( conf, CONF_FILE ); |
---|
| 70 | if( i == 0 ) |
---|
| 71 | { |
---|
| 72 | fprintf( stderr, "Error: Syntax error in configuration file `%s'.\n", CONF_FILE ); |
---|
| 73 | return( NULL ); |
---|
| 74 | } |
---|
| 75 | else if( i == -1 ) |
---|
| 76 | { |
---|
| 77 | fprintf( stderr, "Warning: Unable to read configuration file `%s'.\n", CONF_FILE ); |
---|
| 78 | } |
---|
| 79 | |
---|
[34b17d9] | 80 | while( argc > 0 && ( opt = getopt( argc, argv, "i:p:P:nvIDFc:d:h" ) ) >= 0 ) |
---|
[f4a5940] | 81 | /* ^^^^ Just to make sure we skip this step from the REHASH handler. */ |
---|
[b7d3cc34] | 82 | { |
---|
| 83 | if( opt == 'i' ) |
---|
| 84 | { |
---|
| 85 | conf->iface = g_strdup( optarg ); |
---|
| 86 | } |
---|
| 87 | else if( opt == 'p' ) |
---|
| 88 | { |
---|
| 89 | if( ( sscanf( optarg, "%d", &i ) != 1 ) || ( i <= 0 ) || ( i > 65535 ) ) |
---|
| 90 | { |
---|
| 91 | fprintf( stderr, "Invalid port number: %s\n", optarg ); |
---|
| 92 | return( NULL ); |
---|
| 93 | } |
---|
| 94 | conf->port = i; |
---|
| 95 | } |
---|
[34b17d9] | 96 | else if( opt == 'p' ) |
---|
| 97 | { |
---|
| 98 | g_free( conf->pidfile ); |
---|
| 99 | conf->pidfile = g_strdup( optarg ); |
---|
| 100 | } |
---|
[b7d3cc34] | 101 | else if( opt == 'n' ) |
---|
[74c119d] | 102 | conf->nofork = 1; |
---|
[b7d3cc34] | 103 | else if( opt == 'v' ) |
---|
[74c119d] | 104 | conf->verbose = 1; |
---|
[b7d3cc34] | 105 | else if( opt == 'I' ) |
---|
[74c119d] | 106 | conf->runmode = RUNMODE_INETD; |
---|
[b7d3cc34] | 107 | else if( opt == 'D' ) |
---|
[74c119d] | 108 | conf->runmode = RUNMODE_DAEMON; |
---|
[d25f6fc] | 109 | else if( opt == 'F' ) |
---|
[74c119d] | 110 | conf->runmode = RUNMODE_FORKDAEMON; |
---|
[b7d3cc34] | 111 | else if( opt == 'c' ) |
---|
| 112 | { |
---|
| 113 | if( strcmp( CONF_FILE, optarg ) != 0 ) |
---|
| 114 | { |
---|
| 115 | g_free( CONF_FILE ); |
---|
| 116 | CONF_FILE = g_strdup( optarg ); |
---|
| 117 | g_free( conf ); |
---|
[f4a5940] | 118 | /* Re-evaluate arguments. Don't use this option twice, |
---|
| 119 | you'll end up in an infinite loop! Hope this trick |
---|
| 120 | works with all libcs BTW.. */ |
---|
[74c119d] | 121 | optind = 1; |
---|
[b7d3cc34] | 122 | return( conf_load( argc, argv ) ); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | else if( opt == 'd' ) |
---|
| 126 | { |
---|
| 127 | g_free( conf->configdir ); |
---|
| 128 | conf->configdir = g_strdup( optarg ); |
---|
| 129 | } |
---|
| 130 | else if( opt == 'h' ) |
---|
| 131 | { |
---|
| 132 | printf( "Usage: bitlbee [-D [-i <interface>] [-p <port>] [-n] [-v]] [-I]\n" |
---|
| 133 | " [-c <file>] [-d <dir>] [-h]\n" |
---|
| 134 | "\n" |
---|
| 135 | "An IRC-to-other-chat-networks gateway\n" |
---|
| 136 | "\n" |
---|
| 137 | " -I Classic/InetD mode. (Default)\n" |
---|
| 138 | " -D Daemon mode. (Still EXPERIMENTAL!)\n" |
---|
[d25f6fc] | 139 | " -F Forking daemon. (one process per client)\n" |
---|
[b7d3cc34] | 140 | " -i Specify the interface (by IP address) to listen on.\n" |
---|
| 141 | " (Default: 0.0.0.0 (any interface))\n" |
---|
| 142 | " -p Port number to listen on. (Default: 6667)\n" |
---|
| 143 | " -n Don't fork.\n" |
---|
| 144 | " -v Be verbose (only works in combination with -n)\n" |
---|
| 145 | " -c Load alternative configuration file\n" |
---|
| 146 | " -d Specify alternative user configuration directory\n" |
---|
| 147 | " -h Show this help page.\n" ); |
---|
| 148 | return( NULL ); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | if( conf->configdir[strlen(conf->configdir)-1] != '/' ) |
---|
| 153 | { |
---|
| 154 | char *s = g_new( char, strlen( conf->configdir ) + 2 ); |
---|
| 155 | |
---|
| 156 | sprintf( s, "%s/", conf->configdir ); |
---|
| 157 | g_free( conf->configdir ); |
---|
| 158 | conf->configdir = s; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | return( conf ); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | static int conf_loadini( conf_t *conf, char *file ) |
---|
| 165 | { |
---|
| 166 | ini_t *ini; |
---|
| 167 | int i; |
---|
| 168 | |
---|
| 169 | ini = ini_open( file ); |
---|
| 170 | if( ini == NULL ) return( -1 ); |
---|
| 171 | while( ini_read( ini ) ) |
---|
| 172 | { |
---|
| 173 | if( g_strcasecmp( ini->section, "settings" ) == 0 ) |
---|
| 174 | { |
---|
| 175 | if( g_strcasecmp( ini->key, "runmode" ) == 0 ) |
---|
| 176 | { |
---|
| 177 | if( g_strcasecmp( ini->value, "daemon" ) == 0 ) |
---|
| 178 | conf->runmode = RUNMODE_DAEMON; |
---|
[d25f6fc] | 179 | else if( g_strcasecmp( ini->value, "forkdaemon" ) == 0 ) |
---|
| 180 | conf->runmode = RUNMODE_FORKDAEMON; |
---|
[b7d3cc34] | 181 | else |
---|
| 182 | conf->runmode = RUNMODE_INETD; |
---|
| 183 | } |
---|
[34b17d9] | 184 | else if( g_strcasecmp( ini->key, "pidfile" ) == 0 ) |
---|
| 185 | { |
---|
| 186 | g_free( conf->pidfile ); |
---|
| 187 | conf->pidfile = g_strdup( ini->value ); |
---|
| 188 | } |
---|
[b7d3cc34] | 189 | else if( g_strcasecmp( ini->key, "daemoninterface" ) == 0 ) |
---|
| 190 | { |
---|
| 191 | conf->iface = g_strdup( ini->value ); |
---|
| 192 | } |
---|
| 193 | else if( g_strcasecmp( ini->key, "daemonport" ) == 0 ) |
---|
| 194 | { |
---|
| 195 | if( ( sscanf( ini->value, "%d", &i ) != 1 ) || ( i <= 0 ) || ( i > 65535 ) ) |
---|
| 196 | { |
---|
| 197 | fprintf( stderr, "Invalid port number: %s\n", ini->value ); |
---|
| 198 | return( 0 ); |
---|
| 199 | } |
---|
| 200 | conf->port = i; |
---|
| 201 | } |
---|
| 202 | else if( g_strcasecmp( ini->key, "authmode" ) == 0 ) |
---|
| 203 | { |
---|
| 204 | if( g_strcasecmp( ini->value, "registered" ) == 0 ) |
---|
| 205 | conf->authmode = AUTHMODE_REGISTERED; |
---|
| 206 | else if( g_strcasecmp( ini->value, "closed" ) == 0 ) |
---|
| 207 | conf->authmode = AUTHMODE_CLOSED; |
---|
| 208 | else |
---|
| 209 | conf->authmode = AUTHMODE_OPEN; |
---|
| 210 | } |
---|
| 211 | else if( g_strcasecmp( ini->key, "authpassword" ) == 0 ) |
---|
| 212 | { |
---|
[d25f6fc] | 213 | conf->auth_pass = g_strdup( ini->value ); |
---|
| 214 | } |
---|
| 215 | else if( g_strcasecmp( ini->key, "operpassword" ) == 0 ) |
---|
| 216 | { |
---|
| 217 | conf->oper_pass = g_strdup( ini->value ); |
---|
[b7d3cc34] | 218 | } |
---|
| 219 | else if( g_strcasecmp( ini->key, "hostname" ) == 0 ) |
---|
| 220 | { |
---|
| 221 | conf->hostname = g_strdup( ini->value ); |
---|
| 222 | } |
---|
| 223 | else if( g_strcasecmp( ini->key, "configdir" ) == 0 ) |
---|
| 224 | { |
---|
| 225 | g_free( conf->configdir ); |
---|
| 226 | conf->configdir = g_strdup( ini->value ); |
---|
| 227 | } |
---|
| 228 | else if( g_strcasecmp( ini->key, "motdfile" ) == 0 ) |
---|
| 229 | { |
---|
| 230 | g_free( conf->motdfile ); |
---|
| 231 | conf->motdfile = g_strdup( ini->value ); |
---|
[1ee6c18] | 232 | } |
---|
[b73ac9c] | 233 | else if( g_strcasecmp( ini->key, "account_storage" ) == 0 ) |
---|
[1ee6c18] | 234 | { |
---|
[b73ac9c] | 235 | g_free( conf->primary_storage ); |
---|
| 236 | conf->primary_storage = g_strdup( ini->value ); |
---|
| 237 | } |
---|
| 238 | else if( g_strcasecmp( ini->key, "account_storage_migrate" ) == 0 ) |
---|
| 239 | { |
---|
| 240 | g_strfreev( conf->migrate_storage ); |
---|
| 241 | conf->migrate_storage = g_strsplit( ini->value, " \t,;", -1 ); |
---|
[b7d3cc34] | 242 | } |
---|
| 243 | else if( g_strcasecmp( ini->key, "pinginterval" ) == 0 ) |
---|
| 244 | { |
---|
| 245 | if( sscanf( ini->value, "%d", &i ) != 1 ) |
---|
| 246 | { |
---|
| 247 | fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value ); |
---|
| 248 | return( 0 ); |
---|
| 249 | } |
---|
| 250 | conf->ping_interval = i; |
---|
| 251 | } |
---|
| 252 | else if( g_strcasecmp( ini->key, "pingtimeout" ) == 0 ) |
---|
| 253 | { |
---|
| 254 | if( sscanf( ini->value, "%d", &i ) != 1 ) |
---|
| 255 | { |
---|
| 256 | fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value ); |
---|
| 257 | return( 0 ); |
---|
| 258 | } |
---|
| 259 | conf->ping_timeout = i; |
---|
| 260 | } |
---|
| 261 | else if( g_strcasecmp( ini->key, "proxy" ) == 0 ) |
---|
| 262 | { |
---|
| 263 | url_t *url = g_new0( url_t, 1 ); |
---|
| 264 | |
---|
| 265 | if( !url_set( url, ini->value ) ) |
---|
| 266 | { |
---|
| 267 | fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value ); |
---|
| 268 | g_free( url ); |
---|
| 269 | return( 0 ); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | strncpy( proxyhost, url->host, sizeof( proxyhost ) ); |
---|
| 273 | strncpy( proxyuser, url->user, sizeof( proxyuser ) ); |
---|
| 274 | strncpy( proxypass, url->pass, sizeof( proxypass ) ); |
---|
| 275 | proxyport = url->port; |
---|
| 276 | if( url->proto == PROTO_HTTP ) |
---|
| 277 | proxytype = PROXY_HTTP; |
---|
| 278 | else if( url->proto == PROTO_SOCKS4 ) |
---|
| 279 | proxytype = PROXY_SOCKS4; |
---|
| 280 | else if( url->proto == PROTO_SOCKS5 ) |
---|
| 281 | proxytype = PROXY_SOCKS5; |
---|
| 282 | |
---|
| 283 | g_free( url ); |
---|
| 284 | } |
---|
| 285 | else |
---|
| 286 | { |
---|
| 287 | fprintf( stderr, "Error: Unknown setting `%s` in configuration file.\n", ini->key ); |
---|
| 288 | return( 0 ); |
---|
| 289 | /* For now just ignore unknown keys... */ |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | else if( g_strcasecmp( ini->section, "defaults" ) != 0 ) |
---|
| 293 | { |
---|
| 294 | fprintf( stderr, "Error: Unknown section [%s] in configuration file. " |
---|
| 295 | "BitlBee configuration must be put in a [settings] section!\n", ini->section ); |
---|
| 296 | return( 0 ); |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | ini_close( ini ); |
---|
| 300 | |
---|
| 301 | return( 1 ); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | void conf_loaddefaults( irc_t *irc ) |
---|
| 305 | { |
---|
| 306 | ini_t *ini; |
---|
| 307 | |
---|
| 308 | ini = ini_open( CONF_FILE ); |
---|
| 309 | if( ini == NULL ) return; |
---|
| 310 | while( ini_read( ini ) ) |
---|
| 311 | { |
---|
| 312 | if( g_strcasecmp( ini->section, "defaults" ) == 0 ) |
---|
| 313 | { |
---|
| 314 | set_t *s = set_find( irc, ini->key ); |
---|
| 315 | |
---|
| 316 | if( s ) |
---|
| 317 | { |
---|
| 318 | if( s->def ) g_free( s->def ); |
---|
| 319 | s->def = g_strdup( ini->value ); |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | ini_close( ini ); |
---|
| 324 | } |
---|