]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
Make OnChannelRestrictionApply take a User* instead of a Membership* [jackmcbarn]
[user/henk/code/inspircd.git] / src / modules / m_messageflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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
16 /* $ModDesc: Provides channel mode +f (message flood protection) */
17
18 /** Holds flood settings and state for mode +f
19  */
20 class floodsettings : public classbase
21 {
22  private:
23         InspIRCd *ServerInstance;
24  public:
25         bool ban;
26         int secs;
27         int lines;
28         time_t reset;
29         std::map<User*,int> counters;
30
31         floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
32         {
33                 reset = ServerInstance->Time() + secs;
34         };
35
36         void addmessage(User* who)
37         {
38                 std::map<User*,int>::iterator iter = counters.find(who);
39                 if (iter != counters.end())
40                 {
41                         iter->second++;
42                 }
43                 else
44                 {
45                         counters[who] = 1;
46                 }
47                 if (ServerInstance->Time() > reset)
48                 {
49                         counters.clear();
50                         reset = ServerInstance->Time() + secs;
51                 }
52         }
53
54         bool shouldkick(User* who)
55         {
56                 std::map<User*,int>::iterator iter = counters.find(who);
57                 if (iter != counters.end())
58                 {
59                         return (iter->second >= this->lines);
60                 }
61                 else return false;
62         }
63
64         void clear(User* who)
65         {
66                 std::map<User*,int>::iterator iter = counters.find(who);
67                 if (iter != counters.end())
68                 {
69                         counters.erase(iter);
70                 }
71         }
72 };
73
74 /** Handles channel mode +f
75  */
76 class MsgFlood : public ModeHandler
77 {
78  public:
79         SimpleExtItem<floodsettings> ext;
80         MsgFlood(Module* Creator) : ModeHandler(Creator, "flood", 'f', PARAM_SETONLY, MODETYPE_CHANNEL),
81                 ext("messageflood", Creator) { }
82
83         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
84         {
85                 floodsettings* x = ext.get(channel);
86                 if (x)
87                         return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
88                 else
89                         return std::make_pair(false, parameter);
90         }
91
92         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
93         {
94                 floodsettings *f = ext.get(channel);
95
96                 if (adding)
97                 {
98                         char ndata[MAXBUF];
99                         char* data = ndata;
100                         strlcpy(ndata,parameter.c_str(),MAXBUF);
101                         char* lines = data;
102                         char* secs = NULL;
103                         bool ban = false;
104                         if (*data == '*')
105                         {
106                                 ban = true;
107                                 lines++;
108                         }
109                         else
110                         {
111                                 ban = false;
112                         }
113                         while (*data)
114                         {
115                                 if (*data == ':')
116                                 {
117                                         *data = 0;
118                                         data++;
119                                         secs = data;
120                                         break;
121                                 }
122                                 else data++;
123                         }
124                         if (secs)
125                         {
126                                 /* Set up the flood parameters for this channel */
127                                 int nlines = atoi(lines);
128                                 int nsecs = atoi(secs);
129                                 if ((nlines<2) || (nsecs<1))
130                                 {
131                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
132                                         parameter.clear();
133                                         return MODEACTION_DENY;
134                                 }
135                                 else
136                                 {
137                                         if (!f)
138                                         {
139                                                 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
140                                                 f = new floodsettings(ban,nsecs,nlines);
141                                                 ext.set(channel, f);
142                                                 channel->SetModeParam('f', parameter);
143                                                 return MODEACTION_ALLOW;
144                                         }
145                                         else
146                                         {
147                                                 std::string cur_param = channel->GetModeParameter('f');
148                                                 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
149                                                 if (cur_param == parameter)
150                                                 {
151                                                         // mode params match
152                                                         return MODEACTION_DENY;
153                                                 }
154                                                 else
155                                                 {
156                                                         if ((((nlines != f->lines) || (nsecs != f->secs) || (ban != f->ban))) && (((nsecs > 0) && (nlines > 0))))
157                                                         {
158                                                                 floodsettings *fs = new floodsettings(ban,nsecs,nlines);
159                                                                 ext.set(channel, fs);
160                                                                 channel->SetModeParam('f', parameter);
161                                                                 return MODEACTION_ALLOW;
162                                                         }
163                                                         else
164                                                         {
165                                                                 return MODEACTION_DENY;
166                                                         }
167                                                 }
168                                         }
169                                 }
170                         }
171                         else
172                         {
173                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
174                                 parameter.clear();
175                                 return MODEACTION_DENY;
176                         }
177                 }
178                 else
179                 {
180                         if (f)
181                         {
182                                 ext.unset(channel);
183                                 channel->SetModeParam('f', "");
184                                 return MODEACTION_ALLOW;
185                         }
186                 }
187
188                 return MODEACTION_DENY;
189         }
190 };
191
192 class ModuleMsgFlood : public Module
193 {
194         MsgFlood mf;
195
196  public:
197
198         ModuleMsgFlood()
199                 : mf(this)
200         {
201                 if (!ServerInstance->Modes->AddMode(&mf))
202                         throw ModuleException("Could not add new modes!");
203                 Extensible::Register(&mf.ext);
204                 Implementation eventlist[] = { I_OnUserPreNotice, I_OnUserPreMessage };
205                 ServerInstance->Modules->Attach(eventlist, this, 2);
206         }
207
208         ModResult ProcessMessages(User* user,Channel* dest, const std::string &text)
209         {
210                 ModResult res;
211                 FIRST_MOD_RESULT(OnChannelRestrictionApply, res, (user,dest,"flood"));
212                 if (!IS_LOCAL(user) || res == MOD_RES_ALLOW)
213                         return MOD_RES_PASSTHRU;
214
215                 floodsettings *f = mf.ext.get(dest);
216                 if (f)
217                 {
218                         f->addmessage(user);
219                         if (f->shouldkick(user))
220                         {
221                                 /* Youre outttta here! */
222                                 f->clear(user);
223                                 if (f->ban)
224                                 {
225                                         std::vector<std::string> parameters;
226                                         parameters.push_back(dest->name);
227                                         parameters.push_back("+b");
228                                         parameters.push_back(user->MakeWildHost());
229                                         ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
230
231                                         ServerInstance->PI->SendModeStr(dest->name, std::string("+b ") + user->MakeWildHost());
232                                 }
233
234                                 char kickmessage[MAXBUF];
235                                 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
236
237                                 if (!dest->ServerKickUser(user, kickmessage))
238                                 {
239                                         delete dest;
240                                 }
241
242                                 return MOD_RES_DENY;
243                         }
244                 }
245
246                 return MOD_RES_PASSTHRU;
247         }
248
249         ModResult OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
250         {
251                 if (target_type == TYPE_CHANNEL)
252                         return ProcessMessages(user,(Channel*)dest,text);
253
254                 return MOD_RES_PASSTHRU;
255         }
256
257         ModResult OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
258         {
259                 if (target_type == TYPE_CHANNEL)
260                         return ProcessMessages(user,(Channel*)dest,text);
261
262                 return MOD_RES_PASSTHRU;
263         }
264
265         ~ModuleMsgFlood()
266         {
267         }
268
269         Version GetVersion()
270         {
271                 return Version("Provides channel mode +f (message flood protection)", VF_COMMON | VF_VENDOR, API_VERSION);
272         }
273 };
274
275 MODULE_INIT(ModuleMsgFlood)