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 * ---------------------------------------------------
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.
28 /* $ModDesc: m_filter with regexps */
29 /* $CompileFlags: -I/usr/local/include -L/usr/local/lib -lpcre */
31 class ModuleFilterPCRE : public Module
34 ConfigReader *Conf, *MyConf;
35 std::vector<pcre*> filters;
43 // read the configuration file on startup.
44 // it is perfectly valid to set <FilterPCRE file> to the value of the
45 // main config file, then append your <keyword> tags to the bottom
46 // of the main config... but rather messy. That's why the capability
47 // of using a seperate config file is provided.
49 Conf = new ConfigReader;
50 std::string filterfile = Conf->ReadValue("filter","file",0);
51 MyConf = new ConfigReader(filterfile);
52 if ((filterfile == "") || (!MyConf->Verify()))
54 printf("Error, could not find <filter file=\"\"> definition in your config file!");
55 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
58 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
61 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
63 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
64 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
67 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
68 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
72 filters.push_back(re);
73 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
79 virtual ~ModuleFilterPCRE()
86 // format of a config entry is <keyword pattern="^regexp$" reason="Some reason here" action="kill/block">
88 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
90 for (int index = 0; index < filters.size(); index++)
92 if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
94 std::string target = "";
95 std::string reason = MyConf->ReadValue("keyword","reason",index);
96 std::string do_action = MyConf->ReadValue("keyword","action",index);
101 if (target_type == TYPE_USER)
103 userrec* t = (userrec*)dest;
104 target = std::string(t->nick);
106 else if (target_type == TYPE_CHANNEL)
108 chanrec* t = (chanrec*)dest;
109 target = std::string(t->name);
111 if (do_action == "block")
113 Srv->SendOpers(std::string("FilterPCRE: ")+std::string(user->nick)+
114 std::string(" had their message filtered, target was ")+
116 // this form of SendTo (with the source as NuLL) sends a server notice
117 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
118 " :Your message has been filtered and opers notified: "+reason);
121 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
122 std::string(" had their message filtered, target was ")+
123 target+": "+reason+" Action: "+do_action);
125 if (do_action == "kill")
127 Srv->QuitUser(user,reason);
135 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
137 for (int index = 0; index < filters.size(); index++)
139 if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
141 std::string target = "";
142 std::string reason = MyConf->ReadValue("keyword","reason",index);
143 std::string do_action = MyConf->ReadValue("keyword","action",index);
148 if (target_type == TYPE_USER)
150 userrec* t = (userrec*)dest;
151 target = std::string(t->nick);
153 else if (target_type == TYPE_CHANNEL)
155 chanrec* t = (chanrec*)dest;
156 target = std::string(t->name);
158 if (do_action == "block")
160 Srv->SendOpers(std::string("Filter: ")+std::string(user->nick)+
161 std::string(" had their notice filtered, target was ")+
163 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
164 " :Your notice has been filtered and opers notified: "+reason);
166 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
167 std::string(" had their notice filtered, target was ")+
168 target+": "+reason+" Action: "+do_action);
170 if (do_action == "kill")
172 Srv->QuitUser(user,reason);
180 virtual void OnRehash()
182 // reload our config file on rehash - we must destroy and re-allocate the classes
183 // to call the constructor again and re-read our data.
186 Conf = new ConfigReader;
187 std::string filterfile = Conf->ReadValue("filter","file",0);
188 // this automatically re-reads the configuration file into the class
189 MyConf = new ConfigReader(filterfile);
190 if ((filterfile == "") || (!MyConf->Verify()))
192 // bail if the user forgot to create a config file
193 printf("Error, could not find <filter file=\"\"> definition in your config file!");
194 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
197 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
200 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
202 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
203 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
206 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
207 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
211 filters.push_back(re);
212 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
218 virtual Version GetVersion()
220 // This is version 2 because version 1.x is the unreleased unrealircd module
221 return Version(3,0,0,0,VF_VENDOR);
226 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
228 class ModuleFilterPCREFactory : public ModuleFactory
231 ModuleFilterPCREFactory()
235 ~ModuleFilterPCREFactory()
239 virtual Module * CreateModule()
241 return new ModuleFilterPCRE;
247 extern "C" void * init_module( void )
249 return new ModuleFilterPCREFactory;