From d96ca9b8240bcd854995946bdddc46632acc658a Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 22 Nov 2016 19:35:11 -0500 Subject: os_logsearch: add a hard max limit and make the limiter smarter --- modules/commands/os_logsearch.cpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'modules') diff --git a/modules/commands/os_logsearch.cpp b/modules/commands/os_logsearch.cpp index f8ee99d4e..37a2f887c 100644 --- a/modules/commands/os_logsearch.cpp +++ b/modules/commands/os_logsearch.cpp @@ -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) @@ -90,7 +92,7 @@ class CommandOSLogSearch : public Command Log(LOG_ADMIN, source, this) << "for " << search_string; const Anope::string &logfile_name = Config->GetModule(this->owner)->Get("logname"); - std::list matches; + std::vector matches; for (int d = days - 1; d >= 0; --d) { Anope::string lf_name = CreateLogName(logfile_name, Anope::CurTime - (d * 86400)); @@ -101,24 +103,37 @@ class CommandOSLogSearch : public Command for (Anope::string buf, token; std::getline(fd, buf.str());) if (Anope::Match(buf, "*" + search_string + "*")) + { 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(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(replies)) + { + matches.erase(matches.begin(), matches.begin() + (matches.size() - static_cast(replies))); + } source.Reply(_("Matches for \002%s\002:"), search_string.c_str()); - unsigned count = 0; - for (std::list::iterator it = matches.begin(), it_end = matches.end(); it != it_end; ++it) + unsigned int count = 0; + for (std::vector::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()); } -- cgit From 437a6dbb2997c51c260115e8514c8cd6150cfaf5 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 22 Nov 2016 19:44:25 -0500 Subject: os_logsearch: optimize non wildcard searches, allow regex --- modules/commands/os_logsearch.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'modules') diff --git a/modules/commands/os_logsearch.cpp b/modules/commands/os_logsearch.cpp index 37a2f887c..7b23e52d3 100644 --- a/modules/commands/os_logsearch.cpp +++ b/modules/commands/os_logsearch.cpp @@ -91,6 +91,9 @@ 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("logname"); std::vector matches; for (int d = days - 1; d >= 0; --d) @@ -102,13 +105,24 @@ 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_first_of_ci(search_string) != Anope::string::npos; + + if (match) { matches.push_back(buf); if (matches.size() >= HARDMAX) break; } + } fd.close(); } -- cgit