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