comparison src/configfile.c @ 43:2842f50feb55 noffle

[svn] * client.c, client.h, common.h, config.c, config.h, content.c, content.h, control.c, control.h, database.c, database.h, dynamicstring.c, dynamicstring.h, fetch.c, fetch.h, fetchlist.c, fetchlist.h, group.c, group.h, itemlist.c, itemlist.h, lock.c, lock.h, log.c, log.h, noffle.c, online.c, online.h, outgoing.c, outgoing.h, over.c, over.h, post.c, post.h, protocol.c, protocol.h, pseudo.c, pseudo.h, request.c, request.h, server.c, server.h, util.c, util.h, wildmat.c, wildmat.h: Moved files to the subdirectory src/ * Makefile.am, acconfig.h, configure.in, docs/Makefile.am, src/Makefile.am, Makefile.in, aclocal.m4, config.h.in, configure, install-sh, missing, mkinstalldirs, stamp-h.in, docs/Makefile.in, src/Makefile.in: Added files. They are used by aclocal, autoheader, autoconf and automake. * src/config.c, src/config.h: Renamed to configfile.c and configfile.h, because configure will generate a config.h file itself. * src/client.c, src/content.c, src/database.c, src/fetch.c, src/fetchlist.c, src/group.c, src/lock.c, src/noffle.c, src/online.c, src/outgoing.c, src/over.c, src/pseudo.c, src/request.c, src/server.c, src/util.c: Changed '#include "config.h"' to '#include "configfile.h"'. * src/client.c, src/content.c, src/database.c, src/fetch.c, src/fetchlist.c, src/group.c, src/lock.c, src/online.c, src/outgoing.c, src/post.c, src/protocol.c, src/request.c, src/server.c: Files now #include <config.h>. Added missing <stdio.h>. This removes the warnings about snprintf() not being declared. * Makefile: Removed. This is now generated by configure.
author uh1763
date Fri, 05 May 2000 22:45:56 +0100
parents
children 125d79c9e586
comparison
equal deleted inserted replaced
42:2467ff423c15 43:2842f50feb55
1 /*
2 configfile.c
3
4 The following macros must be set, when compiling this file:
5 CONFIGFILE
6 SPOOLDIR
7 VERSION
8
9 $Id: configfile.c 49 2000-05-05 21:45:56Z uh1763 $
10 */
11
12 #if HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15
16 #include "configfile.h"
17
18 #include <limits.h>
19 #include "log.h"
20 #include "util.h"
21
22 typedef struct
23 {
24 Str name;
25 Str user;
26 Str pass;
27 }
28 ServEntry;
29
30 typedef struct
31 {
32 Str pattern;
33 int days;
34 }
35 ExpireEntry;
36
37 struct
38 {
39 /* Compile time options */
40 const char *spoolDir;
41 const char *version;
42 /* Options from the config file */
43 int maxFetch;
44 int autoUnsubscribeDays;
45 int threadFollowTime;
46 int connectTimeout;
47 Bool autoSubscribe;
48 Bool autoUnsubscribe;
49 Bool removeMsgId;
50 Bool replaceMsgId;
51 Str autoSubscribeMode;
52 Str mailTo;
53 int defaultExpire;
54 int numServ;
55 int maxServ;
56 ServEntry *serv;
57 int servIdx; /* for server enumeration */
58 int numExpire;
59 int maxExpire;
60 ExpireEntry *expire;
61 int expireIdx;
62 } config =
63 {
64 SPOOLDIR, /* spoolDir */
65 VERSION, /* version */
66 300, /* maxFetch */
67 30, /* autoUnsubscribeDays */
68 7, /* threadFollowTime */
69 30, /* connectTimeout */
70 FALSE, /* autoSubscribe */
71 FALSE, /* autoUnsubscribe */
72 FALSE, /* removeMsgId */
73 TRUE, /* replaceMsgId */
74 "over", /* autoSubscribeMode */
75 "", /* mailTo */
76 14, /* defaultExpire */
77 0, /* numServ */
78 0, /* maxServ */
79 NULL, /* serv */
80 0, /* servIdx */
81 0, /* numExpire */
82 0, /* maxExpire */
83 NULL, /* expire */
84 0 /* expireIdx */
85 };
86
87 const char * Cfg_spoolDir( void ) { return config.spoolDir; }
88 const char * Cfg_version( void ) { return config.version; }
89
90 int Cfg_maxFetch( void ) { return config.maxFetch; }
91 int Cfg_autoUnsubscribeDays( void ) { return config.autoUnsubscribeDays; }
92 int Cfg_threadFollowTime( void ) { return config.threadFollowTime; }
93 int Cfg_connectTimeout( void ) { return config.connectTimeout; }
94 Bool Cfg_autoUnsubscribe( void ) { return config.autoUnsubscribe; }
95 Bool Cfg_autoSubscribe( void ) { return config.autoSubscribe; }
96 Bool Cfg_removeMsgId( void ) { return config.removeMsgId; }
97 Bool Cfg_replaceMsgId( void ) { return config.replaceMsgId; }
98 const char * Cfg_autoSubscribeMode( void ) {
99 return config.autoSubscribeMode; }
100 const char * Cfg_mailTo( void ) { return config.mailTo; }
101 int Cfg_expire( void ) { return config.defaultExpire; }
102
103 void
104 Cfg_beginServEnum( void )
105 {
106 config.servIdx = 0;
107 }
108
109 Bool
110 Cfg_nextServ( Str name )
111 {
112 if ( config.servIdx >= config.numServ )
113 return FALSE;
114 strcpy( name, config.serv[ config.servIdx ].name );
115 ++config.servIdx;
116 return TRUE;
117 }
118
119 static Bool
120 searchServ( const char *name, int *idx )
121 {
122 int i;
123
124 for ( i = 0; i < config.numServ; ++i )
125 if ( strcmp( name, config.serv[ i ].name ) == 0 )
126 {
127 *idx = i;
128 return TRUE;
129 }
130 return FALSE;
131 }
132
133 Bool
134 Cfg_servListContains( const char *name )
135 {
136 int idx;
137
138 return searchServ( name, &idx );
139 }
140
141 Bool
142 Cfg_servIsPreferential( const char *name1, const char *name2 )
143 {
144 Bool exists1, exists2;
145 int idx1, idx2;
146
147 exists1 = searchServ( name1, &idx1 );
148 exists2 = searchServ( name2, &idx2 );
149 if ( exists1 && exists2 )
150 return ( idx1 < idx2 );
151 if ( exists1 && ! exists2 )
152 return TRUE;
153 /* ( ! exists1 && exists2 ) || ( ! exists1 && ! exists2 ) */
154 return FALSE;
155 }
156
157 void
158 Cfg_authInfo( const char *name, Str user, Str pass )
159 {
160 int idx;
161
162 if ( searchServ( name, &idx ) )
163 {
164 strcpy( user, config.serv[ idx ].user );
165 strcpy( pass, config.serv[ idx ].pass );
166 }
167 else
168 {
169 user[ 0 ] = '\0';
170 pass[ 0 ] = '\0';
171 }
172 }
173
174 void
175 Cfg_beginExpireEnum( void )
176 {
177 config.expireIdx = 0;
178 }
179
180 int
181 Cfg_nextExpire( Str pattern )
182 {
183 if ( config.expireIdx >= config.numExpire )
184 return -1;
185 strcpy( pattern, config.expire[ config.expireIdx ].pattern );
186 return config.expire[ config.expireIdx++ ].days;
187 }
188
189 static void
190 logSyntaxErr( const char *line )
191 {
192 Log_err( "Syntax error in config file: %s", line );
193 }
194
195 static void
196 getBool( Bool *variable, const char *line )
197 {
198 Str value, name, lowerLn;
199
200 strcpy( lowerLn, line );
201 Utl_toLower( lowerLn );
202 if ( sscanf( lowerLn, "%s %s", name, value ) != 2 )
203 {
204 logSyntaxErr( line );
205 return;
206 }
207
208 if ( strcmp( value, "yes" ) == 0 )
209 *variable = TRUE;
210 else if ( strcmp( value, "no" ) == 0 )
211 *variable = FALSE;
212 else
213 Log_err( "Error in config file %s must be yes or no", name );
214 }
215
216 static void
217 getInt( int *variable, int min, int max, const char *line )
218 {
219 int value;
220 Str name;
221
222 if ( sscanf( line, "%s %d", name, &value ) != 2 )
223 {
224 logSyntaxErr( line );
225 return;
226 }
227 if ( value < min || value > max )
228 {
229 Log_err( "Range error in config file %s [%d,%d]", name, min, max );
230 return;
231 }
232 *variable = value;
233 }
234
235 static void
236 getStr( char *variable, const char *line )
237 {
238 Str dummy;
239
240 if ( sscanf( line, "%s %s", dummy, variable ) != 2 )
241 {
242 logSyntaxErr( line );
243 return;
244 }
245 }
246
247 static void
248 getServ( const char *line )
249 {
250 Str dummy;
251 int r, len;
252 ServEntry entry;
253
254 entry.user[ 0 ] = '\0';
255 entry.pass[ 0 ] = '\0';
256 r = sscanf( line, "%s %s %s %s",
257 dummy, entry.name, entry.user, entry.pass );
258 if ( r < 2 )
259 {
260 logSyntaxErr( line );
261 return;
262 }
263 len = strlen( entry.name );
264 /* To make server name more definit, it is made lowercase and
265 port is removed, if it is the default port */
266 if ( len > 4 && strcmp( entry.name + len - 4, ":119" ) == 0 )
267 entry.name[ len - 4 ] = '\0';
268 Utl_toLower( entry.name );
269
270 if ( config.maxServ < config.numServ + 1 )
271 {
272 if ( ! ( config.serv = realloc( config.serv,
273 ( config.maxServ + 5 )
274 * sizeof( ServEntry ) ) ) )
275 {
276 Log_err( "Could not realloc server list" );
277 exit( EXIT_FAILURE );
278 }
279 config.maxServ += 5;
280 }
281 config.serv[ config.numServ++ ] = entry;
282 }
283
284 static void
285 getExpire( const char *line )
286 {
287 Str dummy;
288 ExpireEntry entry;
289 int days;
290
291 /*
292 The line is either "expire <num>" or "expire <pat> <num>".
293 The former updates the overall default.
294 */
295 if ( sscanf( line, "%s %s %d", dummy, entry.pattern, &days ) != 3 )
296 {
297 logSyntaxErr( line );
298 return;
299 }
300 else
301 {
302 if ( days < 0 )
303 {
304 Log_err( "Expire days error in '%s': must be integer > 0",
305 line, days );
306 return;
307 }
308
309 Utl_toLower( entry.pattern );
310 entry.days = days;
311
312 if ( config.maxExpire < config.numExpire + 1 )
313 {
314 if ( ! ( config.expire = realloc( config.expire,
315 ( config.maxExpire + 5 )
316 * sizeof( ExpireEntry ) ) ) )
317 {
318 Log_err( "Could not realloc exipre list" );
319 exit( EXIT_FAILURE );
320 }
321 config.maxExpire += 5;
322 }
323 config.expire[ config.numExpire++ ] = entry;
324 }
325 }
326
327 void
328 Cfg_read( void )
329 {
330 char *p;
331 FILE *f;
332 Str file, line, lowerLine, name, s;
333
334 snprintf( file, MAXCHAR, CONFIGFILE );
335 if ( ! ( f = fopen( file, "r" ) ) )
336 {
337 Log_err( "Cannot read %s", file );
338 return;
339 }
340 while ( fgets( line, MAXCHAR, f ) )
341 {
342 p = Utl_stripWhiteSpace( line );
343 Utl_stripComment( p );
344 Utl_cpyStr( lowerLine, p );
345 Utl_toLower( lowerLine );
346 if ( *p == '\0' )
347 continue;
348 if ( sscanf( p, "%s", name ) != 1 )
349 Log_err( "Syntax error in %s: %s", file, line );
350 else if ( strcmp( "max-fetch", name ) == 0 )
351 getInt( &config.maxFetch, 0, INT_MAX, p );
352 else if ( strcmp( "auto-unsubscribe-days", name ) == 0 )
353 getInt( &config.autoUnsubscribe, -1, INT_MAX, p );
354 else if ( strcmp( "thread-follow-time", name ) == 0 )
355 getInt( &config.threadFollowTime, 0, INT_MAX, p );
356 else if ( strcmp( "connect-timeout", name ) == 0 )
357 getInt( &config.connectTimeout, 0, INT_MAX, p );
358 else if ( strcmp( "default-expire", name ) == 0 )
359 getInt( &config.defaultExpire, 0, INT_MAX, p );
360 else if ( strcmp( "auto-subscribe", name ) == 0 )
361 getBool( &config.autoSubscribe, p );
362 else if ( strcmp( "auto-unsubscribe", name ) == 0 )
363 getBool( &config.autoUnsubscribe, p );
364 else if ( strcmp( "remove-messageid", name ) == 0 )
365 getBool( &config.removeMsgId, p );
366 else if ( strcmp( "replace-messageid", name ) == 0 )
367 getBool( &config.replaceMsgId, p );
368 else if ( strcmp( "auto-subscribe-mode", name ) == 0 )
369 {
370 getStr( s, p );
371 Utl_toLower( s );
372 if ( strcmp( s, "full" ) != 0
373 && strcmp( s, "thread" ) != 0
374 && strcmp( s, "over" ) != 0
375 && strcmp( s, "off" ) != 0 )
376 {
377 Log_err( "Syntax error in config file: %s", line );
378 return;
379 }
380 else
381 strcpy( config.autoSubscribeMode, s );
382 }
383 else if ( strcmp( "server", name ) == 0 )
384 /* Server needs line not p,
385 because password may contain uppercase */
386 getServ( line );
387 else if ( strcmp( "mail-to", name ) == 0 )
388 getStr( config.mailTo, p );
389 else if ( strcmp( "expire", name ) == 0 )
390 getExpire( p );
391 else
392 Log_err( "Unknown config option: %s", name );
393 }
394 fclose( f );
395 if ( ! config.numServ )
396 {
397 Log_err( "Config file contains no server" );
398 exit( EXIT_FAILURE );
399 }
400 }