]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/nickcollide.cpp
Add SAVE s2s protocol command
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / nickcollide.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 #include "xline.h"
16
17 #include "treesocket.h"
18 #include "treeserver.h"
19 #include "utils.h"
20
21 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
22
23
24 /*
25  * Yes, this function looks a little ugly.
26  * However, in some circumstances we may not have a User, so we need to do things this way.
27  * Returns 1 if colliding local client, 2 if colliding remote, 3 if colliding both.
28  * Sends SAVEs as appropriate and forces nickchanges too.
29  */
30 int TreeSocket::DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid)
31 {
32         /*
33          * Under old protocol rules, we would have had to kill both clients.
34          * Really, this sucks.
35          * These days, we have UID. And, so what we do is, force nick change client(s)
36          * involved according to timestamp rules.
37          *
38          * RULES:
39          *  user@ip equal:
40          *   Force nick change on OLDER timestamped client
41          *  user@ip differ:
42          *   Force nick change on NEWER timestamped client
43          *  TS EQUAL:
44          *   FNC both.
45          *
46          * This stops abusive use of collisions, simplifies problems with loops, and so on.
47          *   -- w00t
48          */
49         bool bChangeLocal = true;
50         bool bChangeRemote = true;
51
52         /* for brevity, don't use the User - use defines to avoid any copy */
53         #define localts u->age
54         #define localident u->ident
55         #define localip u->GetIPString()
56
57         /* mmk. let's do this again. */
58         if (remotets == localts)
59         {
60                 /* equal. fuck them both! do nada, let the handler at the bottom figure this out. */
61         }
62         else
63         {
64                 /* fuck. now it gets complex. */
65
66                 /* first, let's see if ident@host matches. */
67                 bool SamePerson = (localident == remoteident)
68                                 && (localip == remoteip);
69
70                 /*
71                  * if ident@ip is equal, and theirs is newer, or
72                  * ident@ip differ, and ours is newer
73                  */
74                 if((SamePerson && remotets < localts) ||
75                    (!SamePerson && remotets > localts))
76                 {
77                         /* remote needs to change */
78                         bChangeLocal = false;
79                 }
80                 else
81                 {
82                         /* ours needs to change */
83                         bChangeRemote = false;
84                 }
85         }
86
87         /*
88          * Cheat a little here. Instead of a dedicated command to change UID,
89          * use SAVE and accept the losing client with its UID (as we know the SAVE will
90          * not fail under any circumstances -- UIDs are netwide exclusive).
91          *
92          * This means that each side of a collide will generate one extra NICK back to where
93          * they have just linked (and where it got the SAVE from), however, it will
94          * be dropped harmlessly as it will come in as :928AAAB NICK 928AAAB, and we already
95          * have 928AAAB's nick set to that.
96          *   -- w00t
97          */
98
99         if (bChangeLocal)
100         {
101                 /*
102                  * Local-side nick needs to change. Just in case we are hub, and
103                  * this "local" nick is actually behind us, send an SAVE out.
104                  */
105                 parameterlist params;
106                 params.push_back(u->uuid);
107                 params.push_back(ConvToStr(u->age));
108                 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"SAVE",params);
109
110                 u->ForceNickChange(u->uuid.c_str());
111
112                 if (!bChangeRemote)
113                         return 1;
114         }
115         if (bChangeRemote)
116         {
117                 User *remote = this->ServerInstance->FindUUID(remoteuid);
118                 /*
119                  * remote side needs to change. If this happens, we will modify
120                  * the UID or halt the propagation of the nick change command,
121                  * so other servers don't need to see the SAVE
122                  */
123                 WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" SAVE "+remoteuid+" "+ ConvToStr(remotets));
124
125                 if (remote)
126                 {
127                         /* nick change collide. Force change their nick. */
128                         remote->ForceNickChange(remoteuid.c_str());
129                 }
130
131                 if (!bChangeLocal)
132                         return 2;
133         }
134
135         return 3;
136 }
137