diff options
Diffstat (limited to 'src/misc.cpp')
-rw-r--r-- | src/misc.cpp | 208 |
1 files changed, 105 insertions, 103 deletions
diff --git a/src/misc.cpp b/src/misc.cpp index 2c879365d..2396ca134 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -1,12 +1,20 @@ -/* Miscellaneous routines. +/* + * Anope IRC Services * - * (C) 2003-2017 Anope Team - * Contact us at team@anope.org + * Copyright (C) 2003-2017 Anope Team <team@anope.org> * - * Please read COPYING and README for further details. + * This file is part of Anope. Anope is free software; you can + * redistribute it and/or modify it under the terms of the GNU + * General Public License as published by the Free Software + * Foundation, version 2. * - * Based on the original code of Epona by Lara. - * Based on the original code of Services by Andy Church. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see see <http://www.gnu.org/licenses/>. */ #include "services.h" @@ -16,8 +24,8 @@ #include "config.h" #include "bots.h" #include "language.h" -#include "regexpr.h" #include "sockets.h" +#include "event.h" #include <errno.h> #include <sys/types.h> @@ -27,11 +35,13 @@ #include <netdb.h> #endif -NumberList::NumberList(const Anope::string &list, bool descending) : is_valid(true), desc(descending) +NumberList::NumberList(const Anope::string &list, bool descending, std::function<void(unsigned int)> nf, std::function<void(void)> ef) : endf(ef) { Anope::string error; commasepstream sep(list); Anope::string token; + bool is_valid = true; + std::set<unsigned> numbers; sep.GetToken(token); if (token.empty()) @@ -55,11 +65,8 @@ NumberList::NumberList(const Anope::string &list, bool descending) : is_valid(tr if (!error.empty()) { - if (!this->InvalidRange(list)) - { - is_valid = false; - return; - } + is_valid = false; + return; } } else @@ -80,47 +87,27 @@ NumberList::NumberList(const Anope::string &list, bool descending) : is_valid(tr if (!error.empty() || !error2.empty()) { - if (!this->InvalidRange(list)) - { - is_valid = false; - return; - } + is_valid = false; + return; } } } while (sep.GetToken(token)); -} - -NumberList::~NumberList() -{ -} -void NumberList::Process() -{ if (!is_valid) return; - if (this->desc) - { - for (std::set<unsigned>::reverse_iterator it = numbers.rbegin(), it_end = numbers.rend(); it != it_end; ++it) - this->HandleNumber(*it); - } + if (descending) + std::for_each(numbers.rbegin(), numbers.rend(), nf); else - { - for (std::set<unsigned>::iterator it = numbers.begin(), it_end = numbers.end(); it != it_end; ++it) - this->HandleNumber(*it); - } -} - -void NumberList::HandleNumber(unsigned) -{ + std::for_each(numbers.begin(), numbers.end(), nf); } -bool NumberList::InvalidRange(const Anope::string &) +NumberList::~NumberList() { - return true; + endf(); } -ListFormatter::ListFormatter(NickCore *acc) : nc(acc) +ListFormatter::ListFormatter(NickServ::Account *acc) : nc(acc) { } @@ -160,14 +147,15 @@ void ListFormatter::Process(std::vector<Anope::string> &buffer) unsigned length = 0; for (std::map<Anope::string, size_t>::iterator it = lenghts.begin(), it_end = lenghts.end(); it != it_end; ++it) { - /* Break lines at 80 chars */ - if (length > 80) + if (length > Config->LineWrap) { breaks.insert(it->first); length = 0; } else + { length += it->second; + } } /* Only put a list header if more than 1 column */ @@ -214,7 +202,7 @@ void ListFormatter::Process(std::vector<Anope::string> &buffer) } } -InfoFormatter::InfoFormatter(NickCore *acc) : nc(acc), longest(0) +InfoFormatter::InfoFormatter(NickServ::Account *acc) : nc(acc), longest(0) { } @@ -311,7 +299,7 @@ time_t Anope::DoTime(const Anope::string &s) return amount; } -Anope::string Anope::Duration(time_t t, const NickCore *nc) +Anope::string Anope::Duration(time_t t, NickServ::Account *nc) { /* We first calculate everything */ time_t years = t / 31536000; @@ -352,7 +340,7 @@ Anope::string Anope::Duration(time_t t, const NickCore *nc) } } -Anope::string Anope::strftime(time_t t, const NickCore *nc, bool short_output) +Anope::string Anope::strftime(time_t t, NickServ::Account *nc, bool short_output) { tm tm = *localtime(&t); char buf[BUFSIZE]; @@ -367,10 +355,10 @@ Anope::string Anope::strftime(time_t t, const NickCore *nc, bool short_output) return Anope::string(buf) + " " + Language::Translate(nc, _("(now)")); } -Anope::string Anope::Expires(time_t expires, const NickCore *nc) +Anope::string Anope::Expires(time_t expires, NickServ::Account *nc) { if (!expires) - return Language::Translate(nc, NO_EXPIRE); + return Language::Translate(nc, _("does not expire")); else if (expires <= Anope::CurTime) return Language::Translate(nc, _("expires momentarily")); else @@ -407,38 +395,29 @@ bool Anope::Match(const Anope::string &str, const Anope::string &mask, bool case { size_t s = 0, m = 0, str_len = str.length(), mask_len = mask.length(); - if (use_regex && mask_len >= 2 && mask[0] == '/' && mask[mask.length() - 1] == '/') + if (use_regex && Config->regex_flags && mask_len >= 2 && mask[0] == '/' && mask[mask.length() - 1] == '/') { Anope::string stripped_mask = mask.substr(1, mask_len - 2); // This is often called with the same mask multiple times in a row, so cache it - static Regex *r = NULL; + static Anope::string pattern; + static std::regex r; - if (r == NULL || r->GetExpression() != stripped_mask) + if (pattern != stripped_mask) { - ServiceReference<RegexProvider> provider("Regex", Config->GetBlock("options")->Get<const Anope::string>("regexengine")); - if (provider) + try { - try - { - delete r; - r = NULL; - // This may throw - r = provider->Compile(stripped_mask); - } - catch (const RegexException &ex) - { - Log(LOG_DEBUG) << ex.GetReason(); - } + r.assign(stripped_mask.str(), Config->regex_flags); + pattern = stripped_mask; } - else + catch (const std::regex_error &error) { - delete r; - r = NULL; + Anope::Logger.Debug(error.what()); } } - if (r != NULL && r->Matches(str)) - return true; + if (pattern == stripped_mask) + if (std::regex_search(str.str(), r)) + return true; // Fall through to non regex match } @@ -509,29 +488,10 @@ bool Anope::Match(const Anope::string &str, const Anope::string &mask, bool case void Anope::Encrypt(const Anope::string &src, Anope::string &dest) { - EventReturn MOD_RESULT; - FOREACH_RESULT(OnEncrypt, MOD_RESULT, (src, dest)); + EventReturn MOD_RESULT = EventManager::Get()->Dispatch(&Event::Encrypt::OnEncrypt, src, dest); static_cast<void>(MOD_RESULT); } -bool Anope::Decrypt(const Anope::string &src, Anope::string &dest) -{ - size_t pos = src.find(':'); - if (pos == Anope::string::npos) - { - Log() << "Error: Anope::Decrypt() called with invalid password string (" << src << ")"; - return false; - } - Anope::string hashm(src.begin(), src.begin() + pos); - - EventReturn MOD_RESULT; - FOREACH_RESULT(OnDecrypt, MOD_RESULT, (hashm, src, dest)); - if (MOD_RESULT == EVENT_ALLOW) - return true; - - return false; -} - Anope::string Anope::printf(const char *fmt, ...) { va_list args; @@ -619,40 +579,44 @@ const Anope::string Anope::LastError() Anope::string Anope::Version() { #ifdef VERSION_GIT - return stringify(VERSION_MAJOR) + "." + stringify(VERSION_MINOR) + "." + stringify(VERSION_PATCH) + VERSION_EXTRA + " (" + VERSION_GIT + ")"; + return Anope::Format("{0}.{1}.{2}{3}-{4}", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_EXTRA, VERSION_GIT); #else - return stringify(VERSION_MAJOR) + "." + stringify(VERSION_MINOR) + "." + stringify(VERSION_PATCH) + VERSION_EXTRA; + return Anope::Format("{0}.{1}.{2}{3}", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_EXTRA); #endif } Anope::string Anope::VersionShort() { - return stringify(VERSION_MAJOR) + "." + stringify(VERSION_MINOR) + "." + stringify(VERSION_PATCH); + return Anope::Format("{0}.{1}.{2}", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); } -Anope::string Anope::VersionBuildString() +Anope::string Anope::VersionBuildTime() { #ifdef REPRODUCIBLE_BUILD - Anope::string s = "build #" + stringify(BUILD); + return "unknown"; #else - Anope::string s = "build #" + stringify(BUILD) + ", compiled " + Anope::compiled; + return Anope::COMPILED; #endif +} + +Anope::string Anope::VersionFlags() +{ Anope::string flags; #ifdef DEBUG_BUILD - flags += "D"; + flags.append('D'); #endif #ifdef VERSION_GIT - flags += "G"; + flags.append('G'); #endif #ifdef _WIN32 - flags += "W"; + flags.append('W'); #endif - if (!flags.empty()) - s += ", flags " + flags; + if (flags.empty()) + flags = "none"; - return s; + return flags; } int Anope::VersionMajor() { return VERSION_MAJOR; } @@ -733,7 +697,7 @@ Anope::string Anope::Resolve(const Anope::string &host, int type) memset(&hints, 0, sizeof(hints)); hints.ai_family = type; - Log(LOG_DEBUG_2) << "Resolver: BlockingQuery: Looking up " << host; + Anope::Logger.Debug2("Resolver: BlockingQuery: Looking up {0}", host); addrinfo *addrresult = NULL; if (getaddrinfo(host.c_str(), NULL, &hints, &addrresult) == 0) @@ -741,7 +705,7 @@ Anope::string Anope::Resolve(const Anope::string &host, int type) sockaddrs addr; memcpy(&addr, addrresult->ai_addr, addrresult->ai_addrlen); result = addr.addr(); - Log(LOG_DEBUG_2) << "Resolver: " << host << " -> " << result; + Anope::Logger.Debug2("Resolver: {0} -> {1}", host, result); freeaddrinfo(addrresult); } @@ -763,3 +727,41 @@ Anope::string Anope::Random(size_t len) buf.append(chars[rand() % sizeof(chars)]); return buf; } + +const kwarg *FormatInfo::GetKwarg(const Anope::string &name) const +{ + for (const kwarg &kw : parameters) + if (kw.name == name) + return &kw; + return nullptr; +} + +void FormatInfo::Format() +{ + size_t start = 0; + size_t s = format.find('{', start); + + while (s != Anope::string::npos) + { + size_t e = format.find('}', s + 1); + if (e == Anope::string::npos) + break; + + Anope::string key = format.substr(s + 1, e - s - 1); + + // Find replacement for key + const kwarg *arg = GetKwarg(key); + + format.erase(s, e - s + 1); + if (arg != nullptr) + format.insert(s, arg->value); + + start = s + (arg != nullptr ? arg->value.length() : 0); + s = format.find('{', start); + } +} + +const Anope::string &FormatInfo::GetFormat() const +{ + return format; +} |