[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 | |
---|
| 48 | conf->iface = "0.0.0.0"; |
---|
| 49 | conf->port = 6667; |
---|
| 50 | conf->nofork = 0; |
---|
| 51 | conf->verbose = 0; |
---|
[b73ac9c] | 52 | conf->primary_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 | |
---|
| 143 | static 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 | } |
---|
[b73ac9c] | 201 | else if( g_strcasecmp( ini->key, "account_storage" ) == 0 ) |
---|
[1ee6c18] | 202 | { |
---|
[b73ac9c] | 203 | g_free( conf->primary_storage ); |
---|
| 204 | conf->primary_storage = g_strdup( ini->value ); |
---|
| 205 | } |
---|
| 206 | else if( g_strcasecmp( ini->key, "account_storage_migrate" ) == 0 ) |
---|
| 207 | { |
---|
| 208 | g_strfreev( conf->migrate_storage ); |
---|
| 209 | conf->migrate_storage = g_strsplit( ini->value, " \t,;", -1 ); |
---|
[b7d3cc34] | 210 | } |
---|
| 211 | else if( g_strcasecmp( ini->key, "pinginterval" ) == 0 ) |
---|
| 212 | { |
---|
| 213 | if( sscanf( ini->value, "%d", &i ) != 1 ) |
---|
| 214 | { |
---|
| 215 | fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value ); |
---|
| 216 | return( 0 ); |
---|
| 217 | } |
---|
| 218 | conf->ping_interval = i; |
---|
| 219 | } |
---|
| 220 | else if( g_strcasecmp( ini->key, "pingtimeout" ) == 0 ) |
---|
| 221 | { |
---|
| 222 | if( sscanf( ini->value, "%d", &i ) != 1 ) |
---|
| 223 | { |
---|
| 224 | fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value ); |
---|
| 225 | return( 0 ); |
---|
| 226 | } |
---|
| 227 | conf->ping_timeout = i; |
---|
| 228 | } |
---|
| 229 | else if( g_strcasecmp( ini->key, "proxy" ) == 0 ) |
---|
| 230 | { |
---|
| 231 | url_t *url = g_new0( url_t, 1 ); |
---|
| 232 | |
---|
| 233 | if( !url_set( url, ini->value ) ) |
---|
| 234 | { |
---|
| 235 | fprintf( stderr, "Invalid %s value: %s\n", ini->key, ini->value ); |
---|
| 236 | g_free( url ); |
---|
| 237 | return( 0 ); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | strncpy( proxyhost, url->host, sizeof( proxyhost ) ); |
---|
| 241 | strncpy( proxyuser, url->user, sizeof( proxyuser ) ); |
---|
| 242 | strncpy( proxypass, url->pass, sizeof( proxypass ) ); |
---|
| 243 | proxyport = url->port; |
---|
| 244 | if( url->proto == PROTO_HTTP ) |
---|
| 245 | proxytype = PROXY_HTTP; |
---|
| 246 | else if( url->proto == PROTO_SOCKS4 ) |
---|
| 247 | proxytype = PROXY_SOCKS4; |
---|
| 248 | else if( url->proto == PROTO_SOCKS5 ) |
---|
| 249 | proxytype = PROXY_SOCKS5; |
---|
| 250 | |
---|
| 251 | g_free( url ); |
---|
| 252 | } |
---|
| 253 | else |
---|
| 254 | { |
---|
| 255 | fprintf( stderr, "Error: Unknown setting `%s` in configuration file.\n", ini->key ); |
---|
| 256 | return( 0 ); |
---|
| 257 | /* For now just ignore unknown keys... */ |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | else if( g_strcasecmp( ini->section, "defaults" ) != 0 ) |
---|
| 261 | { |
---|
| 262 | fprintf( stderr, "Error: Unknown section [%s] in configuration file. " |
---|
| 263 | "BitlBee configuration must be put in a [settings] section!\n", ini->section ); |
---|
| 264 | return( 0 ); |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | ini_close( ini ); |
---|
| 268 | |
---|
| 269 | return( 1 ); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | void conf_loaddefaults( irc_t *irc ) |
---|
| 273 | { |
---|
| 274 | ini_t *ini; |
---|
| 275 | |
---|
| 276 | ini = ini_open( CONF_FILE ); |
---|
| 277 | if( ini == NULL ) return; |
---|
| 278 | while( ini_read( ini ) ) |
---|
| 279 | { |
---|
| 280 | if( g_strcasecmp( ini->section, "defaults" ) == 0 ) |
---|
| 281 | { |
---|
| 282 | set_t *s = set_find( irc, ini->key ); |
---|
| 283 | |
---|
| 284 | if( s ) |
---|
| 285 | { |
---|
| 286 | if( s->def ) g_free( s->def ); |
---|
| 287 | s->def = g_strdup( ini->value ); |
---|
| 288 | } |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | ini_close( ini ); |
---|
| 292 | } |
---|