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 */
33 /* $LinkerFlags: -L/usr/local/lib -lpcre */
35 class ModuleFilterPCRE : public Module
38 ConfigReader *Conf, *MyConf;
39 std::vector<pcre*> filters;
45 ModuleFilterPCRE(Server* Me)
48 // read the configuration file on startup.
49 // it is perfectly valid to set <filter file> to the value of the
50 // main config file, then append your <keyword> tags to the bottom
51 // of the main config... but rather messy. That's why the capability
52 // of using a seperate config file is provided.
54 Conf = new ConfigReader;
55 std::string filterfile = Conf->ReadValue("filter","file",0);
56 MyConf = new ConfigReader(filterfile);
57 if ((filterfile == "") || (!MyConf->Verify()))
59 printf("Error, could not find <filter file=\"\"> definition in your config file!\n");
60 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
63 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
66 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
68 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
69 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
72 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
73 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
77 filters.push_back(re);
78 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
84 virtual ~ModuleFilterPCRE()
90 // format of a config entry is <keyword pattern="^regexp$" reason="Some reason here" action="kill/block">
92 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
94 for (unsigned int index = 0; index < filters.size(); index++)
96 if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
98 std::string target = "";
99 std::string reason = MyConf->ReadValue("keyword","reason",index);
100 std::string do_action = MyConf->ReadValue("keyword","action",index);
105 if (target_type == TYPE_USER)
107 userrec* t = (userrec*)dest;
108 target = std::string(t->nick);
110 else if (target_type == TYPE_CHANNEL)
112 chanrec* t = (chanrec*)dest;
113 target = std::string(t->name);
115 if (do_action == "block")
117 Srv->SendOpers(std::string("FilterPCRE: ")+std::string(user->nick)+
118 std::string(" had their message filtered, target was ")+
120 // this form of SendTo (with the source as NuLL) sends a server notice
121 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
122 " :Your message has been filtered and opers notified: "+reason);
125 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
126 std::string(" had their message filtered, target was ")+
127 target+": "+reason+" Action: "+do_action);
129 if (do_action == "kill")
131 Srv->QuitUser(user,reason);
139 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
141 for (unsigned int index = 0; index < filters.size(); index++)
143 if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
145 std::string target = "";
146 std::string reason = MyConf->ReadValue("keyword","reason",index);
147 std::string do_action = MyConf->ReadValue("keyword","action",index);
152 if (target_type == TYPE_USER)
154 userrec* t = (userrec*)dest;
155 target = std::string(t->nick);
157 else if (target_type == TYPE_CHANNEL)
159 chanrec* t = (chanrec*)dest;
160 target = std::string(t->name);
162 if (do_action == "block")
164 Srv->SendOpers(std::string("Filter: ")+std::string(user->nick)+
165 std::string(" had their notice filtered, target was ")+
167 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
168 " :Your notice has been filtered and opers notified: "+reason);
170 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
171 std::string(" had their notice filtered, target was ")+
172 target+": "+reason+" Action: "+do_action);
174 if (do_action == "kill")
176 Srv->QuitUser(user,reason);
184 virtual void OnRehash(std::string parameter)
186 // reload our config file on rehash - we must destroy and re-allocate the classes
187 // to call the constructor again and re-read our data.
190 Conf = new ConfigReader;
191 std::string filterfile = Conf->ReadValue("filter","file",0);
192 // this automatically re-reads the configuration file into the class
193 MyConf = new ConfigReader(filterfile);
194 if ((filterfile == "") || (!MyConf->Verify()))
196 // bail if the user forgot to create a config file
197 printf("Error, could not find <filter file=\"\"> definition in your config file!");
198 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
201 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
204 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
206 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
207 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
210 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
211 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
215 filters.push_back(re);
216 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
222 virtual Version GetVersion()
224 // This is version 2 because version 1.x is the unreleased unrealircd module
225 return Version(3,0,0,0,VF_VENDOR);
230 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
232 class ModuleFilterPCREFactory : public ModuleFactory
235 ModuleFilterPCREFactory()
239 ~ModuleFilterPCREFactory()
243 virtual Module * CreateModule(Server* Me)
245 return new ModuleFilterPCRE(Me);
251 extern "C" void * init_module( void )
253 return new ModuleFilterPCREFactory;