]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
Add -Wshadow to cflags, and fix a bunch of warnings that come with it. Add a note...
[user/henk/code/inspircd.git] / src / modules / m_filter.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 "m_filter.h"
16
17 /* $ModDesc: An advanced spam filtering module */
18 /* $ModDep: m_filter.h */
19
20 typedef std::map<std::string,FilterResult*> filter_t;
21
22 class ModuleFilter : public FilterBase
23 {
24  
25  filter_t filters;
26
27  public:
28         ModuleFilter(InspIRCd* Me)
29         : FilterBase(Me, "m_filter.so")
30         {
31                 OnRehash(NULL,"");
32
33         }
34         
35         virtual ~ModuleFilter()
36         {
37         }
38
39         virtual FilterResult* FilterMatch(User* user, const std::string &text, int iflags)
40         {
41                 for (filter_t::iterator index = filters.begin(); index != filters.end(); index++)
42                 {
43
44                         /* Skip ones that dont apply to us */
45                         if (!FilterBase::AppliesToMe(user, index->second, iflags))
46                                 continue;
47
48                         if (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, const std::string &sflags)
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(freeform, reason, type, duration, sflags);
82                 filters[freeform] = x;
83
84                 return std::make_pair(true, "");
85         }
86
87         virtual void SyncFilters(Module* proto, void* opaque)
88         {
89                 for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
90                 {
91                         this->SendFilter(proto, opaque, n->second);
92                 }
93         }
94
95         virtual void OnRehash(User* user, const std::string &parameter)
96         {
97                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
98
99                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
100                 {
101                         this->DeleteFilter(MyConf->ReadValue("keyword","pattern",index));
102
103                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
104                         std::string reason = MyConf->ReadValue("keyword","reason",index);
105                         std::string do_action = MyConf->ReadValue("keyword","action",index);
106                         std::string sflags = MyConf->ReadValue("keyword","flags",index);
107                         long gline_time = ServerInstance->Duration(MyConf->ReadValue("keyword","duration",index));
108                         if (do_action.empty())
109                                 do_action = "none";
110                         if (sflags.empty())
111                                 sflags = "*";
112                         FilterResult* x = new FilterResult(pattern, reason, do_action, gline_time, sflags);
113                         filters[pattern] = x;
114                 }
115                 delete MyConf;
116                 FilterBase::OnRehash(user, parameter);
117         }
118
119         virtual int OnStats(char symbol, User* user, string_list &results)
120         {
121                 if (symbol == 's')
122                 {
123                         std::string sn = ServerInstance->Config->ServerName;
124                         for (filter_t::iterator n = filters.begin(); n != filters.end(); n++)
125                         {
126                                 results.push_back(sn+" 223 "+user->nick+" :GLOB:"+n->second->freeform+" "+n->second->flags+" "+n->second->action+" "+ConvToStr(n->second->gline_time)+" :"+n->second->reason);
127                         }
128                 }
129                 return 0;
130         }
131 };
132
133
134 MODULE_INIT(ModuleFilter)