diff options
-rw-r--r-- | src/modules/extra/m_filter_pcre.cpp | 49 | ||||
-rw-r--r-- | src/modules/m_filter.cpp | 28 | ||||
-rw-r--r-- | src/modules/m_filter.h | 200 |
3 files changed, 219 insertions, 58 deletions
diff --git a/src/modules/extra/m_filter_pcre.cpp b/src/modules/extra/m_filter_pcre.cpp index e03d27b59..8d50193af 100644 --- a/src/modules/extra/m_filter_pcre.cpp +++ b/src/modules/extra/m_filter_pcre.cpp @@ -30,15 +30,16 @@ /* $ModDesc: m_filter with regexps */ /* $CompileFlags: `pcre-config --cflags` */ /* $LinkerFlags: `pcre-config --libs` `perl extra/pcre_rpath.pl` -lpcre */ -/* $ModDep: ../m_filter.h */ +/* $ModDep: m_filter.h */ class PCREFilter : public FilterResult { public: pcre* regexp; + std::string pattern; - PCREFilter(pcre* r, const std::string &rea, const std::string &act, long gline_time) - : FilterResult::FilterResult(rea, act, gline_time), regexp(r) + PCREFilter(pcre* r, const std::string &rea, const std::string &act, long gline_time, const std::string &pat) + : FilterResult::FilterResult(rea, act, gline_time), regexp(r), pattern(pat) { } }; @@ -74,7 +75,45 @@ class ModuleFilterPCRE : public FilterBase } return NULL; } - + + virtual bool DeleteFilter(const std::string &freeform) + { + for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++) + { + if (i->pattern == freeform) + { + filters.erase(i); + return true; + } + } + return false; + } + + virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration) + { + for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++) + { + if (i->pattern == freeform) + { + return std::make_pair(false, "Filter already exists"); + } + } + + re = pcre_compile(freeform.c_str(),0,&error,&erroffset,NULL); + + if (!re) + { + ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", freeform.c_str(), erroffset, error); + ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", freeform.c_str()); + return std::make_pair(false, "Error in regular expression at offset " + ConvToStr(erroffset) + ": "+error); + } + else + { + filters.push_back(PCREFilter(re, reason, type, duration, freeform)); + return std::make_pair(true, ""); + } + } + virtual void OnRehash(const std::string ¶meter) { ConfigReader MyConf(ServerInstance); @@ -100,7 +139,7 @@ class ModuleFilterPCRE : public FilterBase } else { - filters.push_back(PCREFilter(re, reason, action, gline_time)); + filters.push_back(PCREFilter(re, reason, action, gline_time, pattern)); ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str()); } } diff --git a/src/modules/m_filter.cpp b/src/modules/m_filter.cpp index 97cc8af5f..4942b7fb4 100644 --- a/src/modules/m_filter.cpp +++ b/src/modules/m_filter.cpp @@ -66,7 +66,33 @@ class ModuleFilter : public FilterBase } return NULL; } - + + virtual bool DeleteFilter(const std::string &freeform) + { + if (filters.find(freeform) != filters.end()) + { + filters.erase(filters.find(freeform)); + return true; + } + return false; + } + + virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration) + { + if (filters.find(freeform) != filters.end()) + { + return std::make_pair(false, "Filter already exists"); + } + + FilterResult* x = new FilterResult; + x->reason = reason; + x->action = type; + x->gline_time = duration; + filters[freeform] = x; + + return std::make_pair(true, ""); + } + virtual void OnRehash(const std::string ¶meter) { // this automatically re-reads the configuration file into the class diff --git a/src/modules/m_filter.h b/src/modules/m_filter.h index b2c3816a8..bd6320e3f 100644 --- a/src/modules/m_filter.h +++ b/src/modules/m_filter.h @@ -36,79 +36,175 @@ class FilterResult : public classbase } }; +class cmd_filter; + class FilterBase : public Module -{ +{ + cmd_filter* filtcommand; public: - FilterBase(InspIRCd* Me) - : Module::Module(Me) - { - } - - virtual ~FilterBase() - { - } - - virtual void Implements(char* List) - { - List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1; - } + FilterBase(InspIRCd* Me); + virtual ~FilterBase(); + virtual void Implements(char* List); + virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status); + virtual FilterResult* FilterMatch(const std::string &text) = 0; + virtual bool DeleteFilter(const std::string &freeform) = 0; + virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration) = 0; + virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status); + virtual void OnRehash(const std::string ¶meter); + virtual Version GetVersion(); +}; - virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status) +class cmd_filter : public command_t +{ + FilterBase* Base; + public: + cmd_filter(FilterBase* f, InspIRCd* Me) : command_t(Me, "FILTER", 'o', 1), Base(f) { - return OnUserPreNotice(user,dest,target_type,text,status); } - /* This must be implemented by the module which uses the header */ - virtual FilterResult* FilterMatch(const std::string &text) = 0; - - virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status) + CmdResult Handle(const char** parameters, int pcnt, userrec *user) { - FilterResult* f = this->FilterMatch(text); - if (f) + if (pcnt == 1) { - std::string target = ""; - if (target_type == TYPE_USER) + /* Deleting a filter */ + if (Base->DeleteFilter(parameters[0])) { - userrec* t = (userrec*)dest; - target = std::string(t->nick); + user->WriteServ("NOTICE %s :*** Deleted filter '%s'", user->nick, parameters[0]); + return CMD_SUCCESS; } - else if (target_type == TYPE_CHANNEL) + else { - chanrec* t = (chanrec*)dest; - target = std::string(t->name); + user->WriteServ("NOTICE %s :*** Filter '%s' not found on list.", user->nick, parameters[0]); + return CMD_FAILURE; } - if (f->action == "block") - { - ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their notice filtered, target was "+target+": "+f->reason); - user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+f->reason); - } - ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their notice filtered, target was ")+target+": "+f->reason+" Action: "+f->action); - - if (f->action == "kill") + } + else + { + /* Adding a filter */ + if (pcnt >= 3) { - userrec::QuitUser(ServerInstance,user,f->reason); - } + std::string freeform = parameters[0]; + std::string type = parameters[1]; + std::string reason; + long duration = 0; - if (f->action == "gline") - { - if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP())) + if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill")) + { + user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', and 'kill'.", user->nick, freeform.c_str()); + return CMD_FAILURE; + } + + if (type == "gline") + { + if (pcnt >= 4) + { + duration = ServerInstance->Duration(parameters[2]); + reason = parameters[3]; + } + else + { + this->TooFewParams(user, " When setting a gline type filter, a gline duration must be specified as the third parameter."); + return CMD_FAILURE; + } + } + else + { + reason = parameters[2]; + } + std::pair<bool, std::string> result = Base->AddFilter(freeform, type, reason, duration); + if (result.first) + { + user->WriteServ("NOTICE %s :*** Added filter '%s', type '%s%s%s', reason: '%s'", user->nick, freeform.c_str(), + (duration ? " duration: " : ""), (duration ? ConvToStr(duration).c_str() : ""), + reason.c_str()); + return CMD_SUCCESS; + } + else { - ServerInstance->XLines->apply_lines(APPLY_GLINES); - FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP())); + user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick, freeform.c_str(), result.second.c_str()); + return CMD_FAILURE; } } - return 1; + else + { + this->TooFewParams(user, "."); + return CMD_FAILURE; + } + } - return 0; } - virtual void OnRehash(const std::string ¶meter) + void TooFewParams(userrec* user, const std::string &extra_text) { + user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick, extra_text.c_str()); } - - virtual Version GetVersion() +}; + +FilterBase::FilterBase(InspIRCd* Me) : Module::Module(Me) +{ + filtcommand = new cmd_filter(this, Me); + ServerInstance->AddCommand(filtcommand); +} + +FilterBase::~FilterBase() +{ +} + +void FilterBase::Implements(char* List) +{ + List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1; +} + +int FilterBase::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status) +{ + return OnUserPreNotice(user,dest,target_type,text,status); +} + +int FilterBase::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status) +{ + FilterResult* f = this->FilterMatch(text); + if (f) { - // This is version 2 because version 1.x is the unreleased unrealircd module - return Version(1,1,0,2,VF_VENDOR,API_VERSION); + std::string target = ""; + if (target_type == TYPE_USER) + { + userrec* t = (userrec*)dest; + target = std::string(t->nick); + } + else if (target_type == TYPE_CHANNEL) + { + chanrec* t = (chanrec*)dest; + target = std::string(t->name); + } + if (f->action == "block") + { + ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their notice filtered, target was "+target+": "+f->reason); + user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+f->reason); + } + ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their notice filtered, target was ")+target+": "+f->reason+" Action: "+f->action); + if (f->action == "kill") + { + userrec::QuitUser(ServerInstance,user,f->reason); + } + if (f->action == "gline") + { + if (ServerInstance->XLines->add_gline(f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), user->MakeHostIP())) + { + ServerInstance->XLines->apply_lines(APPLY_GLINES); + FOREACH_MOD(I_OnAddGLine,OnAddGLine(f->gline_time, NULL, f->reason, user->MakeHostIP())); + } + } + return 1; } -}; + return 0; +} + +void FilterBase::OnRehash(const std::string ¶meter) +{ +} + +Version FilterBase::GetVersion() +{ + return Version(1,1,0,2,VF_VENDOR,API_VERSION); +} + |