2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
26 #include "treesocket.h"
27 #include "treeserver.h"
32 * Creates FMODE messages, used only when syncing channels
34 class FModeBuilder : public CmdBuilder
36 static const size_t maxline = 480;
39 std::string::size_type startpos;
42 FModeBuilder(Channel* chan)
43 : CmdBuilder("FMODE"), modes(0)
45 push(chan->name).push_int(chan->age).push_raw(" +");
46 startpos = str().size();
49 /** Add a mode to the message
51 void push_mode(const char modeletter, const std::string& mask)
54 params.push_back(' ');
59 /** Remove all modes from the message
63 content.erase(startpos);
68 /** Prepare the message for sending, next mode can only be added after clear()
70 const std::string& finalize()
72 return push_raw(params);
75 /** Returns true if the given mask can be added to the message, false if the message
76 * has no room for the mask
78 bool has_room(const std::string& mask) const
80 return ((str().size() + params.size() + mask.size() + 2 <= maxline) &&
81 (modes < ServerInstance->Config->Limits.MaxModes));
84 /** Returns true if this message is empty (has no modes)
92 struct TreeSocket::BurstState
94 SpanningTreeProtocolInterface::Server server;
95 BurstState(TreeSocket* sock) : server(sock) { }
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.
103 void TreeSocket::DoBurst(TreeServer* s)
105 ServerInstance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (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 /* Send server tree */
112 this->SendServers(Utils->TreeRoot, s);
115 /* Send users and their oper status */
118 const chan_hash& chans = ServerInstance->GetChans();
119 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
120 SyncChannel(i->second, bs);
123 FOREACH_MOD(OnSyncNetwork, (bs.server));
124 this->WriteLine(CmdBuilder("ENDBURST"));
125 ServerInstance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+ s->GetName()+"\2.");
127 this->burstsent = true;
130 void TreeSocket::SendServerInfo(TreeServer* from)
132 // Send public version string
133 this->WriteLine(CommandSInfo::Builder(from, "version", from->GetVersion()));
135 // Send full version string that contains more information and is shown to opers
136 this->WriteLine(CommandSInfo::Builder(from, "fullversion", from->GetFullVersion()));
139 /** Recursively send the server tree.
140 * This is used during network burst to inform the other server
141 * (and any of ITS servers too) of what servers we know about.
142 * If at any point any of these servers already exist on the other
143 * end, our connection may be terminated.
145 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s)
147 SendServerInfo(Current);
149 const TreeServer::ChildServers& children = Current->GetChildren();
150 for (TreeServer::ChildServers::const_iterator i = children.begin(); i != children.end(); ++i)
152 TreeServer* recursive_server = *i;
153 if (recursive_server != s)
155 this->WriteLine(CommandServer::Builder(recursive_server));
156 /* down to next level */
157 this->SendServers(recursive_server, s);
162 /** Send one or more FJOINs for a channel of users.
163 * If the length of a single line is more than 480-NICKMAX
164 * in length, it is split over multiple lines.
165 * Send one or more FMODEs for a channel with the
166 * channel bans, if there's any.
168 void TreeSocket::SendFJoins(Channel* c)
170 CommandFJoin::Builder fjoin(c);
172 const Channel::MemberMap& ulist = c->GetUsers();
173 for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
175 Membership* memb = i->second;
176 if (!fjoin.has_room(memb))
178 // No room for this user, send the line and prepare a new one
179 this->WriteLine(fjoin.finalize());
184 this->WriteLine(fjoin.finalize());
187 /** Send all XLines we know about */
188 void TreeSocket::SendXLines()
190 std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
192 for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
194 /* Expired lines are removed in XLineManager::GetAll() */
195 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
197 /* lookup cannot be NULL in this case but a check won't hurt */
200 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
202 /* Is it burstable? this is better than an explicit check for type 'K'.
203 * We break the loop as NONE of the items in this group are worth iterating.
205 if (!i->second->IsBurstable())
208 this->WriteLine(CommandAddLine::Builder(i->second));
214 void TreeSocket::SendListModes(Channel* chan)
216 FModeBuilder fmode(chan);
217 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
218 for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
220 ListModeBase* mh = *i;
221 ListModeBase::ModeList* list = mh->GetList(chan);
225 // Add all items on the list to the FMODE, send it whenever it becomes too long
226 const char modeletter = mh->GetModeChar();
227 for (ListModeBase::ModeList::const_iterator j = list->begin(); j != list->end(); ++j)
229 const std::string& mask = j->mask;
230 if (!fmode.has_room(mask))
232 // No room for this mask, send the current line as-is then add the mask to a
233 // new, empty FMODE message
234 this->WriteLine(fmode.finalize());
237 fmode.push_mode(modeletter, mask);
242 this->WriteLine(fmode.finalize());
245 /** Send channel topic, modes and metadata */
246 void TreeSocket::SyncChannel(Channel* chan, BurstState& bs)
250 // If the topic was ever set, send it, even if it's empty now
251 // because a new empty topic should override an old non-empty topic
252 if (chan->topicset != 0)
253 this->WriteLine(CommandFTopic::Builder(chan));
257 for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
259 ExtensionItem* item = i->first;
260 std::string value = item->serialize(FORMAT_NETWORK, chan, i->second);
262 this->WriteLine(CommandMetadata::Builder(chan, item->name, value));
265 FOREACH_MOD(OnSyncChannel, (chan, bs.server));
268 void TreeSocket::SyncChannel(Channel* chan)
271 SyncChannel(chan, bs);
274 /** send all users and their oper state/modes */
275 void TreeSocket::SendUsers(BurstState& bs)
277 ProtocolInterface::Server& piserver = bs.server;
279 const user_hash& users = ServerInstance->Users->GetUsers();
280 for (user_hash::const_iterator u = users.begin(); u != users.end(); ++u)
282 User* user = u->second;
283 if (user->registered != REG_ALL)
286 this->WriteLine(CommandUID::Builder(user));
289 this->WriteLine(CommandOpertype::Builder(user));
292 this->WriteLine(CommandAway::Builder(user));
294 const Extensible::ExtensibleStore& exts = user->GetExtList();
295 for (Extensible::ExtensibleStore::const_iterator i = exts.begin(); i != exts.end(); ++i)
297 ExtensionItem* item = i->first;
298 std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
300 this->WriteLine(CommandMetadata::Builder(user, item->name, value));
303 FOREACH_MOD(OnSyncUser, (user, piserver));