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