]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/fjoin.cpp
Remove many unneeded headers from spanningtree files
[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, similar to TS6 SJOIN, but not quite. */
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
61         if (params.size() < 2)
62                 return true;
63
64         irc::modestacker modestack(true);                               /* Modes to apply from the users in the user list */
65         User* who = NULL;                                               /* User we are currently checking */
66         std::string channel = params[0];                                /* Channel name, as a string */
67         time_t TS = atoi(params[1].c_str());                            /* Timestamp given to us for remote side */
68         irc::tokenstream users((params.size() > 2) ? params[2] : "");   /* users from the user list */
69         bool apply_other_sides_modes = true;                            /* True if we are accepting the other side's modes */
70         Channel* chan = this->Instance->FindChan(channel);              /* The channel we're sending joins to */
71         time_t ourTS = chan ? chan->age : Instance->Time(true)+600;     /* The TS of our side of the link */
72         bool created = !chan;                                           /* True if the channel doesnt exist here yet */
73         std::string item;                                               /* One item in the list of nicks */
74
75         if (params.size() > 2)
76                 params[2] = ":" + params[2];
77                 
78         Utils->DoOneToAllButSender(source,"FJOIN",params,source);
79
80         if (!TS)
81         {
82                 Instance->Log(DEFAULT,"*** BUG? *** TS of 0 sent to FJOIN. Are some services authors smoking craq, or is it 1970 again?. Dropped.");
83                 Instance->SNO->WriteToSnoMask('d', "WARNING: The server %s is sending FJOIN with a TS of zero. Total craq. Command was dropped.", source.c_str());
84                 return true;
85         }
86
87         if (created)
88                 chan = new Channel(Instance, channel, ourTS);
89
90         /* If our TS is less than theirs, we dont accept their modes */
91         if (ourTS < TS)
92                 apply_other_sides_modes = false;
93
94         /* Our TS greater than theirs, clear all our modes from the channel, accept theirs. */
95         if (ourTS > TS)
96         {
97                 std::deque<std::string> param_list;
98                 if (Utils->AnnounceTSChange && chan)
99                         chan->WriteChannelWithServ(Instance->Config->ServerName, "NOTICE %s :TS for %s changed from %lu to %lu", chan->name, chan->name, ourTS, TS);
100                 ourTS = TS;
101                 if (!created)
102                 {
103                         chan->age = TS;
104                         param_list.push_back(channel);
105                         this->RemoveStatus(Instance->Config->GetSID(), param_list);
106                 }
107         }
108
109         /* Now, process every 'prefixes,nick' pair */
110         while (users.GetToken(item))
111         {
112                 const char* usr = item.c_str();
113                 if (usr && *usr)
114                 {
115                         const char* permissions = usr;
116                         /* Iterate through all the prefix values, convert them from prefixes to mode letters */
117                         std::string modes;
118                         while ((*permissions) && (*permissions != ','))
119                         {
120                                 ModeHandler* mh = Instance->Modes->FindPrefix(*permissions);
121                                 if (mh)
122                                         modes = modes + mh->GetModeChar();
123                                 else
124                                 {
125                                         this->SendError(std::string("Invalid prefix '")+(*permissions)+"' in FJOIN");
126                                         return false;
127                                 }
128                                 usr++;
129                                 permissions++;
130                         }
131                         /* Advance past the comma, to the nick */
132                         usr++;
133                         
134                         /* Check the user actually exists */
135                         who = this->Instance->FindUUID(usr);
136                         if (who)
137                         {
138                                 /* Check that the user's 'direction' is correct */
139                                 TreeServer* route_back_again = Utils->BestRouteTo(who->server);
140                                 if ((!route_back_again) || (route_back_again->GetSocket() != this))
141                                         continue;
142
143                                 /* Add any permissions this user had to the mode stack */
144                                 for (std::string::iterator x = modes.begin(); x != modes.end(); ++x)
145                                         modestack.Push(*x, who->nick);
146
147                                 Channel::JoinUser(this->Instance, who, channel.c_str(), true, "", true, TS);
148                         }
149                         else
150                         {
151                                 Instance->Log(SPARSE,"Warning! Invalid user %s in FJOIN to channel %s IGNORED", usr, channel.c_str());
152                                 continue;
153                         }
154                 }
155         }
156
157         /* Flush mode stacker if we lost the FJOIN or had equal TS */
158         if (apply_other_sides_modes)
159         {
160                 std::deque<std::string> stackresult;
161                 const char* mode_junk[MAXMODES+2];
162                 mode_junk[0] = channel.c_str();
163
164                 while (modestack.GetStackedLine(stackresult))
165                 {
166                         for (size_t j = 0; j < stackresult.size(); j++)
167                         {
168                                 mode_junk[j+1] = stackresult[j].c_str();
169                         }
170                         Instance->SendMode(mode_junk, stackresult.size() + 1, Instance->FakeClient);
171                 }
172         }
173
174         return true;
175 }
176
177 /** TODO: This creates a total mess of output and needs to really use irc::modestacker.
178  */
179 bool TreeSocket::RemoveStatus(const std::string &prefix, std::deque<std::string> &params)
180 {
181         if (params.size() < 1)
182                 return true;
183
184         Channel* c = Instance->FindChan(params[0]);
185
186         if (c)
187         {
188                 for (char modeletter = 'A'; modeletter <= 'z'; modeletter++)
189                 {
190                         ModeHandler* mh = Instance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
191                         if (mh)
192                                 mh->RemoveMode(c);
193                 }
194         }
195         return true;
196 }
197