]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
133406ccb7e33d9f4fd51a898cb98e19ef565000
[user/henk/code/inspircd.git] / src / modules / m_filter.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <stdio.h>
15 #include <string>
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "inspircd.h"
20 #include "m_filter.h"
21
22 /* $ModDesc: An advanced spam filtering module */
23 /* $ModDep: m_filter.h */
24
25 typedef std::map<std::string,FilterResult*> filter_t;
26
27 class ModuleFilter : public FilterBase
28 {
29  
30  filter_t filters;
31
32  public:
33         ModuleFilter(InspIRCd* Me)
34         : FilterBase::FilterBase(Me, "m_filter.so")
35         {
36                 OnRehash(NULL,"");
37         }
38         
39         virtual ~ModuleFilter()
40         {
41         }
42
43         virtual FilterResult* FilterMatch(const std::string &text)
44         {
45                 std::string text2 = text+" ";
46                 for (filter_t::iterator index = filters.begin(); index != filters.end(); index++)
47                 {
48                         if ((ServerInstance->MatchText(text2,index->first)) || (ServerInstance->MatchText(text,index->first)))
49                         {
50                                 FilterResult* fr = index->second;
51                                 if (index != filters.begin())
52                                 {
53                                         std::string pat = index->first;
54                                         filters.erase(index);
55                                         filters.insert(filters.begin(), std::make_pair(pat,fr));
56                                 }
57                                 return fr;
58                         }
59                 }
60                 return NULL;
61         }
62
63         virtual bool DeleteFilter(const std::string &freeform)
64         {
65                 if (filters.find(freeform) != filters.end())
66                 {
67                         delete (filters.find(freeform))->second;
68                         filters.erase(filters.find(freeform));
69                         return true;
70                 }
71                 return false;
72         }
73
74         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration)
75         {
76                 if (filters.find(freeform) != filters.end())
77                 {
78                         return std::make_pair(false, "Filter already exists");
79                 }
80
81                 FilterResult* x = new FilterResult;
82                 x->reason = reason;
83                 x->action = type;
84                 x->gline_time = duration;
85                 x->freeform = freeform;
86                 filters[freeform] = x;
87
88                 return std::make_pair(true, "");
89         }
90
91         virtual void SyncFilters(Module* proto, void* opaque)
92         {
93                 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
94                 {
95                         this->SendFilter(proto, opaque, n->second);
96                 }
97         }
98
99         virtual void OnRehash(userrec* user, const std::string &parameter)
100         {
101                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
102
103                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
104                 {
105                         this->DeleteFilter(MyConf->ReadValue("keyword","pattern",index));
106
107                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
108                         std::string reason = MyConf->ReadValue("keyword","reason",index);
109                         std::string do_action = MyConf->ReadValue("keyword","action",index);
110                         long gline_time = ServerInstance->Duration(MyConf->ReadValue("keyword","duration",index).c_str());
111                         if (do_action == "")
112                                 do_action = "none";
113                         FilterResult* x = new FilterResult;
114                         x->reason = reason;
115                         x->action = do_action;
116                         x->gline_time = gline_time;
117                         x->freeform = pattern;
118                         filters[pattern] = x;
119                 }
120                 DELETE(MyConf);
121         }
122
123         virtual int OnStats(char symbol, userrec* user, string_list &results)
124         {
125                 if (symbol == 's')
126                 {
127                         std::string sn = ServerInstance->Config->ServerName;
128                         for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
129                         {
130                                 results.push_back(sn+" 223 "+user->nick+" :GLOB:"+n->second->freeform+" "+n->second->action+" "+ConvToStr(n->second->gline_time)+" :"+n->second->reason);
131                         }
132                 }
133                 return 0;
134         }
135 };
136
137 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
138
139 class ModuleFilterFactory : public ModuleFactory
140 {
141  public:
142         ModuleFilterFactory()
143         {
144         }
145         
146         ~ModuleFilterFactory()
147         {
148         }
149         
150         virtual Module * CreateModule(InspIRCd* Me)
151         {
152                 return new ModuleFilter(Me);
153         }
154         
155 };
156
157
158 extern "C" void * init_module( void )
159 {
160         return new ModuleFilterFactory;
161 }
162