summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcyberbotx <cyberbotx@5417fbe8-f217-4b02-8779-1006273d7864>2008-11-18 23:58:33 +0000
committercyberbotx <cyberbotx@5417fbe8-f217-4b02-8779-1006273d7864>2008-11-18 23:58:33 +0000
commit1a2fdff0c653562ebde967850875e5445bc0c5ae (patch)
treec4bd693f9e154b067cfd5e52705899cce1f3101b
parent4cd33b4290c0a4c5012877e874a39ebbc68fd7f8 (diff)
Added ConfigReader class from InspIRCd to be used in modules to read config files using the new config parser.
git-svn-id: http://anope.svn.sourceforge.net/svnroot/anope/trunk@1755 5417fbe8-f217-4b02-8779-1006273d7864
-rw-r--r--include/configreader.h125
-rw-r--r--include/extern.h2
-rw-r--r--include/services.h1
-rw-r--r--src/Makefile4
-rw-r--r--src/configreader.cpp100
5 files changed, 226 insertions, 6 deletions
diff --git a/include/configreader.h b/include/configreader.h
index 87bc4e34d..89b28e672 100644
--- a/include/configreader.h
+++ b/include/configreader.h
@@ -375,10 +375,6 @@ class ServerConfig
void ValidateNoSpaces(const char *, const std::string &, const std::string &);
};
-/** Initialize the disabled commands list
- */
-E bool InitializeDisabledCommands(const char *);
-
/** This class can be used on its own to represent an exception, or derived to represent a module-specific exception.
* When a module whishes to abort, e.g. within a constructor, it should throw an exception using ModuleException or
* a class derived from ModuleException. If a module throws an exception during its constructor, the module will not
@@ -412,4 +408,125 @@ class ConfigException : public std::exception
}
};
+#define CONF_NO_ERROR 0x000000
+#define CONF_NOT_A_NUMBER 0x000010
+#define CONF_INT_NEGATIVE 0x000080
+#define CONF_VALUE_NOT_FOUND 0x000100
+#define CONF_FILE_NOT_FOUND 0x000200
+
+/** Allows reading of values from configuration files
+ * This class allows a module to read from either the main configuration file (inspircd.conf) or from
+ * a module-specified configuration file. It may either be instantiated with one parameter or none.
+ * Constructing the class using one parameter allows you to specify a path to your own configuration
+ * file, otherwise, inspircd.conf is read.
+ */
+class ConfigReader
+{
+ protected:
+ /** The contents of the configuration file
+ * This protected member should never be accessed by a module (and cannot be accessed unless the
+ * core is changed). It will contain a pointer to the configuration file data with unneeded data
+ * (such as comments) stripped from it.
+ */
+ ConfigDataHash *data;
+ /** Used to store errors
+ */
+ std::ostringstream *errorlog;
+ /** If we're using our own config data hash or not
+ */
+ bool privatehash;
+ /** True if an error occured reading the config file
+ */
+ bool readerror;
+ /** Error code
+ */
+ long error;
+ public:
+ /** Default constructor.
+ * This constructor initialises the ConfigReader class to read services.conf.
+ */
+ ConfigReader();
+ /** Overloaded constructor.
+ * This constructor initialises the ConfigReader class to read a user-specified config file
+ */
+ ConfigReader(const std::string &);
+ /** Default destructor.
+ * This method destroys the ConfigReader class.
+ */
+ ~ConfigReader();
+ /** Retrieves a value from the config file.
+ * This method retrieves a value from the config file. Where multiple copies of the tag
+ * exist in the config file, index indicates which of the values to retrieve.
+ */
+ std::string ReadValue(const std::string &, const std::string &, int, bool = false);
+ /** Retrieves a value from the config file.
+ * This method retrieves a value from the config file. Where multiple copies of the tag
+ * exist in the config file, index indicates which of the values to retrieve. If the
+ * tag is not found the default value is returned instead.
+ */
+ std::string ReadValue(const std::string &, const std::string &, const std::string &, int, bool = false);
+ /** Retrieves a boolean value from the config file.
+ * This method retrieves a boolean value from the config file. Where multiple copies of the tag
+ * exist in the config file, index indicates which of the values to retrieve. The values "1", "yes"
+ * and "true" in the config file count as true to ReadFlag, and any other value counts as false.
+ */
+ bool ReadFlag(const std::string &, const std::string &, int);
+ /** Retrieves a boolean value from the config file.
+ * This method retrieves a boolean value from the config file. Where multiple copies of the tag
+ * exist in the config file, index indicates which of the values to retrieve. The values "1", "yes"
+ * and "true" in the config file count as true to ReadFlag, and any other value counts as false.
+ * If the tag is not found, the default value is used instead.
+ */
+ bool ReadFlag(const std::string &, const std::string &, const std::string &, int);
+ /** Retrieves an integer value from the config file.
+ * This method retrieves an integer value from the config file. Where multiple copies of the tag
+ * exist in the config file, index indicates which of the values to retrieve. Any invalid integer
+ * values in the tag will cause the objects error value to be set, and any call to GetError() will
+ * return CONF_INVALID_NUMBER to be returned. need_positive is set if the number must be non-negative.
+ * If a negative number is placed into a tag which is specified positive, 0 will be returned and GetError()
+ * will return CONF_INT_NEGATIVE. Note that need_positive is not suitable to get an unsigned int - you
+ * should cast the result to achieve that effect.
+ */
+ int ReadInteger(const std::string &, const std::string &, int, bool);
+ /** Retrieves an integer value from the config file.
+ * This method retrieves an integer value from the config file. Where multiple copies of the tag
+ * exist in the config file, index indicates which of the values to retrieve. Any invalid integer
+ * values in the tag will cause the objects error value to be set, and any call to GetError() will
+ * return CONF_INVALID_NUMBER to be returned. needs_unsigned is set if the number must be unsigned.
+ * If a signed number is placed into a tag which is specified unsigned, 0 will be returned and GetError()
+ * will return CONF_NOT_UNSIGNED. If the tag is not found, the default value is used instead.
+ */
+ int ReadInteger(const std::string &, const std::string &, const std::string &, int, bool);
+ /** Returns the last error to occur.
+ * Valid errors can be found by looking in modules.h. Any nonzero value indicates an error condition.
+ * A call to GetError() resets the error flag back to 0.
+ */
+ long GetError();
+ /** Counts the number of times a given tag appears in the config file.
+ * This method counts the number of times a tag appears in a config file, for use where
+ * there are several tags of the same kind, e.g. with opers and connect types. It can be
+ * used with the index value of ConfigReader::ReadValue to loop through all copies of a
+ * multiple instance tag.
+ */
+ int Enumerate(const std::string &);
+ /** Returns true if a config file is valid.
+ * This method is partially implemented and will only return false if the config
+ * file does not exist or could not be opened.
+ */
+ bool Verify();
+ /** Dumps the list of errors in a config file to an output location. If bail is true,
+ * then the program will abort. If bail is false and user points to a valid user
+ * record, the error report will be spooled to the given user by means of NOTICE.
+ * if bool is false AND user is false, the error report will be spooled to all opers
+ * by means of a NOTICE to all opers.
+ */
+ void DumpErrors(bool);
+ /** Returns the number of items within a tag.
+ * For example if the tag was &lt;test tag="blah" data="foo"&gt; then this
+ * function would return 2. Spaces and newlines both qualify as valid seperators
+ * between values.
+ */
+ int EnumerateValues(const std::string &, int);
+};
+
#endif
diff --git a/include/extern.h b/include/extern.h
index 4441c1451..63b2c7af4 100644
--- a/include/extern.h
+++ b/include/extern.h
@@ -227,6 +227,8 @@ char *sockstrerror(int error);
/**** config.c ****/
+E ServerConfig serverConfig;
+
E std::list<Uplink *> Uplinks;
E char *LocalHost;
E int LocalPort;
diff --git a/include/services.h b/include/services.h
index 8ba6db085..30e3103cb 100644
--- a/include/services.h
+++ b/include/services.h
@@ -224,6 +224,7 @@ extern int strncasecmp(const char *, const char *, size_t);
#include "defs.h"
#include "slist.h"
#include "events.h"
+#include "configreader.h"
/* pull in the various bits of STL to pull in */
#include <string>
diff --git a/src/Makefile b/src/Makefile
index 7973ba4d7..bde534274 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,11 +1,11 @@
OBJS = actions.o base64.o bots.o botserv.o channels.o chanserv.o commands.o compat.o \
config.o datafiles.o encrypt.o events.o hashcomp.o helpserv.o hostserv.o init.o ircd.o language.o log.o mail.o main.o \
memory.o memoserv.o messages.o misc.o modules.o news.o nickserv.o operserv.o \
- process.o send.o servers.o sessions.o slist.o sockutil.o timeout.o users.o module.o modulemanager.o
+ process.o send.o servers.o sessions.o slist.o sockutil.o timeout.o users.o module.o modulemanager.o configreader.o
SRCS = actions.c base64.c bots.cpp botserv.c channels.c chanserv.c commands.c compat.c \
config.c datafiles.c encrypt.c events.c hashcomp.cpp helpserv.c hostserv.c init.c ircd.c language.c log.c mail.c main.c \
memory.c memoserv.c messages.c misc.c modules.c news.c nickserv.c operserv.c \
- process.c send.c servers.c sessions.c s sockutil.c timeout.c users.c module.cpp modulemanager.cpp
+ process.c send.c servers.c sessions.c s sockutil.c timeout.c users.c module.cpp modulemanager.cpp configreader.cpp
INCLUDES = ../include/commands.h ../include/defs.h ../include/language.h \
../include/pseudo.h ../include/sysconf.h ../include/config.h \
diff --git a/src/configreader.cpp b/src/configreader.cpp
new file mode 100644
index 000000000..ba65e173b
--- /dev/null
+++ b/src/configreader.cpp
@@ -0,0 +1,100 @@
+/* +------------------------------------+
+ * | Inspire Internet Relay Chat Daemon |
+ * +------------------------------------+
+ *
+ * InspIRCd: (C) 2002-2008 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
+ * This program is free but copyrighted software; see
+ * the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
+#include "services.h"
+
+ConfigReader::ConfigReader() : data(&serverConfig.config_data), errorlog(new std::ostringstream(std::stringstream::in | std::stringstream::out)), privatehash(false), error(CONF_NO_ERROR)
+{
+}
+
+ConfigReader::~ConfigReader()
+{
+ if (this->errorlog)
+ delete this->errorlog;
+ if(this->privatehash)
+ delete this->data;
+}
+
+ConfigReader::ConfigReader(const std::string &filename) : data(new ConfigDataHash), errorlog(new std::ostringstream(std::stringstream::in | std::stringstream::out)), privatehash(true), error(CONF_NO_ERROR)
+{
+ serverConfig.ClearStack();
+}
+
+std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds)
+{
+ /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
+ std::string result;
+ if (!serverConfig.ConfValue(*this->data, tag, name, default_value, index, result, allow_linefeeds)) this->error = CONF_VALUE_NOT_FOUND;
+ return result;
+}
+
+std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds)
+{
+ return ReadValue(tag, name, "", index, allow_linefeeds);
+}
+
+bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index)
+{
+ return serverConfig.ConfValueBool(*this->data, tag, name, default_value, index);
+}
+
+bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
+{
+ return ReadFlag(tag, name, "", index);
+}
+
+int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool need_positive)
+{
+ int result;
+ if (!serverConfig.ConfValueInteger(*this->data, tag, name, default_value, index, result)) {
+ this->error = CONF_VALUE_NOT_FOUND;
+ return 0;
+ }
+ if (need_positive && result < 0) {
+ this->error = CONF_INT_NEGATIVE;
+ return 0;
+ }
+ return result;
+}
+
+int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool need_positive)
+{
+ return ReadInteger(tag, name, "", index, need_positive);
+}
+
+long ConfigReader::GetError()
+{
+ long olderr = this->error;
+ this->error = 0;
+ return olderr;
+}
+
+void ConfigReader::DumpErrors(bool bail)
+{
+ serverConfig.ReportConfigError(this->errorlog->str(), bail);
+}
+
+int ConfigReader::Enumerate(const std::string &tag)
+{
+ return serverConfig.ConfValueEnum(*this->data, tag);
+}
+
+int ConfigReader::EnumerateValues(const std::string &tag, int index)
+{
+ return serverConfig.ConfVarEnum(*this->data, tag, index);
+}
+
+bool ConfigReader::Verify()
+{
+ return this->readerror;
+}