]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/inspircd_io.h
Slight optimisation to config reader, looks nicer even if it's not faster.
[user/henk/code/inspircd.git] / include / inspircd_io.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifndef __INSPIRCD_IO_H__
18 #define __INSPIRCD_IO_H__
19
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 #include <map>
24 #include "inspircd.h"
25 #include "globals.h"
26 #include "modules.h"
27
28 /** Flags for use with log()
29  */
30 #define DEBUG 10
31 #define VERBOSE 20
32 #define DEFAULT 30
33 #define SPARSE 40
34 #define NONE 50
35
36 typedef bool (*Validator)(const char*, const char*, void*);
37 typedef bool (*MultiValidator)(const char*, char**, void**, int*);
38 typedef bool (*MultiNotify)(const char*);
39
40 enum ConfigDataType { DT_NOTHING, DT_INTEGER, DT_CHARPTR, DT_BOOLEAN };
41
42 struct InitialConfig {
43         char* tag;
44         char* value;
45         void* val;
46         ConfigDataType datatype;
47         Validator validation_function;
48 };
49
50 struct MultiConfig {
51         const char* tag;
52         char* items[12];
53         int datatype[12];
54         MultiNotify     init_function;
55         MultiValidator  validation_function;
56         MultiNotify     finish_function;
57 };
58
59 /** This class holds the bulk of the runtime configuration for the ircd.
60  * It allows for reading new config values, accessing configuration files,
61  * and storage of the configuration data needed to run the ircd, such as
62  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
63  */
64 class ServerConfig : public classbase
65 {
66   private:
67         /** This variable holds the names of all
68          * files included from the main one. This
69          * is used to make sure that no files are
70          * recursively included.
71          */
72         std::vector<std::string> include_stack;
73
74         /** This private method processes one line of
75          * configutation, appending errors to errorstream
76          * and setting error if an error has occured.
77          */
78         bool ParseLine(ConfigDataHash &target, std::string &line, long linenumber, std::ostringstream &errorstream);
79   
80         bool DoInclude(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream);
81
82         /** Check that there is only one of each configuration item
83          */
84         bool CheckOnce(char* tag, bool bail, userrec* user);
85   
86   public:
87           
88         /** This holds all the information in the config file,
89          * it's indexed by tag name to a vector of key/values.
90          */
91         ConfigDataHash config_data;
92
93         /** Holds the server name of the local server
94          * as defined by the administrator.
95          */
96         char ServerName[MAXBUF];
97         
98         /* Holds the network name the local server
99          * belongs to. This is an arbitary field defined
100          * by the administrator.
101          */
102         char Network[MAXBUF];
103
104         /** Holds the description of the local server
105          * as defined by the administrator.
106          */
107         char ServerDesc[MAXBUF];
108
109         /** Holds the admin's name, for output in
110          * the /ADMIN command.
111          */
112         char AdminName[MAXBUF];
113
114         /** Holds the email address of the admin,
115          * for output in the /ADMIN command.
116          */
117         char AdminEmail[MAXBUF];
118
119         /** Holds the admin's nickname, for output
120          * in the /ADMIN command
121          */
122         char AdminNick[MAXBUF];
123
124         /** The admin-configured /DIE password
125          */
126         char diepass[MAXBUF];
127
128         /** The admin-configured /RESTART password
129          */
130         char restartpass[MAXBUF];
131
132         /** The pathname and filename of the message of the
133          * day file, as defined by the administrator.
134          */
135         char motd[MAXBUF];
136
137         /** The pathname and filename of the rules file,
138          * as defined by the administrator.
139          */
140         char rules[MAXBUF];
141
142         /** The quit prefix in use, or an empty string
143          */
144         char PrefixQuit[MAXBUF];
145
146         /** The last string found within a <die> tag, or
147          * an empty string.
148          */
149         char DieValue[MAXBUF];
150
151         /** The DNS server to use for DNS queries
152          */
153         char DNSServer[MAXBUF];
154
155         /** This variable contains a space-seperated list
156          * of commands which are disabled by the
157          * administrator of the server for non-opers.
158          */
159         char DisabledCommands[MAXBUF];
160
161         /** The full path to the modules directory.
162          * This is either set at compile time, or
163          * overridden in the configuration file via
164          * the <options> tag.
165          */
166         char ModPath[1024];
167
168         /** The temporary directory where modules are copied
169          */
170         char TempDir[1024];
171
172         /** The full pathname to the executable, as
173          * given in argv[0] when the program starts.
174          */
175         char MyExecutable[1024];
176
177         /** The file handle of the logfile. If this
178          * value is NULL, the log file is not open,
179          * probably due to a permissions error on
180          * startup (this should not happen in normal
181          * operation!).
182          */
183         FILE *log_file;
184
185         /** If this value is true, the owner of the
186          * server specified -nofork on the command
187          * line, causing the daemon to stay in the
188          * foreground.
189          */
190         bool nofork;
191         
192         /** If this value if true then all log
193          * messages will be output, regardless of
194          * the level given in the config file.
195          * This is set with the -debug commandline
196          * option.
197          */
198         bool forcedebug;
199         
200         /** If this is true then log output will be
201          * written to the logfile. This is the default.
202          * If you put -nolog on the commandline then
203          * the logfile will not be written.
204          * This is meant to be used in conjunction with
205          * -debug for debugging without filling up the
206          * hard disk.
207          */
208         bool writelog;
209
210         /** If this value is true, halfops have been
211          * enabled in the configuration file.
212          */
213         bool AllowHalfop;
214
215         /** The number of seconds the DNS subsystem
216          * will wait before timing out any request.
217          */
218         int dns_timeout;
219
220         /** The size of the read() buffer in the user
221          * handling code, used to read data into a user's
222          * recvQ.
223          */
224         int NetBufferSize;
225
226         /** The value to be used for listen() backlogs
227          * as default.
228          */
229         int MaxConn;
230
231         /** The soft limit value assigned to the irc server.
232          * The IRC server will not allow more than this
233          * number of local users.
234          */
235         unsigned int SoftLimit;
236
237         /** Maximum number of targets for a multi target command
238          * such as PRIVMSG or KICK
239          */
240         unsigned int MaxTargets;
241
242         /** The maximum number of /WHO results allowed
243          * in any single /WHO command.
244          */
245         int MaxWhoResults;
246
247         /** True if the DEBUG loglevel is selected.
248          */
249         int debugging;
250
251         /** The loglevel in use by the IRC server
252          */
253         int LogLevel;
254
255         /** How many seconds to wait before exiting
256          * the program when /DIE is correctly issued.
257          */
258         int DieDelay;
259
260         /** True if we're going to hide netsplits as *.net *.split for non-opers
261          */
262         bool HideSplits;
263
264         /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
265          * K-Lines, Z-Lines)
266          */
267         bool HideBans;
268
269         /** If this is enabled then operators will
270          * see invisible (+i) channels in /whois.
271          */
272         bool OperSpyWhois;
273
274         /** Set to a non-empty string to obfuscate the server name of users in WHOIS
275          */
276         char HideWhoisServer[MAXBUF];
277
278         /** A list of IP addresses the server is listening
279          * on.
280          */
281         char addrs[MAXBUF][255];
282
283         /** The MOTD file, cached in a file_cache type.
284          */
285         file_cache MOTD;
286
287         /** The RULES file, cached in a file_cache type.
288          */
289         file_cache RULES;
290
291         /** The full pathname and filename of the PID
292          * file as defined in the configuration.
293          */
294         char PID[1024];
295
296         /** The connect classes in use by the IRC server.
297          */
298         ClassVector Classes;
299
300         /** A list of module names (names only, no paths)
301          * which are currently loaded by the server.
302          */
303         std::vector<std::string> module_names;
304
305         /** A list of ports which the server is listening on
306          */
307         int ports[255];
308
309         /** Boolean sets of which modules implement which functions
310          */
311         char implement_lists[255][255];
312
313         /** Global implementation list
314          */
315         char global_implementation[255];
316
317         /** A list of ports claimed by IO Modules
318          */
319         std::map<int,Module*> IOHookModule;
320
321         /** The 005 tokens of this server (ISUPPORT)
322          * populated/repopulated upon loading or unloading
323          * modules.
324          */
325         std::string data005;
326
327         /** STATS characters in this list are available
328          * only to operators.
329          */
330         char OperOnlyStats[MAXBUF];
331         
332         /** The path and filename of the ircd.log file
333          */
334         std::string logpath;
335
336         /** Custom version string, which if defined can replace the system info in VERSION.
337          */
338         char CustomVersion[MAXBUF];
339
340         /** List of u-lined servers
341          */
342         std::vector<irc::string> ulines;
343
344         /** Max banlist sizes for channels (the std::string is a glob)
345          */
346         std::map<std::string,int> maxbans;
347
348         ServerConfig();
349
350         /** Clears the include stack in preperation for
351          * a Read() call.
352          */
353         void ClearStack();
354
355         /** Read the entire configuration into memory
356          * and initialize this class. All other methods
357          * should be used only by the core.
358          */
359         void Read(bool bail, userrec* user);
360
361         /** Load 'filename' into 'target', with the new config parser everything is parsed into
362          * tag/key/value at load-time rather than at read-value time.
363          */
364         bool LoadConf(ConfigDataHash &target, const char* filename, std::ostringstream &errorstream);
365         bool LoadConf(ConfigDataHash &target, const std::string &filename, std::ostringstream &errorstream);
366         
367         /* Both these return true if the value existed or false otherwise */
368         
369         /* Writes 'length' chars into 'result' as a string */
370         bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length);
371         bool ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result);
372         
373         /* Tries to convert the value to an integer and write it to 'result' */
374         bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, int index, int &result);
375         bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, int &result);
376         
377         /* Returns true if the value exists and has a true value, false otherwise */
378         bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, int index);
379         bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, int index);
380         
381         /* Returns the number of occurences of tag in the config file */
382         int ConfValueEnum(ConfigDataHash &target, const char* tag);
383         int ConfValueEnum(ConfigDataHash &target, const std::string &tag);
384         
385         /* Returns the numbers of vars inside the index'th 'tag in the config file */
386         int ConfVarEnum(ConfigDataHash &target, const char* tag, int index);
387         int ConfVarEnum(ConfigDataHash &target, const std::string &tag, int index);
388         
389         Module* GetIOHook(int port);
390         bool AddIOHook(int port, Module* iomod);
391         bool DelIOHook(int port);
392 };
393
394
395 void Exit(int status); 
396 void Start(); 
397 void SetSignals();
398 bool DaemonSeed(); 
399 bool FileExists(const char* file);
400 int OpenTCPSocket(); 
401 bool BindSocket(int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr);
402 void WritePID(const std::string &filename);
403 int BindPorts(bool bail);
404 char* CleanFilename(char* name);
405
406 #endif