1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 // Message and notice filtering using regex patterns
18 // a module based on the original work done by Craig Edwards in 2003
19 // for the chatspike network.
30 /* $ModDesc: m_filter with regexps */
31 /* $CompileFlags: `pcre-config --cflags` */
32 /* $LinkerFlags: `pcre-config --libs` `perl extra/pcre_rpath.pl` -lpcre */
33 /* $ModDep: ../m_filter.h */
35 class PCREFilter : public FilterResult
40 PCREFilter(pcre* r, const std::string &rea, const std::string &act, long gline_time)
41 : FilterResult::FilterResult(rea, act, gline_time), regexp(r)
46 class ModuleFilterPCRE : public FilterBase
48 std::vector<PCREFilter> filters;
54 ModuleFilterPCRE(InspIRCd* Me)
55 : FilterBase::FilterBase(Me)
60 virtual ~ModuleFilterPCRE()
64 virtual FilterResult* FilterMatch(const std::string &text)
66 for (unsigned int index = 0; index < filters.size(); index++)
68 PCREFilter& filt = filters[index];
70 if (pcre_exec(filt.regexp,NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
78 virtual void OnRehash(const std::string ¶meter)
80 ConfigReader MyConf(ServerInstance);
82 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
83 pcre_free((*i).regexp);
87 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
89 std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
90 std::string reason = MyConf.ReadValue("keyword", "reason", index);
91 std::string action = MyConf.ReadValue("keyword", "action", index);
92 long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index).c_str());
94 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
98 ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
99 ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
103 filters.push_back(PCREFilter(re, reason, action, gline_time));
104 ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
111 class ModuleFilterPCREFactory : public ModuleFactory
114 ModuleFilterPCREFactory()
118 ~ModuleFilterPCREFactory()
122 virtual Module * CreateModule(InspIRCd* Me)
124 return new ModuleFilterPCRE(Me);
130 extern "C" void * init_module( void )
132 return new ModuleFilterPCREFactory;