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 /* $ModDesc: m_filter with regexps */
30 /* $CompileFlags: `pcre-config --cflags` */
31 /* $LinkerFlags: `pcre-config --libs` `perl extra/pcre_rpath.pl` -lpcre */
33 class ModuleFilterPCRE : public Module
42 Filter(pcre* r, const std::string &rea, const std::string &act)
43 : regexp(r), reason(rea), action(act)
49 std::vector<Filter> filters;
55 ModuleFilterPCRE(InspIRCd* Me)
56 : Module::Module(Me), Srv(Me)
61 virtual ~ModuleFilterPCRE()
65 void Implements(char* List)
67 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
70 // format of a config entry is <keyword pattern="^regexp$" reason="Some reason here" action="kill/block">
72 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
74 return OnUserPreNotice(user,dest,target_type,text,status);
77 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
79 for (unsigned int index = 0; index < filters.size(); index++)
81 Filter& filt = filters[index];
83 if (pcre_exec(filt.regexp,NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
87 if(filt.action.empty())
90 if (target_type == TYPE_USER)
92 userrec* t = (userrec*)dest;
95 else if (target_type == TYPE_CHANNEL)
97 chanrec* t = (chanrec*)dest;
105 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());
107 if (filt.action == "block")
109 Srv->WriteOpers("Filter: %s had their notice filtered, target was %s: %s", user->nick, target, filt.reason.c_str());
110 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+filt.reason);
112 else if (filt.action == "kill")
114 userrec::QuitUser(Srv, user, filt.reason);
123 virtual void OnRehash(const std::string ¶meter)
125 /* Read the configuration file on startup and rehash.
126 * It is perfectly valid to set <filter file> to the value of the
127 * main config file, then append your <keyword> tags to the bottom
128 * of the main config... but rather messy. That's why the capability
129 * of using a seperate config file is provided.
132 ConfigReader MyConf(Srv);
134 for (std::vector<Filter>::iterator i = filters.begin(); i != filters.end(); i++)
135 pcre_free((*i).regexp);
139 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
141 std::string pattern = MyConf.ReadValue("keyword","pattern",index);
142 std::string reason = MyConf.ReadValue("keyword","reason",index);
143 std::string action = MyConf.ReadValue("keyword","action",index);
145 re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
149 ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
150 ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
154 filters.push_back(Filter(re, reason, action));
155 ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
160 virtual Version GetVersion()
162 /* Version 1.x is the unreleased unrealircd module */
163 return Version(3,2,1,0,VF_VENDOR,API_VERSION);
168 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
170 class ModuleFilterPCREFactory : public ModuleFactory
173 ModuleFilterPCREFactory()
177 ~ModuleFilterPCREFactory()
181 virtual Module * CreateModule(InspIRCd* Me)
183 return new ModuleFilterPCRE(Me);
189 extern "C" void * init_module( void )
191 return new ModuleFilterPCREFactory;