]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
Note to self, cast time_t to long int for printf... thanks Ankit for pointing this...
[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://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
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(InspIRCd *Instance, bool a, int b, int c) : ServerInstance(Instance), 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         MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
80
81         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
82         {
83                 floodsettings* x;
84                 if (channel->GetExt("flood",x))
85                         return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
86                 else
87                         return std::make_pair(false, parameter);
88         }
89
90         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
91         {
92                 /* When TS is equal, the alphabetically later one wins */
93                 return (their_param < our_param);
94         }
95
96         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
97         {
98                 floodsettings *f;
99
100                 if (adding)
101                 {
102                         char ndata[MAXBUF];
103                         char* data = ndata;
104                         strlcpy(ndata,parameter.c_str(),MAXBUF);
105                         char* lines = data;
106                         char* secs = NULL;
107                         bool ban = false;
108                         if (*data == '*')
109                         {
110                                 ban = true;
111                                 lines++;
112                         }
113                         else
114                         {
115                                 ban = false;
116                         }
117                         while (*data)
118                         {
119                                 if (*data == ':')
120                                 {
121                                         *data = 0;
122                                         data++;
123                                         secs = data;
124                                         break;
125                                 }
126                                 else data++;
127                         }
128                         if (secs)
129                         {
130                                 /* Set up the flood parameters for this channel */
131                                 int nlines = atoi(lines);
132                                 int nsecs = atoi(secs);
133                                 if ((nlines<2) || (nsecs<1))
134                                 {
135                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
136                                         parameter.clear();
137                                         return MODEACTION_DENY;
138                                 }
139                                 else
140                                 {
141                                         if (!channel->GetExt("flood", f))
142                                         {
143                                                 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
144                                                 floodsettings *fs = new floodsettings(ServerInstance,ban,nsecs,nlines);
145                                                 channel->Extend("flood",fs);
146                                                 channel->SetMode('f', true);
147                                                 channel->SetModeParam('f', parameter.c_str(), true);
148                                                 return MODEACTION_ALLOW;
149                                         }
150                                         else
151                                         {
152                                                 std::string cur_param = channel->GetModeParameter('f');
153                                                 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
154                                                 if (cur_param == parameter)
155                                                 {
156                                                         // mode params match
157                                                         return MODEACTION_DENY;
158                                                 }
159                                                 else
160                                                 {
161                                                         if ((((nlines != f->lines) || (nsecs != f->secs) || (ban != f->ban))) && (((nsecs > 0) && (nlines > 0))))
162                                                         {
163                                                                 delete f;
164                                                                 floodsettings *fs = new floodsettings(ServerInstance,ban,nsecs,nlines);
165                                                                 channel->Shrink("flood");
166                                                                 channel->Extend("flood",fs);
167                                                                 channel->SetModeParam('f', cur_param.c_str(), false);
168                                                                 channel->SetModeParam('f', parameter.c_str(), true);
169                                                                 return MODEACTION_ALLOW;
170                                                         }
171                                                         else
172                                                         {
173                                                                 return MODEACTION_DENY;
174                                                         }
175                                                 }
176                                         }
177                                 }
178                         }
179                         else
180                         {
181                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
182                                 parameter.clear();
183                                 return MODEACTION_DENY;
184                         }
185                 }
186                 else
187                 {
188                         if (channel->GetExt("flood", f))
189                         {
190                                 delete f;
191                                 channel->Shrink("flood");
192                                 channel->SetMode('f', false);
193                                 return MODEACTION_ALLOW;
194                         }
195                 }
196
197                 return MODEACTION_DENY;
198         }
199 };
200
201 class ModuleMsgFlood : public Module
202 {
203
204         MsgFlood* mf;
205
206  public:
207
208         ModuleMsgFlood(InspIRCd* Me)
209                 : Module(Me)
210         {
211
212                 mf = new MsgFlood(ServerInstance);
213                 if (!ServerInstance->Modes->AddMode(mf))
214                         throw ModuleException("Could not add new modes!");
215                 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNotice, I_OnUserPreMessage };
216                 ServerInstance->Modules->Attach(eventlist, this, 3);
217         }
218
219         int ProcessMessages(User* user,Channel* dest, const std::string &text)
220         {
221                 if (!IS_LOCAL(user) || (CHANOPS_EXEMPT(ServerInstance, 'f') && dest->GetStatus(user) == STATUS_OP))
222                 {
223                         return 0;
224                 }
225
226                 floodsettings *f;
227                 if (dest->GetExt("flood", f))
228                 {
229                         f->addmessage(user);
230                         if (f->shouldkick(user))
231                         {
232                                 /* Youre outttta here! */
233                                 f->clear(user);
234                                 if (f->ban)
235                                 {
236                                         std::vector<std::string> parameters;
237                                         parameters.push_back(dest->name);
238                                         parameters.push_back("+b");
239                                         parameters.push_back(user->MakeWildHost());
240                                         ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
241
242                                         ServerInstance->PI->SendModeStr(dest->name, std::string("+b ") + user->MakeWildHost());
243                                 }
244
245                                 char kickmessage[MAXBUF];
246                                 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
247
248                                 if (!dest->ServerKickUser(user, kickmessage, true))
249                                 {
250                                         delete dest;
251                                 }
252
253                                 return 1;
254                         }
255                 }
256
257                 return 0;
258         }
259
260         virtual int OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
261         {
262                 if (target_type == TYPE_CHANNEL)
263                         return ProcessMessages(user,(Channel*)dest,text);
264
265                 return 0;
266         }
267
268         virtual int OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
269         {
270                 if (target_type == TYPE_CHANNEL)
271                         return ProcessMessages(user,(Channel*)dest,text);
272
273                 return 0;
274         }
275
276         void OnChannelDelete(Channel* chan)
277         {
278                 floodsettings* f;
279                 if (chan->GetExt("flood", f))
280                 {
281                         delete f;
282                         chan->Shrink("flood");
283                 }
284         }
285
286
287         virtual ~ModuleMsgFlood()
288         {
289                 ServerInstance->Modes->DelMode(mf);
290                 delete mf;
291         }
292
293         virtual Version GetVersion()
294         {
295                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
296         }
297 };
298
299 MODULE_INIT(ModuleMsgFlood)