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