]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/svsnick.cpp
e9292f445a9a89e4ff38c9b0312e13b1d55dcd90
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / svsnick.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 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) 2008 Robin Burchell <robin+git@viroteck.net>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #include "inspircd.h"
26
27 #include "main.h"
28 #include "commands.h"
29
30 CmdResult CommandSVSNick::Handle(User* user, Params& parameters)
31 {
32         User* u = ServerInstance->FindNick(parameters[0]);
33
34         if (u && IS_LOCAL(u))
35         {
36                 // The 4th parameter is optional and it is the expected nick TS of the target user. If this parameter is
37                 // present and it doesn't match the user's nick TS, the SVSNICK is not acted upon.
38                 // This makes it possible to detect the case when services wants to change the nick of a user, but the
39                 // user changes their nick before the SVSNICK arrives, making the SVSNICK nick change (usually to a guest nick)
40                 // unnecessary. Consider the following for example:
41                 //
42                 // 1. test changes nick to Attila which is protected by services
43                 // 2. Services SVSNICKs the user to Guest12345
44                 // 3. Attila changes nick to Attila_ which isn't protected by services
45                 // 4. SVSNICK arrives
46                 // 5. Attila_ gets his nick changed to Guest12345 unnecessarily
47                 //
48                 // In this case when the SVSNICK is processed the target has already changed their nick to something
49                 // which isn't protected, so changing the nick again to a Guest nick is not desired.
50                 // However, if the expected nick TS parameter is present in the SVSNICK then the nick change in step 5
51                 // won't happen because the timestamps won't match.
52                 if (parameters.size() > 3)
53                 {
54                         time_t ExpectedTS = ConvToNum<time_t>(parameters[3]);
55                         if (u->age != ExpectedTS)
56                                 return CMD_FAILURE; // Ignore SVSNICK
57                 }
58
59                 std::string nick = parameters[1];
60                 if (isdigit(nick[0]))
61                         nick = u->uuid;
62
63                 time_t NickTS = ConvToNum<time_t>(parameters[2]);
64                 if (NickTS <= 0)
65                         return CMD_FAILURE;
66
67                 if (!u->ChangeNick(nick, NickTS))
68                 {
69                         // Changing to 'nick' failed (it may already be in use), change to the uuid
70                         u->ChangeNick(u->uuid);
71                 }
72         }
73
74         return CMD_SUCCESS;
75 }
76
77 RouteDescriptor CommandSVSNick::GetRouting(User* user, const Params& parameters)
78 {
79         return ROUTE_OPT_UCAST(parameters[0]);
80 }