]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/fjoin.cpp
21327581a55bca8f85553ae4bc2a0b29fe455c6d
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / fjoin.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2008 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "commands.h"
25 #include "treeserver.h"
26 #include "treesocket.h"
27
28 /** FJOIN, almost identical to TS6 SJOIN, except for nicklist handling. */
29 CmdResult CommandFJoin::Handle(const std::vector<std::string>& params, User *srcuser)
30 {
31         /* 1.1+ FJOIN works as follows:
32          *
33          * Each FJOIN is sent along with a timestamp, and the side with the lowest
34          * timestamp 'wins'. From this point on we will refer to this side as the
35          * winner. The side with the higher timestamp loses, from this point on we
36          * will call this side the loser or losing side. This should be familiar to
37          * anyone who's dealt with dreamforge or TS6 before.
38          *
39          * When two sides of a split heal and this occurs, the following things
40          * will happen:
41          *
42          * If the timestamps are exactly equal, both sides merge their privilages
43          * and users, as in InspIRCd 1.0 and ircd2.8. The channels have not been
44          * re-created during a split, this is safe to do.
45          *
46          * If the timestamps are NOT equal, the losing side removes all of its
47          * modes from the channel, before introducing new users into the channel
48          * which are listed in the FJOIN command's parameters. The losing side then
49          * LOWERS its timestamp value of the channel to match that of the winning
50          * side, and the modes of the users of the winning side are merged in with
51          * the losing side.
52          *
53          * The winning side on the other hand will ignore all user modes from the
54          * losing side, so only its own modes get applied. Life is simple for those
55          * who succeed at internets. :-)
56          *
57          * Syntax:
58          * :<sid> FJOIN <chan> <TS> <modes> :[[modes,]<uuid> [[modes,]<uuid> ... ]]
59          * The last parameter is a list consisting of zero or more (modelist, uuid)
60          * pairs (permanent channels may have zero users). The mode list for each
61          * user is a concatenation of the mode letters the user has on the channel
62          * (e.g.: "ov" if the user is opped and voiced). The order of the mode letters
63          * are not important but if a server ecounters an unknown mode letter, it will
64          * drop the link to avoid desync.
65          *
66          * InspIRCd 2.0 and older required a comma before the uuid even if the user
67          * had no prefix modes on the channel, InspIRCd 2.2 and later does not require
68          * a comma in this case anymore.
69          *
70          */
71
72         time_t TS = ConvToInt(params[1]);
73         if (!TS)
74         {
75                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "*** BUG? *** TS of 0 sent to FJOIN. Are some services authors smoking craq, or is it 1970 again?. Dropped.");
76                 ServerInstance->SNO->WriteToSnoMask('d', "WARNING: The server %s is sending FJOIN with a TS of zero. Total craq. Command was dropped.", srcuser->server.c_str());
77                 return CMD_INVALID;
78         }
79
80         const std::string& channel = params[0];
81         Channel* chan = ServerInstance->FindChan(channel);
82         bool apply_other_sides_modes = true;
83
84         if (!chan)
85         {
86                 chan = new Channel(channel, TS);
87         }
88         else
89         {
90                 time_t ourTS = chan->age;
91                 if (TS != ourTS)
92                 {
93                         ServerInstance->SNO->WriteToSnoMask('d', "Merge FJOIN received for %s, ourTS: %lu, TS: %lu, difference: %lu",
94                                 chan->name.c_str(), (unsigned long)ourTS, (unsigned long)TS, (unsigned long)(ourTS - TS));
95                         /* If our TS is less than theirs, we dont accept their modes */
96                         if (ourTS < TS)
97                         {
98                                 apply_other_sides_modes = false;
99                         }
100                         else if (ourTS > TS)
101                         {
102                                 /* Our TS greater than theirs, clear all our modes from the channel, accept theirs. */
103                                 if (Utils->AnnounceTSChange)
104                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :TS for %s changed from %lu to %lu", chan->name.c_str(), channel.c_str(), (unsigned long) ourTS, (unsigned long) TS);
105
106                                 // while the name is equal in case-insensitive compare, it might differ in case; use the remote version
107                                 chan->name = channel;
108                                 chan->age = TS;
109                                 chan->ClearInvites();
110
111                                 CommandFJoin::RemoveStatus(chan);
112
113                                 // XXX: If the channel does not exist in the chan hash at this point, create it so the remote modes can be applied on it.
114                                 // This happens to 0-user permanent channels on the losing side, because those are removed (from the chan hash, then
115                                 // deleted later) as soon as the permchan mode is removed from them.
116                                 if (ServerInstance->FindChan(channel) == NULL)
117                                 {
118                                         chan = new Channel(channel, TS);
119                                 }
120                         }
121                 }
122         }
123
124         /* First up, apply their channel modes if they won the TS war */
125         if (apply_other_sides_modes)
126         {
127                 std::vector<std::string> modelist;
128                 modelist.push_back(channel);
129
130                 /* Remember, params[params.size() - 1] is userlist, and we don't want to apply *that* */
131                 modelist.insert(modelist.end(), params.begin()+2, params.end()-1);
132                 ServerInstance->Modes->Process(modelist, srcuser, ModeParser::MODE_LOCALONLY | ModeParser::MODE_MERGE);
133         }
134
135         irc::modestacker modestack(true);
136         TreeSocket* src_socket = Utils->FindServer(srcuser->server)->GetRoute()->GetSocket();
137
138         /* Now, process every 'modes,uuid' pair */
139         irc::tokenstream users(*params.rbegin());
140         std::string item;
141         irc::modestacker* modestackptr = (apply_other_sides_modes ? &modestack : NULL);
142         while (users.GetToken(item))
143         {
144                 if (!ProcessModeUUIDPair(item, src_socket, chan, modestackptr))
145                         return CMD_INVALID;
146         }
147
148         /* Flush mode stacker if we lost the FJOIN or had equal TS */
149         if (apply_other_sides_modes)
150                 CommandFJoin::ApplyModeStack(srcuser, chan, modestack);
151
152         return CMD_SUCCESS;
153 }
154
155 bool CommandFJoin::ProcessModeUUIDPair(const std::string& item, TreeSocket* src_socket, Channel* chan, irc::modestacker* modestack)
156 {
157         std::string::size_type comma = item.find(',');
158
159         // Comma not required anymore if the user has no modes
160         std::string uuid = ((comma == std::string::npos) ? item : item.substr(comma+1));
161         User* who = ServerInstance->FindUUID(uuid);
162         if (!who)
163         {
164                 // Probably KILLed, ignore
165                 return true;
166         }
167
168         /* Check that the user's 'direction' is correct */
169         TreeServer* route_back_again = Utils->BestRouteTo(who->server);
170         if ((!route_back_again) || (route_back_again->GetSocket() != src_socket))
171         {
172                 return true;
173         }
174
175         /* Check if the user received at least one mode */
176         if ((modestack) && (comma > 0) && (comma != std::string::npos))
177         {
178                 /* Iterate through the modes and see if they are valid here, if so, apply */
179                 std::string::const_iterator commait = item.begin()+comma;
180                 for (std::string::const_iterator i = item.begin(); i != commait; ++i)
181                 {
182                         if (!ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL))
183                         {
184                                 ServerInstance->SNO->WriteToSnoMask('d', "Unrecognised mode '%c' for a user in FJOIN, dropping link", *i);
185                                 return false;
186                         }
187
188                         /* Add any modes this user had to the mode stack */
189                         modestack->Push(*i, who->nick);
190                 }
191         }
192
193         chan->ForceJoin(who, NULL, route_back_again->bursting);
194         return true;
195 }
196
197 void CommandFJoin::RemoveStatus(Channel* c)
198 {
199         irc::modestacker stack(false);
200
201         for (char modeletter = 'A'; modeletter <= 'z'; ++modeletter)
202         {
203                 ModeHandler* mh = ServerInstance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
204
205                 /* Passing a pointer to a modestacker here causes the mode to be put onto the mode stack,
206                  * rather than applied immediately. Module unloads require this to be done immediately,
207                  * for this function we require tidyness instead. Fixes bug #493
208                  */
209                 if (mh)
210                         mh->RemoveMode(c, stack);
211         }
212
213         ApplyModeStack(ServerInstance->FakeClient, c, stack);
214 }
215
216 void CommandFJoin::ApplyModeStack(User* srcuser, Channel* c, irc::modestacker& stack)
217 {
218         parameterlist stackresult;
219         stackresult.push_back(c->name);
220
221         while (stack.GetStackedLine(stackresult))
222         {
223                 ServerInstance->Modes->Process(stackresult, srcuser, ModeParser::MODE_LOCALONLY);
224                 stackresult.erase(stackresult.begin() + 1, stackresult.end());
225         }
226 }