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