diff options
Diffstat (limited to 'modules/commands/os_logsearch.cpp')
-rw-r--r-- | modules/commands/os_logsearch.cpp | 47 |
1 files changed, 38 insertions, 9 deletions
diff --git a/modules/commands/os_logsearch.cpp b/modules/commands/os_logsearch.cpp index f8ee99d4e..dfd67a492 100644 --- a/modules/commands/os_logsearch.cpp +++ b/modules/commands/os_logsearch.cpp @@ -1,6 +1,6 @@ /* OperServ core functions * - * (C) 2003-2016 Anope Team + * (C) 2003-2017 Anope Team * Contact us at team@anope.org * * Please read COPYING and README for further details. @@ -11,6 +11,8 @@ #include "module.h" +static unsigned int HARDMAX = 65536; + class CommandOSLogSearch : public Command { static inline Anope::string CreateLogName(const Anope::string &file, time_t t = Anope::CurTime) @@ -18,7 +20,7 @@ class CommandOSLogSearch : public Command char timestamp[32]; tm *tm = localtime(&t); - + strftime(timestamp, sizeof(timestamp), "%Y%m%d", tm); return Anope::LogDir + "/" + file + "." + timestamp; @@ -89,8 +91,11 @@ class CommandOSLogSearch : public Command Log(LOG_ADMIN, source, this) << "for " << search_string; + bool wildcard = search_string.find_first_of("?*") != Anope::string::npos; + bool regex = search_string.empty() == false && search_string[0] == '/' && search_string[search_string.length() - 1] == '/'; + const Anope::string &logfile_name = Config->GetModule(this->owner)->Get<const Anope::string>("logname"); - std::list<Anope::string> matches; + std::vector<Anope::string> matches; for (int d = days - 1; d >= 0; --d) { Anope::string lf_name = CreateLogName(logfile_name, Anope::CurTime - (d * 86400)); @@ -100,25 +105,49 @@ class CommandOSLogSearch : public Command continue; for (Anope::string buf, token; std::getline(fd, buf.str());) - if (Anope::Match(buf, "*" + search_string + "*")) + { + bool match = false; + + if (regex) + match = Anope::Match(buf, search_string, false, true); + else if (wildcard) + match = Anope::Match(buf, "*" + search_string + "*"); + else + match = buf.find_ci(search_string) != Anope::string::npos; + + if (match) + { matches.push_back(buf); + if (matches.size() >= HARDMAX) + break; + } + } + fd.close(); } - unsigned found = matches.size(); + unsigned int found = matches.size(); if (!found) { source.Reply(_("No matches for \002%s\002 found."), search_string.c_str()); return; } - while (matches.size() > static_cast<unsigned>(replies)) - matches.pop_front(); + if (matches.size() >= HARDMAX) + { + source.Reply(_("Too many results for \002%s\002."), search_string.c_str()); + return; + } + + if (matches.size() > static_cast<unsigned int>(replies)) + { + matches.erase(matches.begin(), matches.begin() + (matches.size() - static_cast<unsigned int>(replies))); + } source.Reply(_("Matches for \002%s\002:"), search_string.c_str()); - unsigned count = 0; - for (std::list<Anope::string>::iterator it = matches.begin(), it_end = matches.end(); it != it_end; ++it) + unsigned int count = 0; + for (std::vector<Anope::string>::iterator it = matches.begin(), it_end = matches.end(); it != it_end; ++it) source.Reply("#%d: %s", ++count, it->c_str()); source.Reply(_("Showed %d/%d matches for \002%s\002."), matches.size(), found, search_string.c_str()); } |