]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/configreader.h
Rewrite ConfigReader again
[user/henk/code/inspircd.git] / include / configreader.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef INSPIRCD_CONFIGREADER
15 #define INSPIRCD_CONFIGREADER
16
17 #include <sstream>
18 #include <string>
19 #include <vector>
20 #include <map>
21 #include "inspircd.h"
22 #include "modules.h"
23 #include "socketengine.h"
24 #include "socket.h"
25
26 /* Required forward definitions */
27 class ServerConfig;
28 class ServerLimits;
29 class InspIRCd;
30 class BufferedSocket;
31
32 /** A cached text file stored with its contents as lines
33  */
34 typedef std::vector<std::string> file_cache;
35
36 /** A configuration key and value pair
37  */
38 typedef std::pair<std::string, std::string> KeyVal;
39
40 struct ConfigTag : public refcountbase
41 {
42         const std::string tag;
43         std::vector<KeyVal> items;
44
45         ConfigTag(const std::string& Tag) : tag(Tag) {}
46
47         std::string getString(const std::string& key, const std::string& def = "");
48         long getInt(const std::string& key, long def = 0);
49         double getFloat(const std::string& key, double def = 0);
50         bool getBool(const std::string& key, bool def = false);
51
52         bool readString(const std::string& key, std::string& value, bool allow_newline = false);
53 };
54
55 /** An entire config file, built up of KeyValLists
56  */
57 typedef std::multimap<std::string, reference<ConfigTag> > ConfigDataHash;
58
59 typedef std::map<std::string, reference<ConfigTag> > TagIndex;
60
61 /** Defines the server's length limits on various length-limited
62  * items such as topics, nicknames, channel names etc.
63  */
64 class ServerLimits
65 {
66  public:
67         /** Maximum nickname length */
68         size_t NickMax;
69         /** Maximum channel length */
70         size_t ChanMax;
71         /** Maximum number of modes per line */
72         size_t MaxModes;
73         /** Maximum length of ident, not including ~ etc */
74         size_t IdentMax;
75         /** Maximum length of a quit message */
76         size_t MaxQuit;
77         /** Maximum topic length */
78         size_t MaxTopic;
79         /** Maximum kick message length */
80         size_t MaxKick;
81         /** Maximum GECOS (real name) length */
82         size_t MaxGecos;
83         /** Maximum away message length */
84         size_t MaxAway;
85
86         /** Creating the class initialises it to the defaults
87          * as in 1.1's ./configure script. Reading other values
88          * from the config will change these values.
89          */
90         ServerLimits() : NickMax(31), ChanMax(64), MaxModes(20), IdentMax(12), MaxQuit(255), MaxTopic(307), MaxKick(255), MaxGecos(128), MaxAway(200)
91         {
92         }
93
94         /** Finalises the settings by adding one. This allows for them to be used as-is
95          * without a 'value+1' when using the std::string assignment methods etc.
96          */
97         void Finalise()
98         {
99                 NickMax++;
100                 ChanMax++;
101                 IdentMax++;
102                 MaxQuit++;
103                 MaxTopic++;
104                 MaxKick++;
105                 MaxGecos++;
106                 MaxAway++;
107         }
108 };
109
110 /** This class holds the bulk of the runtime configuration for the ircd.
111  * It allows for reading new config values, accessing configuration files,
112  * and storage of the configuration data needed to run the ircd, such as
113  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
114  */
115 class CoreExport ServerConfig : public classbase
116 {
117   private:
118         /** This variable holds the names of all
119          * files included from the main one. This
120          * is used to make sure that no files are
121          * recursively included.
122          */
123         std::vector<std::string> include_stack;
124
125         /** This private method processes one line of
126          * configutation, appending errors to errorstream
127          * and setting error if an error has occured.
128          */
129         bool ParseLine(const std::string &filename, std::string &line, long &linenumber, bool allowexeinc);
130
131         /** Check that there is only one of each configuration item
132          */
133         bool CheckOnce(const char* tag);
134
135         void CrossCheckOperClassType();
136         void CrossCheckConnectBlocks(ServerConfig* current);
137
138  public:
139         /** Process an include executable directive
140          */
141         bool DoPipe(const std::string &file);
142
143         /** Process an include file directive
144          */
145         bool DoInclude(const std::string &file, bool allowexeinc);
146
147         /** Error stream, contains error output from any failed configuration parsing.
148          */
149         std::stringstream errstr;
150
151         /** True if this configuration is valid enough to run with */
152         bool valid;
153
154         /** Set of included files. Do we use this any more?
155          */
156         std::map<std::string, std::istream*> IncludedFiles;
157
158         /** Used to indicate who we announce invites to on a channel */
159         enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
160
161         /** Not used any more as it is named, can probably be removed or renamed.
162          */
163         int DoDownloads();
164
165         /** This holds all the information in the config file,
166          * it's indexed by tag name to a vector of key/values.
167          */
168         ConfigDataHash config_data;
169
170         /** Length limits, see definition of ServerLimits class
171          */
172         ServerLimits Limits;
173
174         /** Clones CIDR range for ipv4 (0-32)
175          * Defaults to 32 (checks clones on all IPs seperately)
176          */
177         int c_ipv4_range;
178
179         /** Clones CIDR range for ipv6 (0-128)
180          * Defaults to 128 (checks on all IPs seperately)
181          */
182         int c_ipv6_range;
183
184         /** Max number of WhoWas entries per user.
185          */
186         int WhoWasGroupSize;
187
188         /** Max number of cumulative user-entries in WhoWas.
189          *  When max reached and added to, push out oldest entry FIFO style.
190          */
191         int WhoWasMaxGroups;
192
193         /** Max seconds a user is kept in WhoWas before being pruned.
194          */
195         int WhoWasMaxKeep;
196
197         /** Both for set(g|u)id.
198          */
199         std::string SetUser;
200         std::string SetGroup;
201
202         /** Holds the server name of the local server
203          * as defined by the administrator.
204          */
205         std::string ServerName;
206
207         /** Notice to give to users when they are Xlined
208          */
209         std::string MoronBanner;
210
211         /* Holds the network name the local server
212          * belongs to. This is an arbitary field defined
213          * by the administrator.
214          */
215         std::string Network;
216
217         /** Holds the description of the local server
218          * as defined by the administrator.
219          */
220         std::string ServerDesc;
221
222         /** Holds the admin's name, for output in
223          * the /ADMIN command.
224          */
225         std::string AdminName;
226
227         /** Holds the email address of the admin,
228          * for output in the /ADMIN command.
229          */
230         std::string AdminEmail;
231
232         /** Holds the admin's nickname, for output
233          * in the /ADMIN command
234          */
235         std::string AdminNick;
236
237         /** The admin-configured /DIE password
238          */
239         std::string diepass;
240
241         /** The admin-configured /RESTART password
242          */
243         std::string restartpass;
244
245         /** The hash method for *BOTH* the die and restart passwords.
246          */
247         std::string powerhash;
248
249         /** The pathname and filename of the message of the
250          * day file, as defined by the administrator.
251          */
252         std::string motd;
253
254         /** The pathname and filename of the rules file,
255          * as defined by the administrator.
256          */
257         std::string rules;
258
259         /** The quit prefix in use, or an empty string
260          */
261         std::string PrefixQuit;
262
263         /** The quit suffix in use, or an empty string
264          */
265         std::string SuffixQuit;
266
267         /** The fixed quit message in use, or an empty string
268          */
269         std::string FixedQuit;
270
271         /** The part prefix in use, or an empty string
272          */
273         std::string PrefixPart;
274
275         /** The part suffix in use, or an empty string
276          */
277         std::string SuffixPart;
278
279         /** The fixed part message in use, or an empty string
280          */
281         std::string FixedPart;
282
283         /** The last string found within a <die> tag, or
284          * an empty string.
285          */
286         std::string DieValue;
287
288         /** The DNS server to use for DNS queries
289          */
290         std::string DNSServer;
291
292         /** Pretend disabled commands don't exist.
293          */
294         bool DisabledDontExist;
295
296         /** This variable contains a space-seperated list
297          * of commands which are disabled by the
298          * administrator of the server for non-opers.
299          */
300         std::string DisabledCommands;
301
302         /** This variable identifies which usermodes have been diabled.
303          */
304
305         char DisabledUModes[64];
306
307         /** This variable identifies which chanmodes have been disabled.
308          */
309         char DisabledCModes[64];
310
311         /** The full path to the modules directory.
312          * This is either set at compile time, or
313          * overridden in the configuration file via
314          * the <options> tag.
315          */
316         std::string ModPath;
317
318         /** The file handle of the logfile. If this
319          * value is NULL, the log file is not open,
320          * probably due to a permissions error on
321          * startup (this should not happen in normal
322          * operation!).
323          */
324         FILE *log_file;
325
326         /** If this value is true, the owner of the
327          * server specified -nofork on the command
328          * line, causing the daemon to stay in the
329          * foreground.
330          */
331         bool nofork;
332
333         /** If this value if true then all log
334          * messages will be output, regardless of
335          * the level given in the config file.
336          * This is set with the -debug commandline
337          * option.
338          */
339         bool forcedebug;
340
341         /** If this is true then log output will be
342          * written to the logfile. This is the default.
343          * If you put -nolog on the commandline then
344          * the logfile will not be written.
345          * This is meant to be used in conjunction with
346          * -debug for debugging without filling up the
347          * hard disk.
348          */
349         bool writelog;
350
351         /** If set to true, then all opers on this server are
352          * shown with a generic 'is an IRC operator' line rather
353          * than the oper type. Oper types are still used internally.
354          */
355         bool GenericOper;
356
357         /** If this value is true, banned users (+b, not extbans) will not be able to change nick
358          * if banned on any channel, nor to message them.
359          */
360         bool RestrictBannedUsers;
361
362         /** If this value is true, halfops have been
363          * enabled in the configuration file.
364          */
365         bool AllowHalfop;
366
367         /** If this is set to true, then mode lists (e.g
368          * MODE #chan b) are hidden from unprivileged
369          * users.
370          */
371         bool HideModeLists[256];
372
373         /** The number of seconds the DNS subsystem
374          * will wait before timing out any request.
375          */
376         int dns_timeout;
377
378         /** The size of the read() buffer in the user
379          * handling code, used to read data into a user's
380          * recvQ.
381          */
382         int NetBufferSize;
383
384         /** The value to be used for listen() backlogs
385          * as default.
386          */
387         int MaxConn;
388
389         /** The soft limit value assigned to the irc server.
390          * The IRC server will not allow more than this
391          * number of local users.
392          */
393         unsigned int SoftLimit;
394
395         /** Maximum number of targets for a multi target command
396          * such as PRIVMSG or KICK
397          */
398         unsigned int MaxTargets;
399
400         /** The maximum number of /WHO results allowed
401          * in any single /WHO command.
402          */
403         int MaxWhoResults;
404
405         /** True if the DEBUG loglevel is selected.
406          */
407         int debugging;
408
409         /** How many seconds to wait before exiting
410          * the program when /DIE is correctly issued.
411          */
412         int DieDelay;
413
414         /** True if we're going to hide netsplits as *.net *.split for non-opers
415          */
416         bool HideSplits;
417
418         /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
419          * K-Lines, Z-Lines)
420          */
421         bool HideBans;
422
423         /** Announce invites to the channel with a server notice
424          */
425         InviteAnnounceState AnnounceInvites;
426
427         /** If this is enabled then operators will
428          * see invisible (+i) channels in /whois.
429          */
430         bool OperSpyWhois;
431
432         /** Set to a non-empty string to obfuscate the server name of users in WHOIS
433          */
434         std::string HideWhoisServer;
435
436         /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
437          */
438         std::string HideKillsServer;
439
440         /** The MOTD file, cached in a file_cache type.
441          */
442         file_cache MOTD;
443
444         /** The RULES file, cached in a file_cache type.
445          */
446         file_cache RULES;
447
448         /** The full pathname and filename of the PID
449          * file as defined in the configuration.
450          */
451         std::string PID;
452
453         /** The connect classes in use by the IRC server.
454          */
455         ClassVector Classes;
456
457         /** The 005 tokens of this server (ISUPPORT)
458          * populated/repopulated upon loading or unloading
459          * modules.
460          */
461         std::string data005;
462
463         /** isupport strings
464          */
465         std::vector<std::string> isupport;
466
467         /** STATS characters in this list are available
468          * only to operators.
469          */
470         std::string UserStats;
471
472         /** The path and filename of the ircd.log file
473          */
474         std::string logpath;
475
476         /** Default channel modes
477          */
478         std::string DefaultModes;
479
480         /** Custom version string, which if defined can replace the system info in VERSION.
481          */
482         std::string CustomVersion;
483
484         /** List of u-lined servers
485          */
486         std::map<irc::string, bool> ulines;
487
488         /** Max banlist sizes for channels (the std::string is a glob)
489          */
490         std::map<std::string, int> maxbans;
491
492         /** Directory where the inspircd binary resides
493          */
494         std::string MyDir;
495
496         /** If set to true, no user DNS lookups are to be performed
497          */
498         bool NoUserDns;
499
500         /** If set to true, provide syntax hints for unknown commands
501          */
502         bool SyntaxHints;
503
504         /** If set to true, users appear to quit then rejoin when their hosts change.
505          * This keeps clients synchronized properly.
506          */
507         bool CycleHosts;
508
509         /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
510          *  added to the outgoing text for undernet style msg prefixing.
511          */
512         bool UndernetMsgPrefix;
513
514         /** If set to true, the full nick!user@host will be shown in the TOPIC command
515          * for who set the topic last. If false, only the nick is shown.
516          */
517         bool FullHostInTopic;
518
519         /** All oper type definitions from the config file
520          */
521         TagIndex opertypes;
522
523         /** All oper class definitions from the config file
524          */
525         TagIndex operclass;
526
527         /** Saved argv from startup
528          */
529         char** argv;
530
531         /** Saved argc from startup
532          */
533         int argc;
534
535         /** Max channels per user
536          */
537         unsigned int MaxChans;
538
539         /** Oper max channels per user
540          */
541         unsigned int OperMaxChans;
542
543         /** TS6-like server ID.
544          * NOTE: 000...999 are usable for InspIRCd servers. This
545          * makes code simpler. 0AA, 1BB etc with letters are reserved
546          * for services use.
547          */
548         std::string sid;
549
550         /** True if we have been told to run the testsuite from the commandline,
551          * rather than entering the mainloop.
552          */
553         bool TestSuite;
554
555         /** Construct a new ServerConfig
556          */
557         ServerConfig();
558
559         /** Get server ID as string with required leading zeroes
560          */
561         std::string GetSID();
562
563         /** Update the 005 vector
564          */
565         void Update005();
566
567         /** Send the 005 numerics (ISUPPORT) to a user
568          */
569         void Send005(User* user);
570
571         /** Read the entire configuration into memory
572          * and initialize this class. All other methods
573          * should be used only by the core.
574          */
575         void Read();
576
577         /** Apply configuration changes from the old configuration.
578          */
579         void Apply(ServerConfig* old, const std::string &useruid);
580         void ApplyModules(User* user);
581
582         void Fill();
583
584         /** Read a file into a file_cache object
585          */
586         bool ReadFile(file_cache &F, const std::string& fname);
587
588         /* Returns true if the given string starts with a windows drive letter
589          */
590         bool StartsWithWindowsDriveLetter(const std::string &path);
591
592         /** Load 'filename' into 'target', with the new config parser everything is parsed into
593          * tag/key/value at load-time rather than at read-value time.
594          */
595         bool LoadConf(FILE* &conf, const char* filename, bool allowexeinc);
596
597         /** Load 'filename' into 'target', with the new config parser everything is parsed into
598          * tag/key/value at load-time rather than at read-value time.
599          */
600         bool LoadConf(FILE* &conf, const std::string &filename, bool allowexeinc);
601
602         ConfigTag* ConfValue(const std::string& tag, int offset = 0);
603
604         bool ApplyDisabledCommands(const std::string& data);
605
606         /** Clean a filename, stripping the directories (and drives) from string.
607          * @param name Directory to tidy
608          * @return The cleaned filename
609          */
610         static const char* CleanFilename(const char* name);
611
612         /** Check if a file exists.
613          * @param file The full path to a file
614          * @return True if the file exists and is readable.
615          */
616         static bool FileExists(const char* file);
617
618         /** If this value is true, invites will bypass more than just +i
619          */
620         bool InvBypassModes;
621
622 };
623 #endif