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