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