From f9911dde529adf3dc03f4f14bbd70756ac2f665c Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sun, 2 Mar 2025 14:51:02 +0000 Subject: Return references instead of pointers from the config system. We used to return NULL from these methods but now we return an empty block so this can never actually be null now. --- src/config.cpp | 227 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 113 insertions(+), 114 deletions(-) (limited to 'src/config.cpp') diff --git a/src/config.cpp b/src/config.cpp index 47ce6feaa..33d7a8de8 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -43,14 +43,14 @@ int Block::CountBlock(const Anope::string &bname) const return blocks.count(bname); } -const Block *Block::GetBlock(const Anope::string &bname, int num) const +const Block &Block::GetBlock(const Anope::string &bname, int num) const { std::pair it = blocks.equal_range(bname); for (int i = 0; it.first != it.second; ++it.first, ++i) if (i == num) - return &it.first->second; - return &EmptyBlock; + return it.first->second; + return EmptyBlock; } Block *Block::GetMutableBlock(const Anope::string &bname, int num) @@ -127,16 +127,16 @@ Conf::Conf() : Block("") for (int i = 0; i < this->CountBlock("include"); ++i) { - const Block *include = this->GetBlock("include", i); + const Block &include = this->GetBlock("include", i); - const Anope::string &type = include->Get("type"), - &file = include->Get("name"); + const Anope::string &type = include.Get("type"), + &file = include.Get("name"); File f(file, type == "executable"); this->LoadConf(f); } - FOREACH_MOD(OnReload, (this)); + FOREACH_MOD(OnReload, (*this)); /* Check for modified values that aren't allowed to be modified */ if (Config) @@ -159,58 +159,58 @@ Conf::Conf() : Block("") for (const auto &tag : noreload) { - if (this->GetBlock(tag.block)->Get(tag.name) != Config->GetBlock(tag.block)->Get(tag.name)) + if (this->GetBlock(tag.block).Get(tag.name) != Config->GetBlock(tag.block).Get(tag.name)) throw ConfigException("<" + tag.block + ":" + tag.name + "> can not be modified once set"); } } - const Block *serverinfo = this->GetBlock("serverinfo"), *options = this->GetBlock("options"), - *mail = this->GetBlock("mail"), *networkinfo = this->GetBlock("networkinfo"); + const Block &serverinfo = this->GetBlock("serverinfo"), &options = this->GetBlock("options"), + &mail = this->GetBlock("mail"), &networkinfo = this->GetBlock("networkinfo"); - const Anope::string &servername = serverinfo->Get("name"); + const Anope::string &servername = serverinfo.Get("name"); ValidateNotEmptyOrSpaces("serverinfo", "name", servername); if (servername.find(' ') != Anope::string::npos || servername.find('.') == Anope::string::npos) throw ConfigException("serverinfo:name is not a valid server name"); - ValidateNotEmpty("serverinfo", "description", serverinfo->Get("description")); - ValidateNotEmpty("serverinfo", "pid", serverinfo->Get("pid")); - ValidateNotEmpty("serverinfo", "motd", serverinfo->Get("motd")); + ValidateNotEmpty("serverinfo", "description", serverinfo.Get("description")); + ValidateNotEmpty("serverinfo", "pid", serverinfo.Get("pid")); + ValidateNotEmpty("serverinfo", "motd", serverinfo.Get("motd")); - ValidateNotZero("options", "readtimeout", options->Get("readtimeout")); + ValidateNotZero("options", "readtimeout", options.Get("readtimeout")); - ValidateNotZero("networkinfo", "nicklen", networkinfo->Get("nicklen", "1")); - ValidateNotZero("networkinfo", "userlen", networkinfo->Get("userlen", "1")); - ValidateNotZero("networkinfo", "hostlen", networkinfo->Get("hostlen", "1")); - ValidateNotZero("networkinfo", "chanlen", networkinfo->Get("chanlen", "1")); + ValidateNotZero("networkinfo", "nicklen", networkinfo.Get("nicklen", "1")); + ValidateNotZero("networkinfo", "userlen", networkinfo.Get("userlen", "1")); + ValidateNotZero("networkinfo", "hostlen", networkinfo.Get("hostlen", "1")); + ValidateNotZero("networkinfo", "chanlen", networkinfo.Get("chanlen", "1")); - spacesepstream(options->Get("ulineservers")).GetTokens(this->Ulines); + spacesepstream(options.Get("ulineservers")).GetTokens(this->Ulines); - if (mail->Get("usemail")) + if (mail.Get("usemail")) { Anope::string check[] = { "sendfrom", "registration_subject", "registration_message", "emailchange_subject", "emailchange_message", "memo_subject", "memo_message" }; for (const auto &field : check) - ValidateNotEmpty("mail", field, mail->Get(field)); + ValidateNotEmpty("mail", field, mail.Get(field)); } - this->ReadTimeout = options->Get("readtimeout"); - this->ServiceAlias = options->Get("servicealias"); + this->ReadTimeout = options.Get("readtimeout"); + this->ServiceAlias = options.Get("servicealias"); { std::vector defaults; - spacesepstream(this->GetModule("nickserv")->Get("defaults")).GetTokens(defaults); + spacesepstream(this->GetModule("nickserv").Get("defaults")).GetTokens(defaults); this->DefPrivmsg = std::find(defaults.begin(), defaults.end(), "msg") != defaults.end(); } - this->DefLanguage = options->Get("defaultlanguage"); - this->TimeoutCheck = options->Get("timeoutcheck"); - this->NickChars = networkinfo->Get("nick_chars"); + this->DefLanguage = options.Get("defaultlanguage"); + this->TimeoutCheck = options.Get("timeoutcheck"); + this->NickChars = networkinfo.Get("nick_chars"); for (int i = 0; i < this->CountBlock("uplink"); ++i) { - const Block *uplink = this->GetBlock("uplink", i); + const Block &uplink = this->GetBlock("uplink", i); int protocol; - const Anope::string &protocolstr = uplink->Get("protocol", "ipv4"); + const Anope::string &protocolstr = uplink.Get("protocol", "ipv4"); if (protocolstr == "ipv4") protocol = AF_INET; else if (protocolstr == "ipv6") @@ -220,17 +220,17 @@ Conf::Conf() : Block("") else throw ConfigException("uplink:protocol must be set to ipv4, ipv6, or unix"); - const Anope::string &host = uplink->Get("host"); + const Anope::string &host = uplink.Get("host"); ValidateNotEmptyOrSpaces("uplink", "host", host); int port = 0; if (protocol != AF_UNIX) { - port = uplink->Get("port"); + port = uplink.Get("port"); ValidateNotZero("uplink", "port", port); } - const Anope::string &password = uplink->Get("password"); + const Anope::string &password = uplink.Get("password"); ValidateNotEmptyOrSpaces("uplink", "password", password); if (password[0] == ':') throw ConfigException("uplink:password is not valid"); @@ -240,9 +240,9 @@ Conf::Conf() : Block("") for (int i = 0; i < this->CountBlock("module"); ++i) { - const Block *module = this->GetBlock("module", i); + const Block &module = this->GetBlock("module", i); - const Anope::string &modname = module->Get("name"); + const Anope::string &modname = module.Get("name"); ValidateNotEmptyOrSpaces("module", "name", modname); @@ -251,13 +251,13 @@ Conf::Conf() : Block("") for (int i = 0; i < this->CountBlock("opertype"); ++i) { - const Block *opertype = this->GetBlock("opertype", i); + const Block &opertype = this->GetBlock("opertype", i); - const Anope::string &oname = opertype->Get("name"), - &modes = opertype->Get("modes"), - &inherits = opertype->Get("inherits"), - &commands = opertype->Get("commands"), - &privs = opertype->Get("privs"); + const Anope::string &oname = opertype.Get("name"), + &modes = opertype.Get("modes"), + &inherits = opertype.Get("inherits"), + &commands = opertype.Get("commands"), + &privs = opertype.Get("privs"); ValidateNotEmpty("opertype", "name", oname); @@ -294,15 +294,15 @@ Conf::Conf() : Block("") for (int i = 0; i < this->CountBlock("oper"); ++i) { - const Block *oper = this->GetBlock("oper", i); + const Block &oper = this->GetBlock("oper", i); - const Anope::string &nname = oper->Get("name"), - &type = oper->Get("type"), - &password = oper->Get("password"), - &certfp = oper->Get("certfp"), - &host = oper->Get("host"), - &vhost = oper->Get("vhost"); - bool require_oper = oper->Get("require_oper"); + const Anope::string &nname = oper.Get("name"), + &type = oper.Get("type"), + &password = oper.Get("password"), + &certfp = oper.Get("certfp"), + &host = oper.Get("host"), + &vhost = oper.Get("vhost"); + bool require_oper = oper.Get("require_oper"); ValidateNotEmptyOrSpaces("oper", "name", nname); ValidateNotEmpty("oper", "type", type); @@ -330,15 +330,15 @@ Conf::Conf() : Block("") bi->conf = false; for (int i = 0; i < this->CountBlock("service"); ++i) { - const Block *service = this->GetBlock("service", i); + const Block &service = this->GetBlock("service", i); - const Anope::string &nick = service->Get("nick"), - &user = service->Get("user"), - &host = service->Get("host"), - &gecos = service->Get("gecos"), - &modes = service->Get("modes"), - &channels = service->Get("channels"), - &alias = service->Get("alias", nick.upper()); + const Anope::string &nick = service.Get("nick"), + &user = service.Get("user"), + &host = service.Get("host"), + &gecos = service.Get("gecos"), + &modes = service.Get("modes"), + &channels = service.Get("channels"), + &alias = service.Get("alias", nick.upper()); ValidateNotEmptyOrSpaces("service", "nick", nick); ValidateNotEmptyOrSpaces("service", "user", user); @@ -396,7 +396,6 @@ Conf::Conf() : Block("") { size_t ch = oldchannel.find('#'); Anope::string chname = oldchannel.substr(ch != Anope::string::npos ? ch : 0); - bool found = false; for (const auto &botchannel : bi->botchannels) { @@ -421,24 +420,24 @@ Conf::Conf() : Block("") for (int i = 0; i < this->CountBlock("log"); ++i) { - const Block *log = this->GetBlock("log", i); + const Block &log = this->GetBlock("log", i); - int logage = log->Get("logage"); - bool rawio = log->Get("rawio"); - bool debug = log->Get("debug"); + int logage = log.Get("logage"); + bool rawio = log.Get("rawio"); + bool debug = log.Get("debug"); LogInfo l(logage, rawio, debug); - l.bot = BotInfo::Find(log->Get("bot", "Global"), true); - spacesepstream(log->Get("target")).GetTokens(l.targets); - spacesepstream(log->Get("source")).GetTokens(l.sources); - spacesepstream(log->Get("admin")).GetTokens(l.admin); - spacesepstream(log->Get("override")).GetTokens(l.override); - spacesepstream(log->Get("commands")).GetTokens(l.commands); - spacesepstream(log->Get("servers")).GetTokens(l.servers); - spacesepstream(log->Get("channels")).GetTokens(l.channels); - spacesepstream(log->Get("users")).GetTokens(l.users); - spacesepstream(log->Get("other")).GetTokens(l.normal); + l.bot = BotInfo::Find(log.Get("bot", "Global"), true); + spacesepstream(log.Get("target")).GetTokens(l.targets); + spacesepstream(log.Get("source")).GetTokens(l.sources); + spacesepstream(log.Get("admin")).GetTokens(l.admin); + spacesepstream(log.Get("override")).GetTokens(l.override); + spacesepstream(log.Get("commands")).GetTokens(l.commands); + spacesepstream(log.Get("servers")).GetTokens(l.servers); + spacesepstream(log.Get("channels")).GetTokens(l.channels); + spacesepstream(log.Get("users")).GetTokens(l.users); + spacesepstream(log.Get("other")).GetTokens(l.normal); this->LogInfos.push_back(l); } @@ -447,14 +446,14 @@ Conf::Conf() : Block("") bi->commands.clear(); for (int i = 0; i < this->CountBlock("command"); ++i) { - const Block *command = this->GetBlock("command", i); + const Block &command = this->GetBlock("command", i); - const Anope::string &service = command->Get("service"), - &nname = command->Get("name"), - &cmd = command->Get("command"), - &permission = command->Get("permission"), - &group = command->Get("group"); - bool hide = command->Get("hide"); + const Anope::string &service = command.Get("service"), + &nname = command.Get("name"), + &cmd = command.Get("command"), + &permission = command.Get("permission"), + &group = command.Get("group"); + bool hide = command.Get("hide"); ValidateNotEmptyOrSpaces("command", "service", service); ValidateNotEmpty("command", "name", nname); @@ -472,25 +471,25 @@ Conf::Conf() : Block("") PrivilegeManager::ClearPrivileges(); for (int i = 0; i < this->CountBlock("privilege"); ++i) { - const Block *privilege = this->GetBlock("privilege", i); + const Block &privilege = this->GetBlock("privilege", i); - const Anope::string &nname = privilege->Get("name"), - &desc = privilege->Get("desc"); - int rank = privilege->Get("rank"); + const Anope::string &nname = privilege.Get("name"), + &desc = privilege.Get("desc"); + int rank = privilege.Get("rank"); PrivilegeManager::AddPrivilege(Privilege(nname, desc, rank)); } for (int i = 0; i < this->CountBlock("fantasy"); ++i) { - const Block *fantasy = this->GetBlock("fantasy", i); + const Block &fantasy = this->GetBlock("fantasy", i); - const Anope::string &nname = fantasy->Get("name"), - &service = fantasy->Get("command"), - &permission = fantasy->Get("permission"), - &group = fantasy->Get("group"); - bool hide = fantasy->Get("hide"), - prepend_channel = fantasy->Get("prepend_channel", "yes"); + const Anope::string &nname = fantasy.Get("name"), + &service = fantasy.Get("command"), + &permission = fantasy.Get("permission"), + &group = fantasy.Get("group"); + bool hide = fantasy.Get("hide"), + prepend_channel = fantasy.Get("prepend_channel", "yes"); ValidateNotEmpty("fantasy", "name", nname); ValidateNotEmptyOrSpaces("fantasy", "command", service); @@ -505,10 +504,10 @@ Conf::Conf() : Block("") for (int i = 0; i < this->CountBlock("command_group"); ++i) { - const Block *command_group = this->GetBlock("command_group", i); + const Block &command_group = this->GetBlock("command_group", i); - const Anope::string &nname = command_group->Get("name"), - &description = command_group->Get("description"); + const Anope::string &nname = command_group.Get("name"), + &description = command_group.Get("description"); CommandGroup gr; gr.name = nname; @@ -544,19 +543,19 @@ Conf::Conf() : Block("") Log() << "Tied oper " << na->nc->display << " to type " << o->ot->GetName(); } - if (options->Get("casemap", "ascii") == "ascii") + if (options.Get("casemap", "ascii") == "ascii") Anope::casemap = std::locale(std::locale(), new Anope::ascii_ctype()); - else if (options->Get("casemap") == "rfc1459") + else if (options.Get("casemap") == "rfc1459") Anope::casemap = std::locale(std::locale(), new Anope::rfc1459_ctype()); else { try { - Anope::casemap = std::locale(options->Get("casemap").c_str()); + Anope::casemap = std::locale(options.Get("casemap").c_str()); } catch (const std::runtime_error &) { - Log() << "Unknown casemap " << options->Get("casemap") << " - casemap not changed"; + Log() << "Unknown casemap " << options.Get("casemap") << " - casemap not changed"; } } Anope::CaseMapRebuild(); @@ -620,30 +619,30 @@ void Conf::Post(Conf *old) } } -Block *Conf::GetModule(const Module *m) +Block &Conf::GetModule(const Module *m) { if (!m) - return &Block::EmptyBlock; + return Block::EmptyBlock; return GetModule(m->name); } -Block *Conf::GetModule(const Anope::string &mname) +Block &Conf::GetModule(const Anope::string &mname) { std::map::iterator it = modules.find(mname); if (it != modules.end()) - return it->second; + return *it->second; Block *&block = modules[mname]; /* Search for the block */ for (std::pair iters = blocks.equal_range("module"); iters.first != iters.second; ++iters.first) { - Block *b = &iters.first->second; + Block &b = iters.first->second; - if (b->Get("name") == mname) + if (b.Get("name") == mname) { - block = b; + block = &b; break; } } @@ -660,25 +659,25 @@ BotInfo *Conf::GetClient(const Anope::string &cname) if (it != bots.end()) return BotInfo::Find(!it->second.empty() ? it->second : cname, true); - Block *block = GetModule(cname.lower()); - const Anope::string &client = block->Get("client"); + Block &block = GetModule(cname.lower()); + const Anope::string &client = block.Get("client"); bots[cname] = client; return GetClient(cname); } -const Block *Conf::GetCommand(CommandSource &source) +const Block &Conf::GetCommand(CommandSource &source) { const Anope::string &block_name = source.c ? "fantasy" : "command"; for (std::pair iters = blocks.equal_range(block_name); iters.first != iters.second; ++iters.first) { - Block *b = &iters.first->second; + Block &b = iters.first->second; - if (b->Get("name") == source.command) + if (b.Get("name") == source.command) return b; } - return &Block::EmptyBlock; + return Block::EmptyBlock; } File::File(const Anope::string &n, bool e) : name(n), executable(e) @@ -929,12 +928,12 @@ void Conf::LoadConf(File &file) /* Check defines */ for (int i = 0; i < this->CountBlock("define"); ++i) { - const Block *define = this->GetBlock("define", i); + const Block &define = this->GetBlock("define", i); - const Anope::string &dname = define->Get("name"); + const Anope::string &dname = define.Get("name"); - if (dname == wordbuffer && define != b) - wordbuffer = define->Get("value"); + if (dname == wordbuffer && &define != b) + wordbuffer = define.Get("value"); } if (b) -- cgit