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