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