mirror of
https://github.com/anope/anope.git
synced 2026-07-01 14:46:39 +02:00
Make config variables a lot more useful.
- Config variables now no longer conflict with regular values.
- Config variables can now be read from the environment.
(e.g. ${env.USER}).
- Config variables can now be used as partial values
(e.g. support@${network.domain})
This commit is contained in:
@@ -79,13 +79,16 @@
|
||||
/*
|
||||
* [OPTIONAL] Defines
|
||||
*
|
||||
* You can define values to other values, which can be used to easily change
|
||||
* many values in the configuration at once.
|
||||
* You can use defines for repeated information, which can be used to easily change many
|
||||
* values in the configuration at once.
|
||||
*
|
||||
* To use a define called foo.bar you use ${foo.bar} in your config file. You can also use
|
||||
* environment variables by prefixing their name with "env." like ${env.USER}.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The services.host define is used in multiple different locations throughout the
|
||||
* configuration for services clients hostnames.
|
||||
* configuration for the server name and pseudoclient hostnames.
|
||||
*/
|
||||
define
|
||||
{
|
||||
@@ -211,7 +214,7 @@ serverinfo
|
||||
* other server names on the rest of your IRC network. Note that it does not have
|
||||
* to be an existing hostname, just one that isn't on your network already.
|
||||
*/
|
||||
name = "services.example.com"
|
||||
name = "${services.host}"
|
||||
|
||||
/*
|
||||
* The text which should appear as the server's information in /WHOIS and similar
|
||||
|
||||
@@ -31,7 +31,7 @@ service
|
||||
/*
|
||||
* The hostname of the BotServ client.
|
||||
*/
|
||||
host = "services.host"
|
||||
host = "${services.host}"
|
||||
|
||||
/*
|
||||
* The realname of the BotServ client.
|
||||
|
||||
@@ -21,7 +21,7 @@ service
|
||||
/*
|
||||
* The hostname of the ChanServ client.
|
||||
*/
|
||||
host = "services.host"
|
||||
host = "${services.host}"
|
||||
|
||||
/*
|
||||
* The realname of the ChanServ client.
|
||||
|
||||
@@ -21,7 +21,7 @@ service
|
||||
/*
|
||||
* The hostname of the Global client.
|
||||
*/
|
||||
host = "services.host"
|
||||
host = "${services.host}"
|
||||
|
||||
/*
|
||||
* The realname of the Global client.
|
||||
|
||||
@@ -21,7 +21,7 @@ service
|
||||
/*
|
||||
* The hostname of the HostServ client.
|
||||
*/
|
||||
host = "services.host"
|
||||
host = "${services.host}"
|
||||
|
||||
/*
|
||||
* The realname of the HostServ client.
|
||||
|
||||
@@ -18,7 +18,7 @@ service
|
||||
/*
|
||||
* The hostname of the StatServ client.
|
||||
*/
|
||||
host = "stats.host"
|
||||
host = "${services.host}"
|
||||
|
||||
/*
|
||||
* The realname of the StatServ client.
|
||||
|
||||
@@ -21,7 +21,7 @@ service
|
||||
/*
|
||||
* The hostname of the MemoServ client.
|
||||
*/
|
||||
host = "services.host"
|
||||
host = "${services.host}"
|
||||
|
||||
/*
|
||||
* The realname of the MemoServ client.
|
||||
|
||||
@@ -21,7 +21,7 @@ service
|
||||
/*
|
||||
* The hostname of the NickServ client.
|
||||
*/
|
||||
host = "services.host"
|
||||
host = "${services.host}"
|
||||
|
||||
/*
|
||||
* The realname of the NickServ client.
|
||||
@@ -210,7 +210,7 @@ module
|
||||
* hold a nickname.
|
||||
*/
|
||||
enforceruser = "enforcer"
|
||||
enforcerhost = "services.host"
|
||||
enforcerhost = "${services.host}"
|
||||
|
||||
/*
|
||||
* The length of time Anope should hold nicknames for.
|
||||
|
||||
@@ -21,7 +21,7 @@ service
|
||||
/*
|
||||
* The hostname of the OperServ client.
|
||||
*/
|
||||
host = "services.host"
|
||||
host = "${services.host}"
|
||||
|
||||
/*
|
||||
* The realname of the OperServ client.
|
||||
|
||||
@@ -79,17 +79,20 @@
|
||||
/*
|
||||
* [OPTIONAL] Defines
|
||||
*
|
||||
* You can define values to other values, which can be used to easily change
|
||||
* many values in the configuration at once.
|
||||
* You can use defines for repeated information, which can be used to easily change many
|
||||
* values in the configuration at once.
|
||||
*
|
||||
* To use a define called foo.bar you use ${foo.bar} in your config file. You can also use
|
||||
* environment variables by prefixing their name with "env." like ${env.USER}.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The stats.host define is used in multiple different locations throughout the
|
||||
* configuration for the stats client hostname.
|
||||
* The services.host define is used in multiple different locations throughout the
|
||||
* configuration for the server name and pseudoclient hostnames.
|
||||
*/
|
||||
define
|
||||
{
|
||||
name = "stats.host"
|
||||
name = "services.host"
|
||||
value = "stats.example.com"
|
||||
}
|
||||
|
||||
@@ -208,7 +211,7 @@ serverinfo
|
||||
* other server names on the rest of your IRC network. Note that it does not have
|
||||
* to be an existing hostname, just one that isn't on your network already.
|
||||
*/
|
||||
name = "stats.example.com"
|
||||
name = "${services.host}"
|
||||
|
||||
/*
|
||||
* The text which should appear as the server's information in /WHOIS and similar
|
||||
|
||||
+7
-2
@@ -83,9 +83,14 @@ namespace Configuration
|
||||
|
||||
struct Uplink;
|
||||
|
||||
struct CoreExport Conf final
|
||||
: Block
|
||||
class CoreExport Conf final
|
||||
: public Block
|
||||
{
|
||||
private:
|
||||
/** Replaces defined variables within a string. */
|
||||
Anope::string ReplaceVars(const Anope::string &str, const File &file, int linenumber);
|
||||
|
||||
public:
|
||||
/* options:readtimeout */
|
||||
time_t ReadTimeout;
|
||||
/* If we should default to privmsging clients */
|
||||
|
||||
+51
-14
@@ -939,24 +939,12 @@ void Conf::LoadConf(File &file)
|
||||
}
|
||||
|
||||
Block *b = block_stack.top();
|
||||
|
||||
if (b)
|
||||
Log(LOG_DEBUG) << "ln " << linenumber << " EOL: s='" << b->name << "' '" << itemname << "' set to '" << wordbuffer << "'";
|
||||
|
||||
/* Check defines */
|
||||
for (int i = 0; i < this->CountBlock("define"); ++i)
|
||||
{
|
||||
const Block &define = this->GetBlock("define", i);
|
||||
|
||||
const Anope::string &dname = define.Get<const Anope::string>("name");
|
||||
|
||||
if (dname == wordbuffer && &define != b)
|
||||
wordbuffer = define.Get<const Anope::string>("value");
|
||||
Log(LOG_DEBUG) << "ln " << linenumber << " EOL: s='" << b->name << "' '" << itemname << "' set to '" << wordbuffer << "'";
|
||||
b->items[itemname] = ReplaceVars(wordbuffer, file, linenumber);
|
||||
}
|
||||
|
||||
if (b)
|
||||
b->items[itemname] = wordbuffer;
|
||||
|
||||
wordbuffer.clear();
|
||||
itemname.clear();
|
||||
}
|
||||
@@ -991,3 +979,52 @@ void Conf::LoadConf(File &file)
|
||||
throw ConfigException("Unterminated commented block at end of file: " + file.GetName());
|
||||
}
|
||||
}
|
||||
|
||||
Anope::string Conf::ReplaceVars(const Anope::string &str, const File &file, int linenumber)
|
||||
{
|
||||
Anope::string ret;
|
||||
for (auto it = str.begin(); it != str.end(); )
|
||||
{
|
||||
if (*it != '$')
|
||||
{
|
||||
ret.push_back(*it++);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (++it == str.end() || *it != '{')
|
||||
continue;
|
||||
|
||||
it++;
|
||||
Anope::string var;
|
||||
while (it != str.end() && *it != '}')
|
||||
var.push_back(*it++);
|
||||
|
||||
if (it == str.end())
|
||||
throw ConfigException("Unterminated variable: " + file.GetName() + ":" + Anope::ToString(linenumber));
|
||||
|
||||
it++;
|
||||
if (var.compare(0, 4, "env.", 4) == 0)
|
||||
{
|
||||
// This is an environment variable rather than a defined variable
|
||||
const char* envstr = getenv(var.c_str() + 4);
|
||||
if (envstr && envstr)
|
||||
ret.append(envstr);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int i = 0; i < this->CountBlock("define"); ++i)
|
||||
{
|
||||
const auto &define = this->GetBlock("define", i);
|
||||
const auto defname = define.Get<const Anope::string>("name");
|
||||
if (defname == var)
|
||||
{
|
||||
ret.append(define.Get<const Anope::string>("value"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!str.equals_cs(ret))
|
||||
Log(LOG_DEBUG) << "Expanded \"" << str << "\" to \"" << ret << "\"";
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user