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 * ---------------------------------------------------
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.
27 #include "helperfuncs.h"
30 class FilterPCREException : public ModuleException
33 virtual const char* GetReason()
35 return "Could not find <filter file=\"\"> definition in your config file!";
39 /* $ModDesc: m_filter with regexps */
40 /* $CompileFlags: -I/usr/local/include */
41 /* $LinkerFlags: -L/usr/local/lib -lpcre */
43 class ModuleFilterPCRE : public Module
52 Filter(pcre* r, const std::string &rea, const std::string &act)
53 : regexp(r), reason(rea), action(act)
59 std::vector<Filter> filters;
65 ModuleFilterPCRE(InspIRCd* Me)
66 : Module::Module(Me), Srv(Me)
71 virtual ~ModuleFilterPCRE()
75 void Implements(char* List)
77 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
80 // format of a config entry is <keyword pattern="^regexp$" reason="Some reason here" action="kill/block">
82 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
84 return OnUserPreNotice(user,dest,target_type,text,status);
87 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
89 for (unsigned int index = 0; index < filters.size(); index++)
91 Filter& filt = filters[index];
93 if (pcre_exec(filt.regexp,NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
97 if(filt.action.empty())
100 if (target_type == TYPE_USER)
102 userrec* t = (userrec*)dest;
105 else if (target_type == TYPE_CHANNEL)
107 chanrec* t = (chanrec*)dest;
115 log(DEFAULT, "Filter: %s had their notice filtered, target was %s: %s Action: %s", user->nick, target, filt.reason.c_str(), filt.action.c_str());
117 if (filt.action == "block")
119 Srv->WriteOpers("Filter: %s had their notice filtered, target was %s: %s", user->nick, target, filt.reason.c_str());
120 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+filt.reason);
122 else if (filt.action == "kill")
124 userrec::QuitUser(Srv, user, filt.reason);
133 virtual void OnRehash(const std::string ¶meter)
135 /* Read the configuration file on startup and rehash.
136 * It is perfectly valid to set <filter file> to the value of the
137 * main config file, then append your <keyword> tags to the bottom
138 * of the main config... but rather messy. That's why the capability
139 * of using a seperate config file is provided.
142 ConfigReader Conf(Srv);
144 std::string filterfile = Conf.ReadValue("filter", "file", 0);
146 ConfigReader MyConf(Srv, filterfile);
148 if (filterfile.empty() || !MyConf.Verify())
150 FilterPCREException e;
154 log(DEFAULT,"m_filter_pcre: read configuration from "+filterfile);
158 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
160 std::string pattern = MyConf.ReadValue("keyword","pattern",index);
161 std::string reason = MyConf.ReadValue("keyword","reason",index);
162 std::string action = MyConf.ReadValue("keyword","action",index);
164 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
168 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
169 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
173 filters.push_back(Filter(re, reason, action));
174 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
179 virtual Version GetVersion()
181 /* Version 1.x is the unreleased unrealircd module */
182 return Version(3,2,0,0,VF_VENDOR);
187 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
189 class ModuleFilterPCREFactory : public ModuleFactory
192 ModuleFilterPCREFactory()
196 ~ModuleFilterPCREFactory()
200 virtual Module * CreateModule(InspIRCd* Me)
202 return new ModuleFilterPCRE(Me);
208 extern "C" void * init_module( void )
210 return new ModuleFilterPCREFactory;