]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
Pass an interface to the OnSync hooks
[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 "protocolinterface.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 Fingerprint and " : "",
109                 capab->auth_challenge ? "challenge-response" : "plaintext password");
110         this->CleanNegotiationInfo();
111         this->WriteLine(":" + ServerInstance->Config->GetSID() + " BURST " + ConvToStr(ServerInstance->Time()));
112         /* send our version string */
113         this->WriteLine(":" + ServerInstance->Config->GetSID() + " VERSION :"+ServerInstance->GetVersionString());
114         /* Send server tree */
115         this->SendServers(Utils->TreeRoot, s);
116
117         BurstState bs(this);
118         /* Send users and their oper status */
119         this->SendUsers(bs);
120
121         for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->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         std::string line(":");
162         line.append(ServerInstance->Config->GetSID()).append(" FJOIN ").append(c->name).append(1, ' ').append(ConvToStr(c->age)).append(" +");
163         std::string::size_type erase_from = line.length();
164         line.append(c->ChanModes(true)).append(" :");
165
166         const UserMembList *ulist = c->GetUsers();
167
168         for (UserMembCIter i = ulist->begin(); i != ulist->end(); ++i)
169         {
170                 const std::string& modestr = i->second->modes;
171                 if ((line.length() + modestr.length() + UIDGenerator::UUID_LENGTH + 2) > 480)
172                 {
173                         this->WriteLine(line);
174                         line.erase(erase_from);
175                         line.append(" :");
176                 }
177                 line.append(modestr).append(1, ',').append(i->first->uuid).push_back(' ');
178         }
179         this->WriteLine(line);
180 }
181
182 /** Send all XLines we know about */
183 void TreeSocket::SendXLines()
184 {
185         std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
186
187         for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
188         {
189                 /* Expired lines are removed in XLineManager::GetAll() */
190                 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
191
192                 /* lookup cannot be NULL in this case but a check won't hurt */
193                 if (lookup)
194                 {
195                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
196                         {
197                                 /* Is it burstable? this is better than an explicit check for type 'K'.
198                                  * We break the loop as NONE of the items in this group are worth iterating.
199                                  */
200                                 if (!i->second->IsBurstable())
201                                         break;
202
203                                 this->WriteLine(CommandAddLine::Builder(i->second));
204                         }
205                 }
206         }
207 }
208
209 void TreeSocket::SendListModes(Channel* chan)
210 {
211         FModeBuilder fmode(chan);
212         const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
213         for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
214         {
215                 ListModeBase* mh = *i;
216                 ListModeBase::ModeList* list = mh->GetList(chan);
217                 if (!list)
218                         continue;
219
220                 // Add all items on the list to the FMODE, send it whenever it becomes too long
221                 const char modeletter = mh->GetModeChar();
222                 for (ListModeBase::ModeList::const_iterator j = list->begin(); j != list->end(); ++j)
223                 {
224                         const std::string& mask = j->mask;
225                         if (!fmode.has_room(mask))
226                         {
227                                 // No room for this mask, send the current line as-is then add the mask to a
228                                 // new, empty FMODE message
229                                 this->WriteLine(fmode.finalize());
230                                 fmode.clear();
231                         }
232                         fmode.push_mode(modeletter, mask);
233                 }
234         }
235
236         if (!fmode.empty())
237                 this->WriteLine(fmode.finalize());
238 }
239
240 /** Send channel topic, modes and metadata */
241 void TreeSocket::SyncChannel(Channel* chan, BurstState& bs)
242 {
243         SendFJoins(chan);
244
245         // If the topic was ever set, send it, even if it's empty now
246         // because a new empty topic should override an old non-empty topic
247         if (chan->topicset != 0)
248                 this->WriteLine(CommandFTopic::Builder(chan));
249
250         SendListModes(chan);
251
252         for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
253         {
254                 ExtensionItem* item = i->first;
255                 std::string value = item->serialize(FORMAT_NETWORK, chan, i->second);
256                 if (!value.empty())
257                         this->WriteLine(CommandMetadata::Builder(chan, item->name, value));
258         }
259
260         FOREACH_MOD(OnSyncChannel, (chan, bs.server));
261 }
262
263 void TreeSocket::SyncChannel(Channel* chan)
264 {
265         BurstState bs(this);
266         SyncChannel(chan, bs);
267 }
268
269 /** send all users and their oper state/modes */
270 void TreeSocket::SendUsers(BurstState& bs)
271 {
272         ProtocolInterface::Server& piserver = bs.server;
273         for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
274         {
275                 User* user = u->second;
276                 if (user->registered != REG_ALL)
277                         continue;
278
279                 this->WriteLine(CommandUID::Builder(user));
280
281                 if (user->IsOper())
282                         this->WriteLine(CommandOpertype::Builder(user));
283
284                 if (user->IsAway())
285                         this->WriteLine(CommandAway::Builder(user));
286
287                 const Extensible::ExtensibleStore& exts = user->GetExtList();
288                 for (Extensible::ExtensibleStore::const_iterator i = exts.begin(); i != exts.end(); ++i)
289                 {
290                         ExtensionItem* item = i->first;
291                         std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
292                         if (!value.empty())
293                                 this->WriteLine(CommandMetadata::Builder(user, item->name, value));
294                 }
295
296                 FOREACH_MOD(OnSyncUser, (user, piserver));
297         }
298 }
299