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