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