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