]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
88665e8b3ac59fe5b412fdde34070f052130c845
[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                 x->freeform = freeform;
87                 filters[freeform] = x;
88
89                 return std::make_pair(true, "");
90         }
91
92         virtual void SyncFilters(Module* proto, void* opaque)
93         {
94                 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
95                 {
96                         this->SendFilter(proto, opaque, n->second);
97                 }
98         }
99
100         virtual void OnRehash(const std::string &parameter)
101         {
102                 // this automatically re-reads the configuration file into the class
103                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
104                 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
105                 {
106                         DELETE(n->second);
107                 }
108                 filters.clear();
109                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
110                 {
111                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
112                         std::string reason = MyConf->ReadValue("keyword","reason",index);
113                         std::string do_action = MyConf->ReadValue("keyword","action",index);
114                         long gline_time = ServerInstance->Duration(MyConf->ReadValue("keyword","duration",index).c_str());
115                         if (do_action == "")
116                                 do_action = "none";
117                         FilterResult* x = new FilterResult;
118                         x->reason = reason;
119                         x->action = do_action;
120                         x->gline_time = gline_time;
121                         x->freeform = pattern;
122                         filters[pattern] = x;
123                 }
124                 DELETE(MyConf);
125         }
126 };
127
128 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
129
130 class ModuleFilterFactory : public ModuleFactory
131 {
132  public:
133         ModuleFilterFactory()
134         {
135         }
136         
137         ~ModuleFilterFactory()
138         {
139         }
140         
141         virtual Module * CreateModule(InspIRCd* Me)
142         {
143                 return new ModuleFilter(Me);
144         }
145         
146 };
147
148
149 extern "C" void * init_module( void )
150 {
151         return new ModuleFilterFactory;
152 }
153