]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
Remove InspIRCd* parameters and fields
[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  public:
23         int secs;
24         int joins;
25         time_t reset;
26         time_t unlocktime;
27         int counter;
28         bool locked;
29
30         joinfloodsettings(int b, int c) : secs(b), joins(c)
31         {
32                 reset = ServerInstance->Time() + secs;
33                 counter = 0;
34                 locked = false;
35         };
36
37         void addjoin()
38         {
39                 counter++;
40                 if (ServerInstance->Time() > reset)
41                 {
42                         counter = 0;
43                         reset = ServerInstance->Time() + secs;
44                 }
45         }
46
47         bool shouldlock()
48         {
49                 return (counter >= this->joins);
50         }
51
52         void clear()
53         {
54                 counter = 0;
55         }
56
57         bool islocked()
58         {
59                 if (locked)
60                 {
61                         if (ServerInstance->Time() > unlocktime)
62                         {
63                                 locked = false;
64                                 return false;
65                         }
66                         else
67                         {
68                                 return true;
69                         }
70                 }
71                 return false;
72         }
73
74         void lock()
75         {
76                 locked = true;
77                 unlocktime = ServerInstance->Time() + 60;
78         }
79
80 };
81
82 /** Handles channel mode +j
83  */
84 class JoinFlood : public ModeHandler
85 {
86  public:
87         SimpleExtItem<joinfloodsettings> ext;
88         JoinFlood(Module* Creator) : ModeHandler(Creator, 'j', PARAM_SETONLY, MODETYPE_CHANNEL),
89                 ext("joinflood", Creator) { }
90
91         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
92         {
93                 joinfloodsettings* x = ext.get(channel);
94                 if (x)
95                         return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
96                 else
97                         return std::make_pair(false, parameter);
98         }
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
193         JoinFlood jf;
194
195  public:
196
197         ModuleJoinFlood()
198                 : jf(this)
199         {
200
201                 if (!ServerInstance->Modes->AddMode(&jf))
202                         throw ModuleException("Could not add new modes!");
203                 Extensible::Register(&jf.ext);
204                 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreJoin, I_OnUserJoin };
205                 ServerInstance->Modules->Attach(eventlist, this, 3);
206         }
207
208         ModResult OnUserPreJoin(User* user, Channel* chan, const char* 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, "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         ~ModuleJoinFlood()
244         {
245                 ServerInstance->Modes->DelMode(&jf);
246         }
247
248         Version GetVersion()
249         {
250                 return Version("Provides channel mode +j (join flood protection)", VF_COMMON | VF_VENDOR, API_VERSION);
251         }
252 };
253
254 MODULE_INIT(ModuleJoinFlood)