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.
29 /** Thrown by m_filter_pcre
31 class FilterPCREException : public ModuleException
34 virtual const char* GetReason()
36 return "Could not find <filter file=\"\"> definition in your config file!";
40 /* $ModDesc: m_filter with regexps */
41 /* $CompileFlags: `pcre-config --cflags` */
42 /* $LinkerFlags: `pcre-config --libs` `perl extra/pcre_rpath.pl` -lpcre */
44 class ModuleFilterPCRE : public Module
53 Filter(pcre* r, const std::string &rea, const std::string &act)
54 : regexp(r), reason(rea), action(act)
60 std::vector<Filter> filters;
66 ModuleFilterPCRE(InspIRCd* Me)
67 : Module::Module(Me), Srv(Me)
72 virtual ~ModuleFilterPCRE()
76 void Implements(char* List)
78 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
81 // format of a config entry is <keyword pattern="^regexp$" reason="Some reason here" action="kill/block">
83 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
85 return OnUserPreNotice(user,dest,target_type,text,status);
88 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
90 for (unsigned int index = 0; index < filters.size(); index++)
92 Filter& filt = filters[index];
94 if (pcre_exec(filt.regexp,NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
98 if(filt.action.empty())
101 if (target_type == TYPE_USER)
103 userrec* t = (userrec*)dest;
106 else if (target_type == TYPE_CHANNEL)
108 chanrec* t = (chanrec*)dest;
116 ServerInstance->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());
118 if (filt.action == "block")
120 Srv->WriteOpers("Filter: %s had their notice filtered, target was %s: %s", user->nick, target, filt.reason.c_str());
121 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+filt.reason);
123 else if (filt.action == "kill")
125 userrec::QuitUser(Srv, user, filt.reason);
134 virtual void OnRehash(const std::string ¶meter)
136 /* Read the configuration file on startup and rehash.
137 * It is perfectly valid to set <filter file> to the value of the
138 * main config file, then append your <keyword> tags to the bottom
139 * of the main config... but rather messy. That's why the capability
140 * of using a seperate config file is provided.
143 ConfigReader Conf(Srv);
145 std::string filterfile = Conf.ReadValue("filter", "file", 0);
147 ConfigReader MyConf(Srv, filterfile);
149 if (filterfile.empty() || !MyConf.Verify())
151 FilterPCREException e;
155 ServerInstance->Log(DEFAULT,"m_filter_pcre: read configuration from "+filterfile);
157 for (std::vector<Filter>::iterator i = filters.begin(); i != filters.end(); i++)
158 pcre_free((*i).regexp);
162 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
164 std::string pattern = MyConf.ReadValue("keyword","pattern",index);
165 std::string reason = MyConf.ReadValue("keyword","reason",index);
166 std::string action = MyConf.ReadValue("keyword","action",index);
168 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
172 ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
173 ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
177 filters.push_back(Filter(re, reason, action));
178 ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
183 virtual Version GetVersion()
185 /* Version 1.x is the unreleased unrealircd module */
186 return Version(3,2,0,0,VF_VENDOR,API_VERSION);
191 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
193 class ModuleFilterPCREFactory : public ModuleFactory
196 ModuleFilterPCREFactory()
200 ~ModuleFilterPCREFactory()
204 virtual Module * CreateModule(InspIRCd* Me)
206 return new ModuleFilterPCRE(Me);
212 extern "C" void * init_module( void )
214 return new ModuleFilterPCREFactory;