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