]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/configreader.cpp
Change more modules to VF_OPTCOMMON and assert identical charsets in m_nationalchars
[user/henk/code/inspircd.git] / src / configreader.cpp
index 5fa56ba8f06a6fbeb09ef83c9122a5890fd2524e..c62973c8993388058aafeefc0192c59a778117bc 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
+ *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
  * See: http://wiki.inspircd.org/Credits
  *
  * This program is free but copyrighted software; see
 #include "xline.h"
 #include "exitcodes.h"
 #include "commands/cmd_whowas.h"
-#include "modes/cmode_h.h"
 #include "configparser.h"
 
 ServerConfig::ServerConfig()
 {
        WhoWasGroupSize = WhoWasMaxGroups = WhoWasMaxKeep = 0;
        NoUserDns = OperSpyWhois = HideBans = HideSplits = UndernetMsgPrefix = false;
-       CycleHosts = AllowHalfop = InvBypassModes = true;
-       dns_timeout = DieDelay = 5;
+       CycleHosts = InvBypassModes = true;
+       dns_timeout = 5;
        MaxTargets = 20;
        NetBufferSize = 10240;
        SoftLimit = ServerInstance->SE->GetMaxFds();
@@ -84,13 +83,6 @@ void ServerConfig::Send005(User* user)
                user->WriteNumeric(RPL_ISUPPORT, "%s %s", user->nick.c_str(), line->c_str());
 }
 
-static void ReqRead(ServerConfig* src, const std::string& tag, const std::string& key, std::string& dest)
-{
-       ConfigTag* t = src->ConfValue(tag);
-       if (!t || !t->readString(key, dest))
-               throw CoreException("You must specify a value for <" + tag + ":" + key + ">");
-}
-
 template<typename T, typename V>
 static void range(T& value, V min, V max, V def, const char* msg)
 {
@@ -103,63 +95,11 @@ static void range(T& value, V min, V max, V def, const char* msg)
 }
 
 
-/* NOTE: Before anyone asks why we're not using inet_pton for this, it is because inet_pton and friends do not return so much detail,
- * even in strerror(errno). They just return 'yes' or 'no' to an address without such detail as to whats WRONG with the address.
- * Because ircd users arent as technical as they used to be (;)) we are going to give more of a useful error message.
- */
 static void ValidIP(const std::string& ip, const std::string& key)
 {
-       const char* p = ip.c_str();
-       int num_dots = 0;
-       int num_seps = 0;
-       int not_numbers = false;
-       int not_hex = false;
-
-       if (*p)
-       {
-               if (*p == '.')
-                       throw CoreException("The value of "+key+" is not an IP address");
-
-               for (const char* ptr = p; *ptr; ++ptr)
-               {
-                       if (*ptr != ':' && *ptr != '.')
-                       {
-                               if (*ptr < '0' || *ptr > '9')
-                                       not_numbers = true;
-                               if ((*ptr < '0' || *ptr > '9') && (toupper(*ptr) < 'A' || toupper(*ptr) > 'F'))
-                                       not_hex = true;
-                       }
-                       switch (*ptr)
-                       {
-                               case ' ':
-                                       throw CoreException("The value of "+key+" is not an IP address");
-                               case '.':
-                                       num_dots++;
-                               break;
-                               case ':':
-                                       num_seps++;
-                               break;
-                       }
-               }
-
-               if (num_dots > 3)
-                       throw CoreException("The value of "+key+" is an IPv4 address with too many fields!");
-
-               if (num_seps > 8)
-                       throw CoreException("The value of "+key+" is an IPv6 address with too many fields!");
-
-               if (num_seps == 0 && num_dots < 3)
-                       throw CoreException("The value of "+key+" looks to be a malformed IPv4 address");
-
-               if (num_seps == 0 && num_dots == 3 && not_numbers)
-                       throw CoreException("The value of "+key+" contains non-numeric characters in an IPv4 address");
-
-               if (num_seps != 0 && not_hex)
-                       throw CoreException("The value of "+key+" contains non-hexdecimal characters in an IPv6 address");
-
-               if (num_seps != 0 && num_dots != 3 && num_dots != 0)
-                       throw CoreException("The value of "+key+" is a malformed IPv6 4in6 address");
-       }
+       irc::sockets::sockaddrs dummy;
+       if (!irc::sockets::aptosa(ip, 0, dummy))
+               throw CoreException("The value of "+key+" is not an IP address");
 }
 
 static void ValidHost(const std::string& p, const std::string& msg)
