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"
31 /** This function is called when we want to send a netburst to a local
32 * server. There is a set order we must do this, because for example
33 * users require their servers to exist, and channels require their
34 * users to exist. You get the idea.
36 void TreeSocket::DoBurst(TreeServer* s)
38 std::string servername = s->GetName();
39 ServerInstance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (Authentication: %s%s).",
41 capab->auth_fingerprint ? "SSL Fingerprint and " : "",
42 capab->auth_challenge ? "challenge-response" : "plaintext password");
43 this->CleanNegotiationInfo();
44 this->WriteLine(":" + ServerInstance->Config->GetSID() + " BURST " + ConvToStr(ServerInstance->Time()));
45 /* send our version string */
46 this->WriteLine(":" + ServerInstance->Config->GetSID() + " VERSION :"+ServerInstance->GetVersionString());
47 /* Send server tree */
48 this->SendServers(Utils->TreeRoot,s,1);
49 /* Send users and their oper status */
51 /* Send everything else (channel modes, xlines etc) */
52 this->SendChannelModes();
54 FOREACH_MOD(I_OnSyncNetwork,OnSyncNetwork(Utils->Creator,(void*)this));
55 this->WriteLine(":" + ServerInstance->Config->GetSID() + " ENDBURST");
56 ServerInstance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+ s->GetName()+"\2.");
59 /** Recursively send the server tree with distances as hops.
60 * This is used during network burst to inform the other server
61 * (and any of ITS servers too) of what servers we know about.
62 * If at any point any of these servers already exist on the other
63 * end, our connection may be terminated. The hopcounts given
64 * by this function are relative, this doesn't matter so long as
65 * they are all >1, as all the remote servers re-calculate them
66 * to be relative too, with themselves as hop 0.
68 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s, int hops)
71 for (unsigned int q = 0; q < Current->ChildCount(); q++)
73 TreeServer* recursive_server = Current->GetChild(q);
74 if (recursive_server != s)
76 std::string recursive_servername = recursive_server->GetName();
77 snprintf(command, MAXBUF, ":%s SERVER %s * %d %s :%s", Current->GetID().c_str(), recursive_servername.c_str(), hops,
78 recursive_server->GetID().c_str(),
79 recursive_server->GetDesc().c_str());
80 this->WriteLine(command);
81 this->WriteLine(":"+recursive_server->GetID()+" VERSION :"+recursive_server->GetVersion());
82 /* down to next level */
83 this->SendServers(recursive_server, s, hops+1);
88 /** Send one or more FJOINs for a channel of users.
89 * If the length of a single line is more than 480-NICKMAX
90 * in length, it is split over multiple lines.
91 * Send one or more FMODEs for a channel with the
92 * channel bans, if there's any.
94 void TreeSocket::SendFJoins(Channel* c)
96 std::string line(":");
97 line.append(ServerInstance->Config->GetSID()).append(" FJOIN ").append(c->name).append(1, ' ').append(ConvToStr(c->age)).append(" +");
98 std::string::size_type erase_from = line.length();
99 line.append(c->ChanModes(true)).append(" :");
101 const UserMembList *ulist = c->GetUsers();
103 for (UserMembCIter i = ulist->begin(); i != ulist->end(); ++i)
105 const std::string& modestr = i->second->modes;
106 if ((line.length() + modestr.length() + (UUID_LENGTH-1) + 2) > 480)
108 this->WriteLine(line);
109 line.erase(erase_from);
112 line.append(modestr).append(1, ',').append(i->first->uuid).push_back(' ');
114 this->WriteLine(line);
116 ModeReference ban(NULL, "ban");
117 static_cast<ListModeBase*>(*ban)->DoSyncChannel(c, Utils->Creator, this);
120 /** Send all XLines we know about */
121 void TreeSocket::SendXLines()
124 std::string n = ServerInstance->Config->GetSID();
125 const char* sn = n.c_str();
127 std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
129 for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
131 /* Expired lines are removed in XLineManager::GetAll() */
132 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
134 /* lookup cannot be NULL in this case but a check won't hurt */
137 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
139 /* Is it burstable? this is better than an explicit check for type 'K'.
140 * We break the loop as NONE of the items in this group are worth iterating.
142 if (!i->second->IsBurstable())
145 snprintf(data,MAXBUF,":%s ADDLINE %s %s %s %lu %lu :%s",sn, it->c_str(), i->second->Displayable(),
146 i->second->source.c_str(),
147 (unsigned long)i->second->set_time,
148 (unsigned long)i->second->duration,
149 i->second->reason.c_str());
150 this->WriteLine(data);
156 /** Send channel topic, modes and metadata */
157 void TreeSocket::SendChannelModes()
160 std::string n = ServerInstance->Config->GetSID();
161 const char* sn = n.c_str();
163 for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
165 SendFJoins(c->second);
166 if (!c->second->topic.empty())
168 snprintf(data,MAXBUF,":%s FTOPIC %s %lu %s :%s", sn, c->second->name.c_str(), (unsigned long)c->second->topicset, c->second->setby.c_str(), c->second->topic.c_str());
169 this->WriteLine(data);
172 for(Extensible::ExtensibleStore::const_iterator i = c->second->GetExtList().begin(); i != c->second->GetExtList().end(); i++)
174 ExtensionItem* item = i->first;
175 std::string value = item->serialize(FORMAT_NETWORK, c->second, i->second);
177 Utils->Creator->ProtoSendMetaData(this, c->second, item->name, value);
180 FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(c->second,Utils->Creator,this));
184 /** send all users and their oper state/modes */
185 void TreeSocket::SendUsers()
188 std::string dataline;
189 for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
191 if (u->second->registered == REG_ALL)
193 TreeServer* theirserver = Utils->FindServer(u->second->server);
196 snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s %s %lu +%s :%s",
197 theirserver->GetID().c_str(), /* Prefix: SID */
198 u->second->uuid.c_str(), /* 0: UUID */
199 (unsigned long)u->second->age, /* 1: TS */
200 u->second->nick.c_str(), /* 2: Nick */
201 u->second->host.c_str(), /* 3: Displayed Host */
202 u->second->dhost.c_str(), /* 4: Real host */
203 u->second->ident.c_str(), /* 5: Ident */
204 u->second->GetIPString().c_str(), /* 6: IP string */
205 (unsigned long)u->second->signon, /* 7: Signon time for WHOWAS */
206 u->second->FormatModes(true), /* 8...n: Modes and params */
207 u->second->fullname.c_str()); /* size-1: GECOS */
208 this->WriteLine(data);
209 if (u->second->IsOper())
211 snprintf(data,MAXBUF,":%s OPERTYPE %s", u->second->uuid.c_str(), u->second->oper->name.c_str());
212 this->WriteLine(data);
214 if (u->second->IsAway())
216 snprintf(data,MAXBUF,":%s AWAY %ld :%s", u->second->uuid.c_str(), (long)u->second->awaytime, u->second->awaymsg.c_str());
217 this->WriteLine(data);
221 for(Extensible::ExtensibleStore::const_iterator i = u->second->GetExtList().begin(); i != u->second->GetExtList().end(); i++)
223 ExtensionItem* item = i->first;
224 std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
226 Utils->Creator->ProtoSendMetaData(this, u->second, item->name, value);
229 FOREACH_MOD(I_OnSyncUser,OnSyncUser(u->second,Utils->Creator,this));