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