]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/configreader.h
024e28909fcaf0dd365691fc5377224ffc0536ac
[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 class CoreExport OperInfo : public refcountbase
114 {
115  public:
116         std::set<std::string> AllowedOperCommands;
117         std::set<std::string> AllowedPrivs;
118
119         /** Allowed user modes from oper classes. */
120         std::bitset<64> AllowedUserModes;
121
122         /** Allowed channel modes from oper classes. */
123         std::bitset<64> AllowedChanModes;
124
125         /** <oper> block used for this oper-up. May be NULL. */
126         reference<ConfigTag> oper_block;
127         /** <type> block used for this oper-up. Valid for local users, may be NULL on remote */
128         reference<ConfigTag> type_block;
129         /** <class> blocks referenced from the <type> block. These define individual permissions */
130         std::vector<reference<ConfigTag> > class_blocks;
131         /** Name of the oper type; i.e. the one shown in WHOIS */
132         std::string name;
133
134         /** Get a configuration item, searching in the oper, type, and class blocks (in that order) */
135         std::string getConfig(const std::string& key);
136         void init();
137
138         inline const char* NameStr()
139         {
140                 return irc::Spacify(name.c_str());
141         }
142 };
143
144 typedef std::map<std::string, reference<ConfigTag> > TagIndex;
145 typedef std::map<std::string, reference<OperInfo> > OperIndex;
146 typedef ConfigDataHash::iterator ConfigIter;
147 typedef std::pair<ConfigDataHash::iterator, ConfigDataHash::iterator> ConfigTagList;
148
149 /** This class holds the bulk of the runtime configuration for the ircd.
150  * It allows for reading new config values, accessing configuration files,
151  * and storage of the configuration data needed to run the ircd, such as
152  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
153  */
154 class CoreExport ServerConfig
155 {
156   private:
157         void CrossCheckOperClassType();
158         void CrossCheckConnectBlocks(ServerConfig* current);
159
160  public:
161
162         /** Get a configuration tag
163          * @param tag The name of the tag to get
164          * @param offset get the Nth occurance of the tag
165          */
166         ConfigTag* ConfValue(const std::string& tag);
167
168         ConfigTagList ConfTags(const std::string& tag);
169
170         /** Error stream, contains error output from any failed configuration parsing.
171          */
172         std::stringstream errstr;
173
174         /** True if this configuration is valid enough to run with */
175         bool valid;
176
177         /** Used to indicate who we announce invites to on a channel */
178         enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
179
180         /** This holds all the information in the config file,
181          * it's indexed by tag name to a vector of key/values.
182          */
183         ConfigDataHash config_data;
184
185         /** Length limits, see definition of ServerLimits class
186          */
187         ServerLimits Limits;
188
189         /** Clones CIDR range for ipv4 (0-32)
190          * Defaults to 32 (checks clones on all IPs seperately)
191          */
192         int c_ipv4_range;
193
194         /** Clones CIDR range for ipv6 (0-128)
195          * Defaults to 128 (checks on all IPs seperately)
196          */
197         int c_ipv6_range;
198
199         /** Max number of WhoWas entries per user.
200          */
201         int WhoWasGroupSize;
202
203         /** Max number of cumulative user-entries in WhoWas.
204          *  When max reached and added to, push out oldest entry FIFO style.
205          */
206         int WhoWasMaxGroups;
207
208         /** Max seconds a user is kept in WhoWas before being pruned.
209          */
210         int WhoWasMaxKeep;
211
212         /** Both for set(g|u)id.
213          */
214         std::string SetUser;
215         std::string SetGroup;
216
217         /** Holds the server name of the local server
218          * as defined by the administrator.
219          */
220         std::string ServerName;
221
222         /** Notice to give to users when they are Xlined
223          */
224         std::string MoronBanner;
225
226         /* Holds the network name the local server
227          * belongs to. This is an arbitary field defined
228          * by the administrator.
229          */
230         std::string Network;
231
232         /** Holds the description of the local server
233          * as defined by the administrator.
234          */
235         std::string ServerDesc;
236
237         /** Holds the admin's name, for output in
238          * the /ADMIN command.
239          */
240         std::string AdminName;
241
242         /** Holds the email address of the admin,
243          * for output in the /ADMIN command.
244          */
245         std::string AdminEmail;
246
247         /** Holds the admin's nickname, for output
248          * in the /ADMIN command
249          */
250         std::string AdminNick;
251
252         /** The admin-configured /DIE password
253          */
254         std::string diepass;
255
256         /** The admin-configured /RESTART password
257          */
258         std::string restartpass;
259
260         /** The hash method for *BOTH* the die and restart passwords.
261          */
262         std::string powerhash;
263
264         /** The pathname and filename of the message of the
265          * day file, as defined by the administrator.
266          */
267         std::string motd;
268
269         /** The pathname and filename of the rules file,
270          * as defined by the administrator.
271          */
272         std::string rules;
273
274         /** The quit prefix in use, or an empty string
275          */
276         std::string PrefixQuit;
277
278         /** The quit suffix in use, or an empty string
279          */
280         std::string SuffixQuit;
281
282         /** The fixed quit message in use, or an empty string
283          */
284         std::string FixedQuit;
285
286         /** The part prefix in use, or an empty string
287          */
288         std::string PrefixPart;
289
290         /** The part suffix in use, or an empty string
291          */
292         std::string SuffixPart;
293
294         /** The fixed part message in use, or an empty string
295          */
296         std::string FixedPart;
297
298         /** The last string found within a <die> tag, or
299          * an empty string.
300          */
301         std::string DieValue;
302
303         /** The DNS server to use for DNS queries
304          */
305         std::string DNSServer;
306
307         /** Pretend disabled commands don't exist.
308          */
309         bool DisabledDontExist;
310
311         /** This variable contains a space-seperated list
312          * of commands which are disabled by the
313          * administrator of the server for non-opers.
314          */
315         std::string DisabledCommands;
316
317         /** This variable identifies which usermodes have been diabled.
318          */
319
320         char DisabledUModes[64];
321
322         /** This variable identifies which chanmodes have been disabled.
323          */
324         char DisabledCModes[64];
325
326         /** The full path to the modules directory.
327          * This is either set at compile time, or
328          * overridden in the configuration file via
329          * the <options> tag.
330          */
331         std::string ModPath;
332
333         /** The file handle of the logfile. If this
334          * value is NULL, the log file is not open,
335          * probably due to a permissions error on
336          * startup (this should not happen in normal
337          * operation!).
338          */
339         FILE *log_file;
340
341         /** If this value is true, the owner of the
342          * server specified -nofork on the command
343          * line, causing the daemon to stay in the
344          * foreground.
345          */
346         bool nofork;
347
348         /** If this value if true then all log
349          * messages will be output, regardless of
350          * the level given in the config file.
351          * This is set with the -debug commandline
352          * option.
353          */
354         bool forcedebug;
355
356         /** If this is true then log output will be
357          * written to the logfile. This is the default.
358          * If you put -nolog on the commandline then
359          * the logfile will not be written.
360          * This is meant to be used in conjunction with
361          * -debug for debugging without filling up the
362          * hard disk.
363          */
364         bool writelog;
365
366         /** If set to true, then all opers on this server are
367          * shown with a generic 'is an IRC operator' line rather
368          * than the oper type. Oper types are still used internally.
369          */
370         bool GenericOper;
371
372         /** If this value is true, banned users (+b, not extbans) will not be able to change nick
373          * if banned on any channel, nor to message them.
374          */
375         bool RestrictBannedUsers;
376
377         /** If this value is true, halfops have been
378          * enabled in the configuration file.
379          */
380         bool AllowHalfop;
381
382         /** If this is set to true, then mode lists (e.g
383          * MODE #chan b) are hidden from unprivileged
384          * users.
385          */
386         bool HideModeLists[256];
387
388         /** The number of seconds the DNS subsystem
389          * will wait before timing out any request.
390          */
391         int dns_timeout;
392
393         /** The size of the read() buffer in the user
394          * handling code, used to read data into a user's
395          * recvQ.
396          */
397         int NetBufferSize;
398
399         /** The value to be used for listen() backlogs
400          * as default.
401          */
402         int MaxConn;
403
404         /** The soft limit value assigned to the irc server.
405          * The IRC server will not allow more than this
406          * number of local users.
407          */
408         unsigned int SoftLimit;
409
410         /** Maximum number of targets for a multi target command
411          * such as PRIVMSG or KICK
412          */
413         unsigned int MaxTargets;
414
415         /** The maximum number of /WHO results allowed
416          * in any single /WHO command.
417          */
418         int MaxWhoResults;
419
420         /** True if the DEBUG loglevel is selected.
421          */
422         int debugging;
423
424         /** How many seconds to wait before exiting
425          * the program when /DIE is correctly issued.
426          */
427         int DieDelay;
428
429         /** True if we're going to hide netsplits as *.net *.split for non-opers
430          */
431         bool HideSplits;
432
433         /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
434          * K-Lines, Z-Lines)
435          */
436         bool HideBans;
437
438         /** Announce invites to the channel with a server notice
439          */
440         InviteAnnounceState AnnounceInvites;
441
442         /** If this is enabled then operators will
443          * see invisible (+i) channels in /whois.
444          */
445         bool OperSpyWhois;
446
447         /** Set to a non-empty string to obfuscate the server name of users in WHOIS
448          */
449         std::string HideWhoisServer;
450
451         /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
452          */
453         std::string HideKillsServer;
454
455         /** The MOTD file, cached in a file_cache type.
456          */
457         file_cache MOTD;
458
459         /** The RULES file, cached in a file_cache type.
460          */
461         file_cache RULES;
462
463         /** The full pathname and filename of the PID
464          * file as defined in the configuration.
465          */
466         std::string PID;
467
468         /** The connect classes in use by the IRC server.
469          */
470         ClassVector Classes;
471
472         /** The 005 tokens of this server (ISUPPORT)
473          * populated/repopulated upon loading or unloading
474          * modules.
475          */
476         std::string data005;
477
478         /** isupport strings
479          */
480         std::vector<std::string> isupport;
481
482         /** STATS characters in this list are available
483          * only to operators.
484          */
485         std::string UserStats;
486
487         /** The path and filename of the ircd.log file
488          */
489         std::string logpath;
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         /** Directory where the inspircd binary resides
508          */
509         std::string MyDir;
510
511         /** If set to true, no user DNS lookups are to be performed
512          */
513         bool NoUserDns;
514
515         /** If set to true, provide syntax hints for unknown commands
516          */
517         bool SyntaxHints;
518
519         /** If set to true, users appear to quit then rejoin when their hosts change.
520          * This keeps clients synchronized properly.
521          */
522         bool CycleHosts;
523
524         /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
525          *  added to the outgoing text for undernet style msg prefixing.
526          */
527         bool UndernetMsgPrefix;
528
529         /** If set to true, the full nick!user@host will be shown in the TOPIC command
530          * for who set the topic last. If false, only the nick is shown.
531          */
532         bool FullHostInTopic;
533
534         /** Oper block and type index.
535          * For anonymous oper blocks (type only), prefix with a space.
536          */
537         OperIndex oper_blocks;
538
539         /** Saved argv from startup
540          */
541         char** argv;
542
543         /** Saved argc from startup
544          */
545         int argc;
546
547         /** Max channels per user
548          */
549         unsigned int MaxChans;
550
551         /** Oper max channels per user
552          */
553         unsigned int OperMaxChans;
554
555         /** TS6-like server ID.
556          * NOTE: 000...999 are usable for InspIRCd servers. This
557          * makes code simpler. 0AA, 1BB etc with letters are reserved
558          * for services use.
559          */
560         std::string sid;
561
562         /** True if we have been told to run the testsuite from the commandline,
563          * rather than entering the mainloop.
564          */
565         bool TestSuite;
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