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