]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
Add extra /map info (connection uptime, and lag time) to /MAP for opers. Adds feature...
[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                                 return index->second;
51                         }
52                 }
53                 return NULL;
54         }
55
56         virtual bool DeleteFilter(const std::string &freeform)
57         {
58                 if (filters.find(freeform) != filters.end())
59                 {
60                         delete (filters.find(freeform))->second;
61                         filters.erase(filters.find(freeform));
62                         return true;
63                 }
64                 return false;
65         }
66
67         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration)
68         {
69                 if (filters.find(freeform) != filters.end())
70                 {
71                         return std::make_pair(false, "Filter already exists");
72                 }
73
74                 FilterResult* x = new FilterResult;
75                 x->reason = reason;
76                 x->action = type;
77                 x->gline_time = duration;
78                 x->freeform = freeform;
79                 filters[freeform] = x;
80
81                 return std::make_pair(true, "");
82         }
83
84         virtual void SyncFilters(Module* proto, void* opaque)
85         {
86                 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
87                 {
88                         this->SendFilter(proto, opaque, n->second);
89                 }
90         }
91
92         virtual void OnRehash(userrec* user, const std::string &parameter)
93         {
94                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
95
96                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
97                 {
98                         this->DeleteFilter(MyConf->ReadValue("keyword","pattern",index));
99
100                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
101                         std::string reason = MyConf->ReadValue("keyword","reason",index);
102                         std::string do_action = MyConf->ReadValue("keyword","action",index);
103                         long gline_time = ServerInstance->Duration(MyConf->ReadValue("keyword","duration",index).c_str());
104                         if (do_action == "")
105                                 do_action = "none";
106                         FilterResult* x = new FilterResult;
107                         x->reason = reason;
108                         x->action = do_action;
109                         x->gline_time = gline_time;
110                         x->freeform = pattern;
111                         filters[pattern] = x;
112                 }
113                 DELETE(MyConf);
114         }
115
116         virtual int OnStats(char symbol, userrec* user, string_list &results)
117         {
118                 if (symbol == 's')
119                 {
120                         std::string sn = ServerInstance->Config->ServerName;
121                         for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
122                         {
123                                 results.push_back(sn+" 223 "+user->nick+" :GLOB:"+n->second->freeform+" "+n->second->action+" "+ConvToStr(n->second->gline_time)+" :"+n->second->reason);
124                         }
125                 }
126                 return 0;
127         }
128 };
129
130 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
131
132 class ModuleFilterFactory : public ModuleFactory
133 {
134  public:
135         ModuleFilterFactory()
136         {
137         }
138         
139         ~ModuleFilterFactory()
140         {
141         }
142         
143         virtual Module * CreateModule(InspIRCd* Me)
144         {
145                 return new ModuleFilter(Me);
146         }
147         
148 };
149
150
151 extern "C" void * init_module( void )
152 {
153         return new ModuleFilterFactory;
154 }
155