]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/uid.cpp
37c54ae601770debbf38b68172d2e6b20006bff0
[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         /** 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
33          */
34         time_t age_t = ConvToInt(params[1]);
35         time_t signon = ConvToInt(params[7]);
36         std::string empty;
37         const std::string& modestr = params[8];
38
39         /* Is this a valid UID, and not misrouted? */
40         if (params[0].length() != UIDGenerator::UUID_LENGTH || params[0].substr(0, 3) != remoteserver->GetID())
41                 return CMD_INVALID;
42         /* Check parameters for validity before introducing the client, discovered by dmb */
43         if (!age_t)
44                 return CMD_INVALID;
45         if (!signon)
46                 return CMD_INVALID;
47         if (modestr[0] != '+')
48                 return CMD_INVALID;
49
50         /* check for collision */
51         User* collideswith = ServerInstance->FindNickOnly(params[2]);
52         if (collideswith)
53         {
54                 /*
55                  * Nick collision.
56                  */
57                 int collide = Utils->DoCollision(collideswith, remoteserver, age_t, params[5], params[6], params[0]);
58                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "*** Collision on %s, collide=%d", params[2].c_str(), collide);
59
60                 if (collide != 1)
61                 {
62                         // Remote client lost, make sure we change their nick for the hash too
63                         params[2] = params[0];
64                 }
65         }
66
67         /* IMPORTANT NOTE: For remote users, we pass the UUID in the constructor. This automatically
68          * sets it up in the UUID hash for us.
69          */
70         User* _new = NULL;
71         try
72         {
73                 _new = new RemoteUser(params[0], remoteserver);
74         }
75         catch (...)
76         {
77                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Duplicate UUID %s in client introduction", params[0].c_str());
78                 return CMD_INVALID;
79         }
80         (*(ServerInstance->Users->clientlist))[params[2]] = _new;
81         _new->nick = params[2];
82         _new->host = params[3];
83         _new->dhost = params[4];
84         _new->ident = params[5];
85         _new->fullname = params[params.size() - 1];
86         _new->registered = REG_ALL;
87         _new->signon = signon;
88         _new->age = age_t;
89
90         unsigned int paramptr = 9;
91
92         for (std::string::const_iterator v = modestr.begin(); v != modestr.end(); ++v)
93         {
94                 // Accept more '+' chars, for now
95                 if (*v == '+')
96                         continue;
97
98                 /* For each mode thats set, find the mode handler and set it on the new user */
99                 ModeHandler* mh = ServerInstance->Modes->FindMode(*v, MODETYPE_USER);
100                 if (!mh)
101                 {
102                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Unrecognised mode '%c' for a user in UID, dropping link", *v);
103                         return CMD_INVALID;
104                 }
105
106                 if (mh->GetNumParams(true))
107                 {
108                         if (paramptr >= params.size() - 1)
109                                 return CMD_INVALID;
110                         std::string mp = params[paramptr++];
111                         /* IMPORTANT NOTE:
112                          * All modes are assumed to succeed here as they are being set by a remote server.
113                          * Modes CANNOT FAIL here. If they DO fail, then the failure is ignored. This is important
114                          * to note as all but one modules currently cannot ever fail in this situation, except for
115                          * m_servprotect which specifically works this way to prevent the mode being set ANYWHERE
116                          * but here, at client introduction. You may safely assume this behaviour is standard and
117                          * will not change in future versions if you want to make use of this protective behaviour
118                          * yourself.
119                          */
120                         mh->OnModeChange(_new, _new, NULL, mp, true);
121                 }
122                 else
123                         mh->OnModeChange(_new, _new, NULL, empty, true);
124                 _new->SetMode(mh, true);
125         }
126
127         _new->SetClientIP(params[6].c_str());
128
129         ServerInstance->Users->AddGlobalClone(_new);
130         remoteserver->UserCount++;
131
132         bool dosend = true;
133
134         if ((Utils->quiet_bursts && remoteserver->bursting) || _new->server->IsSilentULine())
135                 dosend = false;
136
137         if (dosend)
138                 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());
139
140         FOREACH_MOD(OnPostConnect, (_new));
141
142         return CMD_SUCCESS;
143 }
144
145 CmdResult CommandFHost::HandleRemote(RemoteUser* src, std::vector<std::string>& params)
146 {
147         src->ChangeDisplayedHost(params[0]);
148         return CMD_SUCCESS;
149 }
150
151 CmdResult CommandFIdent::HandleRemote(RemoteUser* src, std::vector<std::string>& params)
152 {
153         src->ChangeIdent(params[0]);
154         return CMD_SUCCESS;
155 }
156
157 CmdResult CommandFName::HandleRemote(RemoteUser* src, std::vector<std::string>& params)
158 {
159         src->ChangeName(params[0]);
160         return CMD_SUCCESS;
161 }
162
163 CommandUID::Builder::Builder(User* user)
164         : CmdBuilder(TreeServer::Get(user)->GetID(), "UID")
165 {
166         push(user->uuid);
167         push_int(user->age);
168         push(user->nick);
169         push(user->host);
170         push(user->dhost);
171         push(user->ident);
172         push(user->GetIPString());
173         push_int(user->signon);
174         push('+').push_raw(user->FormatModes(true));
175         push_last(user->fullname);
176 }