1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
20 #include "transport.h"
22 #include "socketengine.h"
24 #include "m_spanningtree/main.h"
25 #include "m_spanningtree/utils.h"
26 #include "m_spanningtree/treeserver.h"
27 #include "m_spanningtree/link.h"
28 #include "m_spanningtree/treesocket.h"
29 #include "m_spanningtree/resolvers.h"
30 #include "m_spanningtree/handshaketimer.h"
32 /* $ModDep: m_spanningtree/timesynctimer.h m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h m_hash.h */
34 /** FJOIN, similar to TS6 SJOIN, but not quite. */
35 bool TreeSocket::ForceJoin(const std::string &source, std::deque<std::string> ¶ms)
37 /* 1.1 FJOIN works as follows:
39 * Each FJOIN is sent along with a timestamp, and the side with the lowest
40 * timestamp 'wins'. From this point on we will refer to this side as the
41 * winner. The side with the higher timestamp loses, from this point on we
42 * will call this side the loser or losing side. This should be familiar to
43 * anyone who's dealt with dreamforge or TS6 before.
45 * When two sides of a split heal and this occurs, the following things
48 * If the timestamps are exactly equal, both sides merge their privilages
49 * and users, as in InspIRCd 1.0 and ircd2.8. The channels have not been
50 * re-created during a split, this is safe to do.
52 * If the timestamps are NOT equal, the losing side removes all of its
53 * modes from the channel, before introducing new users into the channel
54 * which are listed in the FJOIN command's parameters. The losing side then
55 * LOWERS its timestamp value of the channel to match that of the winning
56 * side, and the modes of the users of the winning side are merged in with
59 * The winning side on the other hand will ignore all user modes from the
60 * losing side, so only its own modes get applied. Life is simple for those
61 * who succeed at internets. :-)
63 * NOTE: Unlike TS6 and dreamforge and other protocols which have SJOIN,
64 * FJOIN does not contain the simple-modes such as +iklmnsp. Why not,
65 * you ask? Well, quite simply because we don't need to. They'll be sent
66 * after the FJOIN by FMODE, and FMODE is timestamped, so in the event
67 * the losing side sends any modes for the channel which shouldnt win,
68 * they wont as their timestamp will be too high :-)
71 if (params.size() < 2)
74 irc::modestacker modestack(true); /* Modes to apply from the users in the user list */
75 User* who = NULL; /* User we are currently checking */
76 std::string channel = params[0]; /* Channel name, as a string */
77 time_t TS = atoi(params[1].c_str()); /* Timestamp given to us for remote side */
78 irc::tokenstream users((params.size() > 2) ? params[2] : ""); /* users from the user list */
79 bool apply_other_sides_modes = true; /* True if we are accepting the other side's modes */
80 Channel* chan = this->Instance->FindChan(channel); /* The channel we're sending joins to */
81 time_t ourTS = chan ? chan->age : Instance->Time(true)+600; /* The TS of our side of the link */
82 bool created = !chan; /* True if the channel doesnt exist here yet */
83 std::string item; /* One item in the list of nicks */
85 if (params.size() > 2)
86 params[2] = ":" + params[2];
88 Utils->DoOneToAllButSender(source,"FJOIN",params,source);
92 Instance->Log(DEFAULT,"*** BUG? *** TS of 0 sent to FJOIN. Are some services authors smoking craq, or is it 1970 again?. Dropped.");
93 Instance->SNO->WriteToSnoMask('d', "WARNING: The server %s is sending FJOIN with a TS of zero. Total craq. Command was dropped.", source.c_str());
98 chan = new Channel(Instance, channel, ourTS);
100 /* If our TS is less than theirs, we dont accept their modes */
102 apply_other_sides_modes = false;
104 /* Our TS greater than theirs, clear all our modes from the channel, accept theirs. */
107 std::deque<std::string> param_list;
108 if (Utils->AnnounceTSChange && chan)
109 chan->WriteChannelWithServ(Instance->Config->ServerName, "NOTICE %s :TS for %s changed from %lu to %lu", chan->name, chan->name, ourTS, TS);
114 param_list.push_back(channel);
115 this->RemoveStatus(Instance->Config->GetSID(), param_list);
119 /* Now, process every 'prefixes,nick' pair */
120 while (users.GetToken(item))
122 const char* usr = item.c_str();
125 const char* permissions = usr;
126 /* Iterate through all the prefix values, convert them from prefixes to mode letters */
128 while ((*permissions) && (*permissions != ','))
130 ModeHandler* mh = Instance->Modes->FindPrefix(*permissions);
132 modes = modes + mh->GetModeChar();
135 this->SendError(std::string("Invalid prefix '")+(*permissions)+"' in FJOIN");
141 /* Advance past the comma, to the nick */
144 /* Check the user actually exists */
145 who = this->Instance->FindUUID(usr);
148 /* Check that the user's 'direction' is correct */
149 TreeServer* route_back_again = Utils->BestRouteTo(who->server);
150 if ((!route_back_again) || (route_back_again->GetSocket() != this))
153 /* Add any permissions this user had to the mode stack */
154 for (std::string::iterator x = modes.begin(); x != modes.end(); ++x)
155 modestack.Push(*x, who->nick);
157 Channel::JoinUser(this->Instance, who, channel.c_str(), true, "", true, TS);
161 Instance->Log(SPARSE,"Warning! Invalid user %s in FJOIN to channel %s IGNORED", usr, channel.c_str());
167 /* Flush mode stacker if we lost the FJOIN or had equal TS */
168 if (apply_other_sides_modes)
170 std::deque<std::string> stackresult;
171 const char* mode_junk[MAXMODES+2];
172 mode_junk[0] = channel.c_str();
174 while (modestack.GetStackedLine(stackresult))
176 for (size_t j = 0; j < stackresult.size(); j++)
178 mode_junk[j+1] = stackresult[j].c_str();
180 Instance->SendMode(mode_junk, stackresult.size() + 1, Instance->FakeClient);