2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
30 #include "treesocket.h"
31 #include "treeserver.h"
36 * Creates FMODE messages, used only when syncing channels
38 class FModeBuilder : public CmdBuilder
40 static const size_t maxline = 480;
43 std::string::size_type startpos;
46 FModeBuilder(Channel* chan)
47 : CmdBuilder("FMODE"), modes(0)
49 push(chan->name).push_int(chan->age).push_raw(" +");
50 startpos = str().size();
53 /** Add a mode to the message
55 void push_mode(const char modeletter, const std::string& mask)
58 params.push_back(' ');
63 /** Remove all modes from the message
67 content.erase(startpos);
72 /** Prepare the message for sending, next mode can only be added after clear()
74 const std::string& finalize()
76 return push_raw(params);
79 /** Returns true if the given mask can be added to the message, false if the message
80 * has no room for the mask
82 bool has_room(const std::string& mask) const
84 return ((str().size() + params.size() + mask.size() + 2 <= maxline) &&
85 (modes < ServerInstance->Config->Limits.MaxModes));
88 /** Returns true if this message is empty (has no modes)
96 struct TreeSocket::BurstState
98 SpanningTreeProtocolInterface::Server server;
99 BurstState(TreeSocket* sock) : server(sock) { }
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.
107 void TreeSocket::DoBurst(TreeServer* s)
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);
119 // Introduce all users
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);
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.");
133 this->burstsent = true;
136 void TreeSocket::SendServerInfo(TreeServer* from)
138 // Send public version string
139 this->WriteLine(CommandSInfo::Builder(from, "version", from->GetVersion()));
141 // Send full version string that contains more information and is shown to opers
142 this->WriteLine(CommandSInfo::Builder(from, "fullversion", from->GetFullVersion()));
144 // Send the raw version string that just contains the base info
145 this->WriteLine(CommandSInfo::Builder(from, "rawversion", from->GetRawVersion()));
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.
154 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s)
156 SendServerInfo(Current);
158 const TreeServer::ChildServers& children = Current->GetChildren();
159 for (TreeServer::ChildServers::const_iterator i = children.begin(); i != children.end(); ++i)
161 TreeServer* recursive_server = *i;
162 if (recursive_server != s)
164 this->WriteLine(CommandServer::Builder(recursive_server));
165 /* down to next level */
166 this->SendServers(recursive_server, s);
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.
174 void TreeSocket::SendFJoins(Channel* c)
176 CommandFJoin::Builder fjoin(c);
178 const Channel::MemberMap& ulist = c->GetUsers();
179 for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
181 Membership* memb = i->second;
182 if (!fjoin.has_room(memb))
184 // No room for this user, send the line and prepare a new one
185 this->WriteLine(fjoin.finalize());
190 this->WriteLine(fjoin.finalize());
193 /** Send all XLines we know about */
194 void TreeSocket::SendXLines()
196 std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
198 for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
200 /* Expired lines are removed in XLineManager::GetAll() */
201 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
203 /* lookup cannot be NULL in this case but a check won't hurt */
206 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
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.
211 if (!i->second->IsBurstable())
214 this->WriteLine(CommandAddLine::Builder(i->second));
220 void TreeSocket::SendListModes(Channel* chan)
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)
226 ListModeBase* mh = *i;
227 ListModeBase::ModeList* list = mh->GetList(chan);
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)
235 const std::string& mask = j->mask;
236 if (!fmode.has_room(mask))
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());
243 fmode.push_mode(modeletter, mask);
248 this->WriteLine(fmode.finalize());
251 /** Send channel users, topic, modes and global metadata */
252 void TreeSocket::SyncChannel(Channel* chan, BurstState& bs)
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));
263 for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
265 ExtensionItem* item = i->first;
266 std::string value = item->ToNetwork(chan, i->second);
268 this->WriteLine(CommandMetadata::Builder(chan, item->name, value));
271 FOREACH_MOD_CUSTOM(Utils->Creator->GetSyncEventProvider(), ServerProtocol::SyncEventListener, OnSyncChannel, (chan, bs.server));
274 void TreeSocket::SyncChannel(Channel* chan)
277 SyncChannel(chan, bs);
280 /** Send all users and their state, including oper and away status and global metadata */
281 void TreeSocket::SendUsers(BurstState& bs)
283 const user_hash& users = ServerInstance->Users->GetUsers();
284 for (user_hash::const_iterator u = users.begin(); u != users.end(); ++u)
286 User* user = u->second;
287 if (user->registered != REG_ALL)
290 this->WriteLine(CommandUID::Builder(user));
293 this->WriteLine(CommandOpertype::Builder(user));
296 this->WriteLine(CommandAway::Builder(user));
298 const Extensible::ExtensibleStore& exts = user->GetExtList();
299 for (Extensible::ExtensibleStore::const_iterator i = exts.begin(); i != exts.end(); ++i)
301 ExtensionItem* item = i->first;
302 std::string value = item->ToNetwork(u->second, i->second);
304 this->WriteLine(CommandMetadata::Builder(user, item->name, value));
307 FOREACH_MOD_CUSTOM(Utils->Creator->GetSyncEventProvider(), ServerProtocol::SyncEventListener, OnSyncUser, (user, bs.server));