]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/nickcollide.cpp
Update copyrights for 2009.
[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://www.inspircd.org/wiki/index.php/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 "m_spanningtree/treesocket.h"
18 #include "m_spanningtree/treeserver.h"
19 #include "m_spanningtree/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 SVSNICKs 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         if (bChangeLocal)
89         {
90                 u->ForceNickChange(u->uuid.c_str());
91
92                 if (!bChangeRemote)
93                         return 1;
94         }
95         if (bChangeRemote)
96         {
97                 /*
98                  * Cheat a little here. Instead of a dedicated command to change UID,
99                  * use SVSNICK and accept their client with it's UID (as we know the SVSNICK will
100                  * not fail under any circumstances -- UIDs are netwide exclusive).
101                  *
102                  * This means that each side of a collide will generate one extra NICK back to where
103                  * they have just linked (and where it got the SVSNICK from), however, it will
104                  * be dropped harmlessly as it will come in as :928AAAB NICK 928AAAB, and we already
105                  * have 928AAAB's nick set to that.
106                  *   -- w00t
107                  */
108                 User *remote = this->ServerInstance->FindUUID(remoteuid);
109
110                 if (remote)
111                 {
112                         /* buh.. nick change collide. force change their nick. */
113                         remote->ForceNickChange(remote->uuid.c_str());
114                 }
115                 else
116                 {
117                         /* user has not been introduced yet, just inform their server */
118                         this->WriteLine(std::string(":")+this->ServerInstance->Config->GetSID()+" SVSNICK "+remoteuid+" " + remoteuid + " " + ConvToStr(remotets));
119                 }
120
121                 if (!bChangeLocal)
122                         return 2;
123         }
124
125         return 3;
126 }
127