]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/configreader.h
Tons of tweaks to the config stuff for the core
[user/henk/code/inspircd.git] / include / configreader.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  *                <omster@gmail.com>
10  *     
11  * Written by Craig Edwards, Craig McLure, and others.
12  * This program is free but copyrighted software; see
13  *            the file COPYING for details.
14  *
15  * ---------------------------------------------------
16  */
17
18 #ifndef INSPIRCD_CONFIGREADER
19 #define INSPIRCD_CONFIGREADER
20
21 #include <sstream>
22 #include <string>
23 #include <vector>
24 #include <map>
25 #include "inspircd.h"
26 #include "globals.h"
27 #include "modules.h"
28 #include "socketengine.h"
29 #include "socket.h"
30
31 class ServerConfig;
32 class InspIRCd;
33
34 /** Types of data in the core config
35  */
36 enum ConfigDataType { DT_NOTHING, DT_INTEGER, DT_CHARPTR, DT_BOOLEAN };
37
38 class ValueItem
39 {
40         std::string v;
41  public:
42         ValueItem(int value)
43         {
44                 std::stringstream n;
45                 n << value;
46                 v = n.str();
47         }
48
49         ValueItem(bool value)
50         {
51                 std::stringstream n;
52                 n << value;
53                 v = n.str();
54         }
55
56         ValueItem(char* value)
57         {
58                 v = value;
59         }
60
61         int GetInteger() { return atoi(v.c_str()); };
62
63         const char* GetString() { return v.c_str(); };
64
65         bool GetBool() { return GetInteger(); };
66 };
67
68 typedef std::deque<ValueItem> ValueList;
69
70 /** A callback for validating a single value
71  */
72 typedef bool (*Validator)(ServerConfig* conf, const char*, const char*, void*);
73 /** A callback for validating multiple value entries
74  */
75 typedef bool (*MultiValidator)(ServerConfig* conf, const char*, char**, ValueList&, int*);
76 /** A callback indicating the end of a group of entries
77  */
78 typedef bool (*MultiNotify)(ServerConfig* conf, const char*);
79
80
81 /** Holds a core configuration item and its callbacks
82  */
83 struct InitialConfig
84 {
85         char* tag;
86         char* value;
87         void* val;
88         ConfigDataType datatype;
89         Validator validation_function;
90 };
91
92 /** Holds a core configuration item and its callbacks
93  * where there may be more than one item
94  */
95 struct MultiConfig
96 {
97         const char* tag;
98         char* items[12];
99         int datatype[12];
100         MultiNotify     init_function;
101         MultiValidator  validation_function;
102         MultiNotify     finish_function;
103 };
104
105 /** A set of oper types
106  */
107 typedef std::map<irc::string,char*> opertype_t;
108
109 /** A Set of oper classes
110  */
111 typedef std::map<irc::string,char*> operclass_t;
112
113
114 /** This class holds the bulk of the runtime configuration for the ircd.
115  * It allows for reading new config values, accessing configuration files,
116  * and storage of the configuration data needed to run the ircd, such as
117  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
118  */
119 class ServerConfig : public Extensible
120 {
121   private:
122         /** Creator/owner
123          */
124         InspIRCd* ServerInstance;
125
126         /** This variable holds the names of all
127          * files included from the main one. This
128          * is used to make sure that no files are
129          * recursively included.
130          */
131         std::vector<std::string> include_stack;
132
133         /** This private method processes one line of
134          * configutation, appending errors to errorstream
135          * and setting error if an error has occured.
136          */
137         bool ParseLine(ConfigDataHash &target, std::string &line, long linenumber, std::ostringstream &errorstream);
138   
139         /** Process an include directive
140          */
141         bool DoInclude(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream);
142
143         /** Check that there is only one of each configuration item
144          */
145         bool CheckOnce(char* tag, bool bail, userrec* user);
146   
147   public:
148
149         InspIRCd* GetInstance();
150           
151         /** This holds all the information in the config file,
152          * it's indexed by tag name to a vector of key/values.
153          */
154         ConfigDataHash config_data;
155
156         /** Max number of WhoWas entries per user.
157          */
158         int WhoWasGroupSize;
159
160         /** Max number of cumulative user-entries in WhoWas.
161          *  When max reached and added to, push out oldest entry FIFO style.
162          */
163         int WhoWasMaxGroups;
164
165         /** Max seconds a user is kept in WhoWas before being pruned.
166          */
167         int WhoWasMaxKeep;
168
169         /** Holds the server name of the local server
170          * as defined by the administrator.
171          */
172         char ServerName[MAXBUF];
173         
174         /* Holds the network name the local server
175          * belongs to. This is an arbitary field defined
176          * by the administrator.
177          */
178         char Network[MAXBUF];
179
180         /** Holds the description of the local server
181          * as defined by the administrator.
182          */
183         char ServerDesc[MAXBUF];
184
185         /** Holds the admin's name, for output in
186          * the /ADMIN command.
187          */
188         char AdminName[MAXBUF];
189
190         /** Holds the email address of the admin,
191          * for output in the /ADMIN command.
192          */
193         char AdminEmail[MAXBUF];
194
195         /** Holds the admin's nickname, for output
196          * in the /ADMIN command
197          */
198         char AdminNick[MAXBUF];
199
200         /** The admin-configured /DIE password
201          */
202         char diepass[MAXBUF];
203
204         /** The admin-configured /RESTART password
205          */
206         char restartpass[MAXBUF];
207
208         /** The pathname and filename of the message of the
209          * day file, as defined by the administrator.
210          */
211         char motd[MAXBUF];
212
213         /** The pathname and filename of the rules file,
214          * as defined by the administrator.
215          */
216         char rules[MAXBUF];
217
218         /** The quit prefix in use, or an empty string
219          */
220         char PrefixQuit[MAXBUF];
221
222         /** The last string found within a <die> tag, or
223          * an empty string.
224          */
225         char DieValue[MAXBUF];
226
227         /** The DNS server to use for DNS queries
228          */
229         char DNSServer[MAXBUF];
230
231         /** This variable contains a space-seperated list
232          * of commands which are disabled by the
233          * administrator of the server for non-opers.
234          */
235         char DisabledCommands[MAXBUF];
236
237         /** The full path to the modules directory.
238          * This is either set at compile time, or
239          * overridden in the configuration file via
240          * the <options> tag.
241          */
242         char ModPath[1024];
243
244         /** The temporary directory where modules are copied
245          */
246         char TempDir[1024];
247
248         /** The full pathname to the executable, as
249          * given in argv[0] when the program starts.
250          */
251         char MyExecutable[1024];
252
253         /** The file handle of the logfile. If this
254          * value is NULL, the log file is not open,
255          * probably due to a permissions error on
256          * startup (this should not happen in normal
257          * operation!).
258          */
259         FILE *log_file;
260
261         /** If this value is true, the owner of the
262          * server specified -nofork on the command
263          * line, causing the daemon to stay in the
264          * foreground.
265          */
266         bool nofork;
267         
268         /** If this value if true then all log
269          * messages will be output, regardless of
270          * the level given in the config file.
271          * This is set with the -debug commandline
272          * option.
273          */
274         bool forcedebug;
275         
276         /** If this is true then log output will be
277          * written to the logfile. This is the default.
278          * If you put -nolog on the commandline then
279          * the logfile will not be written.
280          * This is meant to be used in conjunction with
281          * -debug for debugging without filling up the
282          * hard disk.
283          */
284         bool writelog;
285
286         /** If this value is true, halfops have been
287          * enabled in the configuration file.
288          */
289         bool AllowHalfop;
290
291         /** The number of seconds the DNS subsystem
292          * will wait before timing out any request.
293          */
294         int dns_timeout;
295
296         /** The size of the read() buffer in the user
297          * handling code, used to read data into a user's
298          * recvQ.
299          */
300         int NetBufferSize;
301
302         /** The value to be used for listen() backlogs
303          * as default.
304          */
305         int MaxConn;
306
307         /** The soft limit value assigned to the irc server.
308          * The IRC server will not allow more than this
309          * number of local users.
310          */
311         unsigned int SoftLimit;
312
313         /** Maximum number of targets for a multi target command
314          * such as PRIVMSG or KICK
315          */
316         unsigned int MaxTargets;
317
318         /** The maximum number of /WHO results allowed
319          * in any single /WHO command.
320          */
321         int MaxWhoResults;
322
323         /** True if the DEBUG loglevel is selected.
324          */
325         int debugging;
326
327         /** The loglevel in use by the IRC server
328          */
329         int LogLevel;
330
331         /** How many seconds to wait before exiting
332          * the program when /DIE is correctly issued.
333          */
334         int DieDelay;
335
336         /** True if we're going to hide netsplits as *.net *.split for non-opers
337          */
338         bool HideSplits;
339
340         /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
341          * K-Lines, Z-Lines)
342          */
343         bool HideBans;
344
345         /** If this is enabled then operators will
346          * see invisible (+i) channels in /whois.
347          */
348         bool OperSpyWhois;
349
350         /** Set to a non-empty string to obfuscate the server name of users in WHOIS
351          */
352         char HideWhoisServer[MAXBUF];
353
354         /** A list of IP addresses the server is listening
355          * on.
356          */
357         char addrs[MAXBUF][255];
358
359         /** The MOTD file, cached in a file_cache type.
360          */
361         file_cache MOTD;
362
363         /** The RULES file, cached in a file_cache type.
364          */
365         file_cache RULES;
366
367         /** The full pathname and filename of the PID
368          * file as defined in the configuration.
369          */
370         char PID[1024];
371
372         /** The connect classes in use by the IRC server.
373          */
374         ClassVector Classes;
375
376         /** A list of module names (names only, no paths)
377          * which are currently loaded by the server.
378          */
379         std::vector<std::string> module_names;
380
381         /** A list of ports which the server is listening on
382          */
383         int ports[255];
384
385         /** A list of the file descriptors for the listening client ports
386          */
387         ListenSocket* openSockfd[255];
388
389         /** Boolean sets of which modules implement which functions
390          */
391         char implement_lists[255][255];
392
393         /** Global implementation list
394          */
395         char global_implementation[255];
396
397         /** A list of ports claimed by IO Modules
398          */
399         std::map<int,Module*> IOHookModule;
400
401         /** The 005 tokens of this server (ISUPPORT)
402          * populated/repopulated upon loading or unloading
403          * modules.
404          */
405         std::string data005;
406
407         /** STATS characters in this list are available
408          * only to operators.
409          */
410         char UserStats[MAXBUF];
411         
412         /** The path and filename of the ircd.log file
413          */
414         std::string logpath;
415
416         /** Custom version string, which if defined can replace the system info in VERSION.
417          */
418         char CustomVersion[MAXBUF];
419
420         /** List of u-lined servers
421          */
422         std::vector<irc::string> ulines;
423
424         /** Max banlist sizes for channels (the std::string is a glob)
425          */
426         std::map<std::string,int> maxbans;
427
428         /** If set to true, no user DNS lookups are to be performed
429          */
430         bool NoUserDns;
431
432         /** If set to true, provide syntax hints for unknown commands
433          */
434         bool SyntaxHints;
435
436         /** If set to true, users appear to quit then rejoin when their hosts change.
437          * This keeps clients synchronized properly.
438          */
439         bool CycleHosts;
440
441
442 #if __GNUC__ == 3 && __GNUC_MINOR__ == 3
443         /** if we're being compiled with GCC 3.3 add
444          *  dummy opertype on heap to fix GCC 3.3 crash
445          */
446         opertype_t dummyopertype;
447 #endif
448
449         /** All oper type definitions from the config file
450          */
451         opertype_t opertypes;
452
453         /** All oper class definitions from the config file
454          */
455         operclass_t operclass;
456
457         /** Construct a new ServerConfig
458          */
459         ServerConfig(InspIRCd* Instance);
460
461         /** Clears the include stack in preperation for a Read() call.
462          */
463         void ClearStack();
464
465         /** Read the entire configuration into memory
466          * and initialize this class. All other methods
467          * should be used only by the core.
468          */
469         void Read(bool bail, userrec* user);
470
471         /** Read a file into a file_cache object
472          */
473         bool ReadFile(file_cache &F, const char* fname);
474
475         /** Load 'filename' into 'target', with the new config parser everything is parsed into
476          * tag/key/value at load-time rather than at read-value time.
477          */
478         bool LoadConf(ConfigDataHash &target, const char* filename, std::ostringstream &errorstream);
479         /** Load 'filename' into 'target', with the new config parser everything is parsed into
480          * tag/key/value at load-time rather than at read-value time.
481          */
482         bool LoadConf(ConfigDataHash &target, const std::string &filename, std::ostringstream &errorstream);
483         
484         /* Both these return true if the value existed or false otherwise */
485         
486         /** Writes 'length' chars into 'result' as a string
487          */
488         bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length);
489         /** Writes 'length' chars into 'result' as a string
490          */
491         bool ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result);
492         
493         /** Tries to convert the value to an integer and write it to 'result'
494          */
495         bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, int index, int &result);
496         /** Tries to convert the value to an integer and write it to 'result'
497          */
498         bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, int &result);
499         
500         /** Returns true if the value exists and has a true value, false otherwise
501          */
502         bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, int index);
503         /** Returns true if the value exists and has a true value, false otherwise
504          */
505         bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, int index);
506         
507         /** Returns the number of occurences of tag in the config file
508          */
509         int ConfValueEnum(ConfigDataHash &target, const char* tag);
510         /** Returns the number of occurences of tag in the config file
511          */
512         int ConfValueEnum(ConfigDataHash &target, const std::string &tag);
513         
514         /** Returns the numbers of vars inside the index'th 'tag in the config file
515          */
516         int ConfVarEnum(ConfigDataHash &target, const char* tag, int index);
517         /** Returns the numbers of vars inside the index'th 'tag in the config file
518          */
519         int ConfVarEnum(ConfigDataHash &target, const std::string &tag, int index);
520         
521         Module* GetIOHook(int port);
522         bool AddIOHook(int port, Module* iomod);
523         bool DelIOHook(int port);
524
525         static std::string GetFullProgDir(char** argv, int argc);
526         static bool DirValid(const char* dirandfile);
527         static char* CleanFilename(char* name);
528         static bool FileExists(const char* file);
529         
530 };
531
532 bool InitializeDisabledCommands(const char* data, InspIRCd* ServerInstance);
533
534 bool InitTypes(ServerConfig* conf, const char* tag);
535 bool InitClasses(ServerConfig* conf, const char* tag);
536 bool DoType(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
537 bool DoClass(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
538 bool DoneClassesAndTypes(ServerConfig* conf, const char* tag);
539
540 #endif