]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
0b6c386ba42c5ba82a6fa9a73bc19a0da1d6e46d
[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, "m_filter.so")
44         {
45                 OnRehash("");
46         }
47         
48         virtual ~ModuleFilter()
49         {
50         }
51
52         virtual FilterResult* FilterMatch(const std::string &text)
53         {
54                 std::string text2 = text+" ";
55                 for (filter_t::iterator index = filters.begin(); index != filters.end(); index++)
56                 {
57                         if ((ServerInstance->MatchText(text2,index->first)) || (ServerInstance->MatchText(text,index->first)))
58                         {
59                                 return index->second;
60                         }
61                 }
62                 return NULL;
63         }
64
65         virtual bool DeleteFilter(const std::string &freeform)
66         {
67                 if (filters.find(freeform) != filters.end())
68                 {
69                         filters.erase(filters.find(freeform));
70                         return true;
71                 }
72                 return false;
73         }
74
75         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration)
76         {
77                 if (filters.find(freeform) != filters.end())
78                 {
79                         return std::make_pair(false, "Filter already exists");
80                 }
81
82                 FilterResult* x = new FilterResult;
83                 x->reason = reason;
84                 x->action = type;
85                 x->gline_time = duration;
86                 filters[freeform] = x;
87
88                 return std::make_pair(true, "");
89         }
90
91         virtual void OnRehash(const std::string &parameter)
92         {
93                 // this automatically re-reads the configuration file into the class
94                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
95                 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
96                 {
97                         DELETE(n->second);
98                 }
99                 filters.clear();
100                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
101                 {
102                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
103                         std::string reason = MyConf->ReadValue("keyword","reason",index);
104                         std::string do_action = MyConf->ReadValue("keyword","action",index);
105                         long gline_time = ServerInstance->Duration(MyConf->ReadValue("keyword","duration",index).c_str());
106                         if (do_action == "")
107                                 do_action = "none";
108                         FilterResult* x = new FilterResult;
109                         x->reason = reason;
110                         x->action = do_action;
111                         x->gline_time = gline_time;
112                         filters[pattern] = x;
113                 }
114                 DELETE(MyConf);
115         }
116 };
117
118 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
119
120 class ModuleFilterFactory : public ModuleFactory
121 {
122  public:
123         ModuleFilterFactory()
124         {
125         }
126         
127         ~ModuleFilterFactory()
128         {
129         }
130         
131         virtual Module * CreateModule(InspIRCd* Me)
132         {
133                 return new ModuleFilter(Me);
134         }
135         
136 };
137
138
139 extern "C" void * init_module( void )
140 {
141         return new ModuleFilterFactory;
142 }
143