diff options
author | peavey <peavey@e03df62e-2008-0410-955e-edbf42e46eb7> | 2006-12-26 00:51:20 +0000 |
---|---|---|
committer | peavey <peavey@e03df62e-2008-0410-955e-edbf42e46eb7> | 2006-12-26 00:51:20 +0000 |
commit | 047f09dd572b89223ff3798dc25c34fe4c4172b3 (patch) | |
tree | 676ea8fe0e15a648d9bb2ae6dbec27b575f0ce74 | |
parent | 1bbe35d493556fd7e6bc2b0b7df3ea888fae8b74 (diff) |
Add possibility to define default value for ConfValue in configreader and ReadConf in modules.
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@6113 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r-- | include/configreader.h | 4 | ||||
-rw-r--r-- | include/modules.h | 7 | ||||
-rw-r--r-- | src/configreader.cpp | 94 | ||||
-rw-r--r-- | src/modules.cpp | 12 |
4 files changed, 74 insertions, 43 deletions
diff --git a/include/configreader.h b/include/configreader.h index 3c8a58e64..d814a398e 100644 --- a/include/configreader.h +++ b/include/configreader.h @@ -139,6 +139,7 @@ struct InitialConfig { char* tag; char* value; + char* default_value; ValueContainerBase* val; ConfigDataType datatype; Validator validation_function; @@ -570,9 +571,12 @@ class ServerConfig : public Extensible /** Writes 'length' chars into 'result' as a string */ bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length, bool allow_linefeeds = false); + bool ConfValue(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, char* result, int length, bool allow_linefeeds = false); + /** Writes 'length' chars into 'result' as a string */ bool ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result, bool allow_linefeeds = false); + 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); /** Tries to convert the value to an integer and write it to 'result' */ diff --git a/include/modules.h b/include/modules.h index 3c9899ded..8f5462b3a 100644 --- a/include/modules.h +++ b/include/modules.h @@ -1394,6 +1394,13 @@ class ConfigReader : public classbase * 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. If the + * tag is not found the default value is returned instead. + */ + std::string ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds = 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. diff --git a/src/configreader.cpp b/src/configreader.cpp index f6c8ffbde..4ac41b1d5 100644 --- a/src/configreader.cpp +++ b/src/configreader.cpp @@ -597,43 +597,43 @@ void ServerConfig::Read(bool bail, userrec* user) /* These tags can occur ONCE or not at all */ InitialConfig Values[] = { - {"options", "softlimit", new ValueContainerUInt (&this->SoftLimit), DT_INTEGER, ValidateSoftLimit}, - {"options", "somaxconn", new ValueContainerInt (&this->MaxConn), DT_INTEGER, ValidateMaxConn}, - {"server", "name", new ValueContainerChar (this->ServerName), DT_CHARPTR, ValidateServerName}, - {"server", "description", new ValueContainerChar (this->ServerDesc), DT_CHARPTR, NoValidation}, - {"server", "network", new ValueContainerChar (this->Network), DT_CHARPTR, NoValidation}, - {"admin", "name", new ValueContainerChar (this->AdminName), DT_CHARPTR, NoValidation}, - {"admin", "email", new ValueContainerChar (this->AdminEmail), DT_CHARPTR, NoValidation}, - {"admin", "nick", new ValueContainerChar (this->AdminNick), DT_CHARPTR, NoValidation}, - {"files", "motd", new ValueContainerChar (this->motd), DT_CHARPTR, ValidateMotd}, - {"files", "rules", new ValueContainerChar (this->rules), DT_CHARPTR, ValidateRules}, - {"power", "diepass", new ValueContainerChar (this->diepass), DT_CHARPTR, NoValidation}, - {"power", "pause", new ValueContainerInt (&this->DieDelay), DT_INTEGER, NoValidation}, - {"power", "restartpass", new ValueContainerChar (this->restartpass), DT_CHARPTR, NoValidation}, - {"options", "prefixquit", new ValueContainerChar (this->PrefixQuit), DT_CHARPTR, NoValidation}, - {"options", "loglevel", new ValueContainerChar (debug), DT_CHARPTR, ValidateLogLevel}, - {"options", "netbuffersize", new ValueContainerInt (&this->NetBufferSize), DT_INTEGER, ValidateNetBufferSize}, - {"options", "maxwho", new ValueContainerInt (&this->MaxWhoResults), DT_INTEGER, ValidateMaxWho}, - {"options", "allowhalfop", new ValueContainerBool (&this->AllowHalfop), DT_BOOLEAN, NoValidation}, - {"dns", "server", new ValueContainerChar (this->DNSServer), DT_CHARPTR, ValidateDnsServer}, - {"dns", "timeout", new ValueContainerInt (&this->dns_timeout), DT_INTEGER, ValidateDnsTimeout}, - {"options", "moduledir", new ValueContainerChar (this->ModPath), DT_CHARPTR, ValidateModPath}, - {"disabled", "commands", new ValueContainerChar (this->DisabledCommands), DT_CHARPTR, NoValidation}, - {"options", "userstats", new ValueContainerChar (this->UserStats), DT_CHARPTR, NoValidation}, - {"options", "customversion", new ValueContainerChar (this->CustomVersion), DT_CHARPTR, NoValidation}, - {"options", "hidesplits", new ValueContainerBool (&this->HideSplits), DT_BOOLEAN, NoValidation}, - {"options", "hidebans", new ValueContainerBool (&this->HideBans), DT_BOOLEAN, NoValidation}, - {"options", "hidewhois", new ValueContainerChar (this->HideWhoisServer), DT_CHARPTR, NoValidation}, - {"options", "operspywhois", new ValueContainerBool (&this->OperSpyWhois), DT_BOOLEAN, NoValidation}, - {"options", "nouserdns", new ValueContainerBool (&this->NoUserDns), DT_BOOLEAN, NoValidation}, - {"options", "syntaxhints", new ValueContainerBool (&this->SyntaxHints), DT_BOOLEAN, NoValidation}, - {"options", "cyclehosts", new ValueContainerBool (&this->CycleHosts), DT_BOOLEAN, NoValidation}, - {"options", "ircumsgprefix", new ValueContainerBool (&this->UndernetMsgPrefix), DT_BOOLEAN, NoValidation}, - {"pid", "file", new ValueContainerChar (this->PID), DT_CHARPTR, NoValidation}, - {"whowas", "groupsize", new ValueContainerInt (&this->WhoWasGroupSize), DT_INTEGER, NoValidation}, - {"whowas", "maxgroups", new ValueContainerInt (&this->WhoWasMaxGroups), DT_INTEGER, NoValidation}, - {"whowas", "maxkeep", new ValueContainerChar (maxkeep), DT_CHARPTR, ValidateWhoWas}, - {"die", "value", new ValueContainerChar (this->DieValue), DT_CHARPTR, NoValidation}, + {"options", "softlimit", "", new ValueContainerUInt (&this->SoftLimit), DT_INTEGER, ValidateSoftLimit}, + {"options", "somaxconn", "", new ValueContainerInt (&this->MaxConn), DT_INTEGER, ValidateMaxConn}, + {"server", "name", "", new ValueContainerChar (this->ServerName), DT_CHARPTR, ValidateServerName}, + {"server", "description", "", new ValueContainerChar (this->ServerDesc), DT_CHARPTR, NoValidation}, + {"server", "network", "", new ValueContainerChar (this->Network), DT_CHARPTR, NoValidation}, + {"admin", "name", "", new ValueContainerChar (this->AdminName), DT_CHARPTR, NoValidation}, + {"admin", "email", "", new ValueContainerChar (this->AdminEmail), DT_CHARPTR, NoValidation}, + {"admin", "nick", "", new ValueContainerChar (this->AdminNick), DT_CHARPTR, NoValidation}, + {"files", "motd", "", new ValueContainerChar (this->motd), DT_CHARPTR, ValidateMotd}, + {"files", "rules", "", new ValueContainerChar (this->rules), DT_CHARPTR, ValidateRules}, + {"power", "diepass", "", new ValueContainerChar (this->diepass), DT_CHARPTR, NoValidation}, + {"power", "pause", "", new ValueContainerInt (&this->DieDelay), DT_INTEGER, NoValidation}, + {"power", "restartpass", "", new ValueContainerChar (this->restartpass), DT_CHARPTR, NoValidation}, + {"options", "prefixquit", "", new ValueContainerChar (this->PrefixQuit), DT_CHARPTR, NoValidation}, + {"options", "loglevel", "", new ValueContainerChar (debug), DT_CHARPTR, ValidateLogLevel}, + {"options", "netbuffersize","", new ValueContainerInt (&this->NetBufferSize), DT_INTEGER, ValidateNetBufferSize}, + {"options", "maxwho", "", new ValueContainerInt (&this->MaxWhoResults), DT_INTEGER, ValidateMaxWho}, + {"options", "allowhalfop", "", new ValueContainerBool (&this->AllowHalfop), DT_BOOLEAN, NoValidation}, + {"dns", "server", "", new ValueContainerChar (this->DNSServer), DT_CHARPTR, ValidateDnsServer}, + {"dns", "timeout", "", new ValueContainerInt (&this->dns_timeout), DT_INTEGER, ValidateDnsTimeout}, + {"options", "moduledir", "", new ValueContainerChar (this->ModPath), DT_CHARPTR, ValidateModPath}, + {"disabled", "commands", "", new ValueContainerChar (this->DisabledCommands), DT_CHARPTR, NoValidation}, + {"options", "userstats", "", new ValueContainerChar (this->UserStats), DT_CHARPTR, NoValidation}, + {"options", "customversion","", new ValueContainerChar (this->CustomVersion), DT_CHARPTR, NoValidation}, + {"options", "hidesplits", "", new ValueContainerBool (&this->HideSplits), DT_BOOLEAN, NoValidation}, + {"options", "hidebans", "", new ValueContainerBool (&this->HideBans), DT_BOOLEAN, NoValidation}, + {"options", "hidewhois", "", new ValueContainerChar (this->HideWhoisServer), DT_CHARPTR, NoValidation}, + {"options", "operspywhois", "", new ValueContainerBool (&this->OperSpyWhois), DT_BOOLEAN, NoValidation}, + {"options", "nouserdns", "", new ValueContainerBool (&this->NoUserDns), DT_BOOLEAN, NoValidation}, + {"options", "syntaxhints", "", new ValueContainerBool (&this->SyntaxHints), DT_BOOLEAN, NoValidation}, + {"options", "cyclehosts", "", new ValueContainerBool (&this->CycleHosts), DT_BOOLEAN, NoValidation}, + {"options", "ircumsgprefix","0", new ValueContainerBool (&this->UndernetMsgPrefix), DT_BOOLEAN, NoValidation}, + {"pid", "file", "", new ValueContainerChar (this->PID), DT_CHARPTR, NoValidation}, + {"whowas", "groupsize", "10", new ValueContainerInt (&this->WhoWasGroupSize), DT_INTEGER, NoValidation}, + {"whowas", "maxgroups", "10240", new ValueContainerInt (&this->WhoWasMaxGroups), DT_INTEGER, NoValidation}, + {"whowas", "maxkeep", "3600", new ValueContainerChar (maxkeep), DT_CHARPTR, ValidateWhoWas}, + {"die", "value", "", new ValueContainerChar (this->DieValue), DT_CHARPTR, NoValidation}, {NULL} }; @@ -733,7 +733,7 @@ void ServerConfig::Read(bool bail, userrec* user) bool allow_newlines = ((dt & DT_ALLOW_NEWLINE) > 0); dt &= ~DT_ALLOW_NEWLINE; - ConfValue(this->config_data, Values[Index].tag, Values[Index].value, 0, item, MAXBUF, allow_newlines); + ConfValue(this->config_data, Values[Index].tag, Values[Index].value, Values[Index].default_value, 0, item, MAXBUF, allow_newlines); ValueItem vi(item); Values[Index].validation_function(this, Values[Index].tag, Values[Index].value, vi); @@ -1262,14 +1262,24 @@ bool ServerConfig::DoInclude(ConfigDataHash &target, const std::string &file, st bool ServerConfig::ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length, bool allow_linefeeds) { + return ConfValue(target, tag, var, "", index, result, length, allow_linefeeds); +} + +bool ServerConfig::ConfValue(ConfigDataHash &target, const char* tag, const char* var, const char* default_value, int index, char* result, int length, bool allow_linefeeds) +{ std::string value; - bool r = ConfValue(target, std::string(tag), std::string(var), index, value, allow_linefeeds); + bool r = ConfValue(target, std::string(tag), std::string(var), std::string(default_value), index, value, allow_linefeeds); strlcpy(result, value.c_str(), length); return r; } bool ServerConfig::ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result, bool allow_linefeeds) { + return ConfValue(target, tag, var, "", index, result, allow_linefeeds); +} + +bool ServerConfig::ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, const std::string &default_value, int index, std::string &result, bool allow_linefeeds) +{ ConfigDataHash::size_type pos = index; if((pos >= 0) && (pos < target.count(tag))) { @@ -1299,6 +1309,12 @@ bool ServerConfig::ConfValue(ConfigDataHash &target, const std::string &tag, con } else if(pos == 0) { + if (!default_value.empty()) + { + result = default_value; + ServerInstance->Log(DEBUG, "No <%s> tags in config file using default.", tag.c_str()); + return true; + } ServerInstance->Log(DEBUG, "No <%s> tags in config file.", tag.c_str()); } else diff --git a/src/modules.cpp b/src/modules.cpp index b218c63ee..18b2bf618 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -625,20 +625,24 @@ ConfigReader::ConfigReader(InspIRCd* Instance, const std::string &filename) : Se this->error = CONF_FILE_NOT_FOUND; }; -std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds) + +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 (!ServerInstance->Config->ConfValue(*this->data, tag, name, index, result, allow_linefeeds)) + if (!ServerInstance->Config->ConfValue(*this->data, tag, name, default_value, index, result, allow_linefeeds)) { this->error = CONF_VALUE_NOT_FOUND; - return ""; } - 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, int index) { return ServerInstance->Config->ConfValueBool(*this->data, tag, name, index); |