]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
854276500d7f65924225575e0490936e62c979e4
[user/henk/code/inspircd.git] / src / modules / m_joinflood.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 +j (join flood protection) */
17
18 /** Holds settings and state associated with channel mode +j
19  */
20 class joinfloodsettings : public classbase
21 {
22  private:
23         InspIRCd* ServerInstance;
24  public:
25         int secs;
26         int joins;
27         time_t reset;
28         time_t unlocktime;
29         int counter;
30         bool locked;
31
32         joinfloodsettings(InspIRCd *Instance, int b, int c) : ServerInstance(Instance), secs(b), joins(c)
33         {
34                 reset = ServerInstance->Time() + secs;
35                 counter = 0;
36                 locked = false;
37         };
38
39         void addjoin()
40         {
41                 counter++;
42                 if (ServerInstance->Time() > reset)
43                 {
44                         counter = 0;
45                         reset = ServerInstance->Time() + secs;
46                 }
47         }
48
49         bool shouldlock()
50         {
51                 return (counter >= this->joins);
52         }
53
54         void clear()
55         {
56                 counter = 0;
57         }
58
59         bool islocked()
60         {
61                 if (locked)
62                 {
63                         if (ServerInstance->Time() > unlocktime)
64                         {
65                                 locked = false;
66                                 return false;
67                         }
68                         else
69                         {
70                                 return true;
71                         }
72                 }
73                 return false;
74         }
75
76         void lock()
77         {
78                 locked = true;
79                 unlocktime = ServerInstance->Time() + 60;
80         }
81
82 };
83
84 /** Handles channel mode +j
85  */
86 class JoinFlood : public ModeHandler
87 {
88  public:
89         SimpleExtItem<joinfloodsettings> ext;
90         JoinFlood(InspIRCd* Instance, Module* Creator) : ModeHandler(Creator, 'j', PARAM_SETONLY, MODETYPE_CHANNEL),
91                 ext("joinflood", Creator) { }
92
93         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
94         {
95                 joinfloodsettings* x = ext.get(channel);
96                 if (x)
97                         return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
98                 else
99                         return std::make_pair(false, parameter);
100         }
101
102         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
103         {
104                 if (adding)
105                 {
106                         char ndata[MAXBUF];
107                         char* data = ndata;
108                         strlcpy(ndata,parameter.c_str(),MAXBUF);
109                         char* joins = data;
110                         char* secs = NULL;
111                         while (*data)
112                         {
113                                 if (*data == ':')
114                                 {
115                                         *data = 0;
116                                         data++;
117                                         secs = data;
118                                         break;
119                                 }
120                                 else data++;
121                         }
122                         if (secs)
123
124                         {
125                                 /* Set up the flood parameters for this channel */
126                                 int njoins = atoi(joins);
127                                 int nsecs = atoi(secs);
128                                 if ((njoins<1) || (nsecs<1))
129                                 {
130                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
131                                         parameter.clear();
132                                         return MODEACTION_DENY;
133                                 }
134                                 else
135                                 {
136                                         joinfloodsettings* f = ext.get(channel);
137                                         if (!f)
138                                         {
139                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
140                                                 f = new joinfloodsettings(ServerInstance, nsecs, njoins);
141                                                 ext.set(channel, f);
142                                                 channel->SetModeParam('j', parameter);
143                                                 return MODEACTION_ALLOW;
144                                         }
145                                         else
146                                         {
147                                                 std::string cur_param = channel->GetModeParameter('j');
148                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
149                                                 if (cur_param == parameter)
150                                                 {
151                                                         // mode params match
152                                                         return MODEACTION_DENY;
153                                                 }
154                                                 else
155                                                 {
156                                                         // new mode param, replace old with new
157                                                         if ((nsecs > 0) && (njoins > 0))
158                                                         {
159                                                                 f = new joinfloodsettings(ServerInstance, nsecs, njoins);
160                                                                 ext.set(channel, f);
161                                                                 channel->SetModeParam('j', parameter);
162                                                                 return MODEACTION_ALLOW;
163                                                         }
164                                                         else
165                                                         {
166                                                                 return MODEACTION_DENY;
167                                                         }
168                                                 }
169                                         }
170                                 }
171                         }
172                         else
173                         {
174                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
175                                 return MODEACTION_DENY;
176                         }
177                 }
178                 else
179                 {
180                         joinfloodsettings* f = ext.get(channel);
181                         if (f)
182                         {
183                                 ext.unset(channel);
184                                 channel->SetModeParam('j', "");
185                                 return MODEACTION_ALLOW;
186                         }
187                 }
188                 return MODEACTION_DENY;
189         }
190 };
191
192 class ModuleJoinFlood : public Module
193 {
194
195         JoinFlood jf;
196
197  public:
198
199         ModuleJoinFlood(InspIRCd* Me)
200                 : Module(Me), jf(Me, this)
201         {
202
203                 if (!ServerInstance->Modes->AddMode(&jf))
204                         throw ModuleException("Could not add new modes!");
205                 Extensible::Register(&jf.ext);
206                 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreJoin, I_OnUserJoin };
207                 ServerInstance->Modules->Attach(eventlist, this, 3);
208         }
209
210         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
211         {
212                 if (chan)
213                 {
214                         joinfloodsettings *f = jf.ext.get(chan);
215                         if (f && f->islocked())
216                         {
217                                 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
218                                 return MOD_RES_DENY;
219                         }
220                 }
221                 return MOD_RES_PASSTHRU;
222         }
223
224         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
225         {
226                 /* We arent interested in JOIN events caused by a network burst */
227                 if (sync)
228                         return;
229
230                 joinfloodsettings *f = jf.ext.get(memb->chan);
231
232                 /* But all others are OK */
233                 if (f)
234                 {
235                         f->addjoin();
236                         if (f->shouldlock())
237                         {
238                                 f->clear();
239                                 f->lock();
240                                 memb->chan->WriteChannelWithServ((char*)ServerInstance->Config->ServerName, "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);
241                         }
242                 }
243         }
244
245         ~ModuleJoinFlood()
246         {
247                 ServerInstance->Modes->DelMode(&jf);
248         }
249
250         Version GetVersion()
251         {
252                 return Version("Provides channel mode +j (join flood protection)", VF_COMMON | VF_VENDOR, API_VERSION);
253         }
254 };
255
256 MODULE_INIT(ModuleJoinFlood)