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