summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2005-04-11 16:30:23 +0000
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2005-04-11 16:30:23 +0000
commit61f2deb53d706c7791724093034f1bdc0a337673 (patch)
tree1b836a5d0313f5152d46dc919538ac9a54eec6fd /src
parentf4046a61e7fb5e017a19a0308bfa42fa5abe68d5 (diff)
Added m_filter_pcre, filters with regular expression matches!
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@1043 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src')
-rw-r--r--src/modules/extra/README7
-rw-r--r--src/modules/extra/m_filter_pcre.cpp252
2 files changed, 259 insertions, 0 deletions
diff --git a/src/modules/extra/README b/src/modules/extra/README
new file mode 100644
index 000000000..7e3096b34
--- /dev/null
+++ b/src/modules/extra/README
@@ -0,0 +1,7 @@
+This directory stores modules which require external libraries to compile.
+For example, m_filter_pcre requires the PCRE libraries.
+
+To compile any of these modules first ensure you have the required dependencies
+(read the online documentation at http://www.inspircd.org/wiki/) and then cp
+the .cpp file from this directory into the parent directory (src/modules/) and
+re-configure your inspircd with ./configure -update to detect the new module.
diff --git a/src/modules/extra/m_filter_pcre.cpp b/src/modules/extra/m_filter_pcre.cpp
new file mode 100644
index 000000000..d05a72200
--- /dev/null
+++ b/src/modules/extra/m_filter_pcre.cpp
@@ -0,0 +1,252 @@
+/* +------------------------------------+
+ * | Inspire Internet Relay Chat Daemon |
+ * +------------------------------------+
+ *
+ * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
+ * E-mail:
+ * <brain@chatspike.net>
+ * <Craig@chatspike.net>
+ *
+ * Written by Craig Edwards, Craig McLure, and others.
+ * This program is free but copyrighted software; see
+ * the file COPYING for details.
+ *
+ * ---------------------------------------------------
+ */
+
+// Message and notice filtering using regex patterns
+// a module based on the original work done by Craig Edwards in 2003
+// for the chatspike network.
+
+#include <stdio.h>
+#include <string>
+#include <pcre.h>
+#include "users.h"
+#include "channels.h"
+#include "modules.h"
+
+/* $ModDesc: m_filter with regexps */
+/* $CompileFlags: -I/usr/local/include -L/usr/local/lib -lpcre */
+
+class ModuleFilterPCRE : public Module
+{
+ Server *Srv;
+ ConfigReader *Conf, *MyConf;
+ std::vector<pcre*> filters;
+ pcre *re;
+ const char *error;
+ int erroffset;
+
+ public:
+ ModuleFilterPCRE()
+ {
+ // read the configuration file on startup.
+ // it is perfectly valid to set <FilterPCRE file> to the value of the
+ // main config file, then append your <keyword> tags to the bottom
+ // of the main config... but rather messy. That's why the capability
+ // of using a seperate config file is provided.
+ Srv = new Server;
+ Conf = new ConfigReader;
+ std::string filterfile = Conf->ReadValue("filter","file",0);
+ MyConf = new ConfigReader(filterfile);
+ if ((filterfile == "") || (!MyConf->Verify()))
+ {
+ printf("Error, could not find <filter file=\"\"> definition in your config file!");
+ log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
+ return;
+ }
+ Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
+
+ filters.clear();
+ for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
+ {
+ std::string pattern = MyConf->ReadValue("keyword","pattern",index);
+ re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
+ if (!re)
+ {
+ log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
+ log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
+ }
+ else
+ {
+ filters.push_back(re);
+ log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
+ }
+ }
+
+ }
+
+ virtual ~ModuleFilterPCRE()
+ {
+ delete Srv;
+ delete MyConf;
+ delete Conf;
+ }
+
+ // format of a config entry is <keyword pattern="^regexp$" reason="Some reason here" action="kill/block">
+
+ virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
+ {
+ for (int index = 0; index < filters.size(); index++)
+ {
+ log(DEFAULT,"Match regexp %d: returned %d",index,pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0));
+ if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
+ {
+ std::string target = "";
+ std::string reason = MyConf->ReadValue("keyword","reason",index);
+ std::string do_action = MyConf->ReadValue("keyword","action",index);
+
+ if (do_action == "")
+ do_action = "none";
+
+ if (target_type == TYPE_USER)
+ {
+ userrec* t = (userrec*)dest;
+ target = std::string(t->nick);
+ }
+ else if (target_type == TYPE_CHANNEL)
+ {
+ chanrec* t = (chanrec*)dest;
+ target = std::string(t->name);
+ }
+ if (do_action == "block")
+ {
+ Srv->SendOpers(std::string("FilterPCRE: ")+std::string(user->nick)+
+ std::string(" had their message filtered, target was ")+
+ target+": "+reason);
+ // this form of SendTo (with the source as NuLL) sends a server notice
+ Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
+ " :Your message has been filtered and opers notified: "+reason);
+ }
+
+ Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
+ std::string(" had their message filtered, target was ")+
+ target+": "+reason+" Action: "+do_action);
+
+ if (do_action == "kill")
+ {
+ Srv->QuitUser(user,reason);
+ }
+ return 1;
+ }
+ }
+ return 0;
+ }
+
+ virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
+ {
+ for (int index = 0; index < filters.size(); index++)
+ {
+ if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
+ {
+ std::string target = "";
+ std::string reason = MyConf->ReadValue("keyword","reason",index);
+ std::string do_action = MyConf->ReadValue("keyword","action",index);
+
+ if (do_action == "")
+ do_action = "none";
+
+ if (target_type == TYPE_USER)
+ {
+ userrec* t = (userrec*)dest;
+ target = std::string(t->nick);
+ }
+ else if (target_type == TYPE_CHANNEL)
+ {
+ chanrec* t = (chanrec*)dest;
+ target = std::string(t->name);
+ }
+ if (do_action == "block")
+ {
+ Srv->SendOpers(std::string("FilterPCRE: ")+std::string(user->nick)+
+ std::string(" had their notice filtered, target was ")+
+ target+": "+reason);
+ Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
+ " :Your notice has been filtered and opers notified: "+reason);
+ }
+ Srv->Log(DEFAULT,std::string("FilterPCRE: ")+std::string(user->nick)+
+ std::string(" had their notice filtered, target was ")+
+ target+": "+reason+" Action: "+do_action);
+
+ if (do_action == "kill")
+ {
+ Srv->QuitUser(user,reason);
+ }
+ return 1;
+ }
+ }
+ return 0;
+ }
+
+ virtual void OnRehash()
+ {
+ // reload our config file on rehash - we must destroy and re-allocate the classes
+ // to call the constructor again and re-read our data.
+ delete Conf;
+ delete MyConf;
+ Conf = new ConfigReader;
+ std::string filterfile = Conf->ReadValue("filter","file",0);
+ // this automatically re-reads the configuration file into the class
+ MyConf = new ConfigReader(filterfile);
+ if ((filterfile == "") || (!MyConf->Verify()))
+ {
+ // bail if the user forgot to create a config file
+ printf("Error, could not find <filter file=\"\"> definition in your config file!");
+ log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
+ return;
+ }
+ Srv->Log(DEFAULT,std::string("m_Filter_pcre: read configuration from ")+filterfile);
+
+ filters.clear();
+ for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
+ {
+ std::string pattern = MyConf->ReadValue("keyword","pattern",index);
+ re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
+ if (!re)
+ {
+ log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
+ log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
+ }
+ else
+ {
+ filters.push_back(re);
+ log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
+ }
+ }
+
+ }
+
+ virtual Version GetVersion()
+ {
+ // This is version 2 because version 1.x is the unreleased unrealircd module
+ return Version(3,0,0,0,VF_VENDOR);
+ }
+
+};
+
+// stuff down here is the module-factory stuff. For basic modules you can ignore this.
+
+class ModuleFilterPCREFactory : public ModuleFactory
+{
+ public:
+ ModuleFilterPCREFactory()
+ {
+ }
+
+ ~ModuleFilterPCREFactory()
+ {
+ }
+
+ virtual Module * CreateModule()
+ {
+ return new ModuleFilterPCRE;
+ }
+
+};
+
+
+extern "C" void * init_module( void )
+{
+ return new ModuleFilterPCREFactory;
+}
+