]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
Whoops, properly handle rehash now that new filters might be added by /FILTER
[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                         delete (filters.find(freeform))->second;
70                         filters.erase(filters.find(freeform));
71                         return true;
72                 }
73                 return false;
74         }
75
76         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration)
77         {
78                 if (filters.find(freeform) != filters.end())
79                 {
80                         return std::make_pair(false, "Filter already exists");
81                 }
82
83                 FilterResult* x = new FilterResult;
84                 x->reason = reason;
85                 x->action = type;
86                 x->gline_time = duration;
87                 x->freeform = freeform;
88                 filters[freeform] = x;
89
90                 return std::make_pair(true, "");
91         }
92
93         virtual void SyncFilters(Module* proto, void* opaque)
94         {
95                 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
96                 {
97                         this->SendFilter(proto, opaque, n->second);
98                 }
99         }
100
101         virtual void OnRehash(const std::string &parameter)
102         {
103                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
104
105                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
106                 {
107                         this->DeleteFilter(MyConf->ReadValue("keyword","pattern",index));
108
109                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
110                         std::string reason = MyConf->ReadValue("keyword","reason",index);
111                         std::string do_action = MyConf->ReadValue("keyword","action",index);
112                         long gline_time = ServerInstance->Duration(MyConf->ReadValue("keyword","duration",index).c_str());
113                         if (do_action == "")
114                                 do_action = "none";
115                         FilterResult* x = new FilterResult;
116                         x->reason = reason;
117                         x->action = do_action;
118                         x->gline_time = gline_time;
119                         x->freeform = pattern;
120                         filters[pattern] = x;
121                 }
122                 DELETE(MyConf);
123         }
124 };
125
126 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
127
128 class ModuleFilterFactory : public ModuleFactory
129 {
130  public:
131         ModuleFilterFactory()
132         {
133         }
134         
135         ~ModuleFilterFactory()
136         {
137         }
138         
139         virtual Module * CreateModule(InspIRCd* Me)
140         {
141                 return new ModuleFilter(Me);
142         }
143         
144 };
145
146
147 extern "C" void * init_module( void )
148 {
149         return new ModuleFilterFactory;
150 }
151