]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/fjoin.cpp
Don't apply channel modes received in an non-burst, non-creation FJOIN, because they...
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / fjoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 "xline.h"
16
17 #include "m_spanningtree/treesocket.h"
18 #include "m_spanningtree/treeserver.h"
19 #include "m_spanningtree/utils.h"
20
21 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
22
23
24 /** FJOIN, almost identical to TS6 SJOIN, except for nicklist handling. */
25 bool TreeSocket::ForceJoin(const std::string &source, std::deque<std::string> &params)
26 {
27         /* 1.1 FJOIN works as follows:
28          *
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.
34          *
35          * When two sides of a split heal and this occurs, the following things
36          * will happen:
37          *
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.
41          *
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
47          * the losing side.
48          *
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. :-)
52          */
53         if (params.size() < 3)
54                 return true;
55
56         irc::modestacker modestack(ServerInstance, 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_umodes = true;                           /* True if we are accepting the other side's user umodes */
62         bool apply_other_sides_cmodes = true;                           /* True if we are accepting the other side's channel modes */
63         Channel* chan = this->ServerInstance->FindChan(channel);                /* The channel we're sending joins to */
64         bool created = !chan;                                           /* True if the channel doesnt exist here yet */
65         std::string item;                                               /* One item in the list of nicks */
66
67         if (params.size() > 3)
68                 params[params.size() - 1] = ":" + params[params.size() - 1];
69
70         Utils->DoOneToAllButSender(source,"FJOIN",params,source);
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.", source.c_str());
76                 return true;
77         }
78
79         if (created)
80         {
81                 chan = new Channel(ServerInstance, channel, TS);
82                 ServerInstance->SNO->WriteToSnoMask('d', "Creation FJOIN recieved for %s, timestamp: %lu", chan->name.c_str(), (unsigned long)TS);
83         }
84         else
85         {
86                 time_t ourTS = chan->age;
87
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 - (unsigned long)TS);
90                 /* If our TS is less than theirs, we dont accept their modes */
91                 if (ourTS < TS)
92                 {
93                         ServerInstance->SNO->WriteToSnoMask('d', "NOT Applying modes from other side");
94                         apply_other_sides_umodes = false;
95                         apply_other_sides_cmodes = 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                         std::deque<std::string> param_list;
102                         if (Utils->AnnounceTSChange && chan)
103                                 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);
104                         ourTS = TS;
105                         chan->age = TS;
106                         param_list.push_back(channel);
107                         this->RemoveStatus(ServerInstance->Config->GetSID(), param_list);
108                 }
109                 else
110                 {
111                         /* Timestamp equal. Apply the user modes always */
112                         apply_other_sides_umodes = true;
113                         /*
114                          * If the server is bursting, then the channel modes need to be applied;
115                          * it is expected that servers will resync modes at this time. Otherwise,
116                          * the mode string sent along with the FJOIN could be out-of-date, and
117                          * applying the mode change could cause modes to be unexpectedly bounced.
118                          */
119                         TreeServer *s = Utils->FindServer(source);
120                         apply_other_sides_cmodes = s->bursting;
121                 }
122         }
123
124         /* First up, apply their modes if they won the TS war */
125         if (apply_other_sides_cmodes)
126         {
127                 ServerInstance->SNO->WriteToSnoMask('d', "Applying remote modestring for %s", params[0].c_str());
128                 unsigned int idx = 2;
129                 std::vector<std::string> modelist;
130
131                 // Mode parser needs to know what channel to act on.
132                 modelist.push_back(params[0]);
133
134                 /* Remember, params[params.size() - 1] is nicklist, and we don't want to apply *that* */
135                 for (idx = 2; idx != (params.size() - 1); idx++)
136                 {
137                         modelist.push_back(params[idx]);
138                 }
139
140                 this->ServerInstance->SendMode(modelist, this->ServerInstance->FakeClient);
141         }
142
143         /* Now, process every 'modes,nick' pair */
144         while (users.GetToken(item))
145         {
146                 const char* usr = item.c_str();
147                 if (usr && *usr)
148                 {
149                         const char* unparsedmodes = usr;
150                         std::string modes;
151
152
153                         /* Iterate through all modes for this user and check they are valid. */
154                         while ((*unparsedmodes) && (*unparsedmodes != ','))
155                         {
156                                 ModeHandler *mh = ServerInstance->Modes->FindMode(*unparsedmodes, MODETYPE_CHANNEL);
157                                 if (mh)
158                                         modes += *unparsedmodes;
159                                 else
160                                 {
161                                         this->SendError(std::string("Unknown status mode '")+(*unparsedmodes)+"' in FJOIN");
162                                         return false;
163                                 }
164
165                                 usr++;
166                                 unparsedmodes++;
167                         }
168
169                         /* Advance past the comma, to the nick */
170                         usr++;
171
172                         /* Check the user actually exists */
173                         who = this->ServerInstance->FindUUID(usr);
174                         if (who)
175                         {
176                                 /* Check that the user's 'direction' is correct */
177                                 TreeServer* route_back_again = Utils->BestRouteTo(who->server);
178                                 if ((!route_back_again) || (route_back_again->GetSocket() != this))
179                                         continue;
180
181                                 /* Add any modes this user had to the mode stack */
182                                 for (std::string::iterator x = modes.begin(); x != modes.end(); ++x)
183                                         modestack.Push(*x, who->nick);
184
185                                 Channel::JoinUser(this->ServerInstance, who, channel.c_str(), true, "", true, TS);
186                         }
187                         else
188                         {
189                                 ServerInstance->Logs->Log("m_spanningtree",SPARSE, "Ignored nonexistant user %s in fjoin to %s (probably quit?)", usr, channel.c_str());
190                                 continue;
191                         }
192                 }
193         }
194
195         /* Flush mode stacker if we lost the FJOIN or had equal TS */
196         if (apply_other_sides_umodes)
197         {
198                 std::deque<std::string> stackresult;
199                 std::vector<std::string> mode_junk;
200                 mode_junk.push_back(channel);
201
202                 while (modestack.GetStackedLine(stackresult))
203                 {
204                         for (size_t j = 0; j < stackresult.size(); j++)
205                         {
206                                 mode_junk.push_back(stackresult[j]);
207                         }
208                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
209                 }
210         }
211
212         return true;
213 }
214
215 bool TreeSocket::RemoveStatus(const std::string &prefix, std::deque<std::string> &params)
216 {
217         if (params.size() < 1)
218                 return true;
219
220         Channel* c = ServerInstance->FindChan(params[0]);
221
222         if (c)
223         {
224                 irc::modestacker stack(ServerInstance, false);
225                 std::deque<std::string> stackresult;
226                 std::vector<std::string> mode_junk;
227                 mode_junk.push_back(c->name);
228
229                 for (char modeletter = 'A'; modeletter <= 'z'; ++modeletter)
230                 {
231                         ModeHandler* mh = ServerInstance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
232
233                         /* Passing a pointer to a modestacker here causes the mode to be put onto the mode stack,
234                          * rather than applied immediately. Module unloads require this to be done immediately,
235                          * for this function we require tidyness instead. Fixes bug #493
236                          */
237                         if (mh)
238                                 mh->RemoveMode(c, &stack);
239                 }
240
241                 while (stack.GetStackedLine(stackresult))
242                 {
243                         for (size_t j = 0; j < stackresult.size(); j++)
244                                 mode_junk.push_back(stackresult[j]);
245
246                         ServerInstance->SendMode(mode_junk, ServerInstance->FakeClient);
247                 }
248         }
249         return true;
250 }
251