]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/uid.cpp
...because every now and again, i have to do a massive commit.
[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
16 #include "main.h"
17 #include "utils.h"
18 #include "treeserver.h"
19 #include "link.h"
20 #include "treesocket.h"
21 #include "resolvers.h"
22
23 /* $ModDep: m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h m_hash.h m_spanningtree/handshaketimer.h */
24
25 bool TreeSocket::ParseUID(const std::string &source, parameterlist &params)
26 {
27         /** Do we have enough parameters:
28          *      0    1    2    3    4    5        6        7     8        9       (n-1)
29          * UID uuid age nick host dhost ident ip.string signon +modes (modepara) :gecos
30          */
31         if (params.size() < 10)
32         {
33                 this->SendError("Invalid client introduction (wanted 10 or more parameters, got " + (params.empty() ? "0" : ConvToStr(params.size())) + "!)");
34                 return false;
35         }
36
37         time_t age_t = ConvToInt(params[1]);
38         time_t signon = ConvToInt(params[7]);
39         std::string empty;
40
41         TreeServer* remoteserver = Utils->FindServer(source);
42
43         if (!remoteserver)
44         {
45                 this->SendError("Invalid client introduction (Unknown server "+source+")");
46                 return false;
47         }
48         /* Check parameters for validity before introducing the client, discovered by dmb */
49         else if (!age_t)
50         {
51                 this->SendError("Invalid client introduction (Invalid TS?)");
52                 return false;
53         }
54         else if (!signon)
55         {
56                 this->SendError("Invalid client introduction (Invalid signon?)");
57                 return false;
58         }
59         else if (params[8][0] != '+')
60         {
61                 this->SendError("Invalid client introduction (Malformed MODE sequence?)");
62                 return false;
63         }
64
65         /* check for collision */
66         user_hash::iterator iter = ServerInstance->Users->clientlist->find(params[2]);
67
68         if (iter != ServerInstance->Users->clientlist->end())
69         {
70                 /*
71                  * Nick collision.
72                  */
73                 int collide = this->DoCollision(iter->second, age_t, params[5], params[8], params[0]);
74                 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"*** Collision on %s, collide=%d", params[2].c_str(), collide);
75
76                 if (collide != 1)
77                 {
78                         /* remote client changed, make sure we change their nick for the hash too */
79                         params[2] = params[0];
80                 }
81         }
82
83         /* IMPORTANT NOTE: For remote users, we pass the UUID in the constructor. This automatically
84          * sets it up in the UUID hash for us.
85          */
86         User* _new = NULL;
87         try
88         {
89                 _new = new RemoteUser(params[0], remoteserver->GetName());
90         }
91         catch (...)
92         {
93                 this->SendError("Protocol violation - Duplicate UUID '" + params[0] + "' on introduction of new user");
94                 return false;
95         }
96         (*(ServerInstance->Users->clientlist))[params[2]] = _new;
97         _new->nick.assign(params[2], 0, MAXBUF);
98         _new->host.assign(params[3], 0, 64);
99         _new->dhost.assign(params[4], 0, 64);
100         _new->ident.assign(params[5], 0, MAXBUF);
101         _new->fullname.assign(params[params.size() - 1], 0, MAXBUF);
102         _new->registered = REG_ALL;
103         _new->signon = signon;
104         _new->age = age_t;
105
106         /* we need to remove the + from the modestring, so we can do our stuff */
107         std::string::size_type pos_after_plus = params[8].find_first_not_of('+');
108         if (pos_after_plus != std::string::npos)
109         params[8] = params[8].substr(pos_after_plus);
110
111         unsigned int paramptr = 9;
112         for (std::string::iterator v = params[8].begin(); v != params[8].end(); v++)
113         {
114                 if (*v == '+')
115                         continue;
116
117                 /* For each mode thats set, increase counter */
118                 ModeHandler* mh = ServerInstance->Modes->FindMode(*v, MODETYPE_USER);
119
120                 if (mh)
121                 {
122                         if (mh->GetNumParams(true))
123                         {
124                                 /* IMPORTANT NOTE:
125                                  * All modes are assumed to succeed here as they are being set by a remote server.
126                                  * Modes CANNOT FAIL here. If they DO fail, then the failure is ignored. This is important
127                                  * to note as all but one modules currently cannot ever fail in this situation, except for
128                                  * m_servprotect which specifically works this way to prevent the mode being set ANYWHERE
129                                  * but here, at client introduction. You may safely assume this behaviour is standard and
130                                  * will not change in future versions if you want to make use of this protective behaviour
131                                  * yourself.
132                                  */
133                                 if (paramptr < params.size() - 1)
134                                         mh->OnModeChange(_new, _new, NULL, params[paramptr++], true);
135                                 else
136                                 {
137                                         this->SendError(std::string("Broken UID command, expected a parameter for user mode '")+(*v)+"' but there aren't enough parameters in the command!");
138                                         return false;
139                                 }
140                         }
141                         else
142                                 mh->OnModeChange(_new, _new, NULL, empty, true);
143                         _new->SetMode(*v, true);
144                 }
145                 else
146                 {
147                         this->SendError(std::string("Warning: Broken UID command, unknown user mode '")+(*v)+"' in the mode string! (mismatched module?)");
148                         return false;
149                 }
150         }
151
152         /* now we've done with modes processing, put the + back for remote servers */
153         if (params[8][0] != '+')
154                 params[8] = "+" + params[8];
155
156         _new->SetClientIP(params[6].c_str());
157
158         ServerInstance->Users->AddGlobalClone(_new);
159         remoteserver->SetUserCount(1); // increment by 1
160
161         bool dosend = true;
162
163         if ((this->Utils->quiet_bursts && remoteserver->bursting) || ServerInstance->SilentULine(_new->server))
164                 dosend = false;
165
166         if (dosend)
167                 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());
168
169         params[params.size() - 1] = ":" + params[params.size() - 1];
170         Utils->DoOneToAllButSender(source, "UID", params, source);
171
172         FOREACH_MOD(I_OnPostConnect,OnPostConnect(_new));
173
174         return true;
175 }
176