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