]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/fjoin.cpp
Log mode errors when processing FJOIN.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / fjoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "commands.h"
16 #include "treeserver.h"
17 #include "treesocket.h"
18
19 /** FJOIN, almost identical to TS6 SJOIN, except for nicklist handling. */
20 CmdResult CommandFJoin::Handle(const std::vector<std::string>& params, User *srcuser)
21 {
22         SpanningTreeUtilities* Utils = ((ModuleSpanningTree*)(Module*)creator)->Utils;
23         /* 1.1 FJOIN works as follows:
24          *
25          * Each FJOIN is sent along with a timestamp, and the side with the lowest
26          * timestamp 'wins'. From this point on we will refer to this side as the
27          * winner. The side with the higher timestamp loses, from this point on we
28          * will call this side the loser or losing side. This should be familiar to
29          * anyone who's dealt with dreamforge or TS6 before.
30          *
31          * When two sides of a split heal and this occurs, the following things
32          * will happen:
33          *
34          * If the timestamps are exactly equal, both sides merge their privilages
35          * and users, as in InspIRCd 1.0 and ircd2.8. The channels have not been
36          * re-created during a split, this is safe to do.
37          *
38          * If the timestamps are NOT equal, the losing side removes all of its
39          * modes from the channel, before introducing new users into the channel
40          * which are listed in the FJOIN command's parameters. The losing side then
41          * LOWERS its timestamp value of the channel to match that of the winning
42          * side, and the modes of the users of the winning side are merged in with
43          * the losing side.
44          *
45          * The winning side on the other hand will ignore all user modes from the
46          * losing side, so only its own modes get applied. Life is simple for those
47          * who succeed at internets. :-)
48          */
49         if (params.size() < 3)
50                 return CMD_INVALID;
51
52         irc::modestacker modestack(true);                       /* Modes to apply from the users in the user list */
53         User* who = NULL;                                               /* User we are currently checking */
54         std::string channel = params[0];                                /* Channel name, as a string */
55         time_t TS = atoi(params[1].c_str());                            /* Timestamp given to us for remote side */
56         irc::tokenstream users((params.size() > 3) ? params[params.size() - 1] : "");   /* users from the user list */
57         bool apply_other_sides_modes = true;                            /* True if we are accepting the other side's modes */
58         Channel* chan = ServerInstance->FindChan(channel);              /* The channel we're sending joins to */
59         bool created = !chan;                                           /* True if the channel doesnt exist here yet */
60         std::string item;                                               /* One item in the list of nicks */
61
62         TreeSocket* src_socket = Utils->FindServer(srcuser->server)->GetRoute()->GetSocket();
63
64         if (!TS)
65         {
66                 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.");
67                 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());
68                 return CMD_INVALID;
69         }
70
71         if (created)
72         {
73                 chan = new Channel(channel, TS);
74                 ServerInstance->SNO->WriteToSnoMask('d', "Creation FJOIN recieved for %s, timestamp: %lu", chan->name.c_str(), (unsigned long)TS);
75         }
76         else
77         {
78                 time_t ourTS = chan->age;
79
80                 if (TS != ourTS)
81                         ServerInstance->SNO->WriteToSnoMask('d', "Merge FJOIN recieved for %s, ourTS: %lu, TS: %lu, difference: %lu",
82                                 chan->name.c_str(), (unsigned long)ourTS, (unsigned long)TS, (unsigned long)(ourTS - TS));
83                 /* If our TS is less than theirs, we dont accept their modes */
84                 if (ourTS < TS)
85                 {
86                         ServerInstance->SNO->WriteToSnoMask('d', "NOT Applying modes from other side");
87                         apply_other_sides_modes = false;
88                 }
89                 else if (ourTS > TS)
90                 {
91                         /* Our TS greater than theirs, clear all our modes from the channel, accept theirs. */
92                         ServerInstance->SNO->WriteToSnoMask('d', "Removing our modes, accepting remote");
93                         parameterlist param_list;
94                         if (Utils->AnnounceTSChange)
95                                 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);
96                         // while the name is equal in case-insensitive compare, it might differ in case; use the remote version
97                         chan->name = channel;
98                         chan->age = TS;
99                         param_list.push_back(channel);
100                         this->RemoveStatus(ServerInstance->FakeClient, param_list);
101                 }
102                 // The silent case here is ourTS == TS, we don't need to remove modes here, just to merge them later on.
103         }
104
105         /* First up, apply their modes if they won the TS war */
106         if (apply_other_sides_modes)
107         {
108                 unsigned int idx = 2;
109                 std::vector<std::string> modelist;
110
111                 // Mode parser needs to know what channel to act on.
112                 modelist.push_back(params[0]);
113
114                 /* Remember, params[params.size() - 1] is nicklist, and we don't want to apply *that* */
115                 for (idx = 2; idx != (params.size() - 1); idx++)
116                 {
117                         modelist.push_back(params[idx]);
118                 }
119
120                 ServerInstance->SendMode(modelist, srcuser);
121         }
122
123         /* Now, process every 'modes,nick' pair */
124         while (users.GetToken(item))
125         {
126                 const char* usr = item.c_str();
127                 if (usr && *usr)
128                 {
129                         const char* unparsedmodes = usr;
130                         std::string modes;
131
132
133                         /* Iterate through all modes for this user and check they are valid. */
134                         while ((*unparsedmodes) && (*unparsedmodes != ','))
135                         {
136                                 ModeHandler *mh = ServerInstance->Modes->FindMode(*unparsedmodes, MODETYPE_CHANNEL);
137                                 if (!mh)
138                                 {
139                                         ServerInstance->Logs->Log("m_spanningtree", SPARSE, "Unrecognised mode %c, dropping link", *unparsedmodes);
140                                         return CMD_INVALID;
141                                 }
142
143                                 modes += *unparsedmodes;
144                                 usr++;
145                                 unparsedmodes++;
146                         }
147
148                         /* Advance past the comma, to the nick */
149                         usr++;
150
151                         /* Check the user actually exists */
152                         who = ServerInstance->FindUUID(usr);
153                         if (who)
154                         {
155                                 /* Check that the user's 'direction' is correct */
156                                 TreeServer* route_back_again = Utils->BestRouteTo(who->server);
157                                 if ((!route_back_again) || (route_back_again->GetSocket() != src_socket))
158                                         continue;
159
160                                 /* Add any modes this user had to the mode stack */
161                                 for (std::string::iterator x = modes.begin(); x != modes.end(); ++x)
162                                         modestack.Push(*x, who->nick);
163
164                                 Channel::JoinUser(who, channel.c_str(), true, "", route_back_again->bursting, TS);
165                         }
166                         else
167                         {
168                                 ServerInstance->Logs->Log("m_spanningtree",SPARSE, "Ignored nonexistant user %s in fjoin to %s (probably quit?)", usr, channel.c_str());
169                                 continue;
170                         }
171                 }
172         }
173
174         /* Flush mode stacker if we lost the FJOIN or had equal TS */
175         if (apply_other_sides_modes)
176         {
177                 parameterlist stackresult;
178                 stackresult.push_back(channel);
179
180                 while (modestack.GetStackedLine(stackresult))
181                 {
182                         ServerInstance->SendMode(stackresult, srcuser);
183                         stackresult.erase(stackresult.begin() + 1, stackresult.end());
184                 }
185         }
186         return CMD_SUCCESS;
187 }
188
189 void CommandFJoin::RemoveStatus(User* srcuser, parameterlist &params)
190 {
191         if (params.size() < 1)
192                 return;
193
194         Channel* c = ServerInstance->FindChan(params[0]);
195
196         if (c)
197         {
198                 irc::modestacker stack(false);
199                 parameterlist stackresult;
200                 stackresult.push_back(c->name);
201
202                 for (char modeletter = 'A'; modeletter <= 'z'; ++modeletter)
203                 {
204                         ModeHandler* mh = ServerInstance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
205
206                         /* Passing a pointer to a modestacker here causes the mode to be put onto the mode stack,
207                          * rather than applied immediately. Module unloads require this to be done immediately,
208                          * for this function we require tidyness instead. Fixes bug #493
209                          */
210                         if (mh)
211                                 mh->RemoveMode(c, &stack);
212                 }
213
214                 while (stack.GetStackedLine(stackresult))
215                 {
216                         ServerInstance->SendMode(stackresult, srcuser);
217                         stackresult.erase(stackresult.begin() + 1, stackresult.end());
218                 }
219         }
220 }
221