]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_user/cmd_nick.cpp
65906935107e3d524959b267fd6a37056bad5bd6
[user/henk/code/inspircd.git] / src / coremods / core_user / cmd_nick.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
5  *   Copyright (C) 2014, 2016 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2013, 2016, 2018 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
13  *
14  * This file is part of InspIRCd.  InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27
28 #include "inspircd.h"
29 #include "core_user.h"
30
31 CommandNick::CommandNick(Module* parent)
32         : SplitCommand(parent, "NICK", 1, 1)
33 {
34         works_before_reg = true;
35         syntax = "<newnick>";
36         Penalty = 0;
37 }
38
39 /** Handle nick changes from users.
40  * NOTE: If you are used to ircds based on ircd2.8, and are looking
41  * for the client introduction code in here, youre in the wrong place.
42  * You need to look in the spanningtree module for this!
43  */
44 CmdResult CommandNick::HandleLocal(LocalUser* user, const Params& parameters)
45 {
46         std::string oldnick = user->nick;
47         std::string newnick = parameters[0];
48
49         // anything except the initial NICK gets a flood penalty
50         if (user->registered == REG_ALL)
51                 user->CommandFloodPenalty += 4000;
52
53         if (newnick.empty())
54         {
55                 user->WriteNumeric(ERR_NONICKNAMEGIVEN, "No nickname given");
56                 return CMD_FAILURE;
57         }
58
59         if (newnick == "0")
60         {
61                 newnick = user->uuid;
62         }
63         else if (!ServerInstance->IsNick(newnick))
64         {
65                 user->WriteNumeric(ERR_ERRONEUSNICKNAME, newnick, "Erroneous Nickname");
66                 return CMD_FAILURE;
67         }
68
69         ModResult MOD_RESULT;
70         FIRST_MOD_RESULT(OnUserPreNick, MOD_RESULT, (user, newnick));
71
72         // If a module denied the change, abort now
73         if (MOD_RESULT == MOD_RES_DENY)
74                 return CMD_FAILURE;
75
76         // Disallow the nick change if <security:restrictbannedusers> is on and there is a ban matching this user in
77         // one of the channels they are on
78         if (ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL)
79         {
80                 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); ++i)
81                 {
82                         Channel* chan = (*i)->chan;
83                         if (chan->GetPrefixValue(user) < VOICE_VALUE && chan->IsBanned(user))
84                         {
85                                 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
86                                         user->WriteNumeric(ERR_CANTCHANGENICK, InspIRCd::Format("Cannot change nickname while on %s (you're banned)",
87                                                 chan->name.c_str()));
88                                 return CMD_FAILURE;
89                         }
90                 }
91         }
92
93         if (!user->ChangeNick(newnick))
94                 return CMD_FAILURE;
95
96         if (user->registered < REG_NICKUSER)
97         {
98                 user->registered = (user->registered | REG_NICK);
99                 return CommandUser::CheckRegister(user);
100         }
101
102         return CMD_SUCCESS;
103 }