]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/uid.cpp
Convert WriteNumeric() calls to pass the parameters of the numeric as method parameters
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / uid.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23 #include "commands.h"
24
25 #include "utils.h"
26 #include "treeserver.h"
27
28 CmdResult CommandUID::HandleServer(TreeServer* remoteserver, std::vector<std::string>& params)
29 {
30         /**
31          *      0    1    2    3    4    5        6        7     8        9       (n-1)
32          * UID uuid age nick host dhost ident ip.string signon +modes (modepara) :gecos
33          */
34         time_t age_t = ServerCommand::ExtractTS(params[1]);
35         time_t signon = ServerCommand::ExtractTS(params[7]);
36         std::string empty;
37         const std::string& modestr = params[8];
38
39         // Check if the length of the uuid is correct and confirm the sid portion of the uuid matches the sid of the server introducing the user
40         if (params[0].length() != UIDGenerator::UUID_LENGTH || params[0].compare(0, 3, remoteserver->GetID()))
41                 throw ProtocolException("Bogus UUID");
42         // Sanity check on mode string: must begin with '+'
43         if (modestr[0] != '+')
44                 throw ProtocolException("Invalid mode string");
45
46         // See if there is a nick collision
47         User* collideswith = ServerInstance->FindNickOnly(params[2]);
48         if ((collideswith) && (collideswith->registered != REG_ALL))
49         {
50                 // User that the incoming user is colliding with is not fully registered, we force nick change the
51                 // unregistered user to their uuid and tell them what happened
52                 collideswith->WriteFrom(collideswith, "NICK %s", collideswith->uuid.c_str());
53                 collideswith->WriteNumeric(ERR_NICKNAMEINUSE, collideswith->nick, "Nickname overruled.");
54
55                 // Clear the bit before calling User::ChangeNick() to make it NOT run the OnUserPostNick() hook
56                 collideswith->registered &= ~REG_NICK;
57                 collideswith->ChangeNick(collideswith->uuid);
58         }
59         else if (collideswith)
60         {
61                 // The user on this side is registered, handle the collision
62                 bool they_change = Utils->DoCollision(collideswith, remoteserver, age_t, params[5], params[6], params[0], "UID");
63                 if (they_change)
64                 {
65                         // The client being introduced needs to change nick to uuid, change the nick in the message before
66                         // processing/forwarding it. Also change the nick TS to CommandSave::SavedTimestamp.
67                         age_t = CommandSave::SavedTimestamp;
68                         params[1] = ConvToStr(CommandSave::SavedTimestamp);
69                         params[2] = params[0];
70                 }
71         }
72
73         /* For remote users, we pass the UUID they sent to the constructor.
74          * If the UUID already exists User::User() throws an exception which causes this connection to be closed.
75          */
76         RemoteUser* _new = new RemoteUser(params[0], remoteserver);
77         ServerInstance->Users->clientlist[params[2]] = _new;
78         _new->nick = params[2];
79         _new->host = params[3];
80         _new->dhost = params[4];
81         _new->ident = params[5];
82         _new->fullname = params.back();
83         _new->registered = REG_ALL;
84         _new->signon = signon;
85         _new->age = age_t;
86
87         unsigned int paramptr = 9;
88
89         for (std::string::const_iterator v = modestr.begin(); v != modestr.end(); ++v)
90         {
91                 // Accept more '+' chars, for now
92                 if (*v == '+')
93                         continue;
94
95                 /* For each mode thats set, find the mode handler and set it on the new user */
96                 ModeHandler* mh = ServerInstance->Modes->FindMode(*v, MODETYPE_USER);
97                 if (!mh)
98                         throw ProtocolException("Unrecognised mode '" + std::string(1, *v) + "'");
99
100                 if (mh->GetNumParams(true))
101                 {
102                         if (paramptr >= params.size() - 1)
103                                 throw ProtocolException("Out of parameters while processing modes");
104                         std::string mp = params[paramptr++];
105                         /* IMPORTANT NOTE:
106                          * All modes are assumed to succeed here as they are being set by a remote server.
107                          * Modes CANNOT FAIL here. If they DO fail, then the failure is ignored. This is important
108                          * to note as all but one modules currently cannot ever fail in this situation, except for
109                          * m_servprotect which specifically works this way to prevent the mode being set ANYWHERE
110                          * but here, at client introduction. You may safely assume this behaviour is standard and
111                          * will not change in future versions if you want to make use of this protective behaviour
112                          * yourself.
113                          */
114                         mh->OnModeChange(_new, _new, NULL, mp, true);
115                 }
116                 else
117                         mh->OnModeChange(_new, _new, NULL, empty, true);
118                 _new->SetMode(mh, true);
119         }
120
121         _new->SetClientIP(params[6].c_str());
122
123         ServerInstance->Users->AddClone(_new);
124         remoteserver->UserCount++;
125
126         bool dosend = true;
127
128         if ((Utils->quiet_bursts && remoteserver->IsBehindBursting()) || _new->server->IsSilentULine())
129                 dosend = false;
130
131         if (dosend)
132                 ServerInstance->SNO->WriteToSnoMask('C',"Client connecting at %s: %s (%s) [%s]", remoteserver->GetName().c_str(), _new->GetFullRealHost().c_str(), _new->GetIPString().c_str(), _new->fullname.c_str());
133
134         FOREACH_MOD(OnPostConnect, (_new));
135
136         return CMD_SUCCESS;
137 }
138
139 CmdResult CommandFHost::HandleRemote(RemoteUser* src, std::vector<std::string>& params)
140 {
141         src->ChangeDisplayedHost(params[0]);
142         return CMD_SUCCESS;
143 }
144
145 CmdResult CommandFIdent::HandleRemote(RemoteUser* src, std::vector<std::string>& params)
146 {
147         src->ChangeIdent(params[0]);
148         return CMD_SUCCESS;
149 }
150
151 CmdResult CommandFName::HandleRemote(RemoteUser* src, std::vector<std::string>& params)
152 {
153         src->ChangeName(params[0]);
154         return CMD_SUCCESS;
155 }
156
157 CommandUID::Builder::Builder(User* user)
158         : CmdBuilder(TreeServer::Get(user)->GetID(), "UID")
159 {
160         push(user->uuid);
161         push_int(user->age);
162         push(user->nick);
163         push(user->host);
164         push(user->dhost);
165         push(user->ident);
166         push(user->GetIPString());
167         push_int(user->signon);
168         push('+').push_raw(user->FormatModes(true));
169         push_last(user->fullname);
170 }