]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/configreader.h
Implement IRCv3 message tag support.
[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 #include "token_list.h"
35
36 /** Structure representing a single \<tag> in config */
37 class CoreExport ConfigTag : public refcountbase
38 {
39         ConfigItems items;
40  public:
41         const std::string tag;
42         const std::string src_name;
43         const int src_line;
44
45         /** Get the value of an option, using def if it does not exist */
46         std::string getString(const std::string& key, const std::string& def, const TR1NS::function<bool(const std::string&)>& validator);
47         /** Get the value of an option, using def if it does not exist */
48         std::string getString(const std::string& key, const std::string& def = "", size_t minlen = 0, size_t maxlen = UINT32_MAX);
49         /** Get the value of an option, using def if it does not exist */
50         long getInt(const std::string& key, long def, long min = LONG_MIN, long max = LONG_MAX);
51         /** Get the value of an option, using def if it does not exist */
52         unsigned long getUInt(const std::string& key, unsigned long def, unsigned long min = 0, unsigned long max = ULONG_MAX);
53         /** Get the value of an option, using def if it does not exist */
54         double getFloat(const std::string& key, double def, double min = DBL_MIN, double max = DBL_MAX);
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 in seconds of a duration that is in the user-friendly "1h2m3s" format,
59          * using a default value if it does not exist or is out of bounds.
60          * @param key The config key name
61          * @param def Default value (optional)
62          * @param min Minimum acceptable value (optional)
63          * @param max Maximum acceptable value (optional)
64          * @return The duration in seconds
65          */
66         unsigned long getDuration(const std::string& key, unsigned long def, unsigned long min = 0, unsigned long max = ULONG_MAX);
67
68         /** Get the value of an option
69          * @param key The option to get
70          * @param value The location to store the value (unmodified if does not exist)
71          * @param allow_newline Allow newlines in the option (normally replaced with spaces)
72          * @return true if the option exists
73          */
74         bool readString(const std::string& key, std::string& value, bool allow_newline = false);
75
76         std::string getTagLocation();
77
78         inline const ConfigItems& getItems() const { return items; }
79
80         /** Create a new ConfigTag, giving access to the private ConfigItems item list */
81         static ConfigTag* create(const std::string& Tag, const std::string& file, int line, ConfigItems*& Items);
82  private:
83         ConfigTag(const std::string& Tag, const std::string& file, int line);
84 };
85
86 /** Defines the server's length limits on various length-limited
87  * items such as topics, nicknames, channel names etc.
88  */
89 class ServerLimits
90 {
91  public:
92         /** Maximum nickname length */
93         size_t NickMax;
94         /** Maximum channel length */
95         size_t ChanMax;
96         /** Maximum number of modes per line */
97         size_t MaxModes;
98         /** Maximum length of ident, not including ~ etc */
99         size_t IdentMax;
100         /** Maximum length of a quit message */
101         size_t MaxQuit;
102         /** Maximum topic length */
103         size_t MaxTopic;
104         /** Maximum kick message length */
105         size_t MaxKick;
106         /** Maximum real name length */
107         size_t MaxReal;
108         /** Maximum away message length */
109         size_t MaxAway;
110         /** Maximum line length */
111         size_t MaxLine;
112         /** Maximum hostname length */
113         size_t MaxHost;
114
115         /** Read all limits from a config tag. Limits which aren't specified in the tag are set to a default value.
116          * @param tag Configuration tag to read the limits from
117          */
118         ServerLimits(ConfigTag* tag);
119
120         /** Maximum length of a n!u\@h mask */
121         size_t GetMaxMask() const { return NickMax + 1 + IdentMax + 1 + MaxHost; }
122 };
123
124 struct CommandLineConf
125 {
126         /** If this value is true, the owner of the
127          * server specified -nofork on the command
128          * line, causing the daemon to stay in the
129          * foreground.
130          */
131         bool nofork;
132
133         /** If this value if true then all log
134          * messages will be output, regardless of
135          * the level given in the config file.
136          * This is set with the -debug commandline
137          * option.
138          */
139         bool forcedebug;
140
141         /** If this is true then log output will be
142          * written to the logfile. This is the default.
143          * If you put -nolog on the commandline then
144          * the logfile will not be written.
145          * This is meant to be used in conjunction with
146          * -debug for debugging without filling up the
147          * hard disk.
148          */
149         bool writelog;
150
151         /** If this is true, a PID file will be written
152          * to the file given in the "file" variable of
153          * the <pid> tag in the config file. This is
154          * the default.
155          * Passing --nopid as a command line argument
156          * sets this to false; in this case, a PID file
157          * will not be written, even the default PID
158          * file that is usually written when the <pid>
159          * tag is not defined in the config file.
160          */
161         bool writepid;
162
163         /** Saved argc from startup
164          */
165         int argc;
166
167         /** Saved argv from startup
168          */
169         char** argv;
170 };
171
172 class CoreExport OperInfo : public refcountbase
173 {
174  public:
175         TokenList AllowedOperCommands;
176         TokenList AllowedPrivs;
177
178         /** Allowed user modes from oper classes. */
179         std::bitset<64> AllowedUserModes;
180
181         /** Allowed channel modes from oper classes. */
182         std::bitset<64> AllowedChanModes;
183
184         /** \<oper> block used for this oper-up. May be NULL. */
185         reference<ConfigTag> oper_block;
186         /** \<type> block used for this oper-up. Valid for local users, may be NULL on remote */
187         reference<ConfigTag> type_block;
188         /** \<class> blocks referenced from the \<type> block. These define individual permissions */
189         std::vector<reference<ConfigTag> > class_blocks;
190         /** Name of the oper type; i.e. the one shown in WHOIS */
191         std::string name;
192
193         /** Creates a new OperInfo with the specified oper type name.
194          * @param Name The name of the oper type.
195          */
196         OperInfo(const std::string& Name);
197
198         /** Get a configuration item, searching in the oper, type, and class blocks (in that order) */
199         std::string getConfig(const std::string& key);
200         void init();
201 };
202
203 /** This class holds the bulk of the runtime configuration for the ircd.
204  * It allows for reading new config values, accessing configuration files,
205  * and storage of the configuration data needed to run the ircd, such as
206  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
207  */
208 class CoreExport ServerConfig
209 {
210   private:
211         void CrossCheckOperClassType();
212         void CrossCheckConnectBlocks(ServerConfig* current);
213
214  public:
215         /** How to treat a user in a channel who is banned. */
216         enum BannedUserTreatment
217         {
218                 /** Don't treat a banned user any different to normal. */
219                 BUT_NORMAL,
220
221                 /** Restrict the actions of a banned user. */
222                 BUT_RESTRICT_SILENT,
223
224                 /** Restrict the actions of a banned user and notify them of their treatment. */
225                 BUT_RESTRICT_NOTIFY
226         };
227
228         class ServerPaths
229         {
230          public:
231                 /** Config path */
232                 std::string Config;
233
234                 /** Data path */
235                 std::string Data;
236
237                 /** Log path */
238                 std::string Log;
239
240                 /** Module path */
241                 std::string Module;
242
243                 ServerPaths(ConfigTag* tag);
244
245                 std::string PrependConfig(const std::string& fn) const { return FileSystem::ExpandPath(Config, fn); }
246                 std::string PrependData(const std::string& fn) const { return FileSystem::ExpandPath(Data, fn); }
247                 std::string PrependLog(const std::string& fn) const { return FileSystem::ExpandPath(Log, fn); }
248                 std::string PrependModule(const std::string& fn) const { return FileSystem::ExpandPath(Module, fn); }
249         };
250
251         /** Holds a complete list of all connect blocks
252          */
253         typedef std::vector<reference<ConnectClass> > ClassVector;
254
255         /** Index of valid oper blocks and types
256          */
257         typedef insp::flat_map<std::string, reference<OperInfo> > OperIndex;
258
259         /** Get a configuration tag
260          * @param tag The name of the tag to get
261          */
262         ConfigTag* ConfValue(const std::string& tag);
263
264         ConfigTagList ConfTags(const std::string& tag);
265
266         /** An empty configuration tag. */
267         ConfigTag* EmptyTag;
268
269         /** Error stream, contains error output from any failed configuration parsing.
270          */
271         std::stringstream errstr;
272
273         /** True if this configuration is valid enough to run with */
274         bool valid;
275
276         /** Bind to IPv6 by default */
277         bool WildcardIPv6;
278
279         /** This holds all the information in the config file,
280          * it's indexed by tag name to a vector of key/values.
281          */
282         ConfigDataHash config_data;
283
284         /** This holds all extra files that have been read in the configuration
285          * (for example, MOTD and RULES files are stored here)
286          */
287         ConfigFileCache Files;
288
289         /** Length limits, see definition of ServerLimits class
290          */
291         ServerLimits Limits;
292
293         /** Locations of various types of file (config, module, etc). */
294         ServerPaths Paths;
295
296         /** Configuration parsed from the command line.
297          */
298         CommandLineConf cmdline;
299
300         /** Clones CIDR range for ipv4 (0-32)
301          * Defaults to 32 (checks clones on all IPs seperately)
302          */
303         unsigned char c_ipv4_range;
304
305         /** Clones CIDR range for ipv6 (0-128)
306          * Defaults to 128 (checks on all IPs seperately)
307          */
308         unsigned char c_ipv6_range;
309
310         /** Holds the server name of the local server
311          * as defined by the administrator.
312          */
313         std::string ServerName;
314
315         /** Notice to give to users when they are banned by an XLine
316          */
317         std::string XLineMessage;
318
319         /* Holds the network name the local server
320          * belongs to. This is an arbitary field defined
321          * by the administrator.
322          */
323         std::string Network;
324
325         /** Holds the description of the local server
326          * as defined by the administrator.
327          */
328         std::string ServerDesc;
329
330         /** Pretend disabled commands don't exist.
331          */
332         bool DisabledDontExist;
333
334         /** This variable identifies which usermodes have been diabled.
335          */
336         std::bitset<64> DisabledUModes;
337
338         /** This variable identifies which chanmodes have been disabled.
339          */
340         std::bitset<64> DisabledCModes;
341
342         /** If set to true, then all opers on this server are
343          * shown with a generic 'is an IRC operator' line rather
344          * than the oper type. Oper types are still used internally.
345          */
346         bool GenericOper;
347
348         /** How to treat a user in a channel who is banned. */
349         BannedUserTreatment RestrictBannedUsers;
350
351         /** The size of the read() buffer in the user
352          * handling code, used to read data into a user's
353          * recvQ.
354          */
355         int NetBufferSize;
356
357         /** The value to be used for listen() backlogs
358          * as default.
359          */
360         int MaxConn;
361
362         /** If we should check for clones during CheckClass() in AddUser()
363          * Setting this to false allows to not trigger on maxclones for users
364          * that may belong to another class after DNS-lookup is complete.
365          * It does, however, make the server spend more time on users we may potentially not want.
366          */
367         bool CCOnConnect;
368
369         /** The soft limit value assigned to the irc server.
370          * The IRC server will not allow more than this
371          * number of local users.
372          */
373         unsigned int SoftLimit;
374
375         /** Maximum number of targets for a multi target command
376          * such as PRIVMSG or KICK
377          */
378         unsigned int MaxTargets;
379
380         /** True if we're going to hide netsplits as *.net *.split for non-opers
381          */
382         bool HideSplits;
383
384         /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
385          * K-Lines, Z-Lines)
386          */
387         bool HideBans;
388
389         /** True if raw I/O is being logged */
390         bool RawLog;
391
392         /** Set to a non-empty string to obfuscate server names. */
393         std::string HideServer;
394
395         /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
396          */
397         std::string HideKillsServer;
398
399         /** Set to hide kills from clients of ulined servers in snotices.
400          */
401         bool HideULineKills;
402
403         /** The full pathname and filename of the PID
404          * file as defined in the configuration.
405          */
406         std::string PID;
407
408         /** The connect classes in use by the IRC server.
409          */
410         ClassVector Classes;
411
412         /** STATS characters in this list are available
413          * only to operators.
414          */
415         std::string UserStats;
416
417         /** Default channel modes
418          */
419         std::string DefaultModes;
420
421         /** Custom version string, which if defined can replace the system info in VERSION.
422          */
423         std::string CustomVersion;
424
425         /** If set to true, provide syntax hints for unknown commands
426          */
427         bool SyntaxHints;
428
429         /** The name of the casemapping method used by this server.
430          */
431         std::string CaseMapping;
432
433         /** If set to true, the full nick!user\@host will be shown in the TOPIC command
434          * for who set the topic last. If false, only the nick is shown.
435          */
436         bool FullHostInTopic;
437
438         /** Oper blocks keyed by their name
439          */
440         OperIndex oper_blocks;
441
442         /** Oper types keyed by their name
443          */
444         OperIndex OperTypes;
445
446         /** Default value for <connect:maxchans>, deprecated in 3.0
447          */
448         unsigned int MaxChans;
449
450         /** Default value for <oper:maxchans>, deprecated in 3.0
451          */
452         unsigned int OperMaxChans;
453
454         /** TS6-like server ID.
455          * NOTE: 000...999 are usable for InspIRCd servers. This
456          * makes code simpler. 0AA, 1BB etc with letters are reserved
457          * for services use.
458          */
459         std::string sid;
460
461         /** Construct a new ServerConfig
462          */
463         ServerConfig();
464
465         ~ServerConfig();
466
467         /** Get server ID as string with required leading zeroes
468          */
469         const std::string& GetSID() const { return sid; }
470
471         /** Read the entire configuration into memory
472          * and initialize this class. All other methods
473          * should be used only by the core.
474          */
475         void Read();
476
477         /** Apply configuration changes from the old configuration.
478          */
479         void Apply(ServerConfig* old, const std::string &useruid);
480         void ApplyModules(User* user);
481
482         void Fill();
483
484         /** Disables the commands specified in <disabled:commands>. */
485         bool ApplyDisabledCommands();
486
487         /** Escapes a value for storage in a configuration key.
488          * @param str The string to escape.
489          * @param xml Are we using the XML config format?
490          */
491         static std::string Escape(const std::string& str, bool xml = true);
492
493         /** If this value is true, snotices will not stack when repeats are sent
494          */
495         bool NoSnoticeStack;
496 };
497
498 /** The background thread for config reading, so that reading from executable includes
499  * does not block.
500  */
501 class CoreExport ConfigReaderThread : public Thread
502 {
503         ServerConfig* Config;
504         volatile bool done;
505  public:
506         const std::string TheUserUID;
507         ConfigReaderThread(const std::string &useruid)
508                 : Config(new ServerConfig), done(false), TheUserUID(useruid)
509         {
510         }
511
512         virtual ~ConfigReaderThread()
513         {
514                 delete Config;
515         }
516
517         void Run() CXX11_OVERRIDE;
518         /** Run in the main thread to apply the configuration */
519         void Finish();
520         bool IsDone() { return done; }
521 };
522
523 class CoreExport ConfigStatus
524 {
525  public:
526         User* const srcuser;
527
528         ConfigStatus(User* user = NULL)
529                 : srcuser(user)
530         {
531         }
532 };