]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
A flags section for filters. Do not use yet.
[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, bool operexclusion)
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                 x->opers_exempt = operexclusion;
84                 filters[freeform] = x;
85
86                 return std::make_pair(true, "");
87         }
88
89         virtual void SyncFilters(Module* proto, void* opaque)
90         {
91                 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
92                 {
93                         this->SendFilter(proto, opaque, n->second);
94                 }
95         }
96
97         virtual void OnRehash(userrec* user, const std::string &parameter)
98         {
99                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
100
101                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
102                 {
103                         this->DeleteFilter(MyConf->ReadValue("keyword","pattern",index));
104
105                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
106                         std::string reason = MyConf->ReadValue("keyword","reason",index);
107                         std::string do_action = MyConf->ReadValue("keyword","action",index);
108                         long gline_time = ServerInstance->Duration(MyConf->ReadValue("keyword","duration",index).c_str());
109                         if (do_action == "")
110                                 do_action = "none";
111                         FilterResult* x = new FilterResult;
112                         x->reason = reason;
113                         x->action = do_action;
114                         x->gline_time = gline_time;
115                         x->freeform = pattern;
116                         filters[pattern] = x;
117                 }
118                 DELETE(MyConf);
119         }
120
121         virtual int OnStats(char symbol, userrec* user, string_list &results)
122         {
123                 if (symbol == 's')
124                 {
125                         std::string sn = ServerInstance->Config->ServerName;
126                         for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
127                         {
128                                 results.push_back(sn+" 223 "+user->nick+" :GLOB:"+n->second->freeform+" "+n->second->action+" "+ConvToStr(n->second->gline_time)+" :"+n->second->reason);
129                         }
130                 }
131                 return 0;
132         }
133 };
134
135 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
136
137 class ModuleFilterFactory : public ModuleFactory
138 {
139  public:
140         ModuleFilterFactory()
141         {
142         }
143         
144         ~ModuleFilterFactory()
145         {
146         }
147         
148         virtual Module * CreateModule(InspIRCd* Me)
149         {
150                 return new ModuleFilter(Me);
151         }
152         
153 };
154
155
156 extern "C" DllExport void * init_module( void )
157 {
158         return new ModuleFilterFactory;
159 }
160