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 log(DEFAULT,"Match regexp %d: returned %d",index,pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0));
93 if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
95 std::string target = "";
96 std::string reason = MyConf->ReadValue("keyword","reason",index);
97 std::string do_action = MyConf->ReadValue("keyword","action",index);
102 if (target_type == TYPE_USER)
104 userrec* t = (userrec*)dest;
105 target = std::string(t->nick);
107 else if (target_type == TYPE_CHANNEL)
109 chanrec* t = (chanrec*)dest;
110 target = std::string(t->name);
112 if (do_action == "block")
114 Srv->SendOpers(std::string("FilterPCRE: ")+std::string(user->nick)+
115 std::string(" had their message filtered, target was ")+
117 // this form of SendTo (with the source as NuLL) sends a server notice
118 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
119 " :Your message has been filtered and opers notified: "+reason);
122 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
123 std::string(" had their message filtered, target was ")+
124 target+": "+reason+" Action: "+do_action);
126 if (do_action == "kill")
128 Srv->QuitUser(user,reason);
136 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
138 for (int index = 0; index < filters.size(); index++)
140 if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
142 std::string target = "";
143 std::string reason = MyConf->ReadValue("keyword","reason",index);
144 std::string do_action = MyConf->ReadValue("keyword","action",index);
149 if (target_type == TYPE_USER)
151 userrec* t = (userrec*)dest;
152 target = std::string(t->nick);
154 else if (target_type == TYPE_CHANNEL)
156 chanrec* t = (chanrec*)dest;
157 target = std::string(t->name);
159 if (do_action == "block")
161 Srv->SendOpers(std::string("Filter: ")+std::string(user->nick)+
162 std::string(" had their notice filtered, target was ")+
164 Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
165 " :Your notice has been filtered and opers notified: "+reason);
167 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
168 std::string(" had their notice filtered, target was ")+
169 target+": "+reason+" Action: "+do_action);
171 if (do_action == "kill")
173 Srv->QuitUser(user,reason);
181 virtual void OnRehash()
183 // reload our config file on rehash - we must destroy and re-allocate the classes
184 // to call the constructor again and re-read our data.
187 Conf = new ConfigReader;
188 std::string filterfile = Conf->ReadValue("filter","file",0);
189 // this automatically re-reads the configuration file into the class
190 MyConf = new ConfigReader(filterfile);
191 if ((filterfile == "") || (!MyConf->Verify()))
193 // bail if the user forgot to create a config file
194 printf("Error, could not find <filter file=\"\"> definition in your config file!");
195 log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
198 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
201 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
203 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
204 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
207 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
208 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
212 filters.push_back(re);
213 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
219 virtual Version GetVersion()
221 // This is version 2 because version 1.x is the unreleased unrealircd module
222 return Version(3,0,0,0,VF_VENDOR);
227 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
229 class ModuleFilterPCREFactory : public ModuleFactory
232 ModuleFilterPCREFactory()
236 ~ModuleFilterPCREFactory()
240 virtual Module * CreateModule()
242 return new ModuleFilterPCRE;
248 extern "C" void * init_module( void )
250 return new ModuleFilterPCREFactory;