]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/fjoin.cpp
Minor spelling errors in m_spanningtree.so
[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(const std::vector<std::string>& params, User *srcuser)
30 {
31         SpanningTreeUtilities* Utils = ((ModuleSpanningTree*)(Module*)creator)->Utils;
32         /* 1.1 FJOIN works as follows:
33          *
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.
39          *
40          * When two sides of a split heal and this occurs, the following things
41          * will happen:
42          *
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.
46          *
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
52          * the losing side.
53          *
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. :-)
57          */
58
59         irc::modestacker modestack(true);                       /* Modes to apply from the users in the user list */
60         User* who = NULL;                                               /* User we are currently checking */
61         std::string channel = params[0];                                /* Channel name, as a string */
62         time_t TS = atoi(params[1].c_str());                            /* Timestamp given to us for remote side */
63         irc::tokenstream users((params.size() > 3) ? params[params.size() - 1] : "");   /* users from the user list */
64         bool apply_other_sides_modes = true;                            /* True if we are accepting the other side's modes */
65         Channel* chan = ServerInstance->FindChan(channel);              /* The channel we're sending joins to */
66         bool created = !chan;                                           /* True if the channel doesnt exist here yet */
67         std::string item;                                               /* One item in the list of nicks */
68
69         TreeServer* src_server = Utils->FindServer(srcuser->server);
70         TreeSocket* src_socket = src_server->GetRoute()->GetSocket();
71
72         if (!TS)
73         {
74                 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.");
75                 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());
76                 return CMD_INVALID;
77         }
78
79         if (created)
80         {
81                 chan = new Channel(channel, TS);
82                 ServerInstance->SNO->WriteToSnoMask('d', "Creation FJOIN received for %s, timestamp: %lu", chan->name.c_str(), (unsigned long)TS);
83         }
84         else
85         {
86                 time_t ourTS = chan->age;
87
88                 if (TS != ourTS)
89                         ServerInstance->SNO->WriteToSnoMask('d', "Merge FJOIN received for %s, ourTS: %lu, TS: %lu, difference: %ld",
90                                 chan->name.c_str(), (unsigned long)ourTS, (unsigned long)TS, (long)(ourTS - TS));
91                 /* If our TS is less than theirs, we dont accept their modes */
92                 if (ourTS < TS)
93                 {
94                         ServerInstance->SNO->WriteToSnoMask('d', "NOT Applying modes from other side");
95                         apply_other_sides_modes = false;
96                 }
97                 else if (ourTS > TS)
98                 {
99                         /* Our TS greater than theirs, clear all our modes from the channel, accept theirs. */
100                         ServerInstance->SNO->WriteToSnoMask('d', "Removing our modes, accepting remote");
101                         parameterlist param_list;
102                         if (Utils->AnnounceTSChange)
103                                 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);
104                         // while the name is equal in case-insensitive compare, it might differ in case; use the remote version
105                         chan->name = channel;
106                         chan->age = TS;
107                         chan->ClearInvites();
108                         param_list.push_back(channel);
109                         this->RemoveStatus(ServerInstance->FakeClient, param_list);
110
111                         // 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.
112                         // This happens to 0-user permanent channels on the losing side, because those are removed (from the chan hash, then
113                         // deleted later) as soon as the permchan mode is removed from them.
114                         if (ServerInstance->FindChan(channel) == NULL)
115                         {
116                                 chan = new Channel(channel, TS);
117                         }
118                 }
119                 // The silent case here is ourTS == TS, we don't need to remove modes here, just to merge them later on.
120         }
121
122         /* First up, apply their modes if they won the TS war */
123         if (apply_other_sides_modes)
124         {
125                 // Need to use a modestacker here due to maxmodes
126                 irc::modestacker stack(true);
127                 std::vector<std::string>::const_iterator paramit = params.begin() + 3;
128                 const std::vector<std::string>::const_iterator lastparamit = ((params.size() > 3) ? (params.end() - 1) : params.end());
129                 for (std::string::const_iterator i = params[2].begin(); i != params[2].end(); ++i)
130                 {
131                         ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
132                         if (!mh)
133                                 continue;
134
135                         std::string modeparam;
136                         if ((paramit != lastparamit) && (mh->GetNumParams(true)))
137                         {
138                                 modeparam = *paramit;
139                                 ++paramit;
140                         }
141
142                         stack.Push(*i, modeparam);
143                 }
144
145                 std::vector<std::string> modelist;
146
147                 // Mode parser needs to know what channel to act on.
148                 modelist.push_back(params[0]);
149
150                 while (stack.GetStackedLine(modelist))
151                 {
152                         ServerInstance->Modes->Process(modelist, srcuser, true);
153                         modelist.erase(modelist.begin() + 1, modelist.end());
154                 }
155
156                 ServerInstance->Modes->Process(modelist, srcuser, true);
157         }
158
159         /* Now, process every 'modes,nick' pair */
160         while (users.GetToken(item))
161         {
162                 const char* usr = item.c_str();
163                 if (usr && *usr)
164                 {
165                         const char* unparsedmodes = usr;
166                         std::string modes;
167
168
169                         /* Iterate through all modes for this user and check they are valid. */
170                         while ((*unparsedmodes) && (*unparsedmodes != ','))
171                         {
172                                 ModeHandler *mh = ServerInstance->Modes->FindMode(*unparsedmodes, MODETYPE_CHANNEL);
173                                 if (!mh)
174                                 {
175                                         ServerInstance->Logs->Log("m_spanningtree", SPARSE, "Unrecognised mode %c, dropping link", *unparsedmodes);
176                                         return CMD_INVALID;
177                                 }
178
179                                 modes += *unparsedmodes;
180                                 usr++;
181                                 unparsedmodes++;
182                         }
183
184                         /* Advance past the comma, to the nick */
185                         usr++;
186
187                         /* Check the user actually exists */
188                         who = ServerInstance->FindUUID(usr);
189                         if (who)
190                         {
191                                 /* Check that the user's 'direction' is correct */
192                                 TreeServer* route_back_again = Utils->BestRouteTo(who->server);
193                                 if ((!route_back_again) || (route_back_again->GetSocket() != src_socket))
194                                         continue;
195
196                                 /* Add any modes this user had to the mode stack */
197                                 for (std::string::iterator x = modes.begin(); x != modes.end(); ++x)
198                                         modestack.Push(*x, who->nick);
199
200                                 Channel::JoinUser(who, channel.c_str(), true, "", src_server->bursting, TS);
201                         }
202                         else
203                         {
204                                 ServerInstance->Logs->Log("m_spanningtree",SPARSE, "Ignored nonexistent user %s in fjoin to %s (probably quit?)", usr, channel.c_str());
205                                 continue;
206                         }
207                 }
208         }
209
210         /* Flush mode stacker if we lost the FJOIN or had equal TS */
211         if (apply_other_sides_modes)
212         {
213                 parameterlist stackresult;
214                 stackresult.push_back(channel);
215
216                 while (modestack.GetStackedLine(stackresult))
217                 {
218                         ServerInstance->SendMode(stackresult, srcuser);
219                         stackresult.erase(stackresult.begin() + 1, stackresult.end());
220                 }
221         }
222         return CMD_SUCCESS;
223 }
224
225 void CommandFJoin::RemoveStatus(User* srcuser, parameterlist &params)
226 {
227         if (params.size() < 1)
228                 return;
229
230         Channel* c = ServerInstance->FindChan(params[0]);
231
232         if (c)
233         {
234                 irc::modestacker stack(false);
235                 parameterlist stackresult;
236                 stackresult.push_back(c->name);
237
238                 for (char modeletter = 'A'; modeletter <= 'z'; ++modeletter)
239                 {
240                         ModeHandler* mh = ServerInstance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
241
242                         /* Passing a pointer to a modestacker here causes the mode to be put onto the mode stack,
243                          * rather than applied immediately. Module unloads require this to be done immediately,
244                          * for this function we require tidyness instead. Fixes bug #493
245                          */
246                         if (mh)
247                                 mh->RemoveMode(c, &stack);
248                 }
249
250                 while (stack.GetStackedLine(stackresult))
251                 {
252                         ServerInstance->SendMode(stackresult, srcuser);
253                         stackresult.erase(stackresult.begin() + 1, stackresult.end());
254                 }
255         }
256 }
257