]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/fjoin.cpp
Convert a few missing
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / fjoin.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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          * NOTE: Unlike TS6 and dreamforge and other protocols which have SJOIN,
54          * FJOIN does not contain the simple-modes such as +iklmnsp. Why not,
55          * you ask? Well, quite simply because we don't need to. They'll be sent
56          * after the FJOIN by FMODE, and FMODE is timestamped, so in the event
57          * the losing side sends any modes for the channel which shouldnt win,
58          * they wont as their timestamp will be too high :-)
59          */
60         if (params.size() < 3)
61                 return true;
62
63         irc::modestacker modestack(true);                               /* Modes to apply from the users in the user list */
64         User* who = NULL;                                               /* User we are currently checking */
65         std::string channel = params[0];                                /* Channel name, as a string */
66         time_t TS = atoi(params[1].c_str());                            /* Timestamp given to us for remote side */
67         irc::tokenstream users((params.size() > 3) ? params[params.size() - 1] : "");   /* users from the user list */
68         bool apply_other_sides_modes = true;                            /* True if we are accepting the other side's modes */
69         Channel* chan = this->Instance->FindChan(channel);              /* The channel we're sending joins to */
70         time_t ourTS = chan ? chan->age : Instance->Time()+600; /* The TS of our side of the link */
71         bool created = !chan;                                           /* True if the channel doesnt exist here yet */
72         std::string item;                                               /* One item in the list of nicks */
73
74         if (params.size() > 3)
75                 params[params.size() - 1] = ":" + params[params.size() - 1];
76                 
77         Utils->DoOneToAllButSender(source,"FJOIN",params,source);
78
79         if (!TS)
80         {
81                 Instance->Logs->Log("m_spanningtree",DEFAULT,"*** BUG? *** TS of 0 sent to FJOIN. Are some services authors smoking craq, or is it 1970 again?. Dropped.");
82                 Instance->SNO->WriteToSnoMask('d', "WARNING: The server %s is sending FJOIN with a TS of zero. Total craq. Command was dropped.", source.c_str());
83                 return true;
84         }
85
86         if (created)
87                 chan = new Channel(Instance, channel, ourTS);
88
89         /* If our TS is less than theirs, we dont accept their modes */
90         if (ourTS < TS)
91                 apply_other_sides_modes = false;
92
93         /* Our TS greater than theirs, clear all our modes from the channel, accept theirs. */
94         if (ourTS > TS)
95         {
96                 std::deque<std::string> param_list;
97                 if (Utils->AnnounceTSChange && chan)
98                         chan->WriteChannelWithServ(Instance->Config->ServerName, "NOTICE %s :TS for %s changed from %lu to %lu", chan->name, chan->name, (unsigned long) ourTS, (unsigned long) TS);
99                 ourTS = TS;
100                 if (!created)
101                 {
102                         chan->age = TS;
103                         param_list.push_back(channel);
104                         this->RemoveStatus(Instance->Config->GetSID(), param_list);
105                 }
106         }
107
108         /* First up, apply their modes if they won the TS war */
109         if (apply_other_sides_modes)
110         {
111                 unsigned int idx = 2;
112                 int numpara = 1;
113                 std::vector<std::string> modelist;
114
115                 // Mode parser needs to know what channel to act on.
116                 modelist[0] = params[0];
117
118                 /* Remember, params[params.size() - 1] is nicklist, and we don't want to apply *that* */
119                 for (idx = 2; idx != (params.size() - 1); idx++)
120                 {
121                         modelist[numpara++] = params[idx];
122                 }
123
124                 this->Instance->SendMode(modelist, this->Instance->FakeClient);
125         }
126
127         /* Now, process every 'modes,nick' pair */
128         while (users.GetToken(item))
129         {
130                 const char* usr = item.c_str();
131                 if (usr && *usr)
132                 {
133                         const char* unparsedmodes = usr;
134                         std::string modes;
135
136
137                         /* Iterate through all modes for this user and check they are valid. */
138                         while ((*unparsedmodes) && (*unparsedmodes != ','))
139                         {
140                                 ModeHandler *mh = Instance->Modes->FindMode(*unparsedmodes, MODETYPE_CHANNEL);
141                                 if (mh)
142                                         modes += *unparsedmodes;
143                                 else
144                                 {
145                                         this->SendError(std::string("Unknown status mode '")+(*unparsedmodes)+"' in FJOIN");
146                                         return false;
147                                 }
148
149                                 usr++;
150                                 unparsedmodes++;
151                         }
152
153                         /* Advance past the comma, to the nick */
154                         usr++;
155                         
156                         /* Check the user actually exists */
157                         who = this->Instance->FindUUID(usr);
158                         if (who)
159                         {
160                                 /* Check that the user's 'direction' is correct */
161                                 TreeServer* route_back_again = Utils->BestRouteTo(who->server);
162                                 if ((!route_back_again) || (route_back_again->GetSocket() != this))
163                                         continue;
164
165                                 /* Add any modes this user had to the mode stack */
166                                 for (std::string::iterator x = modes.begin(); x != modes.end(); ++x)
167                                         modestack.Push(*x, who->nick);
168
169                                 Channel::JoinUser(this->Instance, who, channel.c_str(), true, "", true, TS);
170                         }
171                         else
172                         {
173                                 Instance->Logs->Log("m_spanningtree",SPARSE, "Ignored nonexistant user %s in fjoin to %s (probably quit?)", usr, channel.c_str());
174                                 continue;
175                         }
176                 }
177         }
178
179         /* Flush mode stacker if we lost the FJOIN or had equal TS */
180         if (apply_other_sides_modes)
181         {
182                 std::deque<std::string> stackresult;
183                 std::vector<std::string> mode_junk;
184                 mode_junk[0] = channel;
185
186                 while (modestack.GetStackedLine(stackresult))
187                 {
188                         for (size_t j = 0; j < stackresult.size(); j++)
189                         {
190                                 mode_junk[j+1] = stackresult[j];
191                         }
192                         Instance->SendMode(mode_junk, Instance->FakeClient);
193                 }
194         }
195
196         return true;
197 }
198
199 /** TODO: This creates a total mess of output and needs to really use irc::modestacker.
200  */
201 bool TreeSocket::RemoveStatus(const std::string &prefix, std::deque<std::string> &params)
202 {
203         if (params.size() < 1)
204                 return true;
205
206         Channel* c = Instance->FindChan(params[0]);
207
208         if (c)
209         {
210                 irc::modestacker stack(false);
211                 std::deque<std::string> stackresult;
212                 std::vector<std::string> mode_junk;
213                 mode_junk[0] = c->name;
214
215                 for (char modeletter = 'A'; modeletter <= 'z'; ++modeletter)
216                 {
217                         ModeHandler* mh = Instance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
218
219                         /* Passing a pointer to a modestacker here causes the mode to be put onto the mode stack,
220                          * rather than applied immediately. Module unloads require this to be done immediately,
221                          * for this function we require tidyness instead. Fixes bug #493
222                          */
223                         if (mh)
224                                 mh->RemoveMode(c, &stack);
225                 }
226
227                 while (stack.GetStackedLine(stackresult))
228                 {
229                         for (size_t j = 0; j < stackresult.size(); j++)
230                                 mode_junk[j+1] = stackresult[j];
231
232                         Instance->SendMode(mode_junk, Instance->FakeClient);
233                 }
234         }
235         return true;
236 }
237