]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/uid.cpp
Remove a few needless string crops, compile fix
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / uid.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "commands.h"
16
17 #include "utils.h"
18 #include "link.h"
19 #include "treesocket.h"
20 #include "treeserver.h"
21 #include "resolvers.h"
22
23 CmdResult CommandUID::Handle(const parameterlist &params, User* serversrc)
24 {
25         SpanningTreeUtilities* Utils = ((ModuleSpanningTree*)(Module*)creator)->Utils;
26         /** Do we have enough parameters:
27          *      0    1    2    3    4    5        6        7     8        9       (n-1)
28          * UID uuid age nick host dhost ident ip.string signon +modes (modepara) :gecos
29          */
30         if (params.size() < 10)
31                 return CMD_INVALID;
32
33         time_t age_t = ConvToInt(params[1]);
34         time_t signon = ConvToInt(params[7]);
35         std::string empty;
36         std::string nick(params[2]);
37         std::string modestr(params[8]);
38
39         TreeServer* remoteserver = Utils->FindServer(serversrc->server);
40
41         if (!remoteserver)
42                 return CMD_INVALID;
43         /* Check parameters for validity before introducing the client, discovered by dmb */
44         if (!age_t)
45                 return CMD_INVALID;
46         if (!signon)
47                 return CMD_INVALID;
48         if (modestr[0] != '+')
49                 return CMD_INVALID;
50         TreeSocket* sock = remoteserver->GetRoute()->GetSocket();
51
52         /* check for collision */
53         user_hash::iterator iter = ServerInstance->Users->clientlist->find(nick);
54
55         if (iter != ServerInstance->Users->clientlist->end())
56         {
57                 /*
58                  * Nick collision.
59                  */
60                 int collide = sock->DoCollision(iter->second, age_t, params[5], modestr, params[0]);
61                 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"*** Collision on %s, collide=%d", params[2].c_str(), collide);
62
63                 if (collide != 1)
64                 {
65                         /* remote client changed, make sure we change their nick for the hash too */
66                         nick = params[0];
67                 }
68         }
69
70         /* IMPORTANT NOTE: For remote users, we pass the UUID in the constructor. This automatically
71          * sets it up in the UUID hash for us.
72          */
73         User* _new = NULL;
74         try
75         {
76                 _new = new RemoteUser(params[0], remoteserver->GetName());
77         }
78         catch (...)
79         {
80                 return CMD_INVALID;
81         }
82         (*(ServerInstance->Users->clientlist))[nick] = _new;
83         _new->nick = nick;
84         _new->host = params[3];
85         _new->dhost = params[4];
86         _new->ident = params[5];
87         _new->fullname = params[params.size() - 1];
88         _new->registered = REG_ALL;
89         _new->signon = signon;
90         _new->age = age_t;
91
92         /* we need to remove the + from the modestring, so we can do our stuff */
93         std::string::size_type pos_after_plus = modestr.find_first_not_of('+');
94         if (pos_after_plus != std::string::npos)
95         modestr = modestr.substr(pos_after_plus);
96
97         unsigned int paramptr = 9;
98         for (std::string::iterator v = modestr.begin(); v != modestr.end(); v++)
99         {
100                 if (*v == '+')
101                         continue;
102
103                 /* For each mode thats set, increase counter */
104                 ModeHandler* mh = ServerInstance->Modes->FindMode(*v, MODETYPE_USER);
105
106                 if (mh)
107                 {
108                         if (mh->GetNumParams(true))
109                         {
110                                 if (paramptr >= params.size() - 1)
111                                         return CMD_INVALID;
112                                 std::string mp = params[paramptr++];
113                                 /* IMPORTANT NOTE:
114                                  * All modes are assumed to succeed here as they are being set by a remote server.
115                                  * Modes CANNOT FAIL here. If they DO fail, then the failure is ignored. This is important
116                                  * to note as all but one modules currently cannot ever fail in this situation, except for
117                                  * m_servprotect which specifically works this way to prevent the mode being set ANYWHERE
118                                  * but here, at client introduction. You may safely assume this behaviour is standard and
119                                  * will not change in future versions if you want to make use of this protective behaviour
120                                  * yourself.
121                                  */
122                                 mh->OnModeChange(_new, _new, NULL, mp, true);
123                         }
124                         else
125                                 mh->OnModeChange(_new, _new, NULL, empty, true);
126                         _new->SetMode(*v, true);
127                 }
128         }
129
130         /* now we've done with modes processing, put the + back for remote servers */
131         if (modestr[0] != '+')
132                 modestr = "+" + modestr;
133
134         _new->SetClientIP(params[6].c_str());
135
136         ServerInstance->Users->AddGlobalClone(_new);
137         remoteserver->SetUserCount(1); // increment by 1
138
139         bool dosend = true;
140
141         if ((Utils->quiet_bursts && remoteserver->bursting) || ServerInstance->SilentULine(_new->server))
142                 dosend = false;
143
144         if (dosend)
145                 ServerInstance->SNO->WriteToSnoMask('C',"Client connecting at %s: %s!%s@%s [%s] [%s]", _new->server.c_str(), _new->nick.c_str(), _new->ident.c_str(), _new->host.c_str(), _new->GetIPString(), _new->fullname.c_str());
146
147         FOREACH_MOD(I_OnPostConnect,OnPostConnect(_new));
148
149         return CMD_SUCCESS;
150 }
151
152 CmdResult CommandFHost::Handle(const parameterlist &params, User* src)
153 {
154         if (params.size() < 1)
155                 return CMD_INVALID;
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 (params.size() < 1)
165                 return CMD_INVALID;
166         if (IS_SERVER(src))
167                 return CMD_FAILURE;
168         src->ChangeIdent(params[0].c_str());
169         return CMD_SUCCESS;
170 }
171
172 CmdResult CommandFName::Handle(const parameterlist &params, User* src)
173 {
174         if (params.size() < 1)
175                 return CMD_INVALID;
176         if (IS_SERVER(src))
177                 return CMD_FAILURE;
178         src->ChangeName(params[0].c_str());
179         return CMD_SUCCESS;
180 }
181