]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_kicknorejoin.cpp
Merge tag 'v2.0.26' into master.
[user/henk/code/inspircd.git] / src / modules / m_kicknorejoin.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2006-2007 Robin Burchell <robin+git@viroteck.net>
8  *   Copyright (C) 2006 John Brooks <john.brooks@dereferenced.net>
9  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
10  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27
28 class KickRejoinData
29 {
30         struct KickedUser
31         {
32                 std::string uuid;
33                 time_t expire;
34
35                 KickedUser(User* user, unsigned int Delay)
36                         : uuid(user->uuid)
37                         , expire(ServerInstance->Time() + Delay)
38                 {
39                 }
40         };
41
42         typedef std::vector<KickedUser> KickedList;
43
44         mutable KickedList kicked;
45
46  public:
47         const unsigned int delay;
48
49         KickRejoinData(unsigned int Delay) : delay(Delay) { }
50
51         bool canjoin(LocalUser* user) const
52         {
53                 for (KickedList::iterator i = kicked.begin(); i != kicked.end(); )
54                 {
55                         KickedUser& rec = *i;
56                         if (rec.expire > ServerInstance->Time())
57                         {
58                                 if (rec.uuid == user->uuid)
59                                         return false;
60                                 ++i;
61                         }
62                         else
63                         {
64                                 // Expired record, remove.
65                                 stdalgo::vector::swaperase(kicked, i);
66                                 if (kicked.empty())
67                                         break;
68                         }
69                 }
70                 return true;
71         }
72
73         void add(User* user)
74         {
75                 // One user can be in the list multiple times if the user gets kicked, force joins
76                 // (skipping OnUserPreJoin) and gets kicked again, but that's okay because canjoin()
77                 // works correctly in this case as well
78                 kicked.push_back(KickedUser(user, delay));
79         }
80 };
81
82 /** Handles channel mode +J
83  */
84 class KickRejoin : public ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >
85 {
86         const unsigned int max;
87  public:
88         KickRejoin(Module* Creator)
89                 : ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >(Creator, "kicknorejoin", 'J')
90                 , max(60)
91         {
92         }
93
94         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
95         {
96                 int v = ConvToInt(parameter);
97                 if (v <= 0)
98                 {
99                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
100                         return MODEACTION_DENY;
101                 }
102
103                 if ((IS_LOCAL(source) && ((unsigned int)v > max)))
104                         v = max;
105
106                 ext.set(channel, new KickRejoinData(v));
107                 return MODEACTION_ALLOW;
108         }
109
110         void SerializeParam(Channel* chan, const KickRejoinData* krd, std::string& out)
111         {
112                 out.append(ConvToStr(krd->delay));
113         }
114
115         std::string GetModuleSettings() const
116         {
117                 return ConvToStr(max);
118         }
119 };
120
121 class ModuleKickNoRejoin : public Module
122 {
123         KickRejoin kr;
124
125 public:
126         ModuleKickNoRejoin()
127                 : kr(this)
128         {
129         }
130
131         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
132         {
133                 if (chan)
134                 {
135                         const KickRejoinData* data = kr.ext.get(chan);
136                         if ((data) && (!data->canjoin(user)))
137                         {
138                                 user->WriteNumeric(ERR_DELAYREJOIN, chan, InspIRCd::Format("You must wait %u seconds after being kicked to rejoin (+J)", data->delay));
139                                 return MOD_RES_DENY;
140                         }
141                 }
142                 return MOD_RES_PASSTHRU;
143         }
144
145         void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts) CXX11_OVERRIDE
146         {
147                 if ((!IS_LOCAL(memb->user)) || (source == memb->user))
148                         return;
149
150                 KickRejoinData* data = kr.ext.get(memb->chan);
151                 if (data)
152                 {
153                         data->add(memb->user);
154                 }
155         }
156
157         Version GetVersion() CXX11_OVERRIDE
158         {
159                 return Version("Channel mode to delay rejoin after kick", VF_VENDOR | VF_COMMON, kr.GetModuleSettings());
160         }
161 };
162
163 MODULE_INIT(ModuleKickNoRejoin)