]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nicklock.cpp
Merge insp20
[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, parameters[1], "Nickname now locked.");
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                 return ROUTE_OPT_UCAST(parameters[0]);
82         }
83 };
84
85 /** Handle /NICKUNLOCK
86  */
87 class CommandNickunlock : public Command
88 {
89  public:
90         LocalIntExt& locked;
91         CommandNickunlock (Module* Creator, LocalIntExt& ext) : Command(Creator,"NICKUNLOCK", 1),
92                 locked(ext)
93         {
94                 flags_needed = 'o';
95                 syntax = "<locked-nick>";
96                 TRANSLATE1(TR_NICK);
97         }
98
99         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
100         {
101                 User* target = ServerInstance->FindNick(parameters[0]);
102
103                 if (!target)
104                 {
105                         user->WriteNotice("*** No such nickname: '" + parameters[0] + "'");
106                         return CMD_FAILURE;
107                 }
108
109                 if (IS_LOCAL(target))
110                 {
111                         if (locked.set(target, 0))
112                         {
113                                 ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used NICKUNLOCK on "+target->nick);
114                                 user->WriteRemoteNumeric(945, target->nick, "Nickname now unlocked.");
115                         }
116                         else
117                         {
118                                 user->WriteRemoteNumeric(946, target->nick, "This user's nickname is not locked.");
119                                 return CMD_FAILURE;
120                         }
121                 }
122
123                 return CMD_SUCCESS;
124         }
125
126         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
127         {
128                 return ROUTE_OPT_UCAST(parameters[0]);
129         }
130 };
131
132 class ModuleNickLock : public Module
133 {
134         LocalIntExt locked;
135         CommandNicklock cmd1;
136         CommandNickunlock cmd2;
137  public:
138         ModuleNickLock()
139                 : locked("nick_locked", ExtensionItem::EXT_USER, this)
140                 , cmd1(this, locked)
141                 , cmd2(this, locked)
142         {
143         }
144
145         Version GetVersion() CXX11_OVERRIDE
146         {
147                 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);
148         }
149
150         ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
151         {
152                 if (locked.get(user))
153                 {
154                         user->WriteNumeric(ERR_CANTCHANGENICK, "You cannot change your nickname (your nick is locked)");
155                         return MOD_RES_DENY;
156                 }
157                 return MOD_RES_PASSTHRU;
158         }
159
160         void Prioritize()
161         {
162                 Module *nflood = ServerInstance->Modules->Find("m_nickflood.so");
163                 ServerInstance->Modules->SetPriority(this, I_OnUserPreNick, PRIORITY_BEFORE, nflood);
164         }
165 };
166
167 MODULE_INIT(ModuleNickLock)