]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Add possibility to define default value for ConfValue in configreader and ReadConf...
authorpeavey <peavey@e03df62e-2008-0410-955e-edbf42e46eb7>
Tue, 26 Dec 2006 00:51:20 +0000 (00:51 +0000)
committerpeavey <peavey@e03df62e-2008-0410-955e-edbf42e46eb7>
Tue, 26 Dec 2006 00:51:20 +0000 (00:51 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@6113 e03df62e-2008-0410-955e-edbf42e46eb7

include/configreader.h
include/modules.h
src/configreader.cpp
src/modules.cpp

index 3c8a58e64e57c3968fa88edb179598c291cf4230..d814a398e15ce0ba14a9e14b51dd4e67b444858a 100644 (file)
@@ -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'
         */
index 3c9899dede1375ee92891ccab2c985056aeac864..8f5462b3a40addb843cf80ab2d7cb69c6bbe2112 100644 (file)
@@ -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.
index f6c8ffbde702a3dd0cccf303be05fc104613760f..4ac41b1d5c17b0911b02b54be9158c30cb0f75ed 100644 (file)
@@ -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);
@@ -1261,14 +1261,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
index b218c63ee6979d9a61c532b4450a2d38b345add0..18b2bf6184a186e25b0a9e1db8b8b718393ddbdf 100644 (file)
@@ -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);