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 extern InspIRCd* ServerInstance;
34 class FilterPCREException : public ModuleException
37 virtual const char* GetReason()
39 return "Could not find <filter file=\"\"> definition in your config file!";
43 /* $ModDesc: m_filter with regexps */
44 /* $CompileFlags: -I/usr/local/include */
45 /* $LinkerFlags: -L/usr/local/lib -lpcre */
47 class ModuleFilterPCRE : public Module
50 ConfigReader *Conf, *MyConf;
51 std::vector<pcre*> filters;
57 ModuleFilterPCRE(Server* Me)
60 // read the configuration file on startup.
61 // it is perfectly valid to set <filter file> to the value of the
62 // main config file, then append your <keyword> tags to the bottom
63 // of the main config... but rather messy. That's why the capability
64 // of using a seperate config file is provided.
66 Conf = new ConfigReader;
67 std::string filterfile = Conf->ReadValue("filter","file",0);
68 MyConf = new ConfigReader(filterfile);
69 if ((filterfile == "") || (!MyConf->Verify()))
71 FilterPCREException e;
74 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
77 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
79 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
80 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
83 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
84 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
88 filters.push_back(re);
89 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
95 virtual ~ModuleFilterPCRE()
101 void Implements(char* List)
103 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
106 // format of a config entry is <keyword pattern="^regexp$" reason="Some reason here" action="kill/block">
108 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
110 return OnUserPreNotice(user,dest,target_type,text,status);
113 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
115 for (unsigned int index = 0; index < filters.size(); index++)
117 if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
119 std::string target = "";
120 std::string reason = MyConf->ReadValue("keyword","reason",index);
121 std::string do_action = MyConf->ReadValue("keyword","action",index);
126 if (target_type == TYPE_USER)
128 userrec* t = (userrec*)dest;
129 target = std::string(t->nick);
131 else if (target_type == TYPE_CHANNEL)
133 chanrec* t = (chanrec*)dest;
134 target = std::string(t->name);
136 if (do_action == "block")
138 Srv->SendOpers(std::string("Filter: ")+std::string(user->nick)+
139 std::string(" had their notice filtered, target was ")+
141 user->WriteServ("NOTICE "+std::string(user->nick)+
142 " :Your notice has been filtered and opers notified: "+reason);
144 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
145 std::string(" had their notice filtered, target was ")+
146 target+": "+reason+" Action: "+do_action);
148 if (do_action == "kill")
150 userrec::QuitUser(ServerInstance,user,reason);
158 virtual void OnRehash(const std::string ¶meter)
160 // reload our config file on rehash - we must destroy and re-allocate the classes
161 // to call the constructor again and re-read our data.
164 Conf = new ConfigReader;
165 std::string filterfile = Conf->ReadValue("filter","file",0);
166 // this automatically re-reads the configuration file into the class
167 MyConf = new ConfigReader(filterfile);
168 if ((filterfile == "") || (!MyConf->Verify()))
170 FilterPCREException e;
171 // bail if the user forgot to create a config file
174 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
177 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
179 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
180 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
183 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
184 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
188 filters.push_back(re);
189 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
195 virtual Version GetVersion()
197 // This is version 2 because version 1.x is the unreleased unrealircd module
198 return Version(3,0,0,0,VF_VENDOR);
203 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
205 class ModuleFilterPCREFactory : public ModuleFactory
208 ModuleFilterPCREFactory()
212 ~ModuleFilterPCREFactory()
216 virtual Module * CreateModule(Server* Me)
218 return new ModuleFilterPCRE(Me);
224 extern "C" void * init_module( void )
226 return new ModuleFilterPCREFactory;