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 * ---------------------------------------------------
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"
32 class FilterPCREException : public ModuleException
35 virtual const char* GetReason()
37 return "Could not find <filter file=\"\"> definition in your config file!";
41 /* $ModDesc: m_filter with regexps */
42 /* $CompileFlags: -I/usr/local/include */
43 /* $LinkerFlags: -L/usr/local/lib -lpcre */
45 class ModuleFilterPCRE : public Module
48 ConfigReader *Conf, *MyConf;
49 std::vector<pcre*> filters;
55 ModuleFilterPCRE(Server* Me)
58 // read the configuration file on startup.
59 // it is perfectly valid to set <filter file> to the value of the
60 // main config file, then append your <keyword> tags to the bottom
61 // of the main config... but rather messy. That's why the capability
62 // of using a seperate config file is provided.
64 Conf = new ConfigReader;
65 std::string filterfile = Conf->ReadValue("filter","file",0);
66 MyConf = new ConfigReader(filterfile);
67 if ((filterfile == "") || (!MyConf->Verify()))
69 FilterPCREException e;
72 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
75 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
77 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
78 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
81 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
82 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
86 filters.push_back(re);
87 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
93 virtual ~ModuleFilterPCRE()
99 void Implements(char* List)
101 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
104 // format of a config entry is <keyword pattern="^regexp$" reason="Some reason here" action="kill/block">
106 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
108 return OnUserPreNotice(user,dest,target_type,text,status);
111 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
113 for (unsigned int index = 0; index < filters.size(); index++)
115 if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
117 std::string target = "";
118 std::string reason = MyConf->ReadValue("keyword","reason",index);
119 std::string do_action = MyConf->ReadValue("keyword","action",index);
124 if (target_type == TYPE_USER)
126 userrec* t = (userrec*)dest;
127 target = std::string(t->nick);
129 else if (target_type == TYPE_CHANNEL)
131 chanrec* t = (chanrec*)dest;
132 target = std::string(t->name);
134 if (do_action == "block")
136 Srv->SendOpers(std::string("Filter: ")+std::string(user->nick)+
137 std::string(" had their notice filtered, target was ")+
139 user->WriteServ("NOTICE "+std::string(user->nick)+
140 " :Your notice has been filtered and opers notified: "+reason);
142 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
143 std::string(" had their notice filtered, target was ")+
144 target+": "+reason+" Action: "+do_action);
146 if (do_action == "kill")
148 userrec::QuitUser(user,reason);
156 virtual void OnRehash(const std::string ¶meter)
158 // reload our config file on rehash - we must destroy and re-allocate the classes
159 // to call the constructor again and re-read our data.
162 Conf = new ConfigReader;
163 std::string filterfile = Conf->ReadValue("filter","file",0);
164 // this automatically re-reads the configuration file into the class
165 MyConf = new ConfigReader(filterfile);
166 if ((filterfile == "") || (!MyConf->Verify()))
168 FilterPCREException e;
169 // bail if the user forgot to create a config file
172 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
175 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
177 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
178 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
181 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
182 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
186 filters.push_back(re);
187 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
193 virtual Version GetVersion()
195 // This is version 2 because version 1.x is the unreleased unrealircd module
196 return Version(3,0,0,0,VF_VENDOR);
201 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
203 class ModuleFilterPCREFactory : public ModuleFactory
206 ModuleFilterPCREFactory()
210 ~ModuleFilterPCREFactory()
214 virtual Module * CreateModule(Server* Me)
216 return new ModuleFilterPCRE(Me);
222 extern "C" void * init_module( void )
224 return new ModuleFilterPCREFactory;