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