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 log(DEFAULT,"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)
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;
131 else if (target_type == TYPE_CHANNEL)
133 chanrec* t = (chanrec*)dest;
141 if (do_action == "block")
143 ServerInstance->WriteOpers("Filter: %s had their notice filtered, target was %s: %s", user->nick, target, reason.c_str());
144 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+reason);
147 log(DEFAULT, "Filter: %s had their notice filtered, target was %s: %s Action: %s", user->nick, target, reason.c_str(), do_action.c_str());
149 if (do_action == "kill")
151 userrec::QuitUser(ServerInstance,user,reason);
159 virtual void OnRehash(const std::string ¶meter)
161 // reload our config file on rehash - we must destroy and re-allocate the classes
162 // to call the constructor again and re-read our data.
165 Conf = new ConfigReader;
166 std::string filterfile = Conf->ReadValue("filter","file",0);
167 // this automatically re-reads the configuration file into the class
168 MyConf = new ConfigReader(filterfile);
169 if ((filterfile == "") || (!MyConf->Verify()))
171 FilterPCREException e;
172 // bail if the user forgot to create a config file
175 log(DEFAULT,"m_filter_pcre: read configuration from "+filterfile);
178 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
180 std::string pattern = MyConf->ReadValue("keyword","pattern",index);
181 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
184 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
185 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
189 filters.push_back(re);
190 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
196 virtual Version GetVersion()
198 // This is version 2 because version 1.x is the unreleased unrealircd module
199 return Version(3,0,0,0,VF_VENDOR);
204 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
206 class ModuleFilterPCREFactory : public ModuleFactory
209 ModuleFilterPCREFactory()
213 ~ModuleFilterPCREFactory()
217 virtual Module * CreateModule(Server* Me)
219 return new ModuleFilterPCRE(Me);
225 extern "C" void * init_module( void )
227 return new ModuleFilterPCREFactory;