]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/configreader.h
Fix various issues with the nationalchars module.
[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 #ifndef INSPIRCD_CONFIGREADER
25 #define INSPIRCD_CONFIGREADER
26
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 #include <map>
31 #include "inspircd.h"
32 #include "modules.h"
33 #include "socketengine.h"
34 #include "socket.h"
35
36 /** Structure representing a single \<tag> in config */
37 class CoreExport ConfigTag : public refcountbase
38 {
39         std::vector<KeyVal> 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 = "");
47         /** Get the value of an option, using def if it does not exist */
48         long getInt(const std::string& key, long def = 0);
49         /** Get the value of an option, using def if it does not exist */
50         double getFloat(const std::string& key, double def = 0);
51         /** Get the value of an option, using def if it does not exist */
52         bool getBool(const std::string& key, bool def = false);
53
54         /** Get the value of an option
55          * @param key The option to get
56          * @param value The location to store the value (unmodified if does not exist)
57          * @param allow_newline Allow newlines in the option (normally replaced with spaces)
58          * @return true if the option exists
59          */
60         bool readString(const std::string& key, std::string& value, bool allow_newline = false);
61
62         std::string getTagLocation();
63
64         inline const std::vector<KeyVal>& getItems() const { return items; }
65
66         /** Create a new ConfigTag, giving access to the private KeyVal item list */
67         static ConfigTag* create(const std::string& Tag, const std::string& file, int line, 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         std::string startup_log;
147 };
148
149 class CoreExport OperInfo : public refcountbase
150 {
151  public:
152         std::set<std::string> AllowedOperCommands;
153         std::set<std::string> AllowedPrivs;
154
155         /** Allowed user modes from oper classes. */
156         std::bitset<64> AllowedUserModes;
157
158         /** Allowed channel modes from oper classes. */
159         std::bitset<64> AllowedChanModes;
160
161         /** \<oper> block used for this oper-up. May be NULL. */
162         reference<ConfigTag> oper_block;
163         /** \<type> block used for this oper-up. Valid for local users, may be NULL on remote */
164         reference<ConfigTag> type_block;
165         /** \<class> blocks referenced from the \<type> block. These define individual permissions */
166         std::vector<reference<ConfigTag> > class_blocks;
167         /** Name of the oper type; i.e. the one shown in WHOIS */
168         std::string name;
169
170         /** Get a configuration item, searching in the oper, type, and class blocks (in that order) */
171         std::string getConfig(const std::string& key);
172         void init();
173
174         inline const char* NameStr()
175         {
176                 return irc::Spacify(name.c_str());
177         }
178 };
179
180 /** This class holds the bulk of the runtime configuration for the ircd.
181  * It allows for reading new config values, accessing configuration files,
182  * and storage of the configuration data needed to run the ircd, such as
183  * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
184  */
185 class CoreExport ServerConfig
186 {
187   private:
188         void CrossCheckOperClassType();
189         void CrossCheckConnectBlocks(ServerConfig* current);
190
191  public:
192
193         /** Get a configuration tag
194          * @param tag The name of the tag to get
195          */
196         ConfigTag* ConfValue(const std::string& tag);
197
198         ConfigTagList ConfTags(const std::string& tag);
199
200         /** An empty configuration tag. */
201         ConfigTag* EmptyTag;
202
203         /** Error stream, contains error output from any failed configuration parsing.
204          */
205         std::stringstream errstr;
206
207         /** True if this configuration is valid enough to run with */
208         bool valid;
209
210         /** Bind to IPv6 by default */
211         bool WildcardIPv6;
212
213         /** Used to indicate who we announce invites to on a channel */
214         enum InviteAnnounceState { INVITE_ANNOUNCE_NONE, INVITE_ANNOUNCE_ALL, INVITE_ANNOUNCE_OPS, INVITE_ANNOUNCE_DYNAMIC };
215         enum OperSpyWhoisState { SPYWHOIS_NONE, SPYWHOIS_SINGLEMSG, SPYWHOIS_SPLITMSG };
216
217         /** This holds all the information in the config file,
218          * it's indexed by tag name to a vector of key/values.
219          */
220         ConfigDataHash config_data;
221
222         /** This holds all extra files that have been read in the configuration
223          * (for example, MOTD and RULES files are stored here)
224          */
225         ConfigFileCache Files;
226
227         /** Length limits, see definition of ServerLimits class
228          */
229         ServerLimits Limits;
230
231         /** Configuration parsed from the command line.
232          */
233         CommandLineConf cmdline;
234
235         /** Clones CIDR range for ipv4 (0-32)
236          * Defaults to 32 (checks clones on all IPs seperately)
237          */
238         int c_ipv4_range;
239
240         /** Clones CIDR range for ipv6 (0-128)
241          * Defaults to 128 (checks on all IPs seperately)
242          */
243         int c_ipv6_range;
244
245         /** Max number of WhoWas entries per user.
246          */
247         int WhoWasGroupSize;
248
249         /** Max number of cumulative user-entries in WhoWas.
250          *  When max reached and added to, push out oldest entry FIFO style.
251          */
252         int WhoWasMaxGroups;
253
254         /** Max seconds a user is kept in WhoWas before being pruned.
255          */
256         int WhoWasMaxKeep;
257
258         /** Holds the server name of the local server
259          * as defined by the administrator.
260          */
261         std::string ServerName;
262
263         /** Notice to give to users when they are Xlined
264          */
265         std::string MoronBanner;
266
267         /* Holds the network name the local server
268          * belongs to. This is an arbitary field defined
269          * by the administrator.
270          */
271         std::string Network;
272
273         /** Holds the description of the local server
274          * as defined by the administrator.
275          */
276         std::string ServerDesc;
277
278         /** Holds the admin's name, for output in
279          * the /ADMIN command.
280          */
281         std::string AdminName;
282
283         /** Holds the email address of the admin,
284          * for output in the /ADMIN command.
285          */
286         std::string AdminEmail;
287
288         /** Holds the admin's nickname, for output
289          * in the /ADMIN command
290          */
291         std::string AdminNick;
292
293         /** The admin-configured /DIE password
294          */
295         std::string diepass;
296
297         /** The admin-configured /RESTART password
298          */
299         std::string restartpass;
300
301         /** The hash method for *BOTH* the die and restart passwords.
302          */
303         std::string powerhash;
304
305         /** The pathname and filename of the message of the
306          * day file, as defined by the administrator.
307          */
308         std::string motd;
309
310         /** The pathname and filename of the rules file,
311          * as defined by the administrator.
312          */
313         std::string rules;
314
315         /** The quit prefix in use, or an empty string
316          */
317         std::string PrefixQuit;
318
319         /** The quit suffix in use, or an empty string
320          */
321         std::string SuffixQuit;
322
323         /** The fixed quit message in use, or an empty string
324          */
325         std::string FixedQuit;
326
327         /** The part prefix in use, or an empty string
328          */
329         std::string PrefixPart;
330
331         /** The part suffix in use, or an empty string
332          */
333         std::string SuffixPart;
334
335         /** The fixed part message in use, or an empty string
336          */
337         std::string FixedPart;
338
339         /** The DNS server to use for DNS queries
340          */
341         std::string DNSServer;
342
343         /** Pretend disabled commands don't exist.
344          */
345         bool DisabledDontExist;
346
347         /** This variable contains a space-seperated list
348          * of commands which are disabled by the
349          * administrator of the server for non-opers.
350          */
351         std::string DisabledCommands;
352
353         /** This variable identifies which usermodes have been diabled.
354          */
355         char DisabledUModes[64];
356
357         /** This variable identifies which chanmodes have been disabled.
358          */
359         char DisabledCModes[64];
360
361         /** The full path to the modules directory.
362          * This is either set at compile time, or
363          * overridden in the configuration file via
364          * the \<path> tag.
365          */
366         std::string ModPath;
367
368         /** If set to true, then all opers on this server are
369          * shown with a generic 'is an IRC operator' line rather
370          * than the oper type. Oper types are still used internally.
371          */
372         bool GenericOper;
373
374         /** If this value is true, banned users (+b, not extbans) will not be able to change nick
375          * if banned on any channel, nor to message them.
376          */
377         bool RestrictBannedUsers;
378
379         /** If this is set to true, then mode lists (e.g
380          * MODE \#chan b) are hidden from unprivileged
381          * users.
382          */
383         bool HideModeLists[256];
384
385         /** The number of seconds the DNS subsystem
386          * will wait before timing out any request.
387          */
388         int dns_timeout;
389
390         /** The size of the read() buffer in the user
391          * handling code, used to read data into a user's
392          * recvQ.
393          */
394         int NetBufferSize;
395
396         /** The value to be used for listen() backlogs
397          * as default.
398          */
399         int MaxConn;
400
401         /** The soft limit value assigned to the irc server.
402          * The IRC server will not allow more than this
403          * number of local users.
404          */
405         unsigned int SoftLimit;
406
407         /** Maximum number of targets for a multi target command
408          * such as PRIVMSG or KICK
409          */
410         unsigned int MaxTargets;
411
412         /** True if we're going to hide netsplits as *.net *.split for non-opers
413          */
414         bool HideSplits;
415
416         /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
417          * K-Lines, Z-Lines)
418          */
419         bool HideBans;
420
421         /** Announce invites to the channel with a server notice
422          */
423         InviteAnnounceState AnnounceInvites;
424
425         /** If this is enabled then operators will
426          * see invisible (+i) channels in /whois.
427          */
428         OperSpyWhoisState OperSpyWhois;
429
430         /** True if raw I/O is being logged */
431         bool RawLog;
432
433         /** Set to a non-empty string to obfuscate the server name of users in WHOIS
434          */
435         std::string HideWhoisServer;
436
437         /** Set to a non empty string to obfuscate nicknames prepended to a KILL.
438          */
439         std::string HideKillsServer;
440
441         /** The full pathname and filename of the PID
442          * file as defined in the configuration.
443          */
444         std::string PID;
445
446         /** The connect classes in use by the IRC server.
447          */
448         ClassVector Classes;
449
450         /** The 005 tokens of this server (ISUPPORT)
451          * populated/repopulated upon loading or unloading
452          * modules.
453          */
454         std::string data005;
455
456         /** isupport strings
457          */
458         std::vector<std::string> isupport;
459
460         /** STATS characters in this list are available
461          * only to operators.
462          */
463         std::string UserStats;
464
465         /** Default channel modes
466          */
467         std::string DefaultModes;
468
469         /** Custom version string, which if defined can replace the system info in VERSION.
470          */
471         std::string CustomVersion;
472
473         /** List of u-lined servers
474          */
475         std::map<irc::string, bool> ulines;
476
477         /** Max banlist sizes for channels (the std::string is a glob)
478          */
479         std::map<std::string, int> maxbans;
480
481         /** If set to true, no user DNS lookups are to be performed
482          */
483         bool NoUserDns;
484
485         /** If set to true, provide syntax hints for unknown commands
486          */
487         bool SyntaxHints;
488
489         /** If set to true, users appear to quit then rejoin when their hosts change.
490          * This keeps clients synchronized properly.
491          */
492         bool CycleHosts;
493
494         /** If set to true, the CycleHosts mode change will be sourced from the user,
495          * rather than the server
496          */
497         bool CycleHostsFromUser;
498
499         /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
500          *  added to the outgoing text for undernet style msg prefixing.
501          */
502         bool UndernetMsgPrefix;
503
504         /** If set to true, the full nick!user\@host will be shown in the TOPIC command
505          * for who set the topic last. If false, only the nick is shown.
506          */
507         bool FullHostInTopic;
508
509         /** Oper block and type index.
510          * For anonymous oper blocks (type only), prefix with a space.
511          */
512         OperIndex oper_blocks;
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         /** Construct a new ServerConfig
530          */
531         ServerConfig();
532
533         ~ServerConfig();
534
535         /** Get server ID as string with required leading zeroes
536          */
537         const std::string& GetSID();
538
539         /** Update the 005 vector
540          */
541         void Update005();
542
543         /** Send the 005 numerics (ISUPPORT) to a user
544          */
545         void Send005(User* user);
546
547         /** Read the entire configuration into memory
548          * and initialize this class. All other methods
549          * should be used only by the core.
550          */
551         void Read();
552
553         /** Apply configuration changes from the old configuration.
554          */
555         void Apply(ServerConfig* old, const std::string &useruid);
556         void ApplyModules(User* user);
557
558         void Fill();
559
560         /** Returns true if the given string starts with a windows drive letter
561          */
562         bool StartsWithWindowsDriveLetter(const std::string &path);
563
564         bool ApplyDisabledCommands(const std::string& data);
565
566         /** Clean a filename, stripping the directories (and drives) from string.
567          * @param name Directory to tidy
568          * @return The cleaned filename
569          */
570         static const char* CleanFilename(const char* name);
571
572         /** Check if a file exists.
573          * @param file The full path to a file
574          * @return True if the file exists and is readable.
575          */
576         static bool FileExists(const char* file);
577
578         /** If this value is true, invites will bypass more than just +i
579          */
580         bool InvBypassModes;
581
582         /** If this value is true, snotices will not stack when repeats are sent
583          */
584         bool NoSnoticeStack;
585
586         /** If true, a "Welcome to <networkname>!" NOTICE will be sent to
587          * connecting users
588          */
589         bool WelcomeNotice;
590 };
591
592 /** The background thread for config reading, so that reading from executable includes
593  * does not block.
594  */
595 class CoreExport ConfigReaderThread : public Thread
596 {
597         ServerConfig* Config;
598         volatile bool done;
599  public:
600         const std::string TheUserUID;
601         ConfigReaderThread(const std::string &useruid)
602                 : Config(new ServerConfig), done(false), TheUserUID(useruid)
603         {
604         }
605
606         virtual ~ConfigReaderThread()
607         {
608                 delete Config;
609         }
610
611         void Run();
612         /** Run in the main thread to apply the configuration */
613         void Finish();
614         bool IsDone() { return done; }
615 };
616
617 #endif