1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "treesocket.h"
18 #include "treeserver.h"
21 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
24 /** This function is called when we want to send a netburst to a local
25 * server. There is a set order we must do this, because for example
26 * users require their servers to exist, and channels require their
27 * users to exist. You get the idea.
29 void TreeSocket::DoBurst(TreeServer* s)
31 std::string name = s->GetName();
32 std::string burst = ":" + this->ServerInstance->Config->GetSID() + " BURST " +ConvToStr(ServerInstance->Time());
33 std::string endburst = ":" + this->ServerInstance->Config->GetSID() + " ENDBURST";
34 this->ServerInstance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (Authentication: %s%s).",
36 this->auth_fingerprint ? "SSL Fingerprint and " : "",
37 this->auth_challenge ? "challenge-response" : "plaintext password");
38 this->CleanNegotiationInfo();
39 this->WriteLine(burst);
40 /* send our version string */
41 this->WriteLine(std::string(":")+this->ServerInstance->Config->GetSID()+" VERSION :"+this->ServerInstance->GetVersionString());
42 /* Send server tree */
43 this->SendServers(Utils->TreeRoot,s,1);
44 /* Send users and their oper status */
46 /* Send everything else (channel modes, xlines etc) */
47 this->SendChannelModes(s);
49 FOREACH_MOD_I(this->ServerInstance,I_OnSyncOtherMetaData,OnSyncOtherMetaData((Module*)Utils->Creator,(void*)this));
50 this->WriteLine(endburst);
51 this->ServerInstance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+name+"\2.");
54 /** Recursively send the server tree with distances as hops.
55 * This is used during network burst to inform the other server
56 * (and any of ITS servers too) of what servers we know about.
57 * If at any point any of these servers already exist on the other
58 * end, our connection may be terminated. The hopcounts given
59 * by this function are relative, this doesn't matter so long as
60 * they are all >1, as all the remote servers re-calculate them
61 * to be relative too, with themselves as hop 0.
63 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s, int hops)
66 for (unsigned int q = 0; q < Current->ChildCount(); q++)
68 TreeServer* recursive_server = Current->GetChild(q);
69 if (recursive_server != s)
71 snprintf(command,1024,":%s SERVER %s * %d %s :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,
72 recursive_server->GetID().c_str(),
73 recursive_server->GetDesc().c_str());
74 this->WriteLine(command);
75 this->WriteLine(":"+recursive_server->GetName()+" VERSION :"+recursive_server->GetVersion());
76 /* down to next level */
77 this->SendServers(recursive_server, s, hops+1);
82 /** Send one or more FJOINs for a channel of users.
83 * If the length of a single line is more than 480-NICKMAX
84 * in length, it is split over multiple lines.
86 void TreeSocket::SendFJoins(TreeServer* Current, Channel* c)
91 size_t curlen, headlen;
92 curlen = headlen = snprintf(list,MAXBUF,":%s FJOIN %s %lu +%s :",
93 this->ServerInstance->Config->GetSID().c_str(), c->name.c_str(), (unsigned long)c->age, c->ChanModes(true));
95 char* ptr = list + curlen;
96 bool looped_once = false;
98 CUList *ulist = c->GetUsers();
102 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
105 std::string modestr = this->ServerInstance->Modes->ModeString(i->first, c, false);
107 if ((curlen + modestr.length() + i->first->uuid.length() + 4) > 480)
109 // remove the final space
112 buffer.append(list).append("\r\n");
114 ptr = list + headlen;
118 ptrlen = snprintf(ptr, MAXBUF-curlen, "%s,%s ", modestr.c_str(), i->first->uuid.c_str());
128 // Okay, permanent channels will (of course) need this \r\n anyway, numusers check is if there
129 // actually were people in the channel (looped_once == true)
130 if (!looped_once || numusers > 0)
132 // remove the final space
135 buffer.append(list).append("\r\n");
139 for (BanList::iterator b = c->bans.begin(); b != c->bans.end(); b++)
141 int size = b->data.length() + 2;
142 int currsize = linesize + size;
146 params.append(" ").append(b->data);
149 if ((params.length() >= ServerInstance->Config->Limits.MaxModes) || (currsize > 350))
151 /* Wrap at MAXMODES */
152 buffer.append(":").append(this->ServerInstance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(modes).append(params).append("\r\n");
159 /* Only send these if there are any */
161 buffer.append(":").append(this->ServerInstance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(modes).append(params);
163 this->WriteLine(buffer);
166 /** Send G, Q, Z and E lines */
167 void TreeSocket::SendXLines(TreeServer* Current)
170 std::string n = this->ServerInstance->Config->GetSID();
171 const char* sn = n.c_str();
173 std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
174 time_t current = ServerInstance->Time();
176 for (std::vector<std::string>::iterator it = types.begin(); it != types.end(); ++it)
178 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
182 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
184 /* Is it burstable? this is better than an explicit check for type 'K'.
185 * We break the loop as NONE of the items in this group are worth iterating.
187 if (!i->second->IsBurstable())
190 /* If it's expired, don't bother to burst it
192 if (i->second->duration && current > i->second->expiry)
195 snprintf(data,MAXBUF,":%s ADDLINE %s %s %s %lu %lu :%s",sn, it->c_str(), i->second->Displayable(),
197 (unsigned long)i->second->set_time,
198 (unsigned long)i->second->duration,
200 this->WriteLine(data);
206 /** Send channel modes and topics */
207 void TreeSocket::SendChannelModes(TreeServer* Current)
210 std::deque<std::string> list;
211 std::string n = this->ServerInstance->Config->GetSID();
212 const char* sn = n.c_str();
213 for (chan_hash::iterator c = this->ServerInstance->chanlist->begin(); c != this->ServerInstance->chanlist->end(); c++)
215 SendFJoins(Current, c->second);
216 if (!c->second->topic.empty())
218 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());
219 this->WriteLine(data);
221 FOREACH_MOD_I(this->ServerInstance,I_OnSyncChannel,OnSyncChannel(c->second,(Module*)Utils->Creator,(void*)this));
223 c->second->GetExtList(list);
224 for (unsigned int j = 0; j < list.size(); j++)
226 FOREACH_MOD_I(this->ServerInstance,I_OnSyncChannelMetaData,OnSyncChannelMetaData(c->second,(Module*)Utils->Creator,(void*)this,list[j]));
231 /** send all users and their oper state/modes */
232 void TreeSocket::SendUsers(TreeServer* Current)
235 std::deque<std::string> list;
236 std::string dataline;
237 for (user_hash::iterator u = this->ServerInstance->Users->clientlist->begin(); u != this->ServerInstance->Users->clientlist->end(); u++)
239 if (u->second->registered == REG_ALL)
241 TreeServer* theirserver = Utils->FindServer(u->second->server);
244 snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s %s %lu +%s :%s",
245 theirserver->GetID().c_str(), /* Prefix: SID */
246 u->second->uuid.c_str(), /* 0: UUID */
247 (unsigned long)u->second->age, /* 1: TS */
248 u->second->nick.c_str(), /* 2: Nick */
249 u->second->host.c_str(), /* 3: Displayed Host */
250 u->second->dhost.c_str(), /* 4: Real host */
251 u->second->ident.c_str(), /* 5: Ident */
252 u->second->GetIPString(), /* 6: IP string */
253 (unsigned long)u->second->signon, /* 7: Signon time for WHOWAS */
254 u->second->FormatModes(true), /* 8...n: Modes and params */
255 u->second->fullname.c_str()); /* size-1: GECOS */
256 this->WriteLine(data);
257 if (IS_OPER(u->second))
259 snprintf(data,MAXBUF,":%s OPERTYPE %s", u->second->uuid.c_str(), u->second->oper.c_str());
260 this->WriteLine(data);
262 if (IS_AWAY(u->second))
264 snprintf(data,MAXBUF,":%s AWAY :%s", u->second->uuid.c_str(), u->second->awaymsg.c_str());
265 this->WriteLine(data);
269 FOREACH_MOD_I(this->ServerInstance,I_OnSyncUser,OnSyncUser(u->second,(Module*)Utils->Creator,(void*)this));
271 u->second->GetExtList(list);
272 for (unsigned int j = 0; j < list.size(); j++)
274 FOREACH_MOD_I(this->ServerInstance,I_OnSyncUserMetaData,OnSyncUserMetaData(u->second,(Module*)Utils->Creator,(void*)this,list[j]));