]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conf.c
fec8903a1fc7408b52d0dd45478ff59ed9120efd
[ngircd-alex.git] / src / ngircd / conf.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001 by Alexander Barton (alex@barton.de)
4  *
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 comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
11  *
12  * $Id: conf.c,v 1.5 2001/12/30 19:26:11 alex Exp $
13  *
14  * conf.h: Konfiguration des ngircd
15  *
16  * $Log: conf.c,v $
17  * Revision 1.5  2001/12/30 19:26:11  alex
18  * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
19  *
20  * Revision 1.4  2001/12/26 22:48:53  alex
21  * - MOTD-Datei ist nun konfigurierbar und wird gelesen.
22  *
23  * Revision 1.3  2001/12/26 14:45:37  alex
24  * - "Code Cleanups".
25  *
26  * Revision 1.2  2001/12/26 03:19:57  alex
27  * - erste Konfigurations-Variablen definiert: PING/PONG-Timeout.
28  *
29  * Revision 1.1  2001/12/12 17:18:20  alex
30  * - Modul fuer Server-Konfiguration begonnen.
31  */
32
33
34 #include <portab.h>
35 #include "global.h"
36
37 #include <imp.h>
38 #include <assert.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 #include "client.h"
44 #include "log.h"
45 #include "tool.h"
46
47 #include <exp.h>
48 #include "conf.h"
49
50
51 #define MAX_LINE_LEN 246                /* max. Laenge einer Konfigurationszeile */
52
53
54 LOCAL VOID Read_Config( VOID );
55 LOCAL VOID Validate_Config( VOID );
56
57
58 GLOBAL VOID Conf_Init( VOID )
59 {
60         /* Konfigurationsvariablen initialisieren: zunaechst Default-
61          * Werte setzen, dann Konfigurationsdtaei einlesen. */
62
63         strcpy( Conf_File, "/usr/local/etc/ngircd.conf" );
64
65         strcpy( Conf_ServerName, "" );
66
67         strcpy( Conf_MotdFile, "/usr/local/etc/ngircd.motd" );
68
69         Conf_ListenPorts_Count = 0;
70         
71         Conf_PingTimeout = 120;
72         Conf_PongTimeout = 10;
73
74         /* Konfigurationsdatei einlesen und validieren */
75         Read_Config( );
76         Validate_Config( );
77 } /* Config_Init */
78
79
80 GLOBAL VOID Conf_Exit( VOID )
81 {
82         /* ... */
83 } /* Config_Exit */
84
85
86 LOCAL VOID Read_Config( VOID )
87 {
88         /* Konfigurationsdatei einlesen. */
89
90         CHAR str[MAX_LINE_LEN], *var, *arg, *ptr;
91         BOOLEAN ok;
92         INT32 port;
93         INT line;
94         FILE *fd;
95
96         fd = fopen( Conf_File, "r" );
97         if( ! fd )
98         {
99                 /* Keine Konfigurationsdatei gefunden */
100                 Log( LOG_ALERT, "Can't read configuration \"%s\": %s", Conf_File, strerror( errno ));
101                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
102                 exit( 1 );
103         }
104
105         line = 0;
106         while( TRUE )
107         {
108                 if( ! fgets( str, MAX_LINE_LEN, fd )) break;
109                 ngt_TrimStr( str );
110                 line++;
111
112                 /* Kommentarzeilen und leere Zeilen ueberspringen */
113                 if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
114
115                 ok = FALSE;
116
117                 ptr = strchr( str, '=' );
118                 if( ! ptr )
119                 {
120                         Log( LOG_ERR, "%s, line %d: Syntax error!", Conf_File, line );
121                         continue;
122                 }
123                 *ptr = '\0';
124
125                 var = str; ngt_TrimStr( var );
126                 arg = ptr + 1; ngt_TrimStr( arg );
127                 
128                 if( strcasecmp( str, "ServerName" ) == 0 )
129                 {
130                         /* Der Server-Name */
131                         strncpy( Conf_ServerName, arg, CLIENT_ID_LEN );
132                         Conf_ServerName[CLIENT_ID_LEN] = '\0';
133                         ok = TRUE;
134                 }
135                 else if( strcasecmp( str, "ListenPorts" ) == 0 )
136                 {
137                         /* Ports, durch "," getrennt, auf denen der Server
138                          * Verbindungen annehmen soll */
139                         ptr = strtok( arg, "," );
140                         while( ptr )
141                         {
142                                 ngt_TrimStr( ptr );
143                                 port = atol( ptr );
144                                 if( Conf_ListenPorts_Count + 1 > LISTEN_PORTS ) Log( LOG_ERR, "Too many listen ports configured. Port %ld ignored.", port );
145                                 if( port > 0 && port < 0xFFFF ) Conf_ListenPorts[Conf_ListenPorts_Count++] = port;
146                                 else Log( LOG_ERR, "Illegal port number: %ld. Ignored.", port );
147                                 ptr = strtok( NULL, "," );
148                         }
149                         ok = TRUE;
150                 }
151                 else if( strcasecmp( str, "MotdFile" ) == 0 )
152                 {
153                         /* Datei mit der "message of the day" (MOTD) */
154                         strncpy( Conf_MotdFile, arg, FNAME_LEN );
155                         Conf_MotdFile[FNAME_LEN] = '\0';
156                         ok = TRUE;
157                 }
158                 else if( strcasecmp( str, "PingTimeout" ) == 0 )
159                 {
160                         /* PING-Timeout */
161                         Conf_PingTimeout = atoi( arg );
162                         if(( Conf_PingTimeout ) < 5 ) Conf_PingTimeout = 5;
163                         ok = TRUE;
164                 }
165                 else if( strcasecmp( str, "PongTimeout" ) == 0 )
166                 {
167                         /* PONG-Timeout */
168                         Conf_PongTimeout = atoi( arg );
169                         if(( Conf_PongTimeout ) < 5 ) Conf_PongTimeout = 5;
170                         ok = TRUE;
171                 }
172                 
173                 if( ! ok ) Log( LOG_ERR, "%s, line %d: Unknown variable \"%s\"!", Conf_File, line, var );
174         }
175         
176         fclose( fd );
177 } /* Read_Config */
178
179
180 LOCAL VOID Validate_Config( VOID )
181 {
182         /* Konfiguration ueberpruefen */
183         
184         if( ! Conf_ServerName[0] )
185         {
186                 /* Kein Servername konfiguriert */
187                 Log( LOG_ALERT, "No server name configured (use \"ServerName\")!", Conf_File, strerror( errno ));
188                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
189                 exit( 1 );
190         }
191 } /* Validate_Config */
192
193 /* -eof- */