]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
m_spanningtree Change allocation of the specialized ProtocolInterface to be physicall...
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / netburst.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23 #include "xline.h"
24 #include "listmode.h"
25
26 #include "treesocket.h"
27 #include "treeserver.h"
28 #include "main.h"
29 #include "commands.h"
30
31 /**
32  * Creates FMODE messages, used only when syncing channels
33  */
34 class FModeBuilder : public CmdBuilder
35 {
36         static const size_t maxline = 480;
37         std::string params;
38         unsigned int modes;
39         std::string::size_type startpos;
40
41  public:
42         FModeBuilder(Channel* chan)
43                 : CmdBuilder("FMODE"), modes(0)
44         {
45                 push(chan->name).push_int(chan->age).push_raw(" +");
46                 startpos = str().size();
47         }
48
49         /** Add a mode to the message
50          */
51         void push_mode(const char modeletter, const std::string& mask)
52         {
53                 push_raw(modeletter);
54                 params.push_back(' ');
55                 params.append(mask);
56                 modes++;
57         }
58
59         /** Remove all modes from the message
60          */
61         void clear()
62         {
63                 content.erase(startpos);
64                 params.clear();
65                 modes = 0;
66         }
67
68         /** Prepare the message for sending, next mode can only be added after clear()
69          */
70         const std::string& finalize()
71         {
72                 return push_raw(params);
73         }
74
75         /** Returns true if the given mask can be added to the message, false if the message
76          * has no room for the mask
77          */
78         bool has_room(const std::string& mask) const
79         {
80                 return ((str().size() + params.size() + mask.size() + 2 <= maxline) &&
81                                 (modes < ServerInstance->Config->Limits.MaxModes));
82         }
83
84         /** Returns true if this message is empty (has no modes)
85          */
86         bool empty() const
87         {
88                 return (modes == 0);
89         }
90 };
91
92 struct TreeSocket::BurstState
93 {
94         SpanningTreeProtocolInterface::Server server;
95         BurstState(TreeSocket* sock) : server(sock) { }
96 };
97
98 /** This function is called when we want to send a netburst to a local
99  * server. There is a set order we must do this, because for example
100  * users require their servers to exist, and channels require their
101  * users to exist. You get the idea.
102  */
103 void TreeSocket::DoBurst(TreeServer* s)
104 {
105         ServerInstance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (Authentication: %s%s).",
106                 s->GetName().c_str(),
107                 capab->auth_fingerprint ? "SSL Fingerprint and " : "",
108                 capab->auth_challenge ? "challenge-response" : "plaintext password");
109         this->CleanNegotiationInfo();
110         this->WriteLine(":" + ServerInstance->Config->GetSID() + " BURST " + ConvToStr(ServerInstance->Time()));
111         /* send our version string */
112         this->WriteLine(":" + ServerInstance->Config->GetSID() + " VERSION :"+ServerInstance->GetVersionString());
113         /* Send server tree */
114         this->SendServers(Utils->TreeRoot, s);
115
116         BurstState bs(this);
117         /* Send users and their oper status */
118         this->SendUsers(bs);
119
120         const chan_hash& chans = ServerInstance->GetChans();
121         for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
122                 SyncChannel(i->second, bs);
123
124         this->SendXLines();
125         FOREACH_MOD(OnSyncNetwork, (bs.server));
126         this->WriteLine(":" + ServerInstance->Config->GetSID() + " ENDBURST");
127         ServerInstance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+ s->GetName()+"\2.");
128 }
129
130 /** Recursively send the server tree.
131  * This is used during network burst to inform the other server
132  * (and any of ITS servers too) of what servers we know about.
133  * If at any point any of these servers already exist on the other
134  * end, our connection may be terminated.
135  * The hopcount parameter (3rd) is deprecated, and is always 0.
136  */
137 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s)
138 {
139         const TreeServer::ChildServers& children = Current->GetChildren();
140         for (TreeServer::ChildServers::const_iterator i = children.begin(); i != children.end(); ++i)
141         {
142                 TreeServer* recursive_server = *i;
143                 if (recursive_server != s)
144                 {
145                         this->WriteLine(CommandServer::Builder(recursive_server));
146                         this->WriteLine(":" + recursive_server->GetID() + " VERSION :" + recursive_server->GetVersion());
147                         /* down to next level */
148                         this->SendServers(recursive_server, s);
149                 }
150         }
151 }
152
153 /** Send one or more FJOINs for a channel of users.
154  * If the length of a single line is more than 480-NICKMAX
155  * in length, it is split over multiple lines.
156  * Send one or more FMODEs for a channel with the
157  * channel bans, if there's any.
158  */
159 void TreeSocket::SendFJoins(Channel* c)
160 {
161         CommandFJoin::Builder fjoin(c);
162         const UserMembList *ulist = c->GetUsers();
163
164         for (UserMembCIter i = ulist->begin(); i != ulist->end(); ++i)
165         {
166                 Membership* memb = i->second;
167                 if (!fjoin.has_room(memb))
168                 {
169                         // No room for this user, send the line and prepare a new one
170                         this->WriteLine(fjoin.finalize());
171                         fjoin.clear();
172                 }
173                 fjoin.add(memb);
174         }
175         this->WriteLine(fjoin.finalize());
176 }
177
178 /** Send all XLines we know about */
179 void TreeSocket::SendXLines()
180 {
181         std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
182
183         for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
184         {
185                 /* Expired lines are removed in XLineManager::GetAll() */
186                 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
187
188                 /* lookup cannot be NULL in this case but a check won't hurt */
189                 if (lookup)
190                 {
191                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
192                         {
193                                 /* Is it burstable? this is better than an explicit check for type 'K'.
194                                  * We break the loop as NONE of the items in this group are worth iterating.
195                                  */
196                                 if (!i->second->IsBurstable())
197                                         break;
198
199                                 this->WriteLine(CommandAddLine::Builder(i->second));
200                         }
201                 }
202         }
203 }
204
205 void TreeSocket::SendListModes(Channel* chan)
206 {
207         FModeBuilder fmode(chan);
208         const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
209         for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
210         {
211                 ListModeBase* mh = *i;
212                 ListModeBase::ModeList* list = mh->GetList(chan);
213                 if (!list)
214                         continue;
215
216                 // Add all items on the list to the FMODE, send it whenever it becomes too long
217                 const char modeletter = mh->GetModeChar();
218                 for (ListModeBase::ModeList::const_iterator j = list->begin(); j != list->end(); ++j)
219                 {
220                         const std::string& mask = j->mask;
221                         if (!fmode.has_room(mask))
222                         {
223                                 // No room for this mask, send the current line as-is then add the mask to a
224                                 // new, empty FMODE message
225                                 this->WriteLine(fmode.finalize());
226                                 fmode.clear();
227                         }
228                         fmode.push_mode(modeletter, mask);
229                 }
230         }
231
232         if (!fmode.empty())
233                 this->WriteLine(fmode.finalize());
234 }
235
236 /** Send channel topic, modes and metadata */
237 void TreeSocket::SyncChannel(Channel* chan, BurstState& bs)
238 {
239         SendFJoins(chan);
240
241         // If the topic was ever set, send it, even if it's empty now
242         // because a new empty topic should override an old non-empty topic
243         if (chan->topicset != 0)
244                 this->WriteLine(CommandFTopic::Builder(chan));
245
246         SendListModes(chan);
247
248         for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
249         {
250                 ExtensionItem* item = i->first;
251                 std::string value = item->serialize(FORMAT_NETWORK, chan, i->second);
252                 if (!value.empty())
253                         this->WriteLine(CommandMetadata::Builder(chan, item->name, value));
254         }
255
256         FOREACH_MOD(OnSyncChannel, (chan, bs.server));
257 }
258
259 void TreeSocket::SyncChannel(Channel* chan)
260 {
261         BurstState bs(this);
262         SyncChannel(chan, bs);
263 }
264
265 /** send all users and their oper state/modes */
266 void TreeSocket::SendUsers(BurstState& bs)
267 {
268         ProtocolInterface::Server& piserver = bs.server;
269
270         const user_hash& users = ServerInstance->Users->GetUsers();
271         for (user_hash::const_iterator u = users.begin(); u != users.end(); ++u)
272         {
273                 User* user = u->second;
274                 if (user->registered != REG_ALL)
275                         continue;
276
277                 this->WriteLine(CommandUID::Builder(user));
278
279                 if (user->IsOper())
280                         this->WriteLine(CommandOpertype::Builder(user));
281
282                 if (user->IsAway())
283                         this->WriteLine(CommandAway::Builder(user));
284
285                 const Extensible::ExtensibleStore& exts = user->GetExtList();
286                 for (Extensible::ExtensibleStore::const_iterator i = exts.begin(); i != exts.end(); ++i)
287                 {
288                         ExtensionItem* item = i->first;
289                         std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
290                         if (!value.empty())
291                                 this->WriteLine(CommandMetadata::Builder(user, item->name, value));
292                 }
293
294                 FOREACH_MOD(OnSyncUser, (user, piserver));
295         }
296 }