1
0
mirror of https://github.com/anope/anope.git synced 2026-06-12 17:24:49 +02:00

Add Block::GetBlocks which returns an iterable list of blocks.

This commit is contained in:
Sadie Powell
2026-06-08 15:19:47 +01:00
parent ab362c9828
commit 90da25f84f
24 changed files with 142 additions and 99 deletions
+3
View File
@@ -18,6 +18,7 @@
#include "regchannel.h"
#include "users.h"
#include "opertype.h"
#include "miscutils.h"
namespace Configuration
{
@@ -28,6 +29,7 @@ namespace Configuration
public:
typedef Anope::map<Anope::string> ItemMap;
typedef Anope::multimap<Block> BlockMap;
typedef Anope::iterator_range<BlockMap::const_iterator> BlockList;
private:
Anope::string name;
@@ -42,6 +44,7 @@ namespace Configuration
Block(const Anope::string &);
const Anope::string &GetName() const;
int CountBlock(const Anope::string &name) const;
BlockList GetBlocks(const Anope::string &name) const;
const Block &GetBlock(const Anope::string &name, int num = 0) const;
Block *GetMutableBlock(const Anope::string &name, int num = 0);
+87
View File
@@ -0,0 +1,87 @@
// Anope IRC Services <https://www.anope.org/>
//
// Copyright (C) 2003-2026 Anope Contributors
//
// Anope is free software. You can use, modify, and/or distribute it under the
// terms of version 2 of the GNU General Public License. See docs/LICENSE.txt
// for the complete terms of this license and docs/AUTHORS.txt for a list of
// contributors.
//
// Based on the original code of Epona by Lara
// Based on the original code of Services by Andy Church
//
// SPDX-License-Identifier: GPL-2.0-only
#pragma once
#include <iterator>
#include <utility>
namespace Anope
{
template <typename Iterator>
class iterator_range;
/** Returns a range containing all elements equivalent to \p value.
* @param collection The collection to search within.
* @param value The value to search for.
*/
template <typename Collection, typename Value>
auto equal_range(const Collection& collection, const Value& value)
{
return iterator_range(collection.equal_range(value));
}
/** Returns a range representing a reverse iterator for the specified colleciton.
* @param collection The collection to create a reverse iterator for.
*/
template <typename Collection>
auto reverse_range(const Collection& collection)
{
return iterator_range(collection.rbegin(), collection.rend());
}
}
/** Represents a range of iterators. */
template <typename Iterator>
class Anope::iterator_range final
{
private:
/** An iterator which points to the start of the range. */
const Iterator begini;
/* An iterator which points to one past the end of the range. */
const Iterator endi;
public:
/** Initialises a new iterator range with the specified iterators.
* @param begin An iterator which points to the start of the range.
* @param end An iterator which points to one past the end of the range.
*/
explicit iterator_range(Iterator begin, Iterator end)
: begini(begin)
, endi(end)
{
}
/** Initialises a new iterator range from a pair of iterators.
* @param range A pair of iterators in the format [first, last).
*/
explicit iterator_range(std::pair<Iterator, Iterator> range)
: begini(range.first)
, endi(range.second)
{
}
/** Determines whether the iterator range is empty. */
bool empty() const { return begini == endi; }
/** Retrieves an iterator which points to the start of the range. */
const Iterator& begin() const { return begini; }
/** Retrieves an iterator which points to one past the end of the range. */
const Iterator& end() const { return endi; }
/** Retrieves the number of hops within the iterator range. */
typename std::iterator_traits<Iterator>::difference_type count() const { return std::distance(begini, endi); }
};
+1 -3
View File
@@ -963,10 +963,8 @@ public:
{
defaultLevels.clear();
for (int i = 0; i < conf.CountBlock("privilege"); ++i)
for (const auto &[_, priv] : conf.GetBlocks("privilege"))
{
const auto &priv = conf.GetBlock("privilege", i);
const Anope::string &pname = priv.Get<const Anope::string>("name");
Privilege *p = PrivilegeManager::FindPrivilege(pname);
+1 -3
View File
@@ -661,10 +661,8 @@ public:
{
defaultFlags.clear();
for (int i = 0; i < conf.CountBlock("privilege"); ++i)
for (const auto &[_, priv] : conf.GetBlocks("privilege"))
{
const auto &priv = conf.GetBlock("privilege", i);
const Anope::string &pname = priv.Get<const Anope::string>("name");
Privilege *p = PrivilegeManager::FindPrivilege(pname);
+1 -3
View File
@@ -331,10 +331,8 @@ public:
const auto &block = conf.GetModule(this);
defaults.clear();
for (int i = 0; i < block.CountBlock("default"); ++i)
for (const auto &[_, def] : block.GetBlocks("default"))
{
const auto &def = block.GetBlock("default", i);
LogDefault ld;
ld.service = def.Get<const Anope::string>("service");
+1 -3
View File
@@ -999,10 +999,8 @@ public:
{
modes.clear();
for (int i = 0; i < conf.CountBlock("command"); ++i)
for (const auto &[_, block] : conf.GetBlocks("command"))
{
const auto &block = conf.GetBlock("command", i);
const Anope::string &cname = block.Get<const Anope::string>("name"),
&cmd = block.Get<const Anope::string>("command");
+2 -2
View File
@@ -268,9 +268,9 @@ public:
void OnReload(Configuration::Conf &conf) override
{
command_data.clear();
for (int i = 0; i < conf.CountBlock("command"); ++i)
for (const auto &[_, block] : conf.GetBlocks("command"))
{
const auto &block = conf.GetBlock("command", i);
if (block.Get<const Anope::string>("command") != "chanserv/set/misc")
continue;
+2 -4
View File
@@ -688,9 +688,8 @@ public:
order.clear();
permissions.clear();
for (int i = 0; i < conf.CountBlock("privilege"); ++i)
for (const auto &[_, block] : conf.GetBlocks("privilege"))
{
const auto &block = conf.GetBlock("privilege", i);
const Anope::string &pname = block.Get<const Anope::string>("name");
Privilege *p = PrivilegeManager::FindPrivilege(pname);
@@ -704,9 +703,8 @@ public:
permissions[xop].push_back(pname);
}
for (int i = 0; i < conf.CountBlock("command"); ++i)
for (const auto &[_, block] : conf.GetBlocks("command"))
{
const auto &block = conf.GetBlock("command", i);
const Anope::string &cname = block.Get<const Anope::string>("name"),
&cserv = block.Get<const Anope::string>("command");
if (cname.empty() || cserv != "chanserv/xop")
+3 -6
View File
@@ -1584,9 +1584,8 @@ public:
const auto &modconf = conf.GetModule(this);
csmiscdata.clear();
for (auto idx = 0; idx < modconf.CountBlock("cs_set_misc"); ++idx)
for (const auto &[_, data] : modconf.GetBlocks("cs_set_misc"))
{
const auto &data = modconf.GetBlock("cs_set_misc", idx);
const auto &anope = data.Get<const Anope::string>("anope");
const auto &atheme = data.Get<const Anope::string>("atheme");
if (!anope.empty() && !atheme.empty())
@@ -1594,9 +1593,8 @@ public:
}
nsmiscdata.clear();
for (auto idx = 0; idx < modconf.CountBlock("ns_set_misc"); ++idx)
for (const auto &[_, data] : modconf.GetBlocks("ns_set_misc"))
{
const auto &data = modconf.GetBlock("ns_set_misc", idx);
const auto &anope = data.Get<const Anope::string>("anope");
const auto &atheme = data.Get<const Anope::string>("atheme");
if (!anope.empty() && !atheme.empty())
@@ -1604,9 +1602,8 @@ public:
}
flags.clear();
for (int i = 0; i < Config->CountBlock("privilege"); ++i)
for (const auto &[_, priv] : conf.GetBlocks("privilege"))
{
const auto &priv = Config->GetBlock("privilege", i);
const Anope::string &name = priv.Get<const Anope::string>("name");
const Anope::string &value = priv.Get<const Anope::string>("flag");
if (!name.empty() && !value.empty())
+1 -2
View File
@@ -1095,9 +1095,8 @@ public:
refresh = block.Get<int>("refresh", "3600");
std::vector<std::pair<Anope::string, short> > notify;
for (int i = 0; i < block.CountBlock("notify"); ++i)
for (const auto &[_, n] : block.GetBlocks("notify"))
{
const auto &n = block.GetBlock("notify", i);
auto nip = n.Get<Anope::string>("ip");
auto nport = n.Get<short>("port");
+3 -6
View File
@@ -126,9 +126,8 @@ public:
this->add_to_akill = block.Get<bool>("add_to_akill", "yes");
this->blacklists.clear();
for (int i = 0; i < block.CountBlock("blacklist"); ++i)
for (const auto &[_, bl] : block.GetBlocks("blacklist"))
{
const auto &bl = block.GetBlock("blacklist", i);
Blacklist blacklist;
blacklist.name = bl.Get<Anope::string>("name");
@@ -137,9 +136,8 @@ public:
blacklist.bantime = bl.Get<time_t>("time", "4h");
blacklist.reason = bl.Get<Anope::string>("reason");
for (int j = 0; j < bl.CountBlock("reply"); ++j)
for (const auto &[_, reply] : block.GetBlocks("reply"))
{
const auto &reply = bl.GetBlock("reply", j);
Blacklist::Reply r;
r.code = reply.Get<int>("code");
@@ -153,9 +151,8 @@ public:
}
this->exempts.clear();
for (int i = 0; i < block.CountBlock("exempt"); ++i)
for (const auto &[_, bl] : block.GetBlocks("exempt"))
{
const auto &bl = block.GetBlock("exempt", i);
this->exempts.insert(bl.Get<Anope::string>("ip"));
}
}
+1 -3
View File
@@ -654,10 +654,8 @@ public:
}
}
for (int i = 0; i < conf.CountBlock("ldap"); ++i)
for (const auto &[_, ldap] : conf.GetBlocks("ldap"))
{
const auto &ldap = conf.GetBlock("ldap", i);
const Anope::string &connname = ldap.Get<const Anope::string>("name", "ldap/main");
if (this->LDAPServices.find(connname) == this->LDAPServices.end())
+1 -2
View File
@@ -298,9 +298,8 @@ public:
}
}
for (int i = 0; i < config.CountBlock("mysql"); ++i)
for (const auto &[_, block] : config.GetBlocks("mysql"))
{
const auto &block = config.GetBlock("mysql", i);
const Anope::string &connname = block.Get<const Anope::string>("name", "mysql/main");
if (this->MySQLServices.find(connname) == this->MySQLServices.end())
+1 -2
View File
@@ -145,9 +145,8 @@ public:
}
}
for (int i = 0; i < config.CountBlock("sqlite"); ++i)
for (const auto &[_, block] : config.GetBlocks("sqlite"))
{
const auto &block = config.GetBlock("sqlite", i);
Anope::string connname = block.Get<const Anope::string>("name", "sqlite/main");
if (this->SQLiteServices.find(connname) == this->SQLiteServices.end())
+1 -3
View File
@@ -331,10 +331,8 @@ public:
throw ConfigException("Unable to find http reference, is httpd loaded?");
xmlrpcinterface.tokens.clear();
for (int i = 0; i < modconf.CountBlock("token"); ++i)
for (const auto &[_, block] : modconf.GetBlocks("token"))
{
const auto &block = modconf.GetBlock("token", i);
RPC::Token token;
token.token = block.Get<const Anope::string>("token");
if (!token.token.empty())
+1 -4
View File
@@ -372,11 +372,8 @@ public:
const auto &conf = config.GetModule(this);
std::set<Anope::string> existing;
for (int i = 0; i < conf.CountBlock("httpd"); ++i)
for (const auto &[_, block] : conf.GetBlocks("httpd"))
{
const auto &block = conf.GetBlock("httpd", i);
const Anope::string &hname = block.Get<const Anope::string>("name", "httpd/main");
existing.insert(hname);
+5 -3
View File
@@ -330,9 +330,9 @@ public:
command_data.clear();
items_by_priority.clear();
for (int i = 0; i < conf.CountBlock("command"); ++i)
time_t default_priority = 0;
for (const auto &[_, block] : conf.GetBlocks("command"))
{
const auto &block = conf.GetBlock("command", i);
const Anope::string &cmd = block.Get<const Anope::string>("command");
if (cmd != "nickserv/set/misc" && cmd != "nickserv/saset/misc")
continue;
@@ -353,6 +353,8 @@ public:
continue;
}
default_priority += 1000;
data.set_description = desc;
data.pattern = block.Get<const Anope::string>("misc_pattern");
data.syntax = block.Get<const Anope::string>("misc_syntax");
@@ -361,7 +363,7 @@ public:
if (data.priority <= 0)
{
// If no priority is specified, go by order processed
data.priority = i * 1000;
data.priority = default_priority;
}
data.swhois = block.Get<bool>("misc_swhois");
items_by_priority.emplace_back(data.priority, item);
+1 -2
View File
@@ -84,9 +84,8 @@ public:
lflist.AddColumn(_("Module Name")).AddColumn(_("Name")).AddColumn(_("Value"));
lflist.SetFlexible(_("\002{}{module_name}}:{name}\002 = {value}"));
for (int i = 0; i < Config->CountBlock("module"); ++i)
for (const auto &[_, block] : Config->GetBlocks("module"))
{
const auto &block = Config->GetBlock("module", i);
const auto &items = block.GetItems();
if (items.size() <= 1)
+1 -3
View File
@@ -529,10 +529,8 @@ public:
fileforbids.clear();
const auto &modconf = conf.GetModule(this);
for (auto i = 0; i < modconf.CountBlock("file"); ++i)
for (const auto &[_, fileblock] : modconf.GetBlocks("file"))
{
const auto &fileblock = modconf.GetBlock("file", i);
const auto reasonstr = fileblock.Get<const Anope::string>("reason");
const auto typestr = fileblock.Get<const Anope::string>("type");
+2 -2
View File
@@ -309,9 +309,9 @@ public:
}
this->proxyscans.clear();
for (int i = 0; i < config.CountBlock("proxyscan"); ++i)
for (const auto &[_, block] : config.GetBlocks("proxyscan"))
{
const auto &block = config.GetBlock("proxyscan", i);
ProxyCheck p;
Anope::string token;
+1 -3
View File
@@ -173,10 +173,8 @@ public:
{
Rewrite::rewrites.clear();
for (int i = 0; i < conf.CountBlock("command"); ++i)
for (const auto &[_, block] : conf.GetBlocks("command"))
{
const auto &block = conf.GetBlock("command", i);
if (!block.Get<bool>("rewrite"))
continue;
+2 -3
View File
@@ -313,10 +313,9 @@ public:
throw ConfigException("Unable to find http reference, is httpd loaded?");
jsonrpcinterface.tokens.clear();
for (int i = 0; i < modconf.CountBlock("token"); ++i)
{
const auto &block = modconf.GetBlock("token", i);
for (const auto &[_, block] : modconf.GetBlocks("token"))
{
RPC::Token token;
token.token = block.Get<const Anope::string>("token");
if (!token.token.empty())
+18 -35
View File
@@ -43,6 +43,11 @@ int Configuration::Block::CountBlock(const Anope::string &bname) const
return blocks.count(bname);
}
Configuration::Block::BlockList Configuration::Block::GetBlocks(const Anope::string &bname) const
{
return Anope::equal_range(blocks, bname);
}
const Configuration::Block &Configuration::Block::GetBlock(const Anope::string &bname, int num) const
{
auto it = blocks.equal_range(bname);
@@ -125,10 +130,8 @@ Configuration::Conf::Conf() : Configuration::Block("")
this->LoadConf(ServicesConf);
for (int i = 0; i < this->CountBlock("include"); ++i)
for (const auto &[_, include] : this->GetBlocks("include"))
{
const auto &include = this->GetBlock("include", i);
const Anope::string &type = include.Get<const Anope::string>("type"),
&file = include.Get<const Anope::string>("name");
@@ -205,10 +208,8 @@ Configuration::Conf::Conf() : Configuration::Block("")
this->TimeoutCheck = options.Get<time_t>("timeoutcheck");
this->NickChars = networkinfo.Get<Anope::string>("nick_chars");
for (int i = 0; i < this->CountBlock("uplink"); ++i)
for (const auto &[_, uplink] : this->GetBlocks("uplink"))
{
const auto &uplink = this->GetBlock("uplink", i);
int protocol;
const Anope::string &protocolstr = uplink.Get<const Anope::string>("protocol", "ipv4");
if (protocolstr == "ipv4")
@@ -238,10 +239,8 @@ Configuration::Conf::Conf() : Configuration::Block("")
this->Uplinks.emplace_back(host, port, password, protocol);
}
for (int i = 0; i < this->CountBlock("module"); ++i)
for (const auto &[_, module] : this->GetBlocks("module"))
{
const auto &module = this->GetBlock("module", i);
const Anope::string &modname = module.Get<const Anope::string>("name");
ValidateNotEmptyOrSpaces("module", "name", modname);
@@ -249,10 +248,8 @@ Configuration::Conf::Conf() : Configuration::Block("")
this->ModulesAutoLoad.push_back(modname);
}
for (int i = 0; i < this->CountBlock("opertype"); ++i)
for (const auto &[_, opertype] : this->GetBlocks("opertype"))
{
const auto &opertype = this->GetBlock("opertype", i);
const Anope::string &oname = opertype.Get<const Anope::string>("name"),
&modes = opertype.Get<const Anope::string>("modes"),
&inherits = opertype.Get<const Anope::string>("inherits"),
@@ -292,10 +289,8 @@ Configuration::Conf::Conf() : Configuration::Block("")
this->MyOperTypes.push_back(ot);
}
for (int i = 0; i < this->CountBlock("oper"); ++i)
for (const auto &[_, oper] : this->GetBlocks("oper"))
{
const auto &oper = this->GetBlock("oper", i);
const Anope::string &nname = oper.Get<const Anope::string>("name"),
&type = oper.Get<const Anope::string>("type"),
&password = oper.Get<const Anope::string>("password"),
@@ -330,10 +325,9 @@ Configuration::Conf::Conf() : Configuration::Block("")
for (const auto &[_, bi] : *BotListByNick)
bi->conf = false;
for (int i = 0; i < this->CountBlock("service"); ++i)
{
const auto &service = this->GetBlock("service", i);
for (const auto &[_, service] : this->GetBlocks("service"))
{
const Anope::string &nick = service.Get<const Anope::string>("nick"),
&user = service.Get<const Anope::string>("user", nick.lower()),
&host = service.Get<const Anope::string>("host", servername),
@@ -421,10 +415,8 @@ Configuration::Conf::Conf() : Configuration::Block("")
}
}
for (int i = 0; i < this->CountBlock("log"); ++i)
for (const auto &[_, log] : this->GetBlocks("log"))
{
const auto &log = this->GetBlock("log", i);
int logage = log.Get<int>("logage");
bool rawio = log.Get<bool>("rawio");
bool debug = log.Get<bool>("debug");
@@ -447,10 +439,8 @@ Configuration::Conf::Conf() : Configuration::Block("")
for (const auto &[_, bi] : *BotListByNick)
bi->commands.clear();
for (int i = 0; i < this->CountBlock("command"); ++i)
for (const auto &[_, command] : this->GetBlocks("command"))
{
const auto &command = this->GetBlock("command", i);
const Anope::string &service = command.Get<const Anope::string>("service"),
&nname = command.Get<const Anope::string>("name"),
&cmd = command.Get<const Anope::string>("command"),
@@ -472,10 +462,8 @@ Configuration::Conf::Conf() : Configuration::Block("")
}
PrivilegeManager::ClearPrivileges();
for (int i = 0; i < this->CountBlock("privilege"); ++i)
for (const auto &[_, privilege] : this->GetBlocks("privilege"))
{
const auto &privilege = this->GetBlock("privilege", i);
const Anope::string &nname = privilege.Get<const Anope::string>("name"),
&desc = privilege.Get<const Anope::string>("desc");
int rank = privilege.Get<int>("rank");
@@ -483,10 +471,8 @@ Configuration::Conf::Conf() : Configuration::Block("")
PrivilegeManager::AddPrivilege(Privilege(nname, desc, rank));
}
for (int i = 0; i < this->CountBlock("fantasy"); ++i)
for (const auto &[_, fantasy] : this->GetBlocks("fantasy"))
{
const auto &fantasy = this->GetBlock("fantasy", i);
const Anope::string &nname = fantasy.Get<const Anope::string>("name"),
&service = fantasy.Get<const Anope::string>("command"),
&permission = fantasy.Get<const Anope::string>("permission"),
@@ -504,10 +490,8 @@ Configuration::Conf::Conf() : Configuration::Block("")
c.require_privilege = fantasy.Get<bool>("require_privilege", "yes");
}
for (int i = 0; i < this->CountBlock("command_group"); ++i)
for (const auto &[_, command_group] : this->GetBlocks("command_group"))
{
const auto &command_group = this->GetBlock("command_group", i);
const Anope::string &nname = command_group.Get<const Anope::string>("name"),
&description = command_group.Get<const Anope::string>("description");
@@ -1020,9 +1004,8 @@ Anope::string Configuration::Conf::ReplaceVars(const Anope::string &str, const C
}
auto found = false;
for (int i = 0; i < this->CountBlock("define"); ++i)
for (const auto &[_, define] : this->GetBlocks("define"))
{
const auto &define = this->GetBlock("define", i);
const auto defname = define.Get<const Anope::string>("name");
if (defname == var)
{
+2 -2
View File
@@ -537,8 +537,8 @@ bool Anope::Init(int ac, char **av)
/* load modules */
Log() << "Loading modules...";
for (int i = 0; i < Config->CountBlock("module"); ++i)
ModuleManager::LoadModule(Config->GetBlock("module", i).Get<const Anope::string>("name"), NULL);
for (const auto &[_, block] : Config->GetBlocks("module"))
ModuleManager::LoadModule(block.Get<const Anope::string>("name"), NULL);
#ifndef _WIN32
/* If we're root, issue a warning now */