diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/config.cpp | 54 | ||||
-rw-r--r-- | src/misc.cpp | 55 |
2 files changed, 96 insertions, 13 deletions
diff --git a/src/config.cpp b/src/config.cpp index 302127b97..f59289c0b 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -158,6 +158,25 @@ Conf::Conf() : Block("") this->LoadConf(f); } + // Set env variables from defines + for (int i = 0; i < this->CountBlock("define"); ++i) + { + Block *define = this->GetBlock("define", i); + + const Anope::string &dname = define->Get<Anope::string>("name"), + &dvalue = define->Get<Anope::string>("value"); + + if (!dname.empty() && !dvalue.empty()) + { + if (setenv(dname.c_str(), dvalue.c_str(), 1) == -1) + { + Anope::Logger.Log("Unable to setenv {0}: {1}", dname, Anope::LastError()); + } + } + } + + ProcessEnvVars(this); + for (Module *m : ModuleManager::Modules) m->OnReload(this); @@ -617,6 +636,26 @@ void Conf::Post(Conf *old) } } +void Conf::ProcessEnvVars(Block *block) +{ + for (auto it = block->items.begin(); it != block->items.end(); ++it) + { + Anope::string &item = it->second; + Anope::string replaced = Anope::ProcessEnvVars(item); + if (item != replaced) + { + Anope::Logger.Debug("Processed conf value \"{0}\" -> \"{1}\"", + item, replaced); + item = replaced; + } + } + for (auto it = block->blocks.begin(); it != block->blocks.end(); ++it) + { + Block *b = &it->second; + ProcessEnvVars(b); + } +} + void Conf::LoadBots() { for (BotInfo *bi : Serialize::GetObjects<BotInfo *>()) @@ -1059,21 +1098,10 @@ void Conf::LoadConf(File &file) Block *b = block_stack.top(); if (b) - Anope::Logger.Debug("ln {0} EOL: s='{1}' '{2}' set to '{3}'", linenumber, b->name, itemname, wordbuffer); - - /* Check defines */ - for (int i = 0; i < this->CountBlock("define"); ++i) { - Block *define = this->GetBlock("define", i); - - const Anope::string &dname = define->Get<Anope::string>("name"); - - if (dname == wordbuffer && define != b) - wordbuffer = define->Get<Anope::string>("value"); - } - - if (b) + Anope::Logger.Debug("ln {0} EOL: s='{1}' '{2}' set to '{3}'", linenumber, b->name, itemname, wordbuffer); b->items[itemname] = wordbuffer; + } wordbuffer.clear(); itemname.clear(); diff --git a/src/misc.cpp b/src/misc.cpp index 2396ca134..c8fd6dbbe 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -765,3 +765,58 @@ const Anope::string &FormatInfo::GetFormat() const { return format; } + +Anope::string Anope::ProcessEnvVars(const Anope::string &in) +{ + Anope::string out; + size_t pos = 0; + for (;;) + { + size_t start = in.find("${", pos); + if (start == Anope::string::npos) + { + // copy from pos + out.append(in.substr(pos)); + break; + } + + if (start > 0 && in[start - 1] == '\\') + { + // literal ${ + out.append(in.substr(pos, start - pos - 1)); + out.append("${"); + pos = start + 2; + continue; + } + + // copy from pos to start + out.append(in.substr(pos, start - pos)); + + size_t end = in.find("}", start); + if (end == Anope::string::npos) + break; + + Anope::string name, def; + + size_t mid = in.find(":-", pos); + if (mid != Anope::string::npos && mid < end) + { + name = in.substr(start + 2, mid - start - 2); + def = in.substr(mid + 2, end - mid - 2); + } + else + { + name = in.substr(start + 2, end - start - 2); + } + + const char *env = getenv(name.c_str()); + if (env) + out.append(env); + else + out.append(def); + + pos = end + 1; + } + + return out; +} |