]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_kicknorejoin.cpp
Attach to events and register services in init()
[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 /* $ModDesc: Provides channel mode +J (delay rejoin after kick) */
29
30 typedef std::map<User*, time_t> delaylist;
31
32 /** Handles channel mode +J
33  */
34 class KickRejoin : public ParamChannelModeHandler
35 {
36  public:
37         SimpleExtItem<delaylist> ext;
38         KickRejoin(Module* Creator) : ParamChannelModeHandler(Creator, "kicknorejoin", 'J'), ext("norejoinusers", Creator) { }
39
40         bool ParamValidate(std::string& parameter)
41         {
42                 int v = atoi(parameter.c_str());
43                 if (v <= 0)
44                         return false;
45                 parameter = ConvToStr(v);
46                 return true;
47         }
48
49         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
50         {
51                 ModeAction rv = ParamChannelModeHandler::OnModeChange(source, dest, channel, parameter, adding);
52                 if (rv == MODEACTION_ALLOW && !adding)
53                         ext.unset(channel);
54                 return rv;
55         }
56 };
57
58 class ModuleKickNoRejoin : public Module
59 {
60         KickRejoin kr;
61
62 public:
63
64         ModuleKickNoRejoin()
65                 : kr(this)
66         {
67         }
68
69         void init()
70         {
71                 if (!ServerInstance->Modes->AddMode(&kr))
72                         throw ModuleException("Could not add new modes!");
73                 ServerInstance->Extensions.Register(&kr.ext);
74                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserKick };
75                 ServerInstance->Modules->Attach(eventlist, this, 2);
76         }
77
78         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
79         {
80                 if (chan)
81                 {
82                         delaylist* dl = kr.ext.get(chan);
83                         if (dl)
84                         {
85                                 std::vector<User*> itemstoremove;
86
87                                 for (delaylist::iterator iter = dl->begin(); iter != dl->end(); iter++)
88                                 {
89                                         if (iter->second > ServerInstance->Time())
90                                         {
91                                                 if (iter->first == user)
92                                                 {
93                                                         std::string modeparam = chan->GetModeParameter(&kr);
94                                                         user->WriteNumeric(ERR_DELAYREJOIN, "%s %s :You must wait %s seconds after being kicked to rejoin (+J)",
95                                                                 user->nick.c_str(), chan->name.c_str(), modeparam.c_str());
96                                                         return MOD_RES_DENY;
97                                                 }
98                                         }
99                                         else
100                                         {
101                                                 // Expired record, remove.
102                                                 itemstoremove.push_back(iter->first);
103                                         }
104                                 }
105
106                                 for (unsigned int i = 0; i < itemstoremove.size(); i++)
107                                         dl->erase(itemstoremove[i]);
108
109                                 if (!dl->size())
110                                         kr.ext.unset(chan);
111                         }
112                 }
113                 return MOD_RES_PASSTHRU;
114         }
115
116         void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts)
117         {
118                 if (memb->chan->IsModeSet(&kr) && (source != memb->user))
119                 {
120                         delaylist* dl = kr.ext.get(memb->chan);
121                         if (!dl)
122                         {
123                                 dl = new delaylist;
124                                 kr.ext.set(memb->chan, dl);
125                         }
126                         (*dl)[memb->user] = ServerInstance->Time() + ConvToInt(memb->chan->GetModeParameter(&kr));
127                 }
128         }
129
130         ~ModuleKickNoRejoin()
131         {
132         }
133
134         Version GetVersion()
135         {
136                 return Version("Channel mode to delay rejoin after kick", VF_VENDOR);
137         }
138 };
139
140
141 MODULE_INIT(ModuleKickNoRejoin)