]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nicklock.cpp
m_kicknorejoin: Advertise upper bound. Closes #913
[user/henk/code/inspircd.git] / src / modules / m_nicklock.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2005-2006 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24
25 /** Handle /NICKLOCK
26  */
27 class CommandNicklock : public Command
28 {
29  public:
30         LocalIntExt& locked;
31         CommandNicklock (Module* Creator, LocalIntExt& ext) : Command(Creator,"NICKLOCK", 2),
32                 locked(ext)
33         {
34                 flags_needed = 'o';
35                 syntax = "<oldnick> <newnick>";
36                 TRANSLATE2(TR_NICK, TR_TEXT);
37         }
38
39         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
40         {
41                 User* target = ServerInstance->FindNick(parameters[0]);
42
43                 if ((!target) || (target->registered != REG_ALL))
44                 {
45                         user->WriteNotice("*** No such nickname: '" + parameters[0] + "'");
46                         return CMD_FAILURE;
47                 }
48
49                 /* Do local sanity checks and bails */
50                 if (IS_LOCAL(user))
51                 {
52                         if (!ServerInstance->IsNick(parameters[1]))
53                         {
54                                 user->WriteNotice("*** Invalid nickname '" + parameters[1] + "'");
55                                 return CMD_FAILURE;
56                         }
57
58                         user->WriteNumeric(947, "%s :Nickname now locked.", parameters[1].c_str());
59                 }
60
61                 /* If we made it this far, extend the user */
62                 if (IS_LOCAL(target))
63                 {
64                         locked.set(target, 1);
65
66                         std::string oldnick = target->nick;
67                         if (target->ChangeNick(parameters[1]))
68                                 ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used NICKLOCK to change and hold "+oldnick+" to "+parameters[1]);
69                         else
70                         {
71                                 std::string newnick = target->nick;
72                                 ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used NICKLOCK, but "+oldnick+" failed nick change to "+parameters[1]+" and was locked to "+newnick+" instead");
73                         }
74                 }
75
76                 return CMD_SUCCESS;
77         }
78
79         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
80         {
81                 User* dest = ServerInstance->FindNick(parameters[0]);
82                 if (dest)
83                         return ROUTE_OPT_UCAST(dest->server);
84                 return ROUTE_LOCALONLY;
85         }
86 };
87
88 /** Handle /NICKUNLOCK
89  */
90 class CommandNickunlock : public Command
91 {
92  public:
93         LocalIntExt& locked;
94         CommandNickunlock (Module* Creator, LocalIntExt& ext) : Command(Creator,"NICKUNLOCK", 1),
95                 locked(ext)
96         {
97                 flags_needed = 'o';
98                 syntax = "<locked-nick>";
99                 TRANSLATE1(TR_NICK);
100         }
101
102         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
103         {
104                 User* target = ServerInstance->FindNick(parameters[0]);
105
106                 if (!target)
107                 {
108                         user->WriteNotice("*** No such nickname: '" + parameters[0] + "'");
109                         return CMD_FAILURE;
110                 }
111
112                 if (IS_LOCAL(target))
113                 {
114                         if (locked.set(target, 0))
115                         {
116                                 ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used NICKUNLOCK on "+target->nick);
117                                 user->SendText(":%s 945 %s %s :Nickname now unlocked.",
118                                         ServerInstance->Config->ServerName.c_str(),user->nick.c_str(),target->nick.c_str());
119                         }
120                         else
121                         {
122                                 user->SendText(":%s 946 %s %s :This user's nickname is not locked.",
123                                         ServerInstance->Config->ServerName.c_str(),user->nick.c_str(),target->nick.c_str());
124                                 return CMD_FAILURE;
125                         }
126                 }
127
128                 return CMD_SUCCESS;
129         }
130
131         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
132         {
133                 User* dest = ServerInstance->FindNick(parameters[0]);
134                 if (dest)
135                         return ROUTE_OPT_UCAST(dest->server);
136                 return ROUTE_LOCALONLY;
137         }
138 };
139
140 class ModuleNickLock : public Module
141 {
142         LocalIntExt locked;
143         CommandNicklock cmd1;
144         CommandNickunlock cmd2;
145  public:
146         ModuleNickLock()
147                 : locked("nick_locked", ExtensionItem::EXT_USER, this)
148                 , cmd1(this, locked)
149                 , cmd2(this, locked)
150         {
151         }
152
153         Version GetVersion() CXX11_OVERRIDE
154         {
155                 return Version("Provides the NICKLOCK command, allows an oper to change a users nick and lock them to it until they quit", VF_OPTCOMMON | VF_VENDOR);
156         }
157
158         ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
159         {
160                 if (locked.get(user))
161                 {
162                         user->WriteNumeric(ERR_CANTCHANGENICK, ":You cannot change your nickname (your nick is locked)");
163                         return MOD_RES_DENY;
164                 }
165                 return MOD_RES_PASSTHRU;
166         }
167
168         void Prioritize()
169         {
170                 Module *nflood = ServerInstance->Modules->Find("m_nickflood.so");
171                 ServerInstance->Modules->SetPriority(this, I_OnUserPreNick, PRIORITY_BEFORE, nflood);
172         }
173 };
174
175 MODULE_INIT(ModuleNickLock)