]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
m_namedmodes Only show chan key to members and opers with channels/auspex
[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
92 /** Handles channel mode +j
93  */
94 class JoinFlood : public ModeHandler
95 {
96  public:
97         SimpleExtItem<joinfloodsettings> ext;
98         JoinFlood(Module* Creator) : ModeHandler(Creator, "joinflood", 'j', PARAM_SETONLY, MODETYPE_CHANNEL),
99                 ext("joinflood", Creator) { }
100
101         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
102         {
103                 if (adding)
104                 {
105                         char ndata[MAXBUF];
106                         char* data = ndata;
107                         strlcpy(ndata,parameter.c_str(),MAXBUF);
108                         char* joins = data;
109                         char* secs = NULL;
110                         while (*data)
111                         {
112                                 if (*data == ':')
113                                 {
114                                         *data = 0;
115                                         data++;
116                                         secs = data;
117                                         break;
118                                 }
119                                 else data++;
120                         }
121                         if (secs)
122
123                         {
124                                 /* Set up the flood parameters for this channel */
125                                 int njoins = atoi(joins);
126                                 int nsecs = atoi(secs);
127                                 if ((njoins<1) || (nsecs<1))
128                                 {
129                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
130                                         parameter.clear();
131                                         return MODEACTION_DENY;
132                                 }
133                                 else
134                                 {
135                                         joinfloodsettings* f = ext.get(channel);
136                                         if (!f)
137                                         {
138                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
139                                                 f = new joinfloodsettings(nsecs, njoins);
140                                                 ext.set(channel, f);
141                                                 channel->SetModeParam('j', parameter);
142                                                 return MODEACTION_ALLOW;
143                                         }
144                                         else
145                                         {
146                                                 std::string cur_param = channel->GetModeParameter('j');
147                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
148                                                 if (cur_param == parameter)
149                                                 {
150                                                         // mode params match
151                                                         return MODEACTION_DENY;
152                                                 }
153                                                 else
154                                                 {
155                                                         // new mode param, replace old with new
156                                                         f = new joinfloodsettings(nsecs, njoins);
157                                                         ext.set(channel, f);
158                                                         channel->SetModeParam('j', parameter);
159                                                         return MODEACTION_ALLOW;
160                                                 }
161                                         }
162                                 }
163                         }
164                         else
165                         {
166                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
167                                 return MODEACTION_DENY;
168                         }
169                 }
170                 else
171                 {
172                         if (channel->IsModeSet('j'))
173                         {
174                                 ext.unset(channel);
175                                 channel->SetModeParam('j', "");
176                                 return MODEACTION_ALLOW;
177                         }
178                 }
179                 return MODEACTION_DENY;
180         }
181 };
182
183 class ModuleJoinFlood : public Module
184 {
185
186         JoinFlood jf;
187
188  public:
189
190         ModuleJoinFlood()
191                 : jf(this)
192         {
193         }
194
195         void init()
196         {
197                 ServerInstance->Modules->AddService(jf);
198                 ServerInstance->Modules->AddService(jf.ext);
199                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserJoin };
200                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
201         }
202
203         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
204         {
205                 if (chan)
206                 {
207                         joinfloodsettings *f = jf.ext.get(chan);
208                         if (f && f->islocked())
209                         {
210                                 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
211                                 return MOD_RES_DENY;
212                         }
213                 }
214                 return MOD_RES_PASSTHRU;
215         }
216
217         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
218         {
219                 /* We arent interested in JOIN events caused by a network burst */
220                 if (sync)
221                         return;
222
223                 joinfloodsettings *f = jf.ext.get(memb->chan);
224
225                 /* But all others are OK */
226                 if ((f) && (!f->islocked()))
227                 {
228                         f->addjoin();
229                         if (f->shouldlock())
230                         {
231                                 f->clear();
232                                 f->lock();
233                                 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);
234                         }
235                 }
236         }
237
238         ~ModuleJoinFlood()
239         {
240         }
241
242         Version GetVersion()
243         {
244                 return Version("Provides channel mode +j (join flood protection)", VF_VENDOR);
245         }
246 };
247
248 MODULE_INIT(ModuleJoinFlood)