1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire 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 * ---------------------------------------------------
19 // Message and notice filtering using regex patterns
20 // a module based on the original work done by Craig Edwards in 2003
21 // for the chatspike network.
29 #include "helperfuncs.h"
31 /* $ModDesc: m_filter with regexps */
32 /* $CompileFlags: -I/usr/local/include -L/usr/local/lib -lpcre */
34 class ModuleFilterPCRE : public Module
37 ConfigReader *Conf, *MyConf;
38 std::vector<pcre*> filters;
46 // read the configuration file on startup.
47 // it is perfectly valid to set <FilterPCRE file> to the value of the
48 // main config file, then append your <keyword> tags to the bottom
49 // of the main config... but rather messy. That's why the capability
50 // of using a seperate config file is provided.
52 Conf = new ConfigReader;
53 std::string filterfile = Conf->ReadValue("filter","file",0);
54 MyConf = new ConfigReader(filterfile);
55 if ((filterfile == "") || (!MyConf->Verify()))
57 printf("Error, could not find <filter file=\"\"> definition in your config file!");
58 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
61 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
64 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
66 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
67 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
70 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
71 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
75 filters.push_back(re);
76 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
82 virtual ~ModuleFilterPCRE()
89 // format of a config entry is <keyword pattern="^regexp$" reason="Some reason here" action="kill/block">
91 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
93 for (unsigned int index = 0; index < filters.size(); index++)
95 if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
97 std::string target = "";
98 std::string reason = MyConf->ReadValue("keyword","reason",index);
99 std::string do_action = MyConf->ReadValue("keyword","action",index);
104 if (target_type == TYPE_USER)
106 userrec* t = (userrec*)dest;
107 target = std::string(t->nick);
109 else if (target_type == TYPE_CHANNEL)
111 chanrec* t = (chanrec*)dest;
112 target = std::string(t->name);
114 if (do_action == "block")
116 Srv->SendOpers(std::string("FilterPCRE: ")+std::string(user->nick)+
117 std::string(" had their message filtered, target was ")+
119 // this form of SendTo (with the source as NuLL) sends a server notice
120 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
121 " :Your message has been filtered and opers notified: "+reason);
124 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
125 std::string(" had their message filtered, target was ")+
126 target+": "+reason+" Action: "+do_action);
128 if (do_action == "kill")
130 Srv->QuitUser(user,reason);
138 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
140 for (unsigned int index = 0; index < filters.size(); index++)
142 if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
144 std::string target = "";
145 std::string reason = MyConf->ReadValue("keyword","reason",index);
146 std::string do_action = MyConf->ReadValue("keyword","action",index);
151 if (target_type == TYPE_USER)
153 userrec* t = (userrec*)dest;
154 target = std::string(t->nick);
156 else if (target_type == TYPE_CHANNEL)
158 chanrec* t = (chanrec*)dest;
159 target = std::string(t->name);
161 if (do_action == "block")
163 Srv->SendOpers(std::string("Filter: ")+std::string(user->nick)+
164 std::string(" had their notice filtered, target was ")+
166 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
167 " :Your notice has been filtered and opers notified: "+reason);
169 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
170 std::string(" had their notice filtered, target was ")+
171 target+": "+reason+" Action: "+do_action);
173 if (do_action == "kill")
175 Srv->QuitUser(user,reason);
183 virtual void OnRehash(std::string parameter)
185 // reload our config file on rehash - we must destroy and re-allocate the classes
186 // to call the constructor again and re-read our data.
189 Conf = new ConfigReader;
190 std::string filterfile = Conf->ReadValue("filter","file",0);
191 // this automatically re-reads the configuration file into the class
192 MyConf = new ConfigReader(filterfile);
193 if ((filterfile == "") || (!MyConf->Verify()))
195 // bail if the user forgot to create a config file
196 printf("Error, could not find <filter file=\"\"> definition in your config file!");
197 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
200 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
203 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
205 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
206 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
209 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
210 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
214 filters.push_back(re);
215 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
221 virtual Version GetVersion()
223 // This is version 2 because version 1.x is the unreleased unrealircd module
224 return Version(3,0,0,0,VF_VENDOR);
229 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
231 class ModuleFilterPCREFactory : public ModuleFactory
234 ModuleFilterPCREFactory()
238 ~ModuleFilterPCREFactory()
242 virtual Module * CreateModule()
244 return new ModuleFilterPCRE;
250 extern "C" void * init_module( void )
252 return new ModuleFilterPCREFactory;