- char data[MAXBUF];
- std::string buffer;
- std::string n = this->Instance->Config->GetSID();
- const char* sn = n.c_str();
- /* Yes, these arent too nice looking, but they get the job done */
- for (std::vector<ZLine*>::iterator i = Instance->XLines->zlines.begin(); i != Instance->XLines->zlines.end(); i++)
- {
- snprintf(data,MAXBUF,":%s ADDLINE Z %s %s %lu %lu :%s\r\n",sn,(*i)->ipaddr,(*i)->source,(unsigned long)(*i)->set_time,(unsigned long)(*i)->duration,(*i)->reason);
- buffer.append(data);
- }
- for (std::vector<QLine*>::iterator i = Instance->XLines->qlines.begin(); i != Instance->XLines->qlines.end(); i++)
- {
- snprintf(data,MAXBUF,":%s ADDLINE Q %s %s %lu %lu :%s\r\n",sn,(*i)->nick,(*i)->source,(unsigned long)(*i)->set_time,(unsigned long)(*i)->duration,(*i)->reason);
- buffer.append(data);
- }
- for (std::vector<GLine*>::iterator i = Instance->XLines->glines.begin(); i != Instance->XLines->glines.end(); i++)
- {
- snprintf(data,MAXBUF,":%s ADDLINE G %s@%s %s %lu %lu :%s\r\n",sn,(*i)->identmask,(*i)->hostmask,(*i)->source,(unsigned long)(*i)->set_time,(unsigned long)(*i)->duration,(*i)->reason);
- buffer.append(data);
- }
- for (std::vector<ELine*>::iterator i = Instance->XLines->elines.begin(); i != Instance->XLines->elines.end(); i++)
- {
- snprintf(data,MAXBUF,":%s ADDLINE E %s@%s %s %lu %lu :%s\r\n",sn,(*i)->identmask,(*i)->hostmask,(*i)->source,(unsigned long)(*i)->set_time,(unsigned long)(*i)->duration,(*i)->reason);
- buffer.append(data);
- }
- for (std::vector<ZLine*>::iterator i = Instance->XLines->pzlines.begin(); i != Instance->XLines->pzlines.end(); i++)
- {
- snprintf(data,MAXBUF,":%s ADDLINE Z %s %s %lu %lu :%s\r\n",sn,(*i)->ipaddr,(*i)->source,(unsigned long)(*i)->set_time,(unsigned long)(*i)->duration,(*i)->reason);
- buffer.append(data);
- }
- for (std::vector<QLine*>::iterator i = Instance->XLines->pqlines.begin(); i != Instance->XLines->pqlines.end(); i++)
- {
- snprintf(data,MAXBUF,":%s ADDLINE Q %s %s %lu %lu :%s\r\n",sn,(*i)->nick,(*i)->source,(unsigned long)(*i)->set_time,(unsigned long)(*i)->duration,(*i)->reason);
- buffer.append(data);
- }
- for (std::vector<GLine*>::iterator i = Instance->XLines->pglines.begin(); i != Instance->XLines->pglines.end(); i++)
- {
- snprintf(data,MAXBUF,":%s ADDLINE G %s@%s %s %lu %lu :%s\r\n",sn,(*i)->identmask,(*i)->hostmask,(*i)->source,(unsigned long)(*i)->set_time,(unsigned long)(*i)->duration,(*i)->reason);
- buffer.append(data);
- }
- for (std::vector<ELine*>::iterator i = Instance->XLines->pelines.begin(); i != Instance->XLines->pelines.end(); i++)
- {
- snprintf(data,MAXBUF,":%s ADDLINE E %s@%s %s %lu %lu :%s\r\n",sn,(*i)->identmask,(*i)->hostmask,(*i)->source,(unsigned long)(*i)->set_time,(unsigned long)(*i)->duration,(*i)->reason);
- buffer.append(data);
- }
-
- if (!buffer.empty())
- this->WriteLine(buffer);
-}
-
-/** Send channel modes and topics */
-void TreeSocket::SendChannelModes(TreeServer* Current)
-{
- char data[MAXBUF];
- std::deque<std::string> list;
- std::string n = this->Instance->Config->GetSID();
- const char* sn = n.c_str();
- Instance->Log(DEBUG,"Sending channels and modes, %d to send", this->Instance->chanlist->size());
- for (chan_hash::iterator c = this->Instance->chanlist->begin(); c != this->Instance->chanlist->end(); c++)
- {
- SendFJoins(Current, c->second);
- if (*c->second->topic)
- {
- snprintf(data,MAXBUF,":%s FTOPIC %s %lu %s :%s",sn,c->second->name,(unsigned long)c->second->topicset,c->second->setby,c->second->topic);
- this->WriteLine(data);
- }
- FOREACH_MOD_I(this->Instance,I_OnSyncChannel,OnSyncChannel(c->second,(Module*)Utils->Creator,(void*)this));
- list.clear();
- c->second->GetExtList(list);
- for (unsigned int j = 0; j < list.size(); j++)
- {
- FOREACH_MOD_I(this->Instance,I_OnSyncChannelMetaData,OnSyncChannelMetaData(c->second,(Module*)Utils->Creator,(void*)this,list[j]));
- }
- }
-}
-
-/** send all users and their oper state/modes */
-void TreeSocket::SendUsers(TreeServer* Current)
-{
- char data[MAXBUF];
- std::deque<std::string> list;
- std::string dataline;
- for (user_hash::iterator u = this->Instance->clientlist->begin(); u != this->Instance->clientlist->end(); u++)
- {
- if (u->second->registered == REG_ALL)
- {
- TreeServer* theirserver = Utils->FindServer(u->second->server);
- if (theirserver)
- {
- snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s +%s %s %lu :%s", theirserver->GetID().c_str(), u->second->uuid,
- (unsigned long)u->second->age, u->second->nick, u->second->host, u->second->dhost,
- u->second->ident, u->second->FormatModes(), u->second->GetIPString(),
- (unsigned long)u->second->signon, u->second->fullname);
- this->WriteLine(data);
- if (*u->second->oper)
- {
- snprintf(data,MAXBUF,":%s OPERTYPE %s", u->second->uuid, u->second->oper);
- this->WriteLine(data);
- }
- if (*u->second->awaymsg)
- {
- snprintf(data,MAXBUF,":%s AWAY :%s", u->second->uuid, u->second->awaymsg);
- this->WriteLine(data);
- }
- }
- }
- }
-
- for (user_hash::iterator u = this->Instance->clientlist->begin(); u != this->Instance->clientlist->end(); u++)
- {
- FOREACH_MOD_I(this->Instance,I_OnSyncUser,OnSyncUser(u->second,(Module*)Utils->Creator,(void*)this));
- list.clear();
- u->second->GetExtList(list);
- for (unsigned int j = 0; j < list.size(); j++)
- {
- FOREACH_MOD_I(this->Instance,I_OnSyncUserMetaData,OnSyncUserMetaData(u->second,(Module*)Utils->Creator,(void*)this,list[j]));
- }
- }
-}
-
-/** This function is called when we want to send a netburst to a local
- * server. There is a set order we must do this, because for example
- * users require their servers to exist, and channels require their
- * users to exist. You get the idea.
- */
-void TreeSocket::DoBurst(TreeServer* s)
-{
- std::string name = s->GetName();
- std::string burst = "BURST "+ConvToStr(Instance->Time(true));
- std::string endburst = "ENDBURST";
- this->Instance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (Authentication: %s).", name.c_str(), this->GetTheirChallenge().empty() ? "plaintext password" : "SHA256-HMAC challenge-response");
- this->WriteLine(burst);
- /* send our version string */
- this->WriteLine(std::string(":")+this->Instance->Config->GetSID()+" VERSION :"+this->Instance->GetVersionString());
- /* Send server tree */
- this->SendServers(Utils->TreeRoot,s,1);
- /* Send users and their oper status */
- this->SendUsers(s);
- /* Send everything else (channel modes, xlines etc) */
- this->SendChannelModes(s);
- this->SendXLines(s);
- FOREACH_MOD_I(this->Instance,I_OnSyncOtherMetaData,OnSyncOtherMetaData((Module*)Utils->Creator,(void*)this));
- this->WriteLine(endburst);
- this->Instance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+name+"\2.");
-}
-
-/** This function is called when we receive data from a remote
- * server. We buffer the data in a std::string (it doesnt stay
- * there for long), reading using InspSocket::Read() which can
- * read up to 16 kilobytes in one operation.
- *
- * IF THIS FUNCTION RETURNS FALSE, THE CORE CLOSES AND DELETES
- * THE SOCKET OBJECT FOR US.
- */
-bool TreeSocket::OnDataReady()
-{
- char* data = this->Read();
- /* Check that the data read is a valid pointer and it has some content */
- if (data && *data)
- {
- this->in_buffer.append(data);
- /* While there is at least one new line in the buffer,
- * do something useful (we hope!) with it.
- */
- while (in_buffer.find("\n") != std::string::npos)
- {
- std::string ret = in_buffer.substr(0,in_buffer.find("\n")-1);
- in_buffer = in_buffer.substr(in_buffer.find("\n")+1,in_buffer.length()-in_buffer.find("\n"));
- /* Use rfind here not find, as theres more
- * chance of the \r being near the end of the
- * string, not the start.
- */
- if (ret.find("\r") != std::string::npos)
- ret = in_buffer.substr(0,in_buffer.find("\r")-1);
- /* Process this one, abort if it
- * didnt return true.
- */
- if (!this->ProcessLine(ret))
- {
- return false;
- }
- }
- return true;
- }
- /* EAGAIN returns an empty but non-NULL string, so this
- * evaluates to TRUE for EAGAIN but to FALSE for EOF.
- */
- return (data && !*data);