]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/fjoin.cpp
m_spanningtree Assign an id to new Memberships
[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(User* srcuser, std::vector<std::string>& params)
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 = ServerCommand::ExtractTS(params[1]);
73
74         const std::string& channel = params[0];
75         Channel* chan = ServerInstance->FindChan(channel);
76         bool apply_other_sides_modes = true;
77
78         if (!chan)
79         {
80                 chan = new Channel(channel, TS);
81         }
82         else
83         {
84                 time_t ourTS = chan->age;
85                 if (TS != ourTS)
86                 {
87                         ServerInstance->SNO->WriteToSnoMask('d', "Merge FJOIN received for %s, ourTS: %lu, TS: %lu, difference: %lu",
88                                 chan->name.c_str(), (unsigned long)ourTS, (unsigned long)TS, (unsigned long)(ourTS - TS));
89                         /* If our TS is less than theirs, we dont accept their modes */
90                         if (ourTS < TS)
91                         {
92                                 apply_other_sides_modes = false;
93                         }
94                         else if (ourTS > TS)
95                         {
96                                 // Our TS is greater than theirs, remove all modes, extensions, etc. from the channel
97                                 LowerTS(chan, TS, channel);
98
99                                 // 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.
100                                 // This happens to 0-user permanent channels on the losing side, because those are removed (from the chan hash, then
101                                 // deleted later) as soon as the permchan mode is removed from them.
102                                 if (ServerInstance->FindChan(channel) == NULL)
103                                 {
104                                         chan = new Channel(channel, TS);
105                                 }
106                         }
107                 }
108         }
109
110         /* First up, apply their channel modes if they won the TS war */
111         if (apply_other_sides_modes)
112         {
113                 // Need to use a modestacker here due to maxmodes
114                 irc::modestacker stack(true);
115                 std::vector<std::string>::const_iterator paramit = params.begin() + 3;
116                 const std::vector<std::string>::const_iterator lastparamit = ((params.size() > 3) ? (params.end() - 1) : params.end());
117                 for (std::string::const_iterator i = params[2].begin(); i != params[2].end(); ++i)
118                 {
119                         ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
120                         if (!mh)
121                                 continue;
122
123                         std::string modeparam;
124                         if ((paramit != lastparamit) && (mh->GetNumParams(true)))
125                         {
126                                 modeparam = *paramit;
127                                 ++paramit;
128                         }
129
130                         stack.Push(*i, modeparam);
131                 }
132
133                 std::vector<std::string> modelist;
134
135                 // Mode parser needs to know what channel to act on.
136                 modelist.push_back(params[0]);
137
138                 while (stack.GetStackedLine(modelist))
139                 {
140                         ServerInstance->Modes->Process(modelist, srcuser, ModeParser::MODE_LOCALONLY | ModeParser::MODE_MERGE);
141                         modelist.erase(modelist.begin() + 1, modelist.end());
142                 }
143         }
144
145         irc::modestacker modestack(true);
146         TreeSocket* src_socket = TreeServer::Get(srcuser)->GetSocket();
147
148         /* Now, process every 'modes,uuid' pair */
149         irc::tokenstream users(*params.rbegin());
150         std::string item;
151         irc::modestacker* modestackptr = (apply_other_sides_modes ? &modestack : NULL);
152         while (users.GetToken(item))
153         {
154                 ProcessModeUUIDPair(item, src_socket, chan, modestackptr);
155         }
156
157         /* Flush mode stacker if we lost the FJOIN or had equal TS */
158         if (apply_other_sides_modes)
159                 CommandFJoin::ApplyModeStack(srcuser, chan, modestack);
160
161         return CMD_SUCCESS;
162 }
163
164 void CommandFJoin::ProcessModeUUIDPair(const std::string& item, TreeSocket* src_socket, Channel* chan, irc::modestacker* modestack)
165 {
166         std::string::size_type comma = item.find(',');
167
168         // Comma not required anymore if the user has no modes
169         std::string uuid = ((comma == std::string::npos) ? item : item.substr(comma+1));
170         User* who = ServerInstance->FindUUID(uuid);
171         if (!who)
172         {
173                 // Probably KILLed, ignore
174                 return;
175         }
176
177         /* Check that the user's 'direction' is correct */
178         TreeServer* route_back_again = TreeServer::Get(who);
179         if (route_back_again->GetSocket() != src_socket)
180         {
181                 return;
182         }
183
184         /* Check if the user received at least one mode */
185         if ((modestack) && (comma > 0) && (comma != std::string::npos))
186         {
187                 /* Iterate through the modes and see if they are valid here, if so, apply */
188                 std::string::const_iterator commait = item.begin()+comma;
189                 for (std::string::const_iterator i = item.begin(); i != commait; ++i)
190                 {
191                         if (!ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL))
192                                 throw ProtocolException("Unrecognised mode '" + std::string(1, *i) + "'");
193
194                         /* Add any modes this user had to the mode stack */
195                         modestack->Push(*i, who->nick);
196                 }
197         }
198
199         chan->ForceJoin(who, NULL, route_back_again->bursting);
200 }
201
202 void CommandFJoin::RemoveStatus(Channel* c)
203 {
204         irc::modestacker stack(false);
205
206         const ModeParser::ModeHandlerMap& mhs = ServerInstance->Modes->GetModes(MODETYPE_CHANNEL);
207         for (ModeParser::ModeHandlerMap::const_iterator i = mhs.begin(); i != mhs.end(); ++i)
208         {
209                 ModeHandler* mh = i->second;
210
211                 /* Passing a pointer to a modestacker here causes the mode to be put onto the mode stack,
212                  * rather than applied immediately. Module unloads require this to be done immediately,
213                  * for this function we require tidyness instead. Fixes bug #493
214                  */
215                 mh->RemoveMode(c, stack);
216         }
217
218         ApplyModeStack(ServerInstance->FakeClient, c, stack);
219 }
220
221 void CommandFJoin::ApplyModeStack(User* srcuser, Channel* c, irc::modestacker& stack)
222 {
223         parameterlist stackresult;
224         stackresult.push_back(c->name);
225
226         while (stack.GetStackedLine(stackresult))
227         {
228                 ServerInstance->Modes->Process(stackresult, srcuser, ModeParser::MODE_LOCALONLY);
229                 stackresult.erase(stackresult.begin() + 1, stackresult.end());
230         }
231 }
232
233 void CommandFJoin::LowerTS(Channel* chan, time_t TS, const std::string& newname)
234 {
235         if (Utils->AnnounceTSChange)
236                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :TS for %s changed from %lu to %lu", chan->name.c_str(), newname.c_str(), (unsigned long) chan->age, (unsigned long) TS);
237
238         // While the name is equal in case-insensitive compare, it might differ in case; use the remote version
239         chan->name = newname;
240         chan->age = TS;
241
242         // Remove all pending invites
243         chan->ClearInvites();
244
245         // Clear all modes
246         CommandFJoin::RemoveStatus(chan);
247
248         // Unset all extensions
249         chan->FreeAllExtItems();
250
251         // Clear the topic, if it isn't empty then send a topic change message to local users
252         if (!chan->topic.empty())
253         {
254                 chan->topic.clear();
255                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "TOPIC %s :", chan->name.c_str());
256         }
257         chan->setby.clear();
258         chan->topicset = 0;
259 }