]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
1a30b747949d9f1efd98415f26c54ebbafa8a4d8
[user/henk/code/inspircd.git] / src / modules / m_joinflood.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2006-2007 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25
26 /* $ModDesc: Provides channel mode +j (join flood protection) */
27
28 /** Holds settings and state associated with channel mode +j
29  */
30 class joinfloodsettings
31 {
32  public:
33         int secs;
34         int joins;
35         time_t reset;
36         time_t unlocktime;
37         int counter;
38         bool locked;
39
40         joinfloodsettings(int b, int c) : secs(b), joins(c)
41         {
42                 reset = ServerInstance->Time() + secs;
43                 counter = 0;
44                 locked = false;
45         };
46
47         void addjoin()
48         {
49                 counter++;
50                 if (ServerInstance->Time() > reset)
51                 {
52                         counter = 0;
53                         reset = ServerInstance->Time() + secs;
54                 }
55         }
56
57         bool shouldlock()
58         {
59                 return (counter >= this->joins);
60         }
61
62         void clear()
63         {
64                 counter = 0;
65         }
66
67         bool islocked()
68         {
69                 if (locked)
70                 {
71                         if (ServerInstance->Time() > unlocktime)
72                         {
73                                 locked = false;
74                                 return false;
75                         }
76                         else
77                         {
78                                 return true;
79                         }
80                 }
81                 return false;
82         }
83
84         void lock()
85         {
86                 locked = true;
87                 unlocktime = ServerInstance->Time() + 60;
88         }
89 };
90
91 /** Handles channel mode +j
92  */
93 class JoinFlood : public ModeHandler
94 {
95  public:
96         SimpleExtItem<joinfloodsettings> ext;
97         JoinFlood(Module* Creator) : ModeHandler(Creator, "joinflood", 'j', PARAM_SETONLY, MODETYPE_CHANNEL),
98                 ext("joinflood", Creator) { }
99
100         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
101         {
102                 if (adding)
103                 {
104                         char ndata[MAXBUF];
105                         char* data = ndata;
106                         strlcpy(ndata,parameter.c_str(),MAXBUF);
107                         char* joins = data;
108                         char* secs = NULL;
109                         while (*data)
110                         {
111                                 if (*data == ':')
112                                 {
113                                         *data = 0;
114                                         data++;
115                                         secs = data;
116                                         break;
117                                 }
118                                 else data++;
119                         }
120                         if (secs)
121
122                         {
123                                 /* Set up the flood parameters for this channel */
124                                 int njoins = atoi(joins);
125                                 int nsecs = atoi(secs);
126                                 if ((njoins<1) || (nsecs<1))
127                                 {
128                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
129                                         parameter.clear();
130                                         return MODEACTION_DENY;
131                                 }
132                                 else
133                                 {
134                                         joinfloodsettings* f = ext.get(channel);
135                                         if (!f)
136                                         {
137                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
138                                                 f = new joinfloodsettings(nsecs, njoins);
139                                                 ext.set(channel, f);
140                                                 channel->SetModeParam('j', parameter);
141                                                 return MODEACTION_ALLOW;
142                                         }
143                                         else
144                                         {
145                                                 std::string cur_param = channel->GetModeParameter('j');
146                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
147                                                 if (cur_param == parameter)
148                                                 {
149                                                         // mode params match
150                                                         return MODEACTION_DENY;
151                                                 }
152                                                 else
153                                                 {
154                                                         // new mode param, replace old with new
155                                                         if ((nsecs > 0) && (njoins > 0))
156                                                         {
157                                                                 f = new joinfloodsettings(nsecs, njoins);
158                                                                 ext.set(channel, f);
159                                                                 channel->SetModeParam('j', parameter);
160                                                                 return MODEACTION_ALLOW;
161                                                         }
162                                                         else
163                                                         {
164                                                                 return MODEACTION_DENY;
165                                                         }
166                                                 }
167                                         }
168                                 }
169                         }
170                         else
171                         {
172                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
173                                 return MODEACTION_DENY;
174                         }
175                 }
176                 else
177                 {
178                         joinfloodsettings* f = ext.get(channel);
179                         if (f)
180                         {
181                                 ext.unset(channel);
182                                 channel->SetModeParam('j', "");
183                                 return MODEACTION_ALLOW;
184                         }
185                 }
186                 return MODEACTION_DENY;
187         }
188 };
189
190 class ModuleJoinFlood : public Module
191 {
192         JoinFlood jf;
193
194  public:
195         ModuleJoinFlood()
196                 : jf(this)
197         {
198         }
199
200         void init()
201         {
202                 ServerInstance->Modules->AddService(jf);
203                 ServerInstance->Modules->AddService(jf.ext);
204                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserJoin };
205                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
206         }
207
208         ModResult OnUserPreJoin(User* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven)
209         {
210                 if (chan)
211                 {
212                         joinfloodsettings *f = jf.ext.get(chan);
213                         if (f && f->islocked())
214                         {
215                                 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
216                                 return MOD_RES_DENY;
217                         }
218                 }
219                 return MOD_RES_PASSTHRU;
220         }
221
222         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
223         {
224                 /* We arent interested in JOIN events caused by a network burst */
225                 if (sync)
226                         return;
227
228                 joinfloodsettings *f = jf.ext.get(memb->chan);
229
230                 /* But all others are OK */
231                 if (f)
232                 {
233                         f->addjoin();
234                         if (f->shouldlock())
235                         {
236                                 f->clear();
237                                 f->lock();
238                                 memb->chan->WriteChannelWithServ((char*)ServerInstance->Config->ServerName.c_str(), "NOTICE %s :This channel has been closed to new users for 60 seconds because there have been more than %d joins in %d seconds.", memb->chan->name.c_str(), f->joins, f->secs);
239                         }
240                 }
241         }
242
243         Version GetVersion()
244         {
245                 return Version("Provides channel mode +j (join flood protection)", VF_VENDOR);
246         }
247 };
248
249 MODULE_INIT(ModuleJoinFlood)