1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef INSPIRCD_CONFIGREADER
15 #define INSPIRCD_CONFIGREADER
24 #include "socketengine.h"
31 /** Types of data in the core config
39 DT_ALLOW_NEWLINE = 128
42 /** Holds a config value, either string, integer or boolean.
43 * Callback functions receive one or more of these, either on
44 * their own as a reference, or in a reference to a deque of them.
45 * The callback function can then alter the values of the ValueItem
46 * classes to validate the settings.
53 ValueItem(bool value);
54 ValueItem(char* value);
55 void Set(char* value);
56 void Set(const char* val);
63 /** The base class of the container 'ValueContainer'
64 * used internally by the core to hold core values.
66 class ValueContainerBase
69 ValueContainerBase() { }
70 virtual ~ValueContainerBase() { }
73 /** ValueContainer is used to contain pointers to different
74 * core values such as the server name, maximum number of
76 * It is specialized to hold a data type, then pointed at
77 * a value in the ServerConfig class. When the value has been
78 * read and validated, the Set method is called to write the
79 * value safely in a type-safe manner.
81 template<typename T> class ValueContainer : public ValueContainerBase
97 void Set(T newval, size_t s)
99 memcpy(val, newval, s);
103 /** A specialization of ValueContainer to hold a pointer to a bool
105 typedef ValueContainer<bool*> ValueContainerBool;
107 /** A specialization of ValueContainer to hold a pointer to
110 typedef ValueContainer<unsigned int*> ValueContainerUInt;
112 /** A specialization of ValueContainer to hold a pointer to
115 typedef ValueContainer<char*> ValueContainerChar;
117 /** A specialization of ValueContainer to hold a pointer to
120 typedef ValueContainer<int*> ValueContainerInt;
122 /** A set of ValueItems used by multi-value validator functions
124 typedef std::deque<ValueItem> ValueList;
126 /** A callback for validating a single value
128 typedef bool (*Validator)(ServerConfig* conf, const char*, const char*, ValueItem&);
129 /** A callback for validating multiple value entries
131 typedef bool (*MultiValidator)(ServerConfig* conf, const char*, char**, ValueList&, int*);
132 /** A callback indicating the end of a group of entries
134 typedef bool (*MultiNotify)(ServerConfig* conf, const char*);
136 /** Holds a core configuration item and its callbacks
143 ValueContainerBase* val;
144 ConfigDataType datatype;
145 Validator validation_function;
148 /** Holds a core configuration item and its callbacks
149 * where there may be more than one item
155 char* items_default[13];
157 MultiNotify init_function;
158 MultiValidator validation_function;
159 MultiNotify finish_function;
162 /** A set of oper types
164 typedef std::map<irc::string,char*> opertype_t;
166 /** A Set of oper classes
168 typedef std::map<irc::string,char*> operclass_t;
171 /** This class holds the bulk of the runtime configuration for the ircd.
172 * It allows for reading new config values, accessing configuration files,
173 * and storage of the configuration data needed to run the ircd, such as
174 * the servername, connect classes, /ADMIN data, MOTDs and filenames etc.
176 class ServerConfig : public Extensible
181 InspIRCd* ServerInstance;
183 /** This variable holds the names of all
184 * files included from the main one. This
185 * is used to make sure that no files are
186 * recursively included.
188 std::vector<std::string> include_stack;
190 /** This private method processes one line of
191 * configutation, appending errors to errorstream
192 * and setting error if an error has occured.
194 bool ParseLine(ConfigDataHash &target, std::string &line, long linenumber, std::ostringstream &errorstream);
196 /** Process an include directive
198 bool DoInclude(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream);
200 /** Check that there is only one of each configuration item
202 bool CheckOnce(char* tag, bool bail, userrec* user);
206 InspIRCd* GetInstance();
208 /** This holds all the information in the config file,
209 * it's indexed by tag name to a vector of key/values.
211 ConfigDataHash config_data;
213 /** Max number of WhoWas entries per user.
217 /** Max number of cumulative user-entries in WhoWas.
218 * When max reached and added to, push out oldest entry FIFO style.
222 /** Max seconds a user is kept in WhoWas before being pruned.
226 /** Holds the server name of the local server
227 * as defined by the administrator.
229 char ServerName[MAXBUF];
231 /* Holds the network name the local server
232 * belongs to. This is an arbitary field defined
233 * by the administrator.
235 char Network[MAXBUF];
237 /** Holds the description of the local server
238 * as defined by the administrator.
240 char ServerDesc[MAXBUF];
242 /** Holds the admin's name, for output in
243 * the /ADMIN command.
245 char AdminName[MAXBUF];
247 /** Holds the email address of the admin,
248 * for output in the /ADMIN command.
250 char AdminEmail[MAXBUF];
252 /** Holds the admin's nickname, for output
253 * in the /ADMIN command
255 char AdminNick[MAXBUF];
257 /** The admin-configured /DIE password
259 char diepass[MAXBUF];
261 /** The admin-configured /RESTART password
263 char restartpass[MAXBUF];
265 /** The pathname and filename of the message of the
266 * day file, as defined by the administrator.
270 /** The pathname and filename of the rules file,
271 * as defined by the administrator.
275 /** The quit prefix in use, or an empty string
277 char PrefixQuit[MAXBUF];
279 /** The quit suffix in use, or an empty string
281 char SuffixQuit[MAXBUF];
283 /** The fixed quit message in use, or an empty string
285 char FixedQuit[MAXBUF];
287 /** The last string found within a <die> tag, or
290 char DieValue[MAXBUF];
292 /** The DNS server to use for DNS queries
294 char DNSServer[MAXBUF];
296 /** This variable contains a space-seperated list
297 * of commands which are disabled by the
298 * administrator of the server for non-opers.
300 char DisabledCommands[MAXBUF];
302 /** The full path to the modules directory.
303 * This is either set at compile time, or
304 * overridden in the configuration file via
309 /** The full pathname to the executable, as
310 * given in argv[0] when the program starts.
312 char MyExecutable[1024];
314 /** The file handle of the logfile. If this
315 * value is NULL, the log file is not open,
316 * probably due to a permissions error on
317 * startup (this should not happen in normal
322 /** If this value is true, the owner of the
323 * server specified -nofork on the command
324 * line, causing the daemon to stay in the
329 /** If this value if true then all log
330 * messages will be output, regardless of
331 * the level given in the config file.
332 * This is set with the -debug commandline
337 /** If this is true then log output will be
338 * written to the logfile. This is the default.
339 * If you put -nolog on the commandline then
340 * the logfile will not be written.
341 * This is meant to be used in conjunction with
342 * -debug for debugging without filling up the
347 /** If this value is true, halfops have been
348 * enabled in the configuration file.
352 /** If this is set to true, then mode lists (e.g
353 * MODE #chan b) are hidden from unprivileged
356 bool HideModeLists[256];
358 /** The number of seconds the DNS subsystem
359 * will wait before timing out any request.
363 /** The size of the read() buffer in the user
364 * handling code, used to read data into a user's
369 /** The value to be used for listen() backlogs
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.
378 unsigned int SoftLimit;
380 /** Maximum number of targets for a multi target command
381 * such as PRIVMSG or KICK
383 unsigned int MaxTargets;
385 /** The maximum number of /WHO results allowed
386 * in any single /WHO command.
390 /** True if the DEBUG loglevel is selected.
394 /** The loglevel in use by the IRC server
398 /** How many seconds to wait before exiting
399 * the program when /DIE is correctly issued.
403 /** True if we're going to hide netsplits as *.net *.split for non-opers
407 /** True if we're going to hide ban reasons for non-opers (e.g. G-Lines,
412 /** Announce invites to the channel with a server notice
414 bool AnnounceInvites;
416 /** If this is enabled then operators will
417 * see invisible (+i) channels in /whois.
421 /** Set to a non-empty string to obfuscate the server name of users in WHOIS
423 char HideWhoisServer[MAXBUF];
425 /** A list of IP addresses the server is listening
428 char addrs[MAXBUF][255];
430 /** The MOTD file, cached in a file_cache type.
434 /** The RULES file, cached in a file_cache type.
438 /** The full pathname and filename of the PID
439 * file as defined in the configuration.
443 /** The connect classes in use by the IRC server.
447 /** A list of module names (names only, no paths)
448 * which are currently loaded by the server.
450 std::vector<std::string> module_names;
452 /** A list of ports which the server is listening on
456 /** A list of the file descriptors for the listening client ports
458 ListenSocket* openSockfd[255];
460 /** Boolean sets of which modules implement which functions
462 char implement_lists[255][255];
464 /** Global implementation list
466 char global_implementation[255];
468 /** A list of ports claimed by IO Modules
470 std::map<int,Module*> IOHookModule;
472 std::map<InspSocket*, Module*> SocketIOHookModule;
474 /** The 005 tokens of this server (ISUPPORT)
475 * populated/repopulated upon loading or unloading
479 std::vector<std::string> isupport;
481 /** STATS characters in this list are available
484 char UserStats[MAXBUF];
486 /** The path and filename of the ircd.log file
490 /** Custom version string, which if defined can replace the system info in VERSION.
492 char CustomVersion[MAXBUF];
494 /** List of u-lined servers
496 std::map<irc::string, bool> ulines;
498 /** Max banlist sizes for channels (the std::string is a glob)
500 std::map<std::string,int> maxbans;
502 /** Directory where the inspircd binary resides
506 /** If set to true, no user DNS lookups are to be performed
510 /** If set to true, provide syntax hints for unknown commands
514 /** If set to true, users appear to quit then rejoin when their hosts change.
515 * This keeps clients synchronized properly.
519 /** If set to true, prefixed channel NOTICEs and PRIVMSGs will have the prefix
520 * added to the outgoing text for undernet style msg prefixing.
522 bool UndernetMsgPrefix;
524 /** If set to true, the full nick!user@host will be shown in the TOPIC command
525 * for who set the topic last. If false, only the nick is shown.
527 bool FullHostInTopic;
529 /** All oper type definitions from the config file
531 opertype_t opertypes;
533 /** All oper class definitions from the config file
535 operclass_t operclass;
537 /** Saved argv from startup
541 /** Saved argc from startup
545 /** Max channels per user
547 unsigned int MaxChans;
549 /** Oper max channels per user
551 unsigned int OperMaxChans;
553 /** Construct a new ServerConfig
555 ServerConfig(InspIRCd* Instance);
557 /** Clears the include stack in preperation for a Read() call.
561 /** Update the 005 vector
565 /** Send the 005 numerics (ISUPPORT) to a user
567 void Send005(userrec* user);
569 /** Read the entire configuration into memory
570 * and initialize this class. All other methods
571 * should be used only by the core.
573 void Read(bool bail, userrec* user);
575 /** Read a file into a file_cache object
577 bool ReadFile(file_cache &F, const char* fname);
579 /** Load 'filename' into 'target', with the new config parser everything is parsed into
580 * tag/key/value at load-time rather than at read-value time.
583 /** Report a configuration error given in errormessage.
584 * @param bail If this is set to true, the error is sent to the console, and the program exits
585 * @param user If this is set to a non-null value, and bail is false, the errors are spooled to
586 * this user as SNOTICEs.
587 * If the parameter is NULL, the messages are spooled to all users via WriteOpers as SNOTICEs.
589 void ReportConfigError(const std::string &errormessage, bool bail, userrec* user);
591 /** Load 'filename' into 'target', with the new config parser everything is parsed into
592 * tag/key/value at load-time rather than at read-value time.
594 bool LoadConf(ConfigDataHash &target, const char* filename, std::ostringstream &errorstream);
596 /** Load 'filename' into 'target', with the new config parser everything is parsed into
597 * tag/key/value at load-time rather than at read-value time.
599 bool LoadConf(ConfigDataHash &target, const std::string &filename, std::ostringstream &errorstream);
601 /* Both these return true if the value existed or false otherwise */
603 /** Writes 'length' chars into 'result' as a string
605 bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length, bool allow_linefeeds = false);
606 bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, char* result, int length, bool allow_linefeeds = false);
608 /** Writes 'length' chars into 'result' as a string
610 bool ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result, bool allow_linefeeds = false);
611 bool ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index, std::string &result, bool allow_linefeeds = false);
613 /** Tries to convert the value to an integer and write it to 'result'
615 bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, int index, int &result);
616 bool ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, int &result);
617 /** Tries to convert the value to an integer and write it to 'result'
619 bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, int &result);
620 bool ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index, int &result);
622 /** Returns true if the value exists and has a true value, false otherwise
624 bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, int index);
625 bool ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index);
626 /** Returns true if the value exists and has a true value, false otherwise
628 bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, int index);
629 bool ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index);
631 /** Returns the number of occurences of tag in the config file
633 int ConfValueEnum(ConfigDataHash &target, const char* tag);
634 /** Returns the number of occurences of tag in the config file
636 int ConfValueEnum(ConfigDataHash &target, const std::string &tag);
638 /** Returns the numbers of vars inside the index'th 'tag in the config file
640 int ConfVarEnum(ConfigDataHash &target, const char* tag, int index);
641 /** Returns the numbers of vars inside the index'th 'tag in the config file
643 int ConfVarEnum(ConfigDataHash &target, const std::string &tag, int index);
645 Module* GetIOHook(int port);
646 bool AddIOHook(int port, Module* iomod);
647 bool DelIOHook(int port);
648 Module* GetIOHook(InspSocket* is);
649 bool AddIOHook(Module* iomod, InspSocket* is);
650 bool DelIOHook(InspSocket* is);
652 static std::string GetFullProgDir(char** argv, int argc);
653 static bool DirValid(const char* dirandfile);
654 static char* CleanFilename(char* name);
655 static bool FileExists(const char* file);
659 bool InitializeDisabledCommands(const char* data, InspIRCd* ServerInstance);
661 bool InitTypes(ServerConfig* conf, const char* tag);
662 bool InitClasses(ServerConfig* conf, const char* tag);
663 bool DoType(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
664 bool DoClass(ServerConfig* conf, const char* tag, char** entries, ValueList &values, int* types);
665 bool DoneClassesAndTypes(ServerConfig* conf, const char* tag);