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