]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/fjoin.cpp
Clean up some FJOIN stuff
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / fjoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
17 #include "socket.h"
18 #include "wildcard.h"
19 #include "xline.h"
20 #include "transport.h"
21 #include "m_hash.h"
22 #include "socketengine.h"
23
24 #include "m_spanningtree/main.h"
25 #include "m_spanningtree/utils.h"
26 #include "m_spanningtree/treeserver.h"
27 #include "m_spanningtree/link.h"
28 #include "m_spanningtree/treesocket.h"
29 #include "m_spanningtree/resolvers.h"
30 #include "m_spanningtree/handshaketimer.h"
31
32 /* $ModDep: m_spanningtree/timesynctimer.h 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 */
33
34 /** FJOIN, similar to TS6 SJOIN, but not quite. */
35 bool TreeSocket::ForceJoin(const std::string &source, std::deque<std::string> &params)
36 {
37         /* 1.1 FJOIN works as follows:
38          *
39          * Each FJOIN is sent along with a timestamp, and the side with the lowest
40          * timestamp 'wins'. From this point on we will refer to this side as the
41          * winner. The side with the higher timestamp loses, from this point on we
42          * will call this side the loser or losing side. This should be familiar to
43          * anyone who's dealt with dreamforge or TS6 before.
44          *
45          * When two sides of a split heal and this occurs, the following things
46          * will happen:
47          *
48          * If the timestamps are exactly equal, both sides merge their privilages
49          * and users, as in InspIRCd 1.0 and ircd2.8. The channels have not been
50          * re-created during a split, this is safe to do.
51          *
52          * If the timestamps are NOT equal, the losing side removes all of its
53          * modes from the channel, before introducing new users into the channel
54          * which are listed in the FJOIN command's parameters. The losing side then
55          * LOWERS its timestamp value of the channel to match that of the winning
56          * side, and the modes of the users of the winning side are merged in with
57          * the losing side.
58          *
59          * The winning side on the other hand will ignore all user modes from the
60          * losing side, so only its own modes get applied. Life is simple for those
61          * who succeed at internets. :-)
62          *
63          * NOTE: Unlike TS6 and dreamforge and other protocols which have SJOIN,
64          * FJOIN does not contain the simple-modes such as +iklmnsp. Why not,
65          * you ask? Well, quite simply because we don't need to. They'll be sent
66          * after the FJOIN by FMODE, and FMODE is timestamped, so in the event
67          * the losing side sends any modes for the channel which shouldnt win,
68          * they wont as their timestamp will be too high :-)
69          */
70
71         if (params.size() < 2)
72                 return true;
73
74         irc::modestacker modestack(true);                               /* Modes to apply from the users in the user list */
75         User* who = NULL;                                               /* User we are currently checking */
76         std::string channel = params[0];                                /* Channel name, as a string */
77         time_t TS = atoi(params[1].c_str());                            /* Timestamp given to us for remote side */
78         irc::tokenstream users((params.size() > 2) ? params[2] : "");   /* users from the user list */
79         bool apply_other_sides_modes = true;                            /* True if we are accepting the other side's modes */
80         Channel* chan = this->Instance->FindChan(channel);              /* The channel we're sending joins to */
81         time_t ourTS = chan ? chan->age : Instance->Time(true)+600;     /* The TS of our side of the link */
82         bool created = !chan;                                           /* True if the channel doesnt exist here yet */
83         std::string item;                                               /* One item in the list of nicks */
84
85         if (params.size() > 2)
86                 params[2] = ":" + params[2];
87                 
88         Utils->DoOneToAllButSender(source,"FJOIN",params,source);
89
90         if (!TS)
91         {
92                 Instance->Log(DEFAULT,"*** BUG? *** TS of 0 sent to FJOIN. Are some services authors smoking craq, or is it 1970 again?. Dropped.");
93                 Instance->SNO->WriteToSnoMask('d', "WARNING: The server %s is sending FJOIN with a TS of zero. Total craq. Command was dropped.", source.c_str());
94                 return true;
95         }
96
97         if (created)
98                 chan = new Channel(Instance, channel, ourTS);
99
100         /* If our TS is less than theirs, we dont accept their modes */
101         if (ourTS < TS)
102                 apply_other_sides_modes = false;
103
104         /* Our TS greater than theirs, clear all our modes from the channel, accept theirs. */
105         if (ourTS > TS)
106         {
107                 std::deque<std::string> param_list;
108                 if (Utils->AnnounceTSChange && chan)
109                         chan->WriteChannelWithServ(Instance->Config->ServerName, "NOTICE %s :TS for %s changed from %lu to %lu", chan->name, chan->name, ourTS, TS);
110                 ourTS = TS;
111                 if (!created)
112                 {
113                         chan->age = TS;
114                         param_list.push_back(channel);
115                         this->RemoveStatus(Instance->Config->GetSID(), param_list);
116                 }
117         }
118
119         /* Now, process every 'prefixes,nick' pair */
120         while (users.GetToken(item))
121         {
122                 const char* usr = item.c_str();
123                 if (usr && *usr)
124                 {
125                         const char* permissions = usr;
126                         /* Iterate through all the prefix values, convert them from prefixes to mode letters */
127                         std::string modes;
128                         while ((*permissions) && (*permissions != ','))
129                         {
130                                 ModeHandler* mh = Instance->Modes->FindPrefix(*permissions);
131                                 if (mh)
132                                         modes = modes + mh->GetModeChar();
133                                 else
134                                 {
135                                         this->SendError(std::string("Invalid prefix '")+(*permissions)+"' in FJOIN");
136                                         return false;
137                                 }
138                                 usr++;
139                                 permissions++;
140                         }
141                         /* Advance past the comma, to the nick */
142                         usr++;
143                         
144                         /* Check the user actually exists */
145                         who = this->Instance->FindUUID(usr);
146                         if (who)
147                         {
148                                 /* Check that the user's 'direction' is correct */
149                                 TreeServer* route_back_again = Utils->BestRouteTo(who->server);
150                                 if ((!route_back_again) || (route_back_again->GetSocket() != this))
151                                         continue;
152
153                                 /* Add any permissions this user had to the mode stack */
154                                 for (std::string::iterator x = modes.begin(); x != modes.end(); ++x)
155                                         modestack.Push(*x, who->nick);
156
157                                 Channel::JoinUser(this->Instance, who, channel.c_str(), true, "", true, TS);
158                         }
159                         else
160                         {
161                                 Instance->Log(SPARSE,"Warning! Invalid user %s in FJOIN to channel %s IGNORED", usr, channel.c_str());
162                                 continue;
163                         }
164                 }
165         }
166
167         /* Flush mode stacker if we lost the FJOIN or had equal TS */
168         if (apply_other_sides_modes)
169         {
170                 std::deque<std::string> stackresult;
171                 const char* mode_junk[MAXMODES+2];
172                 mode_junk[0] = channel.c_str();
173
174                 while (modestack.GetStackedLine(stackresult))
175                 {
176                         for (size_t j = 0; j < stackresult.size(); j++)
177                         {
178                                 mode_junk[j+1] = stackresult[j].c_str();
179                         }
180                         Instance->SendMode(mode_junk, stackresult.size() + 1, Instance->FakeClient);
181                 }
182         }
183
184         return true;
185 }
186
187 /** TODO: This creates a total mess of output and needs to really use irc::modestacker.
188  */
189 bool TreeSocket::RemoveStatus(const std::string &prefix, std::deque<std::string> &params)
190 {
191         if (params.size() < 1)
192                 return true;
193
194         Channel* c = Instance->FindChan(params[0]);
195
196         if (c)
197         {
198                 for (char modeletter = 'A'; modeletter <= 'z'; modeletter++)
199                 {
200                         ModeHandler* mh = Instance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
201                         if (mh)
202                                 mh->RemoveMode(c);
203                 }
204         }
205         return true;
206 }
207