]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
e0b30163c28c5af598e895c69973167ad4ff8e08
[user/henk/code/inspircd.git] / src / modules / m_messageflood.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
8  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
10  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27
28 /* $ModDesc: Provides channel mode +f (message flood protection) */
29
30 /** Holds flood settings and state for mode +f
31  */
32 class floodsettings
33 {
34  public:
35         bool ban;
36         unsigned int secs;
37         unsigned int lines;
38         time_t reset;
39         std::map<User*, unsigned int> counters;
40
41         floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
42         {
43                 reset = ServerInstance->Time() + secs;
44         }
45
46         bool addmessage(User* who)
47         {
48                 if (ServerInstance->Time() > reset)
49                 {
50                         counters.clear();
51                         reset = ServerInstance->Time() + secs;
52                 }
53
54                 return (++counters[who] >= this->lines);
55         }
56
57         void clear(User* who)
58         {
59                 std::map<User*, unsigned int>::iterator iter = counters.find(who);
60                 if (iter != counters.end())
61                 {
62                         counters.erase(iter);
63                 }
64         }
65 };
66
67 /** Handles channel mode +f
68  */
69 class MsgFlood : public ModeHandler
70 {
71  public:
72         SimpleExtItem<floodsettings> ext;
73         MsgFlood(Module* Creator) : ModeHandler(Creator, "flood", 'f', PARAM_SETONLY, MODETYPE_CHANNEL),
74                 ext("messageflood", Creator) { }
75
76         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
77         {
78                 floodsettings *f = ext.get(channel);
79
80                 if (adding)
81                 {
82                         std::string::size_type colon = parameter.find(':');
83                         if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
84                         {
85                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
86                                 return MODEACTION_DENY;
87                         }
88
89                         /* Set up the flood parameters for this channel */
90                         bool ban = (parameter[0] == '*');
91                         unsigned int nlines = ConvToInt(parameter.substr(ban ? 1 : 0, ban ? colon-1 : colon));
92                         unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
93
94                         if ((nlines<2) || (nsecs<1))
95                         {
96                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
97                                 return MODEACTION_DENY;
98                         }
99
100                         if ((f) && (nlines == f->lines) && (nsecs == f->secs) && (ban == f->ban))
101                                 // mode params match
102                                 return MODEACTION_DENY;
103
104                         ext.set(channel, new floodsettings(ban, nsecs, nlines));
105                         parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" + ConvToStr(nsecs);
106                         channel->SetModeParam('f', parameter);
107                         return MODEACTION_ALLOW;
108                 }
109                 else
110                 {
111                         if (f)
112                         {
113                                 ext.unset(channel);
114                                 channel->SetModeParam('f', "");
115                                 return MODEACTION_ALLOW;
116                         }
117                 }
118
119                 return MODEACTION_DENY;
120         }
121 };
122
123 class ModuleMsgFlood : public Module
124 {
125         MsgFlood mf;
126
127  public:
128
129         ModuleMsgFlood()
130                 : mf(this)
131         {
132                 if (!ServerInstance->Modes->AddMode(&mf))
133                         throw ModuleException("Could not add new modes!");
134                 ServerInstance->Extensions.Register(&mf.ext);
135                 Implementation eventlist[] = { I_OnUserPreNotice, I_OnUserPreMessage };
136                 ServerInstance->Modules->Attach(eventlist, this, 2);
137         }
138
139         ModResult ProcessMessages(User* user,Channel* dest, const std::string &text)
140         {
141                 if ((!IS_LOCAL(user)) || !dest->IsModeSet('f'))
142                         return MOD_RES_PASSTHRU;
143
144                 if (ServerInstance->OnCheckExemption(user,dest,"flood") == MOD_RES_ALLOW)
145                         return MOD_RES_PASSTHRU;
146
147                 floodsettings *f = mf.ext.get(dest);
148                 if (f)
149                 {
150                         if (f->addmessage(user))
151                         {
152                                 /* Youre outttta here! */
153                                 f->clear(user);
154                                 if (f->ban)
155                                 {
156                                         std::vector<std::string> parameters;
157                                         parameters.push_back(dest->name);
158                                         parameters.push_back("+b");
159                                         parameters.push_back(user->MakeWildHost());
160                                         ServerInstance->SendGlobalMode(parameters, ServerInstance->FakeClient);
161                                 }
162
163                                 char kickmessage[MAXBUF];
164                                 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %u lines in %u secs)", f->lines, f->secs);
165
166                                 dest->KickUser(ServerInstance->FakeClient, user, kickmessage);
167
168                                 return MOD_RES_DENY;
169                         }
170                 }
171
172                 return MOD_RES_PASSTHRU;
173         }
174
175         ModResult OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
176         {
177                 if (target_type == TYPE_CHANNEL)
178                         return ProcessMessages(user,(Channel*)dest,text);
179
180                 return MOD_RES_PASSTHRU;
181         }
182
183         ModResult OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
184         {
185                 if (target_type == TYPE_CHANNEL)
186                         return ProcessMessages(user,(Channel*)dest,text);
187
188                 return MOD_RES_PASSTHRU;
189         }
190
191         ~ModuleMsgFlood()
192         {
193         }
194
195         Version GetVersion()
196         {
197                 return Version("Provides channel mode +f (message flood protection)", VF_VENDOR);
198         }
199 };
200
201 MODULE_INIT(ModuleMsgFlood)