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