]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
Change allocation of commands/modes
[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(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->SetModeParam('f', parameter);
147                                                 return MODEACTION_ALLOW;
148                                         }
149                                         else
150                                         {
151                                                 std::string cur_param = channel->GetModeParameter('f');
152                                                 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
153                                                 if (cur_param == parameter)
154                                                 {
155                                                         // mode params match
156                                                         return MODEACTION_DENY;
157                                                 }
158                                                 else
159                                                 {
160                                                         if ((((nlines != f->lines) || (nsecs != f->secs) || (ban != f->ban))) && (((nsecs > 0) && (nlines > 0))))
161                                                         {
162                                                                 delete f;
163                                                                 floodsettings *fs = new floodsettings(ServerInstance,ban,nsecs,nlines);
164                                                                 channel->Shrink("flood");
165                                                                 channel->Extend("flood",fs);
166                                                                 channel->SetModeParam('f', parameter);
167                                                                 return MODEACTION_ALLOW;
168                                                         }
169                                                         else
170                                                         {
171                                                                 return MODEACTION_DENY;
172                                                         }
173                                                 }
174                                         }
175                                 }
176                         }
177                         else
178                         {
179                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
180                                 parameter.clear();
181                                 return MODEACTION_DENY;
182                         }
183                 }
184                 else
185                 {
186                         if (channel->GetExt("flood", f))
187                         {
188                                 delete f;
189                                 channel->Shrink("flood");
190                                 channel->SetModeParam('f', "");
191                                 return MODEACTION_ALLOW;
192                         }
193                 }
194
195                 return MODEACTION_DENY;
196         }
197 };
198
199 class ModuleMsgFlood : public Module
200 {
201         MsgFlood mf;
202
203  public:
204
205         ModuleMsgFlood(InspIRCd* Me)
206                 : Module(Me), mf(Me)
207         {
208                 if (!ServerInstance->Modes->AddMode(&mf))
209                         throw ModuleException("Could not add new modes!");
210                 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNotice, I_OnUserPreMessage };
211                 ServerInstance->Modules->Attach(eventlist, this, 3);
212         }
213
214         int ProcessMessages(User* user,Channel* dest, const std::string &text)
215         {
216                 if (!IS_LOCAL(user) || (CHANOPS_EXEMPT(ServerInstance, 'f') && dest->GetStatus(user) == STATUS_OP))
217                 {
218                         return 0;
219                 }
220
221                 floodsettings *f;
222                 if (dest->GetExt("flood", f))
223                 {
224                         f->addmessage(user);
225                         if (f->shouldkick(user))
226                         {
227                                 /* Youre outttta here! */
228                                 f->clear(user);
229                                 if (f->ban)
230                                 {
231                                         std::vector<std::string> parameters;
232                                         parameters.push_back(dest->name);
233                                         parameters.push_back("+b");
234                                         parameters.push_back(user->MakeWildHost());
235                                         ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
236
237                                         ServerInstance->PI->SendModeStr(dest->name, std::string("+b ") + user->MakeWildHost());
238                                 }
239
240                                 char kickmessage[MAXBUF];
241                                 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
242
243                                 if (!dest->ServerKickUser(user, kickmessage))
244                                 {
245                                         delete dest;
246                                 }
247
248                                 return 1;
249                         }
250                 }
251
252                 return 0;
253         }
254
255         virtual int OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
256         {
257                 if (target_type == TYPE_CHANNEL)
258                         return ProcessMessages(user,(Channel*)dest,text);
259
260                 return 0;
261         }
262
263         virtual int OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
264         {
265                 if (target_type == TYPE_CHANNEL)
266                         return ProcessMessages(user,(Channel*)dest,text);
267
268                 return 0;
269         }
270
271         void OnChannelDelete(Channel* chan)
272         {
273                 floodsettings* f;
274                 if (chan->GetExt("flood", f))
275                 {
276                         delete f;
277                         chan->Shrink("flood");
278                 }
279         }
280
281
282         virtual ~ModuleMsgFlood()
283         {
284                 ServerInstance->Modes->DelMode(&mf);
285         }
286
287         virtual Version GetVersion()
288         {
289                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
290         }
291 };
292
293 MODULE_INIT(ModuleMsgFlood)