]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
Make various self contained methods static.
[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 /** Holds settings and state associated with channel mode +j
27  */
28 class joinfloodsettings
29 {
30  public:
31         unsigned int secs;
32         unsigned int joins;
33         time_t reset;
34         time_t unlocktime;
35         unsigned int counter;
36
37         joinfloodsettings(unsigned int b, unsigned int c)
38                 : secs(b), joins(c), unlocktime(0), counter(0)
39         {
40                 reset = ServerInstance->Time() + secs;
41         }
42
43         void addjoin()
44         {
45                 if (ServerInstance->Time() > reset)
46                 {
47                         counter = 1;
48                         reset = ServerInstance->Time() + secs;
49                 }
50                 else
51                         counter++;
52         }
53
54         bool shouldlock()
55         {
56                 return (counter >= this->joins);
57         }
58
59         void clear()
60         {
61                 counter = 0;
62         }
63
64         bool islocked()
65         {
66                 if (ServerInstance->Time() > unlocktime)
67                         unlocktime = 0;
68
69                 return (unlocktime != 0);
70         }
71
72         void lock()
73         {
74                 unlocktime = ServerInstance->Time() + 60;
75         }
76
77         bool operator==(const joinfloodsettings& other) const
78         {
79                 return ((this->secs == other.secs) && (this->joins == other.joins));
80         }
81 };
82
83 /** Handles channel mode +j
84  */
85 class JoinFlood : public ModeHandler
86 {
87  public:
88         SimpleExtItem<joinfloodsettings> ext;
89         JoinFlood(Module* Creator) : ModeHandler(Creator, "joinflood", 'j', PARAM_SETONLY, MODETYPE_CHANNEL),
90                 ext("joinflood", Creator) { }
91
92         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
93         {
94                 if (adding)
95                 {
96                         std::string::size_type colon = parameter.find(':');
97                         if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
98                         {
99                                 source->WriteNumeric(608, "%s :Invalid flood parameter",channel->name.c_str());
100                                 return MODEACTION_DENY;
101                         }
102
103                         /* Set up the flood parameters for this channel */
104                         unsigned int njoins = ConvToInt(parameter.substr(0, colon));
105                         unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
106                         if ((njoins<1) || (nsecs<1))
107                         {
108                                 source->WriteNumeric(608, "%s :Invalid flood parameter",channel->name.c_str());
109                                 return MODEACTION_DENY;
110                         }
111
112                         joinfloodsettings jfs(nsecs, njoins);
113                         joinfloodsettings* f = ext.get(channel);
114                         if ((f) && (*f == jfs))
115                                 // mode params match
116                                 return MODEACTION_DENY;
117
118                         ext.set(channel, jfs);
119                         parameter = ConvToStr(njoins) + ":" + ConvToStr(nsecs);
120                         return MODEACTION_ALLOW;
121                 }
122                 else
123                 {
124                         if (!channel->IsModeSet(this))
125                                 return MODEACTION_DENY;
126
127                         joinfloodsettings* f = ext.get(channel);
128                         if (f)
129                         {
130                                 ext.unset(channel);
131                                 return MODEACTION_ALLOW;
132                         }
133                 }
134                 return MODEACTION_DENY;
135         }
136 };
137
138 class ModuleJoinFlood : public Module
139 {
140         JoinFlood jf;
141
142  public:
143         ModuleJoinFlood()
144                 : jf(this)
145         {
146         }
147
148         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
149         {
150                 if (chan)
151                 {
152                         joinfloodsettings *f = jf.ext.get(chan);
153                         if (f && f->islocked())
154                         {
155                                 user->WriteNumeric(609, "%s :This channel is temporarily unavailable (+j). Please try again later.",chan->name.c_str());
156                                 return MOD_RES_DENY;
157                         }
158                 }
159                 return MOD_RES_PASSTHRU;
160         }
161
162         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
163         {
164                 /* We arent interested in JOIN events caused by a network burst */
165                 if (sync)
166                         return;
167
168                 joinfloodsettings *f = jf.ext.get(memb->chan);
169
170                 /* But all others are OK */
171                 if (f)
172                 {
173                         f->addjoin();
174                         if (f->shouldlock())
175                         {
176                                 f->clear();
177                                 f->lock();
178                                 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);
179                         }
180                 }
181         }
182
183         Version GetVersion() CXX11_OVERRIDE
184         {
185                 return Version("Provides channel mode +j (join flood protection)", VF_VENDOR);
186         }
187 };
188
189 MODULE_INIT(ModuleJoinFlood)