summaryrefslogtreecommitdiff
path: root/src/config.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.cpp')
-rw-r--r--src/config.cpp188
1 files changed, 105 insertions, 83 deletions
diff --git a/src/config.cpp b/src/config.cpp
index 3783e1285..18dea1b6a 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -17,11 +17,14 @@
#include "channels.h"
#include "hashcomp.h"
+#include <stack>
+#include <stdexcept>
+
using Configuration::File;
using Configuration::Conf;
using Configuration::Internal::Block;
-File ServicesConf("services.conf", false); // Services configuration file name
+File ServicesConf("anope.conf", false); // Configuration file name
Conf *Config = NULL;
Block Block::EmptyBlock("");
@@ -40,7 +43,7 @@ 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<block_map::const_iterator, block_map::const_iterator> it = blocks.equal_range(bname);
@@ -50,7 +53,7 @@ const Block* Block::GetBlock(const Anope::string &bname, int num) const
return &EmptyBlock;
}
-Block* Block::GetMutableBlock(const Anope::string &bname, int num)
+Block *Block::GetMutableBlock(const Anope::string &bname, int num)
{
std::pair<block_map::iterator, block_map::iterator> it = blocks.equal_range(bname);
@@ -66,12 +69,12 @@ bool Block::Set(const Anope::string &tag, const Anope::string &value)
return true;
}
-const Block::item_map* Block::GetItems() const
+const Block::item_map &Block::GetItems() const
{
- return &items;
+ return items;
}
-template<> const Anope::string Block::Get(const Anope::string &tag, const Anope::string& def) const
+template<> const Anope::string Block::Get(const Anope::string &tag, const Anope::string &def) const
{
Anope::map<Anope::string>::const_iterator it = items.find(tag);
if (it != items.end())
@@ -154,9 +157,11 @@ Conf::Conf() : Block("")
{"networkinfo", "chanlen"},
};
- for (unsigned i = 0; i < sizeof(noreload) / sizeof(noreload[0]); ++i)
- if (this->GetBlock(noreload[i].block)->Get<const Anope::string>(noreload[i].name) != Config->GetBlock(noreload[i].block)->Get<const Anope::string>(noreload[i].name))
- throw ConfigException("<" + noreload[i].block + ":" + noreload[i].name + "> can not be modified once set");
+ for (const auto &tag : noreload)
+ {
+ if (this->GetBlock(tag.block)->Get<const Anope::string>(tag.name) != Config->GetBlock(tag.block)->Get<const Anope::string>(tag.name))
+ throw ConfigException("<" + tag.block + ":" + tag.name + "> can not be modified once set");
+ }
}
const Block *serverinfo = this->GetBlock("serverinfo"), *options = this->GetBlock("options"),
@@ -176,18 +181,18 @@ Conf::Conf() : Block("")
ValidateNotZero("options", "readtimeout", options->Get<time_t>("readtimeout"));
ValidateNotZero("options", "warningtimeout", options->Get<time_t>("warningtimeout"));
- ValidateNotZero("networkinfo", "nicklen", networkinfo->Get<unsigned>("nicklen"));
- ValidateNotZero("networkinfo", "userlen", networkinfo->Get<unsigned>("userlen"));
- ValidateNotZero("networkinfo", "hostlen", networkinfo->Get<unsigned>("hostlen"));
- ValidateNotZero("networkinfo", "chanlen", networkinfo->Get<unsigned>("chanlen"));
+ ValidateNotZero("networkinfo", "nicklen", networkinfo->Get<unsigned>("nicklen", "1"));
+ ValidateNotZero("networkinfo", "userlen", networkinfo->Get<unsigned>("userlen", "1"));
+ ValidateNotZero("networkinfo", "hostlen", networkinfo->Get<unsigned>("hostlen", "1"));
+ ValidateNotZero("networkinfo", "chanlen", networkinfo->Get<unsigned>("chanlen", "1"));
spacesepstream(options->Get<const Anope::string>("ulineservers")).GetTokens(this->Ulines);
if (mail->Get<bool>("usemail"))
{
- Anope::string check[] = { "sendmailpath", "sendfrom", "registration_subject", "registration_message", "emailchange_subject", "emailchange_message", "memo_subject", "memo_message" };
- for (unsigned i = 0; i < sizeof(check) / sizeof(Anope::string); ++i)
- ValidateNotEmpty("mail", check[i], mail->Get<const Anope::string>(check[i]));
+ 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<const Anope::string>(field));
}
this->ReadTimeout = options->Get<time_t>("readtimeout");
@@ -207,19 +212,33 @@ Conf::Conf() : Block("")
{
const Block *uplink = this->GetBlock("uplink", i);
- const Anope::string &host = uplink->Get<const Anope::string>("host");
- bool ipv6 = uplink->Get<bool>("ipv6");
- int port = uplink->Get<int>("port");
- const Anope::string &password = uplink->Get<const Anope::string>("password");
+ int protocol;
+ const Anope::string &protocolstr = uplink->Get<const Anope::string>("protocol", "ipv4");
+ if (protocolstr == "ipv4")
+ protocol = AF_INET;
+ else if (protocolstr == "ipv6")
+ protocol = AF_INET6;
+ else if (protocolstr == "unix")
+ protocol = AF_UNIX;
+ else
+ throw ConfigException("uplink:protocol must be set to ipv4, ipv6, or unix");
+ const Anope::string &host = uplink->Get<const Anope::string>("host");
ValidateNotEmptyOrSpaces("uplink", "host", host);
- ValidateNotZero("uplink", "port", port);
- ValidateNotEmptyOrSpaces("uplink", "password", password);
- if (password.find(' ') != Anope::string::npos || password[0] == ':')
+ int port = 0;
+ if (protocol != AF_UNIX)
+ {
+ port = uplink->Get<int>("port");
+ ValidateNotZero("uplink", "port", port);
+ }
+
+ const Anope::string &password = uplink->Get<const Anope::string>("password");
+ ValidateNotEmptyOrSpaces("uplink", "password", password);
+ if (password[0] == ':')
throw ConfigException("uplink:password is not valid");
- this->Uplinks.push_back(Uplink(host, port, password, ipv6));
+ this->Uplinks.emplace_back(host, port, password, protocol);
}
for (int i = 0; i < this->CountBlock("module"); ++i)
@@ -245,7 +264,7 @@ Conf::Conf() : Block("")
ValidateNotEmpty("opertype", "name", oname);
- OperType *ot = new OperType(oname);
+ auto *ot = new OperType(oname);
ot->modes = modes;
spacesepstream cmdstr(commands);
@@ -262,10 +281,8 @@ Conf::Conf() : Block("")
/* Strip leading ' ' after , */
if (str.length() > 1 && str[0] == ' ')
str.erase(str.begin());
- for (unsigned j = 0; j < this->MyOperTypes.size(); ++j)
+ for (auto *ot2 : this->MyOperTypes)
{
- OperType *ot2 = this->MyOperTypes[j];
-
if (ot2->GetName().equals_ci(str))
{
Log() << "Inheriting commands and privs from " << ot2->GetName() << " to " << ot->GetName();
@@ -294,24 +311,26 @@ Conf::Conf() : Block("")
ValidateNotEmpty("oper", "type", type);
OperType *ot = NULL;
- for (unsigned j = 0; j < this->MyOperTypes.size(); ++j)
- if (this->MyOperTypes[j]->GetName() == type)
- ot = this->MyOperTypes[j];
+ for (auto *opertype : this->MyOperTypes)
+ {
+ if (opertype->GetName() == type)
+ ot = opertype;
+ }
if (ot == NULL)
throw ConfigException("Oper block for " + nname + " has invalid oper type " + type);
- Oper *o = new Oper(nname, ot);
+ auto *o = new Oper(nname, ot);
o->require_oper = require_oper;
o->password = password;
- o->certfp = certfp;
+ spacesepstream(certfp).GetTokens(o->certfp);
spacesepstream(host).GetTokens(o->hosts);
o->vhost = vhost;
this->Opers.push_back(o);
}
- for (botinfo_map::const_iterator it = BotListByNick->begin(), it_end = BotListByNick->end(); it != it_end; ++it)
- it->second->conf = false;
+ for (const auto &[_, bi] : *BotListByNick)
+ bi->conf = false;
for (int i = 0; i < this->CountBlock("service"); ++i)
{
const Block *service = this->GetBlock("service", i);
@@ -359,28 +378,30 @@ Conf::Conf() : Block("")
/* Remove all existing modes */
ChanUserContainer *cu = c->FindUser(bi);
if (cu != NULL)
- for (size_t j = 0; j < cu->status.Modes().length(); ++j)
- c->RemoveMode(bi, ModeManager::FindChannelModeByChar(cu->status.Modes()[j]), bi->GetUID());
+ {
+ for (auto mode : cu->status.Modes())
+ c->RemoveMode(bi, ModeManager::FindChannelModeByChar(mode), bi->GetUID());
+ }
/* Set the new modes */
- for (unsigned j = 0; j < want_modes.length(); ++j)
+ for (char want_mode : want_modes)
{
- ChannelMode *cm = ModeManager::FindChannelModeByChar(want_modes[j]);
+ ChannelMode *cm = ModeManager::FindChannelModeByChar(want_mode);
if (cm == NULL)
- cm = ModeManager::FindChannelModeByChar(ModeManager::GetStatusChar(want_modes[j]));
+ cm = ModeManager::FindChannelModeByChar(ModeManager::GetStatusChar(want_mode));
if (cm && cm->type == MODE_STATUS)
c->SetMode(bi, cm, bi->GetUID());
}
}
- for (unsigned k = 0; k < oldchannels.size(); ++k)
+ for (const auto &oldchannel : oldchannels)
{
- size_t ch = oldchannels[k].find('#');
- Anope::string chname = oldchannels[k].substr(ch != Anope::string::npos ? ch : 0);
+ size_t ch = oldchannel.find('#');
+ Anope::string chname = oldchannel.substr(ch != Anope::string::npos ? ch : 0);
bool found = false;
- for (unsigned j = 0; j < bi->botchannels.size(); ++j)
+ for (const auto &botchannel : bi->botchannels)
{
- ch = bi->botchannels[j].find('#');
- Anope::string ochname = bi->botchannels[j].substr(ch != Anope::string::npos ? ch : 0);
+ ch = botchannel.find('#');
+ Anope::string ochname = botchannel.substr(ch != Anope::string::npos ? ch : 0);
if (chname.equals_ci(ochname))
found = true;
@@ -422,8 +443,8 @@ Conf::Conf() : Block("")
this->LogInfos.push_back(l);
}
- for (botinfo_map::const_iterator it = BotListByNick->begin(), it_end = BotListByNick->end(); it != it_end; ++it)
- it->second->commands.clear();
+ for (const auto &[_, bi] : *BotListByNick)
+ bi->commands.clear();
for (int i = 0; i < this->CountBlock("command"); ++i)
{
const Block *command = this->GetBlock("command", i);
@@ -500,17 +521,14 @@ Conf::Conf() : Block("")
if (Config)
/* Clear existing conf opers */
- for (nickcore_map::const_iterator it = NickCoreList->begin(), it_end = NickCoreList->end(); it != it_end; ++it)
+ for (const auto &[_, nc] : *NickCoreList)
{
- NickCore *nc = it->second;
if (nc->o && std::find(Config->Opers.begin(), Config->Opers.end(), nc->o) != Config->Opers.end())
nc->o = NULL;
}
/* Apply new opers */
- for (unsigned i = 0; i < this->Opers.size(); ++i)
+ for (auto *o : this->Opers)
{
- Oper *o = this->Opers[i];
-
NickAlias *na = NickAlias::Find(o->name);
if (!na)
continue;
@@ -542,29 +560,31 @@ Conf::Conf() : Block("")
}
}
Anope::CaseMapRebuild();
-
- /* Check the user keys */
- if (!options->Get<unsigned>("seed"))
- Log() << "Configuration option options:seed should be set. It's for YOUR safety! Remember that!";
}
Conf::~Conf()
{
- for (unsigned i = 0; i < MyOperTypes.size(); ++i)
- delete MyOperTypes[i];
- for (unsigned i = 0; i < Opers.size(); ++i)
- delete Opers[i];
+ for (const auto *opertype : MyOperTypes)
+ delete opertype;
+
+ for (const auto *oper : Opers)
+ delete oper;
}
void Conf::Post(Conf *old)
{
/* Apply module changes */
- for (unsigned i = 0; i < old->ModulesAutoLoad.size(); ++i)
- if (std::find(this->ModulesAutoLoad.begin(), this->ModulesAutoLoad.end(), old->ModulesAutoLoad[i]) == this->ModulesAutoLoad.end())
- ModuleManager::UnloadModule(ModuleManager::FindModule(old->ModulesAutoLoad[i]), NULL);
- for (unsigned i = 0; i < this->ModulesAutoLoad.size(); ++i)
- if (std::find(old->ModulesAutoLoad.begin(), old->ModulesAutoLoad.end(), this->ModulesAutoLoad[i]) == old->ModulesAutoLoad.end())
- ModuleManager::LoadModule(this->ModulesAutoLoad[i], NULL);
+ for (const auto &mod : old->ModulesAutoLoad)
+ {
+ if (std::find(this->ModulesAutoLoad.begin(), this->ModulesAutoLoad.end(), mod) == this->ModulesAutoLoad.end())
+ ModuleManager::UnloadModule(ModuleManager::FindModule(mod), NULL);
+ }
+
+ for (const auto &mod : this->ModulesAutoLoad)
+ {
+ if (std::find(old->ModulesAutoLoad.begin(), old->ModulesAutoLoad.end(), mod) == old->ModulesAutoLoad.end())
+ ModuleManager::LoadModule(mod, NULL);
+ }
/* Apply opertype changes, as non-conf opers still point to the old oper types */
for (unsigned i = Oper::opers.size(); i > 0; --i)
@@ -577,9 +597,11 @@ void Conf::Post(Conf *old)
OperType *ot = o->ot;
o->ot = NULL;
- for (unsigned j = 0; j < MyOperTypes.size(); ++j)
- if (ot->GetName() == MyOperTypes[j]->GetName())
- o->ot = MyOperTypes[j];
+ for (auto *opertype : MyOperTypes)
+ {
+ if (ot->GetName() == opertype->GetName())
+ o->ot = opertype;
+ }
if (o->ot == NULL)
{
@@ -612,7 +634,7 @@ Block *Conf::GetModule(const Anope::string &mname)
if (it != modules.end())
return it->second;
- Block* &block = modules[mname];
+ Block *&block = modules[mname];
/* Search for the block */
for (std::pair<block_map::iterator, block_map::iterator> iters = blocks.equal_range("module"); iters.first != iters.second; ++iters.first)
@@ -659,7 +681,7 @@ const Block *Conf::GetCommand(CommandSource &source)
return &Block::EmptyBlock;
}
-File::File(const Anope::string &n, bool e) : name(n), executable(e), fp(NULL)
+File::File(const Anope::string &n, bool e) : name(n), executable(e)
{
}
@@ -675,7 +697,7 @@ const Anope::string &File::GetName() const
Anope::string File::GetPath() const
{
- return (this->executable ? "" : Anope::ConfigDir + "/") + this->name;
+ return this->executable ? this->name : Anope::ExpandConfig(this->name);
}
bool File::IsOpen() const
@@ -686,7 +708,7 @@ bool File::IsOpen() const
bool File::Open()
{
this->Close();
- this->fp = (this->executable ? popen(this->name.c_str(), "r") : fopen((Anope::ConfigDir + "/" + this->name).c_str(), "r"));
+ this->fp = (this->executable ? popen(GetPath().c_str(), "r") : fopen(GetPath().c_str(), "r"));
return this->fp != NULL;
}
@@ -802,12 +824,12 @@ void Conf::LoadConf(File &file)
if (block_stack.empty() || itemname.empty())
{
file.Close();
- throw ConfigException("Unexpected quoted string: " + file.GetName() + ":" + stringify(linenumber));
+ throw ConfigException("Unexpected quoted string: " + file.GetName() + ":" + Anope::ToString(linenumber));
}
if (in_word || !wordbuffer.empty())
{
file.Close();
- throw ConfigException("Unexpected quoted string (prior unhandled words): " + file.GetName() + ":" + stringify(linenumber));
+ throw ConfigException("Unexpected quoted string (prior unhandled words): " + file.GetName() + ":" + Anope::ToString(linenumber));
}
in_quote = in_word = true;
}
@@ -816,13 +838,13 @@ void Conf::LoadConf(File &file)
if (block_stack.empty())
{
file.Close();
- throw ConfigException("Config item outside of section (or stray '='): " + file.GetName() + ":" + stringify(linenumber));
+ throw ConfigException("Config item outside of section (or stray '='): " + file.GetName() + ":" + Anope::ToString(linenumber));
}
if (!itemname.empty() || wordbuffer.empty())
{
file.Close();
- throw ConfigException("Stray '=' sign or item without value: " + file.GetName() + ":" + stringify(linenumber));
+ throw ConfigException("Stray '=' sign or item without value: " + file.GetName() + ":" + Anope::ToString(linenumber));
}
in_word = false;
@@ -848,7 +870,7 @@ void Conf::LoadConf(File &file)
}
Block *b = block_stack.empty() ? this : block_stack.top();
- block_map::iterator it = b->blocks.insert(std::make_pair(wordbuffer, Configuration::Block(wordbuffer)));
+ block_map::iterator it = b->blocks.emplace(wordbuffer, Configuration::Block(wordbuffer));
b = &it->second;
b->linenum = linenumber;
block_stack.push(b);
@@ -869,7 +891,7 @@ void Conf::LoadConf(File &file)
if (!in_word && !wordbuffer.empty())
{
file.Close();
- throw ConfigException("Unexpected word: " + file.GetName() + ":" + stringify(linenumber));
+ throw ConfigException("Unexpected word: " + file.GetName() + ":" + Anope::ToString(linenumber));
}
wordbuffer += ch;
in_word = true;
@@ -896,7 +918,7 @@ void Conf::LoadConf(File &file)
if (block_stack.empty())
{
file.Close();
- throw ConfigException("Stray ';' outside of block: " + file.GetName() + ":" + stringify(linenumber));
+ throw ConfigException("Stray ';' outside of block: " + file.GetName() + ":" + Anope::ToString(linenumber));
}
Block *b = block_stack.top();
@@ -927,7 +949,7 @@ void Conf::LoadConf(File &file)
if (block_stack.empty())
{
file.Close();
- throw ConfigException("Stray '}': " + file.GetName() + ":" + stringify(linenumber));
+ throw ConfigException("Stray '}': " + file.GetName() + ":" + Anope::ToString(linenumber));
}
block_stack.pop();
@@ -947,7 +969,7 @@ void Conf::LoadConf(File &file)
if (!block_stack.empty())
{
if (block_stack.top())
- throw ConfigException("Unterminated block at end of file: " + file.GetName() + ". Block was opened on line " + stringify(block_stack.top()->linenum));
+ throw ConfigException("Unterminated block at end of file: " + file.GetName() + ". Block was opened on line " + Anope::ToString(block_stack.top()->linenum));
else
throw ConfigException("Unterminated commented block at end of file: " + file.GetName());
}