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"
30 #include "protocolinterface.h"
33 * Creates FMODE messages, used only when syncing channels
35 class FModeBuilder : public CmdBuilder
37 static const size_t maxline = 480;
40 std::string::size_type startpos;
43 FModeBuilder(Channel* chan)
44 : CmdBuilder("FMODE"), modes(0)
46 push(chan->name).push_int(chan->age).push_raw(" +");
47 startpos = str().size();
50 /** Add a mode to the message
52 void push_mode(const char modeletter, const std::string& mask)
55 params.push_back(' ');
60 /** Remove all modes from the message
64 content.erase(startpos);
69 /** Prepare the message for sending, next mode can only be added after clear()
71 const std::string& finalize()
73 return push_raw(params);
76 /** Returns true if the given mask can be added to the message, false if the message
77 * has no room for the mask
79 bool has_room(const std::string& mask) const
81 return ((str().size() + params.size() + mask.size() + 2 <= maxline) &&
82 (modes < ServerInstance->Config->Limits.MaxModes));
85 /** Returns true if this message is empty (has no modes)
93 struct TreeSocket::BurstState
95 SpanningTreeProtocolInterface::Server server;
96 BurstState(TreeSocket* sock) : server(sock) { }
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.
104 void TreeSocket::DoBurst(TreeServer* s)
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);
118 /* Send users and their oper status */
121 const chan_hash& chans = ServerInstance->GetChans();
122 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
123 SyncChannel(i->second, bs);
126 FOREACH_MOD(OnSyncNetwork, (bs.server));
127 this->WriteLine(":" + ServerInstance->Config->GetSID() + " ENDBURST");
128 ServerInstance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+ s->GetName()+"\2.");
131 /** Recursively send the server tree.
132 * This is used during network burst to inform the other server
133 * (and any of ITS servers too) of what servers we know about.
134 * If at any point any of these servers already exist on the other
135 * end, our connection may be terminated.
136 * The hopcount parameter (3rd) is deprecated, and is always 0.
138 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s)
140 const TreeServer::ChildServers& children = Current->GetChildren();
141 for (TreeServer::ChildServers::const_iterator i = children.begin(); i != children.end(); ++i)
143 TreeServer* recursive_server = *i;
144 if (recursive_server != s)
146 this->WriteLine(CommandServer::Builder(recursive_server));
147 this->WriteLine(":" + recursive_server->GetID() + " VERSION :" + recursive_server->GetVersion());
148 /* down to next level */
149 this->SendServers(recursive_server, s);
154 /** Send one or more FJOINs for a channel of users.
155 * If the length of a single line is more than 480-NICKMAX
156 * in length, it is split over multiple lines.
157 * Send one or more FMODEs for a channel with the
158 * channel bans, if there's any.
160 void TreeSocket::SendFJoins(Channel* c)
162 std::string line(":");
163 line.append(ServerInstance->Config->GetSID()).append(" FJOIN ").append(c->name).append(1, ' ').append(ConvToStr(c->age)).append(" +");
164 std::string::size_type erase_from = line.length();
165 line.append(c->ChanModes(true)).append(" :");
167 const UserMembList *ulist = c->GetUsers();
169 for (UserMembCIter i = ulist->begin(); i != ulist->end(); ++i)
171 const std::string& modestr = i->second->modes;
172 if ((line.length() + modestr.length() + UIDGenerator::UUID_LENGTH + 2) > 480)
174 this->WriteLine(line);
175 line.erase(erase_from);
178 line.append(modestr).append(1, ',').append(i->first->uuid).push_back(' ');
180 this->WriteLine(line);
183 /** Send all XLines we know about */
184 void TreeSocket::SendXLines()
186 std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
188 for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
190 /* Expired lines are removed in XLineManager::GetAll() */
191 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
193 /* lookup cannot be NULL in this case but a check won't hurt */
196 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
198 /* Is it burstable? this is better than an explicit check for type 'K'.
199 * We break the loop as NONE of the items in this group are worth iterating.
201 if (!i->second->IsBurstable())
204 this->WriteLine(CommandAddLine::Builder(i->second));
210 void TreeSocket::SendListModes(Channel* chan)
212 FModeBuilder fmode(chan);
213 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
214 for (ModeParser::ListModeList::const_iterator i = listmodes.begin(); i != listmodes.end(); ++i)
216 ListModeBase* mh = *i;
217 ListModeBase::ModeList* list = mh->GetList(chan);
221 // Add all items on the list to the FMODE, send it whenever it becomes too long
222 const char modeletter = mh->GetModeChar();
223 for (ListModeBase::ModeList::const_iterator j = list->begin(); j != list->end(); ++j)
225 const std::string& mask = j->mask;
226 if (!fmode.has_room(mask))
228 // No room for this mask, send the current line as-is then add the mask to a
229 // new, empty FMODE message
230 this->WriteLine(fmode.finalize());
233 fmode.push_mode(modeletter, mask);
238 this->WriteLine(fmode.finalize());
241 /** Send channel topic, modes and metadata */
242 void TreeSocket::SyncChannel(Channel* chan, BurstState& bs)
246 // If the topic was ever set, send it, even if it's empty now
247 // because a new empty topic should override an old non-empty topic
248 if (chan->topicset != 0)
249 this->WriteLine(CommandFTopic::Builder(chan));
253 for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
255 ExtensionItem* item = i->first;
256 std::string value = item->serialize(FORMAT_NETWORK, chan, i->second);
258 this->WriteLine(CommandMetadata::Builder(chan, item->name, value));
261 FOREACH_MOD(OnSyncChannel, (chan, bs.server));
264 void TreeSocket::SyncChannel(Channel* chan)
267 SyncChannel(chan, bs);
270 /** send all users and their oper state/modes */
271 void TreeSocket::SendUsers(BurstState& bs)
273 ProtocolInterface::Server& piserver = bs.server;
275 const user_hash& users = ServerInstance->Users->GetUsers();
276 for (user_hash::const_iterator u = users.begin(); u != users.end(); ++u)
278 User* user = u->second;
279 if (user->registered != REG_ALL)
282 this->WriteLine(CommandUID::Builder(user));
285 this->WriteLine(CommandOpertype::Builder(user));
288 this->WriteLine(CommandAway::Builder(user));
290 const Extensible::ExtensibleStore& exts = user->GetExtList();
291 for (Extensible::ExtensibleStore::const_iterator i = exts.begin(); i != exts.end(); ++i)
293 ExtensionItem* item = i->first;
294 std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
296 this->WriteLine(CommandMetadata::Builder(user, item->name, value));
299 FOREACH_MOD(OnSyncUser, (user, piserver));