[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 ); |
---|
[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' ) |
---|
| 94 | conf->nofork=1; |
---|
| 95 | else if( opt == 'v' ) |
---|
| 96 | conf->verbose=1; |
---|
| 97 | else if( opt == 'I' ) |
---|
| 98 | conf->runmode=RUNMODE_INETD; |
---|
| 99 | else if( opt == 'D' ) |
---|
| 100 | conf->runmode=RUNMODE_DAEMON; |
---|
[d25f6fc] | 101 | else if( opt == 'F' ) |
---|
| 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 ); |
---|
| 110 | return( conf_load( argc, argv ) ); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | else if( opt == 'd' ) |
---|
| 114 | { |
---|
| 115 | g_free( conf->configdir ); |
---|
| 116 | conf->configdir = g_strdup( optarg ); |
---|
| 117 | } |
---|
| 118 | else if( opt == 'h' ) |
---|
| 119 | { |
---|
| 120 | printf( "Usage: bitlbee [-D [-i <interface>] [-p <port>] [-n] [-v]] [-I]\n" |
---|
| 121 | " [-c <file>] [-d <dir>] [-h]\n" |
---|
| 122 | "\n" |
---|
| 123 | "An IRC-to-other-chat-networks gateway\n" |
---|
| 124 | "\n" |
---|
| 125 | " -I Classic/InetD mode. (Default)\n" |
---|
| 126 | " -D Daemon mode. (Still EXPERIMENTAL!)\n" |
---|
[d25f6fc] | 127 | " -F Forking daemon. (one process per client)\n" |
---|
[b7d3cc34] | 128 | " -i Specify the interface (by IP address) to listen on.\n" |
---|
| 129 | " (Default: 0.0.0.0 (any interface))\n" |
---|
| 130 | " -p Port number to listen on. (Default: 6667)\n" |
---|
| 131 | " -n Don't fork.\n" |
---|
| 132 | " -v Be verbose (only works in combination with -n)\n" |
---|
| 133 | " -c Load alternative configuration file\n" |
---|
| 134 | " -d Specify alternative user configuration directory\n" |
---|
| 135 | " -h Show this help page.\n" ); |
---|
| 136 | return( NULL ); |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | if( conf->configdir[strlen(conf->configdir)-1] != '/' ) |
---|
| 141 | { |
---|
| 142 | char *s = g_new( char, strlen( conf->configdir ) + 2 ); |
---|
| 143 | |
---|
| 144 | sprintf( s, "%s/", conf->configdir ); |
---|
| 145 | g_free( conf->configdir ); |
---|
| 146 | conf->configdir = s; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | return( conf ); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | static int conf_loadini( conf_t *conf, char *file ) |
---|
| 153 | { |
---|
| 154 | ini_t *ini; |
---|
| 155 | int i; |
---|
| 156 | |
---|
| 157 | ini = ini_open( file ); |
---|
| 158 | if( ini == NULL ) return( -1 ); |
---|
| 159 | while( ini_read( ini ) ) |
---|
| 160 | { |
---|
| 161 | if( g_strcasecmp( ini->section, "settings" ) == 0 ) |
---|
| 162 | { |
---|
| 163 | if( g_strcasecmp( ini->key, "runmode" ) == 0 ) |
---|
| 164 | { |
---|
| 165 | if( g_strcasecmp( ini->value, "daemon" ) == 0 ) |
---|
| 166 | conf->runmode = RUNMODE_DAEMON; |
---|
[d25f6fc] | 167 | else if( g_strcasecmp( ini->value, "forkdaemon" ) == 0 ) |
---|
| 168 | conf->runmode = RUNMODE_FORKDAEMON; |
---|
[b7d3cc34] | 169 | else |
---|
| 170 | conf->runmode = RUNMODE_INETD; |
---|
| 171 | } |
---|
| 172 | else if( g_strcasecmp( ini->key, "daemoninterface" ) == 0 ) |
---|
| 173 | { |
---|
| 174 | conf->iface = g_strdup( ini->value ); |
---|
| 175 | } |
---|
| 176 | else if( g_strcasecmp( ini->key, "daemonport" ) == 0 ) |
---|
| 177 | { |
---|
| 178 | if( ( sscanf( ini->value, "%d", &i ) != 1 ) || ( i <= 0 ) || ( i > 65535 ) ) |
---|
| 179 | { |
---|
| 180 | fprintf( stderr, "Invalid port number: %s\n", ini->value ); |
---|
| 181 | return( 0 ); |
---|
| 182 | } |
---|
| 183 | conf->port = i; |
---|
| 184 | } |
---|
| 185 | else if( g_strcasecmp( ini->key, "authmode" ) == 0 ) |
---|
| 186 | { |
---|
| 187 | if( g_strcasecmp( ini->value, "registered" ) == 0 ) |
---|
| 188 | conf->authmode = AUTHMODE_REGISTERED; |
---|
| 189 | else if( g_strcasecmp( ini->value, "closed" ) == 0 ) |
---|
| 190 | conf->authmode = AUTHMODE_CLOSED; |
---|
| 191 | else |
---|
| 192 | conf->authmode = AUTHMODE_OPEN; |
---|
| 193 | } |
---|
| 194 | else if( g_strcasecmp( ini->key, "authpassword" ) == 0 ) |
---|
| 195 | { |
---|
[d25f6fc] | 196 | conf->auth_pass = g_strdup( ini->value ); |
---|
| 197 | } |
---|
| 198 | else if( g_strcasecmp( ini->key, "operpassword" ) == 0 ) |
---|
| 199 | { |
---|
| 200 | conf->oper_pass = g_strdup( ini->value ); |
---|
[b7d3cc34] | 201 | } |
---|
| 202 | else if( g_strcasecmp( ini->key, "hostname" ) == 0 ) |
---|
| 203 | { |
---|
| 204 | conf->hostname = g_strdup( ini->value ); |
---|
| 205 | } |
---|
| 206 | else if( g_strcasecmp( ini->key, "configdir" ) == 0 ) |
---|
| 207 | { |
---|
| 208 | g_free( conf->configdir ); |
---|
| 209 | conf->configdir = g_strdup( ini->value ); |
---|
| 210 | } |
---|
| 211 | else if( g_strcasecmp( ini->key, "motdfile" ) == 0 ) |
---|
| 212 | { |
---|
| 213 | g_free( conf->motdfile ); |
---|
| 214 | conf->motdfile = g_strdup( ini->value ); |
---|
[1ee6c18] | 215 | } |
---|
[b73ac9c] | 216 | else if( g_strcasecmp( ini->key, "account_storage" ) == 0 ) |
---|
[1ee6c18] | 217 | { |
---|
[b73ac9c] | 218 | g_free( conf->primary_storage ); |
---|
| 219 | conf->primary_storage = g_strdup( ini->value ); |
---|
| 220 | } |
---|
| 221 | else if( g_strcasecmp( ini->key, "account_storage_migrate" ) == 0 ) |
---|
| 222 | { |
---|
| 223 | g_strfreev( conf->migrate_storage ); |
---|
| 224 | conf->migrate_storage = g_strsplit( ini->value, " \t,;", -1 ); |
---|
[b7d3cc34] | 225 | } |
---|
| 226 | else if( g_strcasecmp( ini->key, "pinginterval" ) == 0 ) |
---|
| 227 | { |
---|
| 228 | if( sscanf( ini->value, "%d", &i ) != 1 ) |
---|
| 229 | { |
---|
| 230 | fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value ); |
---|
| 231 | return( 0 ); |
---|
| 232 | } |
---|
| 233 | conf->ping_interval = i; |
---|
| 234 | } |
---|
| 235 | else if( g_strcasecmp( ini->key, "pingtimeout" ) == 0 ) |
---|
| 236 | { |
---|
| 237 | if( sscanf( ini->value, "%d", &i ) != 1 ) |
---|
| 238 | { |
---|
| 239 | fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value ); |
---|
| 240 | return( 0 ); |
---|
| 241 | } |
---|
| 242 | conf->ping_timeout = i; |
---|
| 243 | } |
---|
| 244 | else if( g_strcasecmp( ini->key, "proxy" ) == 0 ) |
---|
| 245 | { |
---|
| 246 | url_t *url = g_new0( url_t, 1 ); |
---|
| 247 | |
---|
| 248 | if( !url_set( url, ini->value ) ) |
---|
| 249 | { |
---|
| 250 | fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value ); |
---|
| 251 | g_free( url ); |
---|
| 252 | return( 0 ); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | strncpy( proxyhost, url->host, sizeof( proxyhost ) ); |
---|
| 256 | strncpy( proxyuser, url->user, sizeof( proxyuser ) ); |
---|
| 257 | strncpy( proxypass, url->pass, sizeof( proxypass ) ); |
---|
| 258 | proxyport = url->port; |
---|
| 259 | if( url->proto == PROTO_HTTP ) |
---|
| 260 | proxytype = PROXY_HTTP; |
---|
| 261 | else if( url->proto == PROTO_SOCKS4 ) |
---|
| 262 | proxytype = PROXY_SOCKS4; |
---|
| 263 | else if( url->proto == PROTO_SOCKS5 ) |
---|
| 264 | proxytype = PROXY_SOCKS5; |
---|
| 265 | |
---|
| 266 | g_free( url ); |
---|
| 267 | } |
---|
| 268 | else |
---|
| 269 | { |
---|
| 270 | fprintf( stderr, "Error: Unknown setting `%s` in configuration file.\n", ini->key ); |
---|
| 271 | return( 0 ); |
---|
| 272 | /* For now just ignore unknown keys... */ |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | else if( g_strcasecmp( ini->section, "defaults" ) != 0 ) |
---|
| 276 | { |
---|
| 277 | fprintf( stderr, "Error: Unknown section [%s] in configuration file. " |
---|
| 278 | "BitlBee configuration must be put in a [settings] section!\n", ini->section ); |
---|
| 279 | return( 0 ); |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | ini_close( ini ); |
---|
| 283 | |
---|
| 284 | return( 1 ); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | void conf_loaddefaults( irc_t *irc ) |
---|
| 288 | { |
---|
| 289 | ini_t *ini; |
---|
| 290 | |
---|
| 291 | ini = ini_open( CONF_FILE ); |
---|
| 292 | if( ini == NULL ) return; |
---|
| 293 | while( ini_read( ini ) ) |
---|
| 294 | { |
---|
| 295 | if( g_strcasecmp( ini->section, "defaults" ) == 0 ) |
---|
| 296 | { |
---|
| 297 | set_t *s = set_find( irc, ini->key ); |
---|
| 298 | |
---|
| 299 | if( s ) |
---|
| 300 | { |
---|
| 301 | if( s->def ) g_free( s->def ); |
---|
| 302 | s->def = g_strdup( ini->value ); |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | ini_close( ini ); |
---|
| 307 | } |
---|