@@ -182,8 +122,6 @@ static void ValidHost(const std::string& p, const std::string& msg)
                throw CoreException("The value of "+msg+" is not a valid hostname");
 }
 
-// Specialized validators
-
 bool ServerConfig::ApplyDisabledCommands(const std::string& data)
 {
        std::stringstream dcmds(data);
@@ -329,8 +267,19 @@ void ServerConfig::CrossCheckConnectBlocks(ServerConfig* current)
                }
        }
 
+       int blk_count = config_data.count("connect");
+       if (blk_count == 0)
+       {
+               // No connect blocks found; make a trivial default block
+               std::vector<KeyVal>* items;
+               ConfigTag* tag = ConfigTag::create("connect", "<auto>", 0, items);
+               items->push_back(std::make_pair("allow", "*"));
+               config_data.insert(std::make_pair("connect", tag));
+               blk_count = 1;
+       }
+
        ClassMap newBlocksByMask;
-       Classes.resize(config_data.count("connect"));
+       Classes.resize(blk_count);
        std::map<std::string, int> names;
 
        bool try_again = true;
@@ -354,7 +303,7 @@ void ServerConfig::CrossCheckConnectBlocks(ServerConfig* current)
                                {
                                        try_again = true;
                                        // couldn't find parent this time. If it's the last time, we'll never find it.
-                                       if (tries == 50)
+                                       if (tries >= blk_count)
                                                throw CoreException("Could not find parent connect class \"" + parentName + "\" for connect block " + ConvToStr(i));
                                        continue;
                                }
@@ -362,13 +311,15 @@ void ServerConfig::CrossCheckConnectBlocks(ServerConfig* current)
                        }
 
                        std::string name = tag->getString("name");
-                       if (!name.empty())
+                       if (name.empty())
                        {
-                               if (names.find(name) != names.end())
-                                       throw CoreException("Two connect classes with name \"" + name + "\" defined!");
-                               names[name] = i;
+                               name = "unnamed-" + ConvToStr(i);
                        }
 
+                       if (names.find(name) != names.end())
+                               throw CoreException("Two connect classes with name \"" + name + "\" defined!");
+                       names[name] = i;
+
                        std::string mask, typeMask;
                        char type;
 
@@ -394,8 +345,7 @@ void ServerConfig::CrossCheckConnectBlocks(ServerConfig* current)
                                new ConnectClass(tag, type, mask, *parent) :
                                new ConnectClass(tag, type, mask);
 
-                       if (!name.empty())
-                               me->name = name;
+                       me->name = name;
 
                        tag->readString("password", me->pass);
                        tag->readString("hash", me->hash);
@@ -416,6 +366,8 @@ void ServerConfig::CrossCheckConnectBlocks(ServerConfig* current)
                        me->hardsendqmax = tag->getInt("hardsendq", me->hardsendqmax);
                        me->recvqmax = tag->getInt("recvq", me->recvqmax);
                        me->penaltythreshold = tag->getInt("threshold", me->penaltythreshold);
+                       me->commandrate = tag->getInt("commandrate", me->commandrate);
+                       me->fakelag = tag->getBool("fakelag", me->fakelag);
                        me->maxlocal = tag->getInt("localmax", me->maxlocal);
                        me->maxglobal = tag->getInt("globalmax", me->maxglobal);
                        me->port = tag->getInt("port", me->port);
@@ -471,19 +423,16 @@ static const Deprecated ChangedConfig[] = {
        {"options", "netbuffersize",    "has been moved to <performance:netbuffersize> as of 1.2a3"},
        {"options", "maxwho",           "has been moved to <performance:maxwho> as of 1.2a3"},
        {"options",     "loglevel",             "1.2 does not use the loglevel value. Please define <log> tags instead."},
-       {"die",     "value",            "has always been deprecated"},
+       {"die",     "value",            "you need to reread your config"},
 };
 
 void ServerConfig::Fill()
 {
-       ReqRead(this, "server", "name", ServerName);
-       ReqRead(this, "power", "diepass", diepass);
-       ReqRead(this, "power", "restartpass", restartpass);
-
        ConfigTag* options = ConfValue("options");
        ConfigTag* security = ConfValue("security");
+       diepass = ConfValue("power")->getString("diepass");
+       restartpass = ConfValue("power")->getString("restartpass");
        powerhash = ConfValue("power")->getString("hash");
-       DieDelay = ConfValue("power")->getInt("pause");
        PrefixQuit = options->getString("prefixquit");
        SuffixQuit = options->getString("suffixquit");
        FixedQuit = options->getString("fixedquit");
@@ -493,6 +442,7 @@ void ServerConfig::Fill()
        SoftLimit = ConfValue("performance")->getInt("softlimit", ServerInstance->SE->GetMaxFds());
        MaxConn = ConfValue("performance")->getInt("somaxconn", SOMAXCONN);
        MoronBanner = options->getString("moronbanner", "You're banned!");
+       ServerName = ConfValue("server")->getString("name");
        ServerDesc = ConfValue("server")->getString("description", "Configure Me");
        Network = ConfValue("server")->getString("network", "Network");
        sid = ConfValue("server")->getString("id", "");
@@ -526,10 +476,10 @@ void ServerConfig::Fill()
        WhoWasMaxGroups = ConfValue("whowas")->getInt("maxgroups");
        WhoWasMaxKeep = ServerInstance->Duration(ConfValue("whowas")->getString("maxkeep"));
        DieValue = ConfValue("die")->getString("value");
-       MaxChans = ConfValue("channels")->getInt("users");
-       OperMaxChans = ConfValue("channels")->getInt("opers");
-       c_ipv4_range = ConfValue("cidr")->getInt("ipv4clone");
-       c_ipv6_range = ConfValue("cidr")->getInt("ipv6clone");
+       MaxChans = ConfValue("channels")->getInt("users", 20);
+       OperMaxChans = ConfValue("channels")->getInt("opers", 60);
+       c_ipv4_range = ConfValue("cidr")->getInt("ipv4clone", 32);
+       c_ipv6_range = ConfValue("cidr")->getInt("ipv6clone", 128);
        Limits.NickMax = ConfValue("limits")->getInt("maxnick", 32);
        Limits.ChanMax = ConfValue("limits")->getInt("maxchan", 64);
        Limits.MaxModes = ConfValue("limits")->getInt("maxmodes", 20);
@@ -612,9 +562,6 @@ void ServerConfig::Fill()
        Limits.Finalise();
 }
 
-/* These tags MUST occur and must ONLY occur once in the config file */
-static const char* const Once[] = { "server", "admin", "files", "power", "options" };
-
 // WARNING: it is not safe to use most of the codebase in this function, as it
 // will run in the config reader thread
 void ServerConfig::Read()
@@ -647,23 +594,6 @@ void ServerConfig::Apply(ServerConfig* old, const std::string &useruid)
        /* The stuff in here may throw CoreException, be sure we're in a position to catch it. */
        try
        {
-               /* Check we dont have more than one of singular tags, or any of them missing
-                */
-               for (int Index = 0; Index * sizeof(*Once) < sizeof(Once); Index++)
-               {
-                       std::string tag = Once[Index];
-                       ConfigTagList tags = ConfTags(tag);
-                       if (tags.first == tags.second)
-                               throw CoreException("You have not defined a <"+tag+"> tag, this is required.");
-                       tags.first++;
-                       if (tags.first != tags.second)
-                       {
-                               errstr << "You have more than one <" << tag << "> tag.\n"
-                                       << "First occurrence at " << ConfValue(tag)->getTagLocation()
-                                       << "; second occurrence at " << tags.first->second->getTagLocation() << std::endl;
-                       }
-               }
-
                for (int Index = 0; Index * sizeof(Deprecated) < sizeof(ChangedConfig); Index++)
                {
                        std::string dummy;
@@ -687,6 +617,9 @@ void ServerConfig::Apply(ServerConfig* old, const std::string &useruid)
        // write once here, to try it out and make sure its ok
        ServerInstance->WritePID(this->PID);
 
+       // Check errors before dealing with failed binds, since continuing on failed bind is wanted in some circumstances.
+       valid = errstr.str().empty();
+
        /*
         * These values can only be set on boot. Keep their old values. Do it before we send messages so we actually have a servername.
         */
@@ -715,7 +648,6 @@ void ServerConfig::Apply(ServerConfig* old, const std::string &useruid)
 
        User* user = useruid.empty() ? NULL : ServerInstance->FindNick(useruid);
 
-       valid = errstr.str().empty();
        if (!valid)
                ServerInstance->Logs->Log("CONFIG",DEFAULT, "There were errors in your configuration file:");
 
@@ -749,9 +681,6 @@ void ServerConfig::Apply(ServerConfig* old, const std::string &useruid)
                        ServerInstance->Exit(EXIT_STATUS_CONFIG);
                }
 
-               if (ConfValue("options")->getBool("allowhalfop"))
-                       ServerInstance->Modes->AddMode(new ModeChannelHalfOp);
-
                return;
        }
 
@@ -768,18 +697,6 @@ void ServerConfig::Apply(ServerConfig* old, const std::string &useruid)
 
 void ServerConfig::ApplyModules(User* user)
 {
-       bool AllowHalfOp = ConfValue("options")->getBool("allowhalfop");
-       ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
-       if (AllowHalfOp && !mh) {
-               ServerInstance->Logs->Log("CONFIG", DEFAULT, "Enabling halfop mode.");
-               mh = new ModeChannelHalfOp;
-               ServerInstance->Modes->AddMode(mh);
-       } else if (!AllowHalfOp && mh) {
-               ServerInstance->Logs->Log("CONFIG", DEFAULT, "Disabling halfop mode.");
-               ServerInstance->Modes->DelMode(mh);
-               delete mh;
-       }
-
        Module* whowas = ServerInstance->Modules->Find("cmd_whowas.so");
        if (whowas)
                WhowasRequest(NULL, whowas, WhowasRequest::WHOWAS_PRUNE).Send();
@@ -802,6 +719,9 @@ void ServerConfig::ApplyModules(User* user)
                }
        }
 
+       if (ConfValue("options")->getBool("allowhalfop") && removed_modules.erase("m_halfop.so") == 0)
+               added_modules.push_back("m_halfop.so");
+
        for (std::set<std::string>::iterator removing = removed_modules.begin(); removing != removed_modules.end(); removing++)
        {
                // Don't remove cmd_*.so, just remove m_*.so
@@ -925,7 +845,6 @@ std::string ServerConfig::GetSID()
 
 void ConfigReaderThread::Run()
 {
-       Config = new ServerConfig;
        Config->Read();
        done = true;
 }
@@ -957,7 +876,7 @@ void ConfigReaderThread::Finish()
                FOREACH_MOD(I_OnRehash, OnRehash(user));
                ServerInstance->BuildISupport();
 
-               delete old;
+               Config = old;
        }
        else
        {
@@ -965,6 +884,5 @@ void ConfigReaderThread::Finish()
                ServerInstance->Logs->CloseLogs();
                ServerInstance->Config = old;
                ServerInstance->Logs->OpenFileLogs();
-               delete this->Config;
        }
 }