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