]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_kicknorejoin.cpp
Improvements and bugfixes to the cgiirc module.
[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 enum
29 {
30         // From RFC 2182.
31         ERR_UNAVAILRESOURCE = 437
32 };
33
34
35 class KickRejoinData
36 {
37         struct KickedUser
38         {
39                 std::string uuid;
40                 time_t expire;
41
42                 KickedUser(User* user, unsigned int Delay)
43                         : uuid(user->uuid)
44                         , expire(ServerInstance->Time() + Delay)
45                 {
46                 }
47         };
48
49         typedef std::vector<KickedUser> KickedList;
50
51         mutable KickedList kicked;
52
53  public:
54         const unsigned int delay;
55
56         KickRejoinData(unsigned int Delay) : delay(Delay) { }
57
58         bool canjoin(LocalUser* user) const
59         {
60                 for (KickedList::iterator i = kicked.begin(); i != kicked.end(); )
61                 {
62                         KickedUser& rec = *i;
63                         if (rec.expire > ServerInstance->Time())
64                         {
65                                 if (rec.uuid == user->uuid)
66                                         return false;
67                                 ++i;
68                         }
69                         else
70                         {
71                                 // Expired record, remove.
72                                 stdalgo::vector::swaperase(kicked, i);
73                                 if (kicked.empty())
74                                         break;
75                         }
76                 }
77                 return true;
78         }
79
80         void add(User* user)
81         {
82                 // One user can be in the list multiple times if the user gets kicked, force joins
83                 // (skipping OnUserPreJoin) and gets kicked again, but that's okay because canjoin()
84                 // works correctly in this case as well
85                 kicked.push_back(KickedUser(user, delay));
86         }
87 };
88
89 /** Handles channel mode +J
90  */
91 class KickRejoin : public ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >
92 {
93         const unsigned int max;
94  public:
95         KickRejoin(Module* Creator)
96                 : ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >(Creator, "kicknorejoin", 'J')
97                 , max(60)
98         {
99         }
100
101         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
102         {
103                 int v = ConvToInt(parameter);
104                 if (v <= 0)
105                 {
106                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
107                         return MODEACTION_DENY;
108                 }
109
110                 if ((IS_LOCAL(source) && ((unsigned int)v > max)))
111                         v = max;
112
113                 ext.set(channel, new KickRejoinData(v));
114                 return MODEACTION_ALLOW;
115         }
116
117         void SerializeParam(Channel* chan, const KickRejoinData* krd, std::string& out)
118         {
119                 out.append(ConvToStr(krd->delay));
120         }
121
122         std::string GetModuleSettings() const
123         {
124                 return ConvToStr(max);
125         }
126 };
127
128 class ModuleKickNoRejoin : public Module
129 {
130         KickRejoin kr;
131
132 public:
133         ModuleKickNoRejoin()
134                 : kr(this)
135         {
136         }
137
138         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
139         {
140                 if (chan)
141                 {
142                         const KickRejoinData* data = kr.ext.get(chan);
143                         if ((data) && (!data->canjoin(user)))
144                         {
145                                 user->WriteNumeric(ERR_UNAVAILRESOURCE, chan, InspIRCd::Format("You must wait %u seconds after being kicked to rejoin (+J)", data->delay));
146                                 return MOD_RES_DENY;
147                         }
148                 }
149                 return MOD_RES_PASSTHRU;
150         }
151
152         void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts) CXX11_OVERRIDE
153         {
154                 if ((!IS_LOCAL(memb->user)) || (source == memb->user))
155                         return;
156
157                 KickRejoinData* data = kr.ext.get(memb->chan);
158                 if (data)
159                 {
160                         data->add(memb->user);
161                 }
162         }
163
164         Version GetVersion() CXX11_OVERRIDE
165         {
166                 return Version("Channel mode to delay rejoin after kick", VF_VENDOR | VF_COMMON, kr.GetModuleSettings());
167         }
168 };
169
170 MODULE_INIT(ModuleKickNoRejoin)