]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/configreader.h
Merge pull request #545 from SaberUK/master+logging-cleanup
[user/henk/code/inspircd.git] / include / configreader.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
8  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #pragma once
25
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 #include <map>
30 #include "inspircd.h"
31 #include "modules.h"
32 #include "socketengine.h"
33 #include "socket.h"
34
35 /** Structure representing a single \<tag> in config */
36 class CoreExport ConfigTag : public refcountbase
37 {
38         std::vector<KeyVal> items;
39  public:
40         const std::string tag;
41         const std::string src_name;
42         const int src_line;
43
44         /** Get the value of an option, using def if it does not exist */
45         std::string getString(const std::string& key, const std::string& def = "");
46         /** Get the value of an option, using def if it does not exist */
47         long getInt(const std::string& key, long def = 0);
48         /** Get the value of an option, using def if it does not exist */
49         double getFloat(const std::string& key, double def = 0);
50         /** Get the value of an option, using def if it does not exist */
51         bool getBool(const std::string& key, bool def = false);
52
53         /** Get the value of an option
54          * @param key The option to get
55          * @param value The location to store the value (unmodified if does not exist)
56          * @param allow_newline Allow newlines in the option (normally replaced with spaces)
57          * @return true if the option exists
58          */
59         bool readString(const std::string& key, std::string& value, bool allow_newline = false);
60
61         std::string getTagLocation();
62
63         inline const std::vector<KeyVal>& getItems() const { return items; }
64
65         /** Create a new ConfigTag, giving access to the private KeyVal item list */
66         static ConfigTag* create(const std::string& Tag, const std::string& file, int line,
67                 std::vector<KeyVal>*&items);
68  private:
69         ConfigTag(const std::string& Tag, const std::string& file, int line);
70 };
71
72 /** Defines the server's length limits on various length-limited
73  * items such as topics, nicknames, channel names etc.
74  */
75 class ServerLimits
76 {
77  public:
78         /** Maximum nickname length */
79         size_t NickMax;
80         /** Maximum channel length */
81         size_t ChanMax;
82         /** Maximum number of modes per line */
83         size_t MaxModes;
84         /** Maximum length of ident, not including ~ etc */
85         size_t IdentMax;
86         /** Maximum length of a quit message */
87         size_t MaxQuit;
88         /** Maximum topic length */
89         size_t MaxTopic;
90         /** Maximum kick message length */
91         size_t MaxKick;
92         /** Maximum GECOS (real name) length */
93         size_t MaxGecos;
94         /** Maximum away message length */
95         size_t MaxAway;
96
97         /** Creating the class initialises it to the defaults
98          * as in 1.1's ./configure script. Reading other values
99          * from the config will change these values.
100          */
101         ServerLimits() : NickMax(31), ChanMax(64), MaxModes(20), IdentMax(12), MaxQuit(255), MaxTopic(307), MaxKick(255), MaxGecos(128), MaxAway(200)
102         {
103         }
104 };
105
106 struct CommandLineConf
107 {
108         /** If this value is true, the owner of the
109          * server specified -nofork on the command
110          * line, causing the daemon to stay in the
111          * foreground.
112          */
113         bool nofork;
114
115         /** If this value if true then all log
116          * messages will be output, regardless of
117          * the level given in the config file.
118          * This is set with the -debug commandline
119          * option.
120          */
121         bool forcedebug;
122
123         /** If this is true then log output will be
124          * written to the logfile. This is the default.
125          * If you put -nolog on the commandline then
126          * the logfile will not be written.
127          * This is meant to be used in conjunction with
128          * -debug for debugging without filling up the
129          * hard disk.
130          */
131         bool writelog;
132
133         /** True if we have been told to run the testsuite from the commandline,
134          * rather than entering the mainloop.
135          */
136         bool TestSuite;
137
138         /** Saved argc from startup
139          */
140         int argc;
141
142         /** Saved argv from startup
143          */
144         char** argv;
145 };
146
147 class CoreExport OperInfo : public refcountbase
148 {
149  public:
150         std::set<std::string> AllowedOperCommands;
151         std::set<std::string> AllowedPrivs;
152
153         /** Allowed user modes from oper classes. */
154         std::bitset<64> AllowedUserModes;
155
156         /** Allowed channel modes from oper classes. */
157         std::bitset<64> AllowedChanModes;
158
159         /** \<oper> block used for this oper-up. May be NULL. */
160         reference<ConfigTag> oper_block;
161         /** \<type> block used for this oper-up. Valid for local users, may be NULL on remote */
162         reference<ConfigTag> type_block;
163         /** \<class> blocks referenced from the \<type> block. These define individual permissions */
164         std::vector<reference<ConfigTag> > class_blocks;
165         /** Name of the oper type; i.e. the one shown in WHOIS */
166         std::string name;
167
168         /** Get a configuration item, searching in the oper, type, and class blocks (in that order) */
169         std::string getConfig(const std::string& key);
170         void init();
171 };
172
173 /** This class holds the bulk of the runtime configuration for the ircd.
174  * It allows for reading new config values, accessing configuration files,
175  * and storage of the configuration data needed to run the ircd, such as
176  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
177  */
178 class CoreExport ServerConfig
179 {
180   private:
181         void CrossCheckOperClassType();
182         void CrossCheckConnectBlocks(ServerConfig* current);
183
184  public:
185
186         /** Get a configuration tag
187          * @param tag The name of the tag to get
188          */
189         ConfigTag* ConfValue(const std::string& tag);
190
191         ConfigTagList ConfTags(const std::string& tag);
192
193         /** Error stream, contains error output from any failed configuration parsing.
194          */
195         std::stringstream errstr;
196
197         /** True if this configuration is valid enough to run with */
198         bool valid;
199
200         /** Bind to IPv6 by default */
201         bool WildcardIPv6;
202
203         /** Used to indicate who we announce invites to on a channel */
204         enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
205         enum OperSpyWhoisState { SPYWHOIS_NONE, SPYWHOIS_SINGLEMSG, SPYWHOIS_SPLITMSG };
206
207         /** This holds all the information in the config file,
208          * it's indexed by tag name to a vector of key/values.
209          */
210         ConfigDataHash config_data;
211
212         /** This holds all extra files that have been read in the configuration
213          * (for example, MOTD and RULES files are stored here)
214          */
215         ConfigFileCache Files;
216
217         /** Length limits, see definition of ServerLimits class
218          */
219         ServerLimits Limits;
220
221         /** Configuration parsed from the command line.
222          */
223         CommandLineConf cmdline;
224
225         /** Clones CIDR range for ipv4 (0-32)
226          * Defaults to 32 (checks clones on all IPs seperately)
227          */
228         int c_ipv4_range;
229
230         /** Clones CIDR range for ipv6 (0-128)
231          * Defaults to 128 (checks on all IPs seperately)
232          */
233         int c_ipv6_range;
234
235         /** Holds the server name of the local server
236          * as defined by the administrator.
237          */
238         std::string ServerName;
239
240         /** Notice to give to users when they are Xlined
241          */
242         std::string MoronBanner;
243
244         /* Holds the network name the local server
245          * belongs to. This is an arbitary field defined
246          * by the administrator.
247          */
248         std::string Network;
249
250         /** Holds the description of the local server
251          * as defined by the administrator.
252          */
253         std::string ServerDesc;
254
255         /** Holds the admin's name, for output in
256          * the /ADMIN command.
257          */
258         std::string AdminName;
259
260         /** Holds the email address of the admin,
261          * for output in the /ADMIN command.
262          */
263         std::string AdminEmail;
264
265         /** Holds the admin's nickname, for output
266          * in the /ADMIN command
267          */
268         std::string AdminNick;
269
270         /** The admin-configured /DIE password
271          */
272         std::string diepass;
273
274         /** The admin-configured /RESTART password
275          */
276         std::string restartpass;
277
278         /** The hash method for *BOTH* the die and restart passwords.
279          */
280         std::string powerhash;
281
282         /** The pathname and filename of the message of the
283          * day file, as defined by the administrator.
284          */
285         std::string motd;
286
287         /** The pathname and filename of the rules file,
288          * as defined by the administrator.
289          */
290         std::string rules;
291
292         /** The quit prefix in use, or an empty string
293          */
294         std::string PrefixQuit;
295
296         /** The quit suffix in use, or an empty string
297          */
298         std::string SuffixQuit;
299
300         /** The fixed quit message in use, or an empty string
301          */
302         std::string FixedQuit;
303
304         /** The part prefix in use, or an empty string
305          */
306         std::string PrefixPart;
307
308         /** The part suffix in use, or an empty string
309          */
310         std::string SuffixPart;
311
312         /** The fixed part message in use, or an empty string
313          */
314         std::string FixedPart;
315
316         /** Pretend disabled commands don't exist.
317          */
318         bool DisabledDontExist;
319
320         /** This variable contains a space-seperated list
321          * of commands which are disabled by the
322          * administrator of the server for non-opers.
323          */
324         std::string DisabledCommands;
325
326         /** This variable identifies which usermodes have been diabled.
327          */
328         char DisabledUModes[64];
329
330         /** This variable identifies which chanmodes have been disabled.
331          */
332         char DisabledCModes[64];
333
334         /** The full path to the modules directory.
335          * This is either set at compile time, or
336          * overridden in the configuration file via
337          * the \<path> tag.
338          */
339         std::string ModPath;
340
341         /** If set to true, then all opers on this server are
342          * shown with a generic 'is an IRC operator' line rather
343          * than the oper type. Oper types are still used internally.
344          */
345         bool GenericOper;
346
347         /** If this value is true, banned users (+b, not extbans) will not be able to change nick
348          * if banned on any channel, nor to message them.
349          */
350         bool RestrictBannedUsers;
351
352         /** If this is set to true, then mode lists (e.g
353          * MODE \#chan b) are hidden from unprivileged
354          * users.
355          */
356         bool HideModeLists[256];
357
358         /** The number of seconds the DNS subsystem
359          * will wait before timing out any request.
360          */
361         int dns_timeout;
362
363         /** The size of the read() buffer in the user
364          * handling code, used to read data into a user's
365          * recvQ.
366          */
367         int NetBufferSize;
368
369         /** The value to be used for listen() backlogs
370          * as default.
371          */
372         int MaxConn;
373
374         /** The soft limit value assigned to the irc server.
375          * The IRC server will not allow more than this
376          * number of local users.
377          */
378         unsigned int SoftLimit;
379
380         /** Maximum number of targets for a multi target command
381          * such as PRIVMSG or KICK
382          */
383         unsigned int MaxTargets;
384
385         /** True if we're going to hide netsplits as *.net *.split for non-opers
386          */
387         bool HideSplits;
388
389         /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
390          * K-Lines, Z-Lines)
391          */
392         bool HideBans;
393
394         /** Announce invites to the channel with a server notice
395          */
396         InviteAnnounceState AnnounceInvites;
397
398         /** If this is enabled then operators will
399          * see invisible (+i) channels in /whois.
400          */
401         OperSpyWhoisState OperSpyWhois;
402
403         /** True if raw I/O is being logged */
404         bool RawLog;
405
406         /** Set to a non-empty string to obfuscate the server name of users in WHOIS
407          */
408         std::string HideWhoisServer;
409
410         /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
411          */
412         std::string HideKillsServer;
413
414         /** The full pathname and filename of the PID
415          * file as defined in the configuration.
416          */
417         std::string PID;
418
419         /** The connect classes in use by the IRC server.
420          */
421         ClassVector Classes;
422
423         /** STATS characters in this list are available
424          * only to operators.
425          */
426         std::string UserStats;
427
428         /** Default channel modes
429          */
430         std::string DefaultModes;
431
432         /** Custom version string, which if defined can replace the system info in VERSION.
433          */
434         std::string CustomVersion;
435
436         /** List of u-lined servers
437          */
438         std::map<irc::string, bool> ulines;
439
440         /** If set to true, provide syntax hints for unknown commands
441          */
442         bool SyntaxHints;
443
444         /** If set to true, users appear to quit then rejoin when their hosts change.
445          * This keeps clients synchronized properly.
446          */
447         bool CycleHosts;
448
449         /** If set to true, the CycleHosts mode change will be sourced from the user,
450          * rather than the server
451          */
452         bool CycleHostsFromUser;
453
454         /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
455          *  added to the outgoing text for undernet style msg prefixing.
456          */
457         bool UndernetMsgPrefix;
458
459         /** If set to true, the full nick!user\@host will be shown in the TOPIC command
460          * for who set the topic last. If false, only the nick is shown.
461          */
462         bool FullHostInTopic;
463
464         /** Oper block and type index.
465          * For anonymous oper blocks (type only), prefix with a space.
466          */
467         OperIndex oper_blocks;
468
469         /** Max channels per user
470          */
471         unsigned int MaxChans;
472
473         /** Oper max channels per user
474          */
475         unsigned int OperMaxChans;
476
477         /** TS6-like server ID.
478          * NOTE: 000...999 are usable for InspIRCd servers. This
479          * makes code simpler. 0AA, 1BB etc with letters are reserved
480          * for services use.
481          */
482         std::string sid;
483
484         /** Construct a new ServerConfig
485          */
486         ServerConfig();
487
488         /** Get server ID as string with required leading zeroes
489          */
490         const std::string& GetSID();
491
492         /** Read the entire configuration into memory
493          * and initialize this class. All other methods
494          * should be used only by the core.
495          */
496         void Read();
497
498         /** Apply configuration changes from the old configuration.
499          */
500         void Apply(ServerConfig* old, const std::string &useruid);
501         void ApplyModules(User* user);
502
503         void Fill();
504
505         /** Returns true if the given string starts with a windows drive letter
506          */
507         bool StartsWithWindowsDriveLetter(const std::string &path);
508
509         bool ApplyDisabledCommands(const std::string& data);
510
511         /** Clean a filename, stripping the directories (and drives) from string.
512          * @param name Directory to tidy
513          * @return The cleaned filename
514          */
515         static const char* CleanFilename(const char* name);
516
517         /** Check if a file exists.
518          * @param file The full path to a file
519          * @return True if the file exists and is readable.
520          */
521         static bool FileExists(const char* file);
522
523         /** If this value is true, invites will bypass more than just +i
524          */
525         bool InvBypassModes;
526
527         /** If this value is true, snotices will not stack when repeats are sent
528          */
529         bool NoSnoticeStack;
530 };
531
532 /** The background thread for config reading, so that reading from executable includes
533  * does not block.
534  */
535 class CoreExport ConfigReaderThread : public Thread
536 {
537         ServerConfig* Config;
538         volatile bool done;
539  public:
540         const std::string TheUserUID;
541         ConfigReaderThread(const std::string &useruid)
542                 : Config(new ServerConfig), done(false), TheUserUID(useruid)
543         {
544         }
545
546         virtual ~ConfigReaderThread()
547         {
548                 delete Config;
549         }
550
551         void Run();
552         /** Run in the main thread to apply the configuration */
553         void Finish();
554         bool IsDone() { return done; }
555 };