]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
97cc8af5f8ba43c77cad11c7cbc4d488d16bf604
[user/henk/code/inspircd.git] / src / modules / m_filter.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 // Message and notice filtering using glob patterns
20 // a module based on the original work done by Craig Edwards in 2003
21 // for the chatspike network.
22
23 #include <stdio.h>
24 #include <string>
25 #include "users.h"
26 #include "channels.h"
27 #include "modules.h"
28 #include "inspircd.h"
29 #include "m_filter.h"
30
31 /* $ModDesc: An advanced spam filtering module */
32 /* $ModDep: m_filter.h */
33
34 typedef std::map<std::string,FilterResult*> filter_t;
35
36 class ModuleFilter : public FilterBase
37 {
38  
39  filter_t filters;
40
41  public:
42         ModuleFilter(InspIRCd* Me)
43                 : FilterBase::FilterBase(Me)
44         {
45                 // read the configuration file on startup.
46                 // it is perfectly valid to set <filter file> to the value of the
47                 // main config file, then append your <keyword> tags to the bottom
48                 // of the main config... but rather messy. That's why the capability
49                 // of using a seperate config file is provided.
50                 OnRehash("");
51         }
52         
53         virtual ~ModuleFilter()
54         {
55         }
56
57         virtual FilterResult* FilterMatch(const std::string &text)
58         {
59                 std::string text2 = text+" ";
60                 for (filter_t::iterator index = filters.begin(); index != filters.end(); index++)
61                 {
62                         if ((ServerInstance->MatchText(text2,index->first)) || (ServerInstance->MatchText(text,index->first)))
63                         {
64                                 return index->second;
65                         }
66                 }
67                 return NULL;
68         }
69         
70         virtual void OnRehash(const std::string &parameter)
71         {
72                 // this automatically re-reads the configuration file into the class
73                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
74                 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
75                 {
76                         DELETE(n->second);
77                 }
78                 filters.clear();
79                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
80                 {
81                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
82                         std::string reason = MyConf->ReadValue("keyword","reason",index);
83                         std::string do_action = MyConf->ReadValue("keyword","action",index);
84                         long gline_time = ServerInstance->Duration(MyConf->ReadValue("keyword","duration",index).c_str());
85                         if (do_action == "")
86                                 do_action = "none";
87                         FilterResult* x = new FilterResult;
88                         x->reason = reason;
89                         x->action = do_action;
90                         x->gline_time = gline_time;
91                         filters[pattern] = x;
92                 }
93                 DELETE(MyConf);
94         }
95 };
96
97 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
98
99 class ModuleFilterFactory : public ModuleFactory
100 {
101  public:
102         ModuleFilterFactory()
103         {
104         }
105         
106         ~ModuleFilterFactory()
107         {
108         }
109         
110         virtual Module * CreateModule(InspIRCd* Me)
111         {
112                 return new ModuleFilter(Me);
113         }
114         
115 };
116
117
118 extern "C" void * init_module( void )
119 {
120         return new ModuleFilterFactory;
121 }
122