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