]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_filter_pcre.cpp
InspSocket -> BufferedSocket. Paves the way for a SimpleSocket class which ident...
[user/henk/code/inspircd.git] / src / modules / extra / m_filter_pcre.cpp
index 0991c7def9c19bfa7b678a5494a01f47fe58b751..43a767a1ccbe2e1e398c5f58c9cf12e2e96e79cb 100644 (file)
@@ -2,43 +2,42 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
- *                       E-mail:
- *                <brain@chatspike.net>
- *               <Craig@chatspike.net>
- *     
- * Written by Craig Edwards, Craig McLure, and others.
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
  * This program is free but copyrighted software; see
- *            the file COPYING for details.
+ *         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 "inspircd.h"
 #include <pcre.h>
 #include "users.h"
 #include "channels.h"
 #include "modules.h"
-#include "inspircd.h"
 #include "m_filter.h"
 
 /* $ModDesc: m_filter with regexps */
-/* $CompileFlags: `pcre-config --cflags` */
-/* $LinkerFlags: `pcre-config --libs` `perl extra/pcre_rpath.pl` -lpcre */
+/* $CompileFlags: exec("pcre-config --cflags") */
+/* $LinkerFlags: exec("pcre-config --libs") rpath("pcre-config --libs") -lpcre */
 /* $ModDep: m_filter.h */
 
+#ifdef WINDOWS
+#pragma comment(lib, "pcre.lib")
+#endif
+
 class PCREFilter : public FilterResult
 {
  public:
         pcre* regexp;
 
-        PCREFilter(pcre* r, const std::string &rea, const std::string &act, long gline_time, const std::string &pat)
-                : FilterResult::FilterResult(pat, rea, act, gline_time), regexp(r)
+        PCREFilter(pcre* r, const std::string &rea, const std::string &act, long gline_time, const std::string &pat, const std::string &flags)
+                : FilterResult(pat, rea, act, gline_time, flags), regexp(r)
+        {
+        }
+
+        PCREFilter()
         {
         }
 };
@@ -49,27 +48,37 @@ class ModuleFilterPCRE : public FilterBase
        pcre *re;
        const char *error;
        int erroffset;
+       PCREFilter fr;
 
  public:
        ModuleFilterPCRE(InspIRCd* Me)
-       : FilterBase::FilterBase(Me, "m_filter_pcre.so")
+       : FilterBase(Me, "m_filter_pcre.so")
        {
-               OnRehash("");
+               OnRehash(NULL,"");
        }
 
        virtual ~ModuleFilterPCRE()
        {
        }
 
-       virtual FilterResult* FilterMatch(const std::string &text)
+       virtual FilterResult* FilterMatch(User* user, const std::string &text, int flags)
        {
-               for (unsigned int index = 0; index < filters.size(); index++)
+               for (std::vector<PCREFilter>::iterator index = filters.begin(); index != filters.end(); index++)
                {
-                       PCREFilter& filt = filters[index];
-                       
-                       if (pcre_exec(filt.regexp,NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
+                       /* Skip ones that dont apply to us */
+
+                       if (!FilterBase::AppliesToMe(user, dynamic_cast<FilterResult*>(&(*index)), flags))
+                               continue;
+
+                       if (pcre_exec(index->regexp, NULL, text.c_str(), text.length(), 0, 0, NULL, 0) > -1)
                        {
-                               return &filt;
+                               fr = *index;
+                               if (index != filters.begin())
+                               {
+                                       filters.erase(index);
+                                       filters.insert(filters.begin(), fr);
+                               }
+                               return &fr;
                        }
                }
                return NULL;
@@ -97,7 +106,7 @@ class ModuleFilterPCRE : public FilterBase
                }
        }
 
-       virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration)
+       virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration, const std::string &flags)
        {
                for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
                {
@@ -117,12 +126,12 @@ class ModuleFilterPCRE : public FilterBase
                }
                else
                {
-                       filters.push_back(PCREFilter(re, reason, type, duration, freeform));
+                       filters.push_back(PCREFilter(re, reason, type, duration, freeform, flags));
                        return std::make_pair(true, "");
                }
        }
 
-       virtual void OnRehash(const std::string &parameter)
+       virtual void OnRehash(User* user, const std::string &parameter)
        {               
                ConfigReader MyConf(ServerInstance);
 
@@ -133,7 +142,12 @@ class ModuleFilterPCRE : public FilterBase
                        std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
                        std::string reason = MyConf.ReadValue("keyword", "reason", index);
                        std::string action = MyConf.ReadValue("keyword", "action", index);
-                       long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index).c_str());
+                       std::string flags = MyConf.ReadValue("keyword", "flags", index);
+                       long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index));
+                       if (action.empty())
+                               action = "none";
+                       if (flags.empty())
+                               flags = "*";
 
                        re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
 
@@ -144,48 +158,25 @@ class ModuleFilterPCRE : public FilterBase
                        }
                        else
                        {
-                               filters.push_back(PCREFilter(re, reason, action, gline_time, pattern));
+                               filters.push_back(PCREFilter(re, reason, action, gline_time, pattern, flags));
                                ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
                        }
                }
        }
 
-       virtual int OnStats(char symbol, userrec* user, string_list &results)
+       virtual int OnStats(char symbol, User* user, string_list &results)
        {
                if (symbol == 's')
                {
                        std::string sn = ServerInstance->Config->ServerName;
                        for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
                        {
-                               results.push_back(sn+" 223 "+user->nick+" :"+i->freeform+" "+i->action+" "+ConvToStr(i->gline_time)+" :"+i->reason);
+                               results.push_back(sn+" 223 "+user->nick+" :REGEXP:"+i->freeform+" "+i->flags+" "+i->action+" "+ConvToStr(i->gline_time)+" :"+i->reason);
                        }
-                       return 1;
                }
                return 0;
        }
 };
-       
 
-class ModuleFilterPCREFactory : public ModuleFactory
-{
- public:
-       ModuleFilterPCREFactory()
-       {
-       }
-       
-       ~ModuleFilterPCREFactory()
-       {
-       }
-       
-       virtual Module * CreateModule(InspIRCd* Me)
-       {
-               return new ModuleFilterPCRE(Me);
-       }
-       
-};
+MODULE_INIT(ModuleFilterPCRE);
 
-
-extern "C" void * init_module( void )
-{
-       return new ModuleFilterPCREFactory;
-}