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