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