]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
be5dc68ae2db40deb942340ba72bd3f180cd8e06
[user/henk/code/inspircd.git] / src / modules / m_messageflood.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 <stdio.h>
15 #include <map>
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "inspircd.h"
20
21 /* $ModDesc: Provides channel mode +f (message flood protection) */
22
23 /** Holds flood settings and state for mode +f
24  */
25 class floodsettings : public classbase
26 {
27  public:
28         bool ban;
29         int secs;
30         int lines;
31         time_t reset;
32         std::map<userrec*,int> counters;
33
34         floodsettings() : ban(0), secs(0), lines(0) {};
35         floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
36         {
37                 reset = time(NULL) + secs;
38         };
39
40         void addmessage(userrec* who)
41         {
42                 std::map<userrec*,int>::iterator iter = counters.find(who);
43                 if (iter != counters.end())
44                 {
45                         iter->second++;
46                 }
47                 else
48                 {
49                         counters[who] = 1;
50                 }
51                 if (time(NULL) > reset)
52                 {
53                         counters.clear();
54                         reset = time(NULL) + secs;
55                 }
56         }
57
58         bool shouldkick(userrec* who)
59         {
60                 std::map<userrec*,int>::iterator iter = counters.find(who);
61                 if (iter != counters.end())
62                 {
63                         return (iter->second >= this->lines);
64                 }
65                 else return false;
66         }
67
68         void clear(userrec* who)
69         {
70                 std::map<userrec*,int>::iterator iter = counters.find(who);
71                 if (iter != counters.end())
72                 {
73                         counters.erase(iter);
74                 }
75         }
76 };
77
78 /** Handles channel mode +f
79  */
80 class MsgFlood : public ModeHandler
81 {
82  public:
83         MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
84
85         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
86         {
87                 floodsettings* x;
88                 if (channel->GetExt("flood",x))
89                         return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
90                 else
91                         return std::make_pair(false, parameter);
92         }
93
94         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
95         {
96                 /* When TS is equal, the alphabetically later one wins */
97                 return (their_param < our_param);
98         }
99
100         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
101         {
102                 floodsettings *f;
103
104                 if (adding)
105                 {
106                         char ndata[MAXBUF];
107                         char* data = ndata;
108                         strlcpy(ndata,parameter.c_str(),MAXBUF);
109                         char* lines = data;
110                         char* secs = NULL;
111                         bool ban = false;
112                         if (*data == '*')
113                         {
114                                 ban = true;
115                                 lines++;
116                         }
117                         else
118                         {
119                                 ban = false;
120                         }
121                         while (*data)
122                         {
123                                 if (*data == ':')
124                                 {
125                                         *data = 0;
126                                         data++;
127                                         secs = data;
128                                         break;
129                                 }
130                                 else data++;
131                         }
132                         if (secs)
133                         {
134                                 /* Set up the flood parameters for this channel */
135                                 int nlines = atoi(lines);
136                                 int nsecs = atoi(secs);
137                                 if ((nlines<1) || (nsecs<1))
138                                 {
139                                         source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
140                                         parameter = "";
141                                         return MODEACTION_DENY;
142                                 }
143                                 else
144                                 {
145                                         if (!channel->GetExt("flood", f))
146                                         {
147                                                 parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
148                                                 floodsettings *f = new floodsettings(ban,nsecs,nlines);
149                                                 channel->Extend("flood",f);
150                                                 channel->SetMode('f', true);
151                                                 channel->SetModeParam('f', parameter.c_str(), true);
152                                                 return MODEACTION_ALLOW;
153                                         }
154                                         else
155                                         {
156                                                 if (((nlines != f->lines) || (nsecs != f->secs)) && ((nsecs > 0) && (nlines > 0)))
157                                                 {
158                                                         delete f;
159                                                         floodsettings *f = new floodsettings(ban,nsecs,nlines);
160                                                         parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
161                                                         channel->Shrink("flood");
162                                                         channel->Extend("flood",f);
163                                                         channel->SetModeParam('f', parameter.c_str(), true);
164                                                         return MODEACTION_ALLOW;
165                                                 }
166                                         }
167                                 }
168                         }
169                         else
170                         {
171                                 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
172                                 parameter = "";
173                                 return MODEACTION_DENY;
174                         }
175                 }
176                 else
177                 {
178                         if (channel->GetExt("flood", f))
179                         {
180                                 DELETE(f);
181                                 channel->Shrink("flood");
182                                 channel->SetMode('f', false);
183                                 return MODEACTION_ALLOW;
184                         }
185                 }
186                 
187                 return MODEACTION_DENY;
188         }
189 };
190
191 class ModuleMsgFlood : public Module
192 {
193         
194         MsgFlood* mf;
195         
196  public:
197  
198         ModuleMsgFlood(InspIRCd* Me)
199                 : Module::Module(Me)
200         {
201                 
202                 mf = new MsgFlood(ServerInstance);
203                 ServerInstance->AddMode(mf, 'f');
204         }
205         
206         void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
207         {
208                 if (IS_LOCAL(user))
209                 {
210                         floodsettings *f;
211                         if (dest->GetExt("flood", f))
212                         {
213                                 f->addmessage(user);
214                                 if (f->shouldkick(user))
215                                 {
216                                         /* Youre outttta here! */
217                                         f->clear(user);
218                                         if (f->ban)
219                                         {
220                                                 const char* parameters[3];
221                                                 parameters[0] = dest->name;
222                                                 parameters[1] = "+b";
223                                                 parameters[2] = user->MakeWildHost();
224                                                 ServerInstance->SendMode(parameters,3,user);
225                                                 std::deque<std::string> n;
226                                                 /* Propogate the ban to other servers.
227                                                  * We dont know what protocol we may be using,
228                                                  * so this event is picked up by our protocol
229                                                  * module and formed into a ban command that
230                                                  * suits the protocol in use.
231                                                  */
232                                                 n.push_back(dest->name);
233                                                 n.push_back("+b");
234                                                 n.push_back(user->MakeWildHost());
235                                                 Event rmode((char *)&n, NULL, "send_mode");
236                                                 rmode.Send(ServerInstance);
237                                         }
238                                         dest->ServerKickUser(user, "Channel flood triggered (mode +f)", true);
239                                 }
240                         }
241                 }
242         }
243
244         virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
245         {
246                 if (target_type == TYPE_CHANNEL)
247                 {
248                         ProcessMessages(user,(chanrec*)dest,text);
249                 }
250         }
251
252         virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
253         {
254                 if (target_type == TYPE_CHANNEL)
255                 {
256                         ProcessMessages(user,(chanrec*)dest,text);
257                 }
258         }
259
260         void OnChannelDelete(chanrec* chan)
261         {
262                 floodsettings* f;
263                 if (chan->GetExt("flood", f))
264                 {
265                         DELETE(f);
266                         chan->Shrink("flood");
267                 }
268         }
269
270         void Implements(char* List)
271         {
272                 List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
273         }
274
275         virtual ~ModuleMsgFlood()
276         {
277                 ServerInstance->Modes->DelMode(mf);
278                 DELETE(mf);
279         }
280         
281         virtual Version GetVersion()
282         {
283                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
284         }
285 };
286
287
288 class ModuleMsgFloodFactory : public ModuleFactory
289 {
290  public:
291         ModuleMsgFloodFactory()
292         {
293         }
294         
295         ~ModuleMsgFloodFactory()
296         {
297         }
298         
299         virtual Module * CreateModule(InspIRCd* Me)
300         {
301                 return new ModuleMsgFlood(Me);
302         }
303         
304 };
305
306
307 extern "C" void * init_module( void )
308 {
309         return new ModuleMsgFloodFactory;
310 }
311