1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "treesocket.h"
18 #include "treeserver.h"
21 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
24 /** FJOIN, almost identical to TS6 SJOIN, except for nicklist handling. */
25 void TreeSocket::ForceJoin(User* srcuser, parameterlist ¶ms)
27 /* 1.1 FJOIN works as follows:
29 * Each FJOIN is sent along with a timestamp, and the side with the lowest
30 * timestamp 'wins'. From this point on we will refer to this side as the
31 * winner. The side with the higher timestamp loses, from this point on we
32 * will call this side the loser or losing side. This should be familiar to
33 * anyone who's dealt with dreamforge or TS6 before.
35 * When two sides of a split heal and this occurs, the following things
38 * If the timestamps are exactly equal, both sides merge their privilages
39 * and users, as in InspIRCd 1.0 and ircd2.8. The channels have not been
40 * re-created during a split, this is safe to do.
42 * If the timestamps are NOT equal, the losing side removes all of its
43 * modes from the channel, before introducing new users into the channel
44 * which are listed in the FJOIN command's parameters. The losing side then
45 * LOWERS its timestamp value of the channel to match that of the winning
46 * side, and the modes of the users of the winning side are merged in with
49 * The winning side on the other hand will ignore all user modes from the
50 * losing side, so only its own modes get applied. Life is simple for those
51 * who succeed at internets. :-)
53 if (params.size() < 3)
56 irc::modestacker modestack(true); /* Modes to apply from the users in the user list */
57 User* who = NULL; /* User we are currently checking */
58 std::string channel = params[0]; /* Channel name, as a string */
59 time_t TS = atoi(params[1].c_str()); /* Timestamp given to us for remote side */
60 irc::tokenstream users((params.size() > 3) ? params[params.size() - 1] : ""); /* users from the user list */
61 bool apply_other_sides_modes = true; /* True if we are accepting the other side's modes */
62 Channel* chan = ServerInstance->FindChan(channel); /* The channel we're sending joins to */
63 bool created = !chan; /* True if the channel doesnt exist here yet */
64 std::string item; /* One item in the list of nicks */
66 if (params.size() > 3)
67 params[params.size() - 1] = ":" + params[params.size() - 1];
69 Utils->DoOneToAllButSender(srcuser->server,"FJOIN",params,srcuser->server);
73 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.");
74 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());
80 chan = new Channel(channel, TS);
81 ServerInstance->SNO->WriteToSnoMask('d', "Creation FJOIN recieved for %s, timestamp: %lu", chan->name.c_str(), (unsigned long)TS);
85 time_t ourTS = chan->age;
88 ServerInstance->SNO->WriteToSnoMask('d', "Merge FJOIN recieved for %s, ourTS: %lu, TS: %lu, difference: %lu",
89 chan->name.c_str(), (unsigned long)ourTS, (unsigned long)TS, (unsigned long)(ourTS - TS));
90 /* If our TS is less than theirs, we dont accept their modes */
93 ServerInstance->SNO->WriteToSnoMask('d', "NOT Applying modes from other side");
94 apply_other_sides_modes = false;
98 /* Our TS greater than theirs, clear all our modes from the channel, accept theirs. */
99 ServerInstance->SNO->WriteToSnoMask('d', "Removing our modes, accepting remote");
100 parameterlist param_list;
101 if (Utils->AnnounceTSChange && chan)
102 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :TS for %s changed from %lu to %lu", chan->name.c_str(), chan->name.c_str(), (unsigned long) ourTS, (unsigned long) TS);
105 param_list.push_back(channel);
106 this->RemoveStatus(ServerInstance->FakeClient, param_list);
108 // The silent case here is ourTS == TS, we don't need to remove modes here, just to merge them later on.
111 /* First up, apply their modes if they won the TS war */
112 if (apply_other_sides_modes)
114 unsigned int idx = 2;
115 std::vector<std::string> modelist;
117 // Mode parser needs to know what channel to act on.
118 modelist.push_back(params[0]);
120 /* Remember, params[params.size() - 1] is nicklist, and we don't want to apply *that* */
121 for (idx = 2; idx != (params.size() - 1); idx++)
123 modelist.push_back(params[idx]);
126 ServerInstance->SendMode(modelist, srcuser);
129 /* Now, process every 'modes,nick' pair */
130 while (users.GetToken(item))
132 const char* usr = item.c_str();
135 const char* unparsedmodes = usr;
139 /* Iterate through all modes for this user and check they are valid. */
140 while ((*unparsedmodes) && (*unparsedmodes != ','))
142 ModeHandler *mh = ServerInstance->Modes->FindMode(*unparsedmodes, MODETYPE_CHANNEL);
144 modes += *unparsedmodes;
147 this->SendError(std::string("Unknown status mode '")+(*unparsedmodes)+"' in FJOIN");
155 /* Advance past the comma, to the nick */
158 /* Check the user actually exists */
159 who = ServerInstance->FindUUID(usr);
162 /* Check that the user's 'direction' is correct */
163 TreeServer* route_back_again = Utils->BestRouteTo(who->server);
164 if ((!route_back_again) || (route_back_again->GetSocket() != this))
167 /* Add any modes this user had to the mode stack */
168 for (std::string::iterator x = modes.begin(); x != modes.end(); ++x)
169 modestack.Push(*x, who->nick);
171 Channel::JoinUser(who, channel.c_str(), true, "", route_back_again->bursting, TS);
175 ServerInstance->Logs->Log("m_spanningtree",SPARSE, "Ignored nonexistant user %s in fjoin to %s (probably quit?)", usr, channel.c_str());
181 /* Flush mode stacker if we lost the FJOIN or had equal TS */
182 if (apply_other_sides_modes)
184 parameterlist stackresult;
185 stackresult.push_back(channel);
187 while (modestack.GetStackedLine(stackresult))
189 ServerInstance->SendMode(stackresult, srcuser);
190 stackresult.erase(stackresult.begin() + 1, stackresult.end());
195 void TreeSocket::RemoveStatus(User* srcuser, parameterlist ¶ms)
197 if (params.size() < 1)
200 Channel* c = ServerInstance->FindChan(params[0]);
204 irc::modestacker stack(false);
205 parameterlist stackresult;
206 stackresult.push_back(c->name);
208 for (char modeletter = 'A'; modeletter <= 'z'; ++modeletter)
210 ModeHandler* mh = ServerInstance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
212 /* Passing a pointer to a modestacker here causes the mode to be put onto the mode stack,
213 * rather than applied immediately. Module unloads require this to be done immediately,
214 * for this function we require tidyness instead. Fixes bug #493
217 mh->RemoveMode(c, &stack);
220 while (stack.GetStackedLine(stackresult))
222 ServerInstance->SendMode(stackresult, srcuser);
223 stackresult.erase(stackresult.begin() + 1, stackresult.end());