2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
25 #include "treeserver.h"
26 #include "treesocket.h"
28 /** FJOIN, almost identical to TS6 SJOIN, except for nicklist handling. */
29 CmdResult CommandFJoin::Handle(const std::vector<std::string>& params, User *srcuser)
31 SpanningTreeUtilities* Utils = ((ModuleSpanningTree*)(Module*)creator)->Utils;
32 /* 1.1 FJOIN works as follows:
34 * Each FJOIN is sent along with a timestamp, and the side with the lowest
35 * timestamp 'wins'. From this point on we will refer to this side as the
36 * winner. The side with the higher timestamp loses, from this point on we
37 * will call this side the loser or losing side. This should be familiar to
38 * anyone who's dealt with dreamforge or TS6 before.
40 * When two sides of a split heal and this occurs, the following things
43 * If the timestamps are exactly equal, both sides merge their privilages
44 * and users, as in InspIRCd 1.0 and ircd2.8. The channels have not been
45 * re-created during a split, this is safe to do.
47 * If the timestamps are NOT equal, the losing side removes all of its
48 * modes from the channel, before introducing new users into the channel
49 * which are listed in the FJOIN command's parameters. The losing side then
50 * LOWERS its timestamp value of the channel to match that of the winning
51 * side, and the modes of the users of the winning side are merged in with
54 * The winning side on the other hand will ignore all user modes from the
55 * losing side, so only its own modes get applied. Life is simple for those
56 * who succeed at internets. :-)
58 if (params.size() < 3)
61 irc::modestacker modestack(true); /* Modes to apply from the users in the user list */
62 User* who = NULL; /* User we are currently checking */
63 std::string channel = params[0]; /* Channel name, as a string */
64 time_t TS = atoi(params[1].c_str()); /* Timestamp given to us for remote side */
65 irc::tokenstream users((params.size() > 3) ? params[params.size() - 1] : ""); /* users from the user list */
66 bool apply_other_sides_modes = true; /* True if we are accepting the other side's modes */
67 Channel* chan = ServerInstance->FindChan(channel); /* The channel we're sending joins to */
68 bool created = !chan; /* True if the channel doesnt exist here yet */
69 std::string item; /* One item in the list of nicks */
71 TreeSocket* src_socket = Utils->FindServer(srcuser->server)->GetRoute()->GetSocket();
75 ServerInstance->Logs->Log("m_spanningtree",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());
82 chan = new Channel(channel, TS);
83 ServerInstance->SNO->WriteToSnoMask('d', "Creation FJOIN received for %s, timestamp: %lu", chan->name.c_str(), (unsigned long)TS);
87 time_t ourTS = chan->age;
90 ServerInstance->SNO->WriteToSnoMask('d', "Merge FJOIN received for %s, ourTS: %lu, TS: %lu, difference: %lu",
91 chan->name.c_str(), (unsigned long)ourTS, (unsigned long)TS, (unsigned long)(ourTS - TS));
92 /* If our TS is less than theirs, we dont accept their modes */
95 ServerInstance->SNO->WriteToSnoMask('d', "NOT Applying modes from other side");
96 apply_other_sides_modes = false;
100 /* Our TS greater than theirs, clear all our modes from the channel, accept theirs. */
101 ServerInstance->SNO->WriteToSnoMask('d', "Removing our modes, accepting remote");
102 parameterlist param_list;
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 // while the name is equal in case-insensitive compare, it might differ in case; use the remote version
106 chan->name = channel;
108 chan->ClearInvites();
109 param_list.push_back(channel);
110 this->RemoveStatus(ServerInstance->FakeClient, param_list);
112 // The silent case here is ourTS == TS, we don't need to remove modes here, just to merge them later on.
115 /* First up, apply their modes if they won the TS war */
116 if (apply_other_sides_modes)
118 unsigned int idx = 2;
119 std::vector<std::string> modelist;
121 // Mode parser needs to know what channel to act on.
122 modelist.push_back(params[0]);
124 /* Remember, params[params.size() - 1] is nicklist, and we don't want to apply *that* */
125 for (idx = 2; idx != (params.size() - 1); idx++)
127 modelist.push_back(params[idx]);
130 ServerInstance->SendMode(modelist, srcuser);
133 /* Now, process every 'modes,nick' pair */
134 while (users.GetToken(item))
136 const char* usr = item.c_str();
139 const char* unparsedmodes = usr;
143 /* Iterate through all modes for this user and check they are valid. */
144 while ((*unparsedmodes) && (*unparsedmodes != ','))
146 ModeHandler *mh = ServerInstance->Modes->FindMode(*unparsedmodes, MODETYPE_CHANNEL);
149 ServerInstance->Logs->Log("m_spanningtree", SPARSE, "Unrecognised mode %c, dropping link", *unparsedmodes);
153 modes += *unparsedmodes;
158 /* Advance past the comma, to the nick */
161 /* Check the user actually exists */
162 who = ServerInstance->FindUUID(usr);
165 /* Check that the user's 'direction' is correct */
166 TreeServer* route_back_again = Utils->BestRouteTo(who->server);
167 if ((!route_back_again) || (route_back_again->GetSocket() != src_socket))
170 /* Add any modes this user had to the mode stack */
171 for (std::string::iterator x = modes.begin(); x != modes.end(); ++x)
172 modestack.Push(*x, who->nick);
174 Channel::JoinUser(who, channel.c_str(), true, "", route_back_again->bursting, TS);
178 ServerInstance->Logs->Log("m_spanningtree",SPARSE, "Ignored nonexistant user %s in fjoin to %s (probably quit?)", usr, channel.c_str());
184 /* Flush mode stacker if we lost the FJOIN or had equal TS */
185 if (apply_other_sides_modes)
187 parameterlist stackresult;
188 stackresult.push_back(channel);
190 while (modestack.GetStackedLine(stackresult))
192 ServerInstance->SendMode(stackresult, srcuser);
193 stackresult.erase(stackresult.begin() + 1, stackresult.end());
199 void CommandFJoin::RemoveStatus(User* srcuser, parameterlist ¶ms)
201 if (params.size() < 1)
204 Channel* c = ServerInstance->FindChan(params[0]);
208 irc::modestacker stack(false);
209 parameterlist stackresult;
210 stackresult.push_back(c->name);
212 for (char modeletter = 'A'; modeletter <= 'z'; ++modeletter)
214 ModeHandler* mh = ServerInstance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
216 /* Passing a pointer to a modestacker here causes the mode to be put onto the mode stack,
217 * rather than applied immediately. Module unloads require this to be done immediately,
218 * for this function we require tidyness instead. Fixes bug #493
221 mh->RemoveMode(c, &stack);
224 while (stack.GetStackedLine(stackresult))
226 ServerInstance->SendMode(stackresult, srcuser);
227 stackresult.erase(stackresult.begin() + 1, stackresult.end());