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
41 PCREFilter(pcre* r, const std::string &rea, const std::string &act, long gline_time, const std::string &pat)
42 : FilterResult::FilterResult(rea, act, gline_time), regexp(r), pattern(pat)
47 class ModuleFilterPCRE : public FilterBase
49 std::vector<PCREFilter> filters;
55 ModuleFilterPCRE(InspIRCd* Me)
56 : FilterBase::FilterBase(Me, "m_filter_pcre.so")
61 virtual ~ModuleFilterPCRE()
65 virtual FilterResult* FilterMatch(const std::string &text)
67 for (unsigned int index = 0; index < filters.size(); index++)
69 PCREFilter& filt = filters[index];
71 if (pcre_exec(filt.regexp,NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
79 virtual bool DeleteFilter(const std::string &freeform)
81 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
83 if (i->pattern == freeform)
92 virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration)
94 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
96 if (i->pattern == freeform)
98 return std::make_pair(false, "Filter already exists");
102 re = pcre_compile(freeform.c_str(),0,&error,&erroffset,NULL);
106 ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", freeform.c_str(), erroffset, error);
107 ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", freeform.c_str());
108 return std::make_pair(false, "Error in regular expression at offset " + ConvToStr(erroffset) + ": "+error);
112 filters.push_back(PCREFilter(re, reason, type, duration, freeform));
113 return std::make_pair(true, "");
117 virtual void OnRehash(const std::string ¶meter)
119 ConfigReader MyConf(ServerInstance);
121 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
122 pcre_free((*i).regexp);
126 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
128 std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
129 std::string reason = MyConf.ReadValue("keyword", "reason", index);
130 std::string action = MyConf.ReadValue("keyword", "action", index);
131 long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index).c_str());
133 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
137 ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
138 ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
142 filters.push_back(PCREFilter(re, reason, action, gline_time, pattern));
143 ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
150 class ModuleFilterPCREFactory : public ModuleFactory
153 ModuleFilterPCREFactory()
157 ~ModuleFilterPCREFactory()
161 virtual Module * CreateModule(InspIRCd* Me)
163 return new ModuleFilterPCRE(Me);
169 extern "C" void * init_module( void )
171 return new ModuleFilterPCREFactory;