2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 * der GNU General Public License (GPL), wie von der Free Software Foundation
7 * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
12 * $Id: conf.c,v 1.20 2002/03/25 19:11:01 alex Exp $
14 * conf.h: Konfiguration des ngircd
36 LOCAL VOID Read_Config( VOID );
38 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg );
39 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg );
40 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg );
42 LOCAL VOID Validate_Config( VOID );
45 GLOBAL VOID Conf_Init( VOID )
47 /* Konfigurationsvariablen initialisieren: zunaechst Default-
48 * Werte setzen, dann Konfigurationsdtaei einlesen. */
50 strcpy( Conf_File, CONFIG_FILE );
52 strcpy( Conf_ServerName, "" );
53 strcpy( Conf_ServerInfo, PACKAGE" "VERSION );
54 strcpy( Conf_ServerPwd, "" );
56 strcpy( Conf_MotdFile, MOTD_FILE );
58 Conf_ListenPorts_Count = 0;
60 Conf_PingTimeout = 120;
61 Conf_PongTimeout = 20;
63 Conf_ConnectRetry = 60;
67 Conf_Server_Count = 0;
69 /* Konfigurationsdatei einlesen und validieren */
75 GLOBAL VOID Conf_Exit( VOID )
81 LOCAL VOID Read_Config( VOID )
83 /* Konfigurationsdatei einlesen. */
85 CHAR section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
89 fd = fopen( Conf_File, "r" );
92 /* Keine Konfigurationsdatei gefunden */
93 Log( LOG_ALERT, "Can't read configuration \"%s\": %s", Conf_File, strerror( errno ));
94 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
99 strcpy( section, "" );
102 if( ! fgets( str, LINE_LEN, fd )) break;
106 /* Kommentarzeilen und leere Zeilen ueberspringen */
107 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
109 /* Anfang eines Abschnittes? */
110 if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' ))
112 strcpy( section, str );
113 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) continue;
114 if( strcasecmp( section, "[OPERATOR]" ) == 0 )
116 if( Conf_Oper_Count + 1 > MAX_OPERATORS ) Log( LOG_ERR, "Too many operators configured." );
119 /* neuen Operator initialisieren */
120 strcpy( Conf_Oper[Conf_Oper_Count].name, "" );
121 strcpy( Conf_Oper[Conf_Oper_Count].pwd, "" );
126 if( strcasecmp( section, "[SERVER]" ) == 0 )
128 if( Conf_Server_Count + 1 > MAX_SERVERS ) Log( LOG_ERR, "Too many servers configured." );
131 /* neuen Server ("Peer") initialisieren */
132 strcpy( Conf_Server[Conf_Server_Count].host, "" );
133 strcpy( Conf_Server[Conf_Server_Count].ip, "" );
134 strcpy( Conf_Server[Conf_Server_Count].name, "" );
135 strcpy( Conf_Server[Conf_Server_Count].pwd, "" );
136 Conf_Server[Conf_Server_Count].port = 0;
137 Conf_Server[Conf_Server_Count].group = -1;
138 Conf_Server[Conf_Server_Count].lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
139 Conf_Server[Conf_Server_Count].res_stat = NULL;
144 Log( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", Conf_File, line, section );
147 if( section[0] == 0x1 ) continue;
149 /* In Variable und Argument zerlegen */
150 ptr = strchr( str, '=' );
153 Log( LOG_ERR, "%s, line %d: Syntax error!", Conf_File, line );
157 var = str; ngt_TrimStr( var );
158 arg = ptr + 1; ngt_TrimStr( arg );
160 if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
161 else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
162 else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
163 else Log( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", Conf_File, line, var );
170 GLOBAL VOID Handle_GLOBAL( INT Line, CHAR *Var, CHAR *Arg )
176 assert( Var != NULL );
177 assert( Arg != NULL );
179 if( strcasecmp( Var, "Name" ) == 0 )
181 /* Der Server-Name */
182 strncpy( Conf_ServerName, Arg, CLIENT_ID_LEN - 1 );
183 Conf_ServerName[CLIENT_ID_LEN - 1] = '\0';
186 if( strcasecmp( Var, "Info" ) == 0 )
188 /* Server-Info-Text */
189 strncpy( Conf_ServerInfo, Arg, CLIENT_INFO_LEN - 1 );
190 Conf_ServerInfo[CLIENT_INFO_LEN - 1] = '\0';
193 if( strcasecmp( Var, "Password" ) == 0 )
195 /* Server-Passwort */
196 strncpy( Conf_ServerPwd, Arg, CLIENT_PASS_LEN - 1 );
197 Conf_ServerPwd[CLIENT_PASS_LEN - 1] = '\0';
200 if( strcasecmp( Var, "Ports" ) == 0 )
202 /* Ports, durch "," getrennt, auf denen der Server
203 * Verbindungen annehmen soll */
204 ptr = strtok( Arg, "," );
209 if( Conf_ListenPorts_Count + 1 > MAX_LISTEN_PORTS ) Log( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
212 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = (INT)port;
213 else Log( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!", Conf_File, Line, port );
215 ptr = strtok( NULL, "," );
219 if( strcasecmp( Var, "MotdFile" ) == 0 )
221 /* Datei mit der "message of the day" (MOTD) */
222 strncpy( Conf_MotdFile, Arg, FNAME_LEN - 1 );
223 Conf_MotdFile[FNAME_LEN - 1] = '\0';
226 if( strcasecmp( Var, "PingTimeout" ) == 0 )
229 Conf_PingTimeout = atoi( Arg );
230 if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
233 if( strcasecmp( Var, "PongTimeout" ) == 0 )
236 Conf_PongTimeout = atoi( Arg );
237 if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
240 if( strcasecmp( Var, "ConnectRetry" ) == 0 )
242 /* Sekunden zwischen Verbindungsversuchen zu anderen Servern */
243 Conf_ConnectRetry = atoi( Arg );
244 if(( Conf_ConnectRetry ) < 5 ) Conf_ConnectRetry = 5;
248 Log( LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
249 } /* Handle_GLOBAL */
252 GLOBAL VOID Handle_OPERATOR( INT Line, CHAR *Var, CHAR *Arg )
255 assert( Var != NULL );
256 assert( Arg != NULL );
257 assert( Conf_Oper_Count > 0 );
259 if( strcasecmp( Var, "Name" ) == 0 )
261 /* Name des IRC Operator */
262 strncpy( Conf_Oper[Conf_Oper_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
263 Conf_Oper[Conf_Oper_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
266 if( strcasecmp( Var, "Password" ) == 0 )
268 /* Passwort des IRC Operator */
269 strncpy( Conf_Oper[Conf_Oper_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
270 Conf_Oper[Conf_Oper_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
274 Log( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
275 } /* Handle_OPERATOR */
278 GLOBAL VOID Handle_SERVER( INT Line, CHAR *Var, CHAR *Arg )
283 assert( Var != NULL );
284 assert( Arg != NULL );
286 if( strcasecmp( Var, "Host" ) == 0 )
288 /* Hostname des Servers */
289 strncpy( Conf_Server[Conf_Server_Count - 1].host, Arg, HOST_LEN - 1 );
290 Conf_Server[Conf_Server_Count - 1].host[HOST_LEN - 1] = '\0';
293 if( strcasecmp( Var, "Name" ) == 0 )
295 /* Name des Servers ("Nick") */
296 strncpy( Conf_Server[Conf_Server_Count - 1].name, Arg, CLIENT_ID_LEN - 1 );
297 Conf_Server[Conf_Server_Count - 1].name[CLIENT_ID_LEN - 1] = '\0';
300 if( strcasecmp( Var, "Password" ) == 0 )
302 /* Passwort des Servers */
303 strncpy( Conf_Server[Conf_Server_Count - 1].pwd, Arg, CLIENT_PASS_LEN - 1 );
304 Conf_Server[Conf_Server_Count - 1].pwd[CLIENT_PASS_LEN - 1] = '\0';
307 if( strcasecmp( Var, "Port" ) == 0 )
309 /* Port, zu dem Verbunden werden soll */
311 if( port > 0 && port < 0xFFFF ) Conf_Server[Conf_Server_Count - 1].port = (INT)port;
312 else Log( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!", Conf_File, Line, port );
315 if( strcasecmp( Var, "Group" ) == 0 )
318 Conf_Server[Conf_Server_Count - 1].group = atoi( Arg );
322 Log( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!", Conf_File, Line, Var );
323 } /* Handle_SERVER */
326 LOCAL VOID Validate_Config( VOID )
328 /* Konfiguration ueberpruefen */
330 if( ! Conf_ServerName[0] )
332 /* Kein Servername konfiguriert */
333 Log( LOG_ALERT, "No server name configured in \"%s\"!", Conf_File );
334 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
337 } /* Validate_Config */