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