]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
Allow Channel::WriteNotice send to other servers and status ranks.
[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 \002%s\002 (Authentication: %s%s).",
106                 s->GetName().c_str(),
107                 capab->auth_fingerprint ? "SSL certificate fingerprint and " : "",
108                 capab->auth_challenge ? "challenge-response" : "plaintext password");
109         this->CleanNegotiationInfo();
110         this->WriteLine(CmdBuilder("BURST").push_int(ServerInstance->Time()));
111         // Introduce all servers behind us
112         this->SendServers(Utils->TreeRoot, s);
113
114         BurstState bs(this);
115         // Introduce all users
116         this->SendUsers(bs);
117
118         // Sync all channels
119         const chan_hash& chans = ServerInstance->GetChans();
120         for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
121                 SyncChannel(i->second, bs);
122
123         // Send all xlines
124         this->SendXLines();
125         FOREACH_MOD_CUSTOM(Utils->Creator->GetSyncEventProvider(), ServerProtocol::SyncEventListener, OnSyncNetwork, (bs.server));
126         this->WriteLine(CmdBuilder("ENDBURST"));
127         ServerInstance->SNO->WriteToSnoMask('l',"Finished bursting to \002"+ s->GetName()+"\002.");
128
129         this->burstsent = true;
130 }
131
132 void TreeSocket::SendServerInfo(TreeServer* from)
133 {
134         // Send public version string
135         this->WriteLine(CommandSInfo::Builder(from, "version", from->GetVersion()));
136
137         // Send full version string that contains more information and is shown to opers
138         this->WriteLine(CommandSInfo::Builder(from, "fullversion", from->GetFullVersion()));
139
140         // Send the raw version string that just contains the base info
141         this->WriteLine(CommandSInfo::Builder(from, "rawversion", from->GetRawVersion()));
142 }
143
144 /** Recursively send the server tree.
145  * This is used during network burst to inform the other server
146  * (and any of ITS servers too) of what servers we know about.
147  * If at any point any of these servers already exist on the other
148  * end, our connection may be terminated.
149  */
150 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s)
151 {
152         SendServerInfo(Current);
153
154         const TreeServer::ChildServers& children = Current->GetChildren();
155         for (TreeServer::ChildServers::const_iterator i = children.begin(); i != children.end(); ++i)
156         {
157                 TreeServer* recursive_server = *i;
158                 if (recursive_server != s)
159                 {
160                         this->WriteLine(CommandServer::Builder(recursive_server));
161                         /* down to next level */
162                         this->SendServers(recursive_server, s);
163                 }
164         }
165 }
166
167 /** Send one or more FJOINs for a channel of users.
168  * If the length of a single line is too long, it is split over multiple lines.
169  */
170 void TreeSocket::SendFJoins(Channel* c)
171 {
172         CommandFJoin::Builder fjoin(c);
173
174         const Channel::MemberMap& ulist = c->GetUsers();
175         for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
176         {
177                 Membership* memb = i->second;
178                 if (!fjoin.has_room(memb))
179                 {
180                         // No room for this user, send the line and prepare a new one
181                         this->WriteLine(fjoin.finalize());
182                         fjoin.clear();
183                 }
184                 fjoin.add(memb);
185         }
186         this->WriteLine(fjoin.finalize());
187 }
188
189 /** Send all XLines we know about */
190 void TreeSocket::SendXLines()
191 {
192         std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
193
194         for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
195         {
196                 /* Expired lines are removed in XLineManager::GetAll() */
197                 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
198
199                 /* lookup cannot be NULL in this case but a check won't hurt */
200                 if (lookup)
201                 {
202                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
203                         {
204                                 /* Is it burstable? this is better than an explicit check for type 'K'.
205                                  * We break the loop as NONE of the items in this group are worth iterating.
206                                  */
207                                 if (!i->second->IsBurstable())
208                                         break;
209
210                                 this->WriteLine(CommandAddLine::Builder(i->second));
211                         }
212                 }
213         }
214 }
215
216 void TreeSocket::SendListModes(Channel* chan)
217 {
218         FModeBuilder fmode(chan);
219         const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
220         for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
221         {
222                 ListModeBase* mh = *i;
223                 ListModeBase::ModeList* list = mh->GetList(chan);
224                 if (!list)
225                         continue;
226
227                 // Add all items on the list to the FMODE, send it whenever it becomes too long
228                 const char modeletter = mh->GetModeChar();
229                 for (ListModeBase::ModeList::const_iterator j = list->begin(); j != list->end(); ++j)
230                 {
231                         const std::string& mask = j->mask;
232                         if (!fmode.has_room(mask))
233                         {
234                                 // No room for this mask, send the current line as-is then add the mask to a
235                                 // new, empty FMODE message
236                                 this->WriteLine(fmode.finalize());
237                                 fmode.clear();
238                         }
239                         fmode.push_mode(modeletter, mask);
240                 }
241         }
242
243         if (!fmode.empty())
244                 this->WriteLine(fmode.finalize());
245 }
246
247 /** Send channel users, topic, modes and global metadata */
248 void TreeSocket::SyncChannel(Channel* chan, BurstState& bs)
249 {
250         SendFJoins(chan);
251
252         // If the topic was ever set, send it, even if it's empty now
253         // because a new empty topic should override an old non-empty topic
254         if (chan->topicset != 0)
255                 this->WriteLine(CommandFTopic::Builder(chan));
256
257         SendListModes(chan);
258
259         for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
260         {
261                 ExtensionItem* item = i->first;
262                 std::string value = item->ToNetwork(chan, i->second);
263                 if (!value.empty())
264                         this->WriteLine(CommandMetadata::Builder(chan, item->name, value));
265         }
266
267         FOREACH_MOD_CUSTOM(Utils->Creator->GetSyncEventProvider(), ServerProtocol::SyncEventListener, OnSyncChannel, (chan, bs.server));
268 }
269
270 void TreeSocket::SyncChannel(Channel* chan)
271 {
272         BurstState bs(this);
273         SyncChannel(chan, bs);
274 }
275
276 /** Send all users and their state, including oper and away status and global metadata */
277 void TreeSocket::SendUsers(BurstState& bs)
278 {
279         const user_hash& users = ServerInstance->Users->GetUsers();
280         for (user_hash::const_iterator u = users.begin(); u != users.end(); ++u)
281         {
282                 User* user = u->second;
283                 if (user->registered != REG_ALL)
284                         continue;
285
286                 this->WriteLine(CommandUID::Builder(user));
287
288                 if (user->IsOper())
289                         this->WriteLine(CommandOpertype::Builder(user));
290
291                 if (user->IsAway())
292                         this->WriteLine(CommandAway::Builder(user));
293
294                 const Extensible::ExtensibleStore& exts = user->GetExtList();
295                 for (Extensible::ExtensibleStore::const_iterator i = exts.begin(); i != exts.end(); ++i)
296                 {
297                         ExtensionItem* item = i->first;
298                         std::string value = item->ToNetwork(u->second, i->second);
299                         if (!value.empty())
300                                 this->WriteLine(CommandMetadata::Builder(user, item->name, value));
301                 }
302
303                 FOREACH_MOD_CUSTOM(Utils->Creator->GetSyncEventProvider(), ServerProtocol::SyncEventListener, OnSyncUser, (user, bs.server));
304         }
305 }