2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
26 #include "treeserver.h"
28 CmdResult CommandUID::HandleServer(TreeServer* remoteserver, std::vector<std::string>& params)
30 /** Do we have enough parameters:
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
34 time_t age_t = ServerCommand::ExtractTS(params[1]);
35 time_t signon = ServerCommand::ExtractTS(params[7]);
37 const std::string& modestr = params[8];
39 /* Is this a valid UID, and not misrouted? */
40 if (params[0].length() != UIDGenerator::UUID_LENGTH || params[0].compare(0, 3, remoteserver->GetID()))
41 throw ProtocolException("Bogus UUID");
42 /* Check parameters for validity before introducing the client, discovered by dmb */
43 if (modestr[0] != '+')
44 throw ProtocolException("Invalid mode string");
46 // See if there is a nick collision
47 User* collideswith = ServerInstance->FindNickOnly(params[2]);
48 if ((collideswith) && (collideswith->registered != REG_ALL))
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, "%s :Nickname overruled.", collideswith->nick.c_str());
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);
59 else if (collideswith)
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]);
63 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Collision on %s %d", params[2].c_str(), they_change);
67 // The client being introduced needs to change nick to uuid, change the nick in the message before
68 // processing/forwarding it. Also change the nick TS to CommandSave::SavedTimestamp.
69 age_t = CommandSave::SavedTimestamp;
70 params[1] = ConvToStr(CommandSave::SavedTimestamp);
71 params[2] = params[0];
75 /* IMPORTANT NOTE: For remote users, we pass the UUID in the constructor. This automatically
76 * sets it up in the UUID hash for us.
78 * If the UUID already exists User::User() throws an exception which causes this connection to be closed.
80 RemoteUser* _new = new RemoteUser(params[0], remoteserver);
81 ServerInstance->Users->clientlist[params[2]] = _new;
82 _new->nick = params[2];
83 _new->host = params[3];
84 _new->dhost = params[4];
85 _new->ident = params[5];
86 _new->fullname = params.back();
87 _new->registered = REG_ALL;
88 _new->signon = signon;
91 unsigned int paramptr = 9;
93 for (std::string::const_iterator v = modestr.begin(); v != modestr.end(); ++v)
95 // Accept more '+' chars, for now
99 /* For each mode thats set, find the mode handler and set it on the new user */
100 ModeHandler* mh = ServerInstance->Modes->FindMode(*v, MODETYPE_USER);
102 throw ProtocolException("Unrecognised mode '" + std::string(1, *v) + "'");
104 if (mh->GetNumParams(true))
106 if (paramptr >= params.size() - 1)
107 throw ProtocolException("Out of parameters while processing modes");
108 std::string mp = params[paramptr++];
110 * All modes are assumed to succeed here as they are being set by a remote server.
111 * Modes CANNOT FAIL here. If they DO fail, then the failure is ignored. This is important
112 * to note as all but one modules currently cannot ever fail in this situation, except for
113 * m_servprotect which specifically works this way to prevent the mode being set ANYWHERE
114 * but here, at client introduction. You may safely assume this behaviour is standard and
115 * will not change in future versions if you want to make use of this protective behaviour
118 mh->OnModeChange(_new, _new, NULL, mp, true);
121 mh->OnModeChange(_new, _new, NULL, empty, true);
122 _new->SetMode(mh, true);
125 _new->SetClientIP(params[6].c_str());
127 ServerInstance->Users->AddClone(_new);
128 remoteserver->UserCount++;
132 if ((Utils->quiet_bursts && remoteserver->IsBehindBursting()) || _new->server->IsSilentULine())
136 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());
138 FOREACH_MOD(OnPostConnect, (_new));
143 CmdResult CommandFHost::HandleRemote(RemoteUser* src, std::vector<std::string>& params)
145 src->ChangeDisplayedHost(params[0]);
149 CmdResult CommandFIdent::HandleRemote(RemoteUser* src, std::vector<std::string>& params)
151 src->ChangeIdent(params[0]);
155 CmdResult CommandFName::HandleRemote(RemoteUser* src, std::vector<std::string>& params)
157 src->ChangeName(params[0]);
161 CommandUID::Builder::Builder(User* user)
162 : CmdBuilder(TreeServer::Get(user)->GetID(), "UID")
170 push(user->GetIPString());
171 push_int(user->signon);
172 push('+').push_raw(user->FormatModes(true));
173 push_last(user->fullname);