1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $ModDesc: Provides a spanning tree server link protocol */
17 #include "commands/cmd_whois.h"
18 #include "commands/cmd_stats.h"
22 #include "transport.h"
24 #include "m_spanningtree/timesynctimer.h"
25 #include "m_spanningtree/resolvers.h"
26 #include "m_spanningtree/main.h"
27 #include "m_spanningtree/utils.h"
28 #include "m_spanningtree/treeserver.h"
29 #include "m_spanningtree/link.h"
30 #include "m_spanningtree/treesocket.h"
31 #include "m_spanningtree/rconnect.h"
32 #include "m_spanningtree/rsquit.h"
34 /* $ModDep: m_spanningtree/timesynctimer.h m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h m_spanningtree/rconnect.h m_spanningtree/rsquit.h */
36 ModuleSpanningTree::ModuleSpanningTree(InspIRCd* Me)
37 : Module(Me), max_local(0), max_global(0)
39 ServerInstance->Modules->UseInterface("BufferedSocketHook");
40 Utils = new SpanningTreeUtilities(Me, this);
41 command_rconnect = new cmd_rconnect(ServerInstance, this, Utils);
42 ServerInstance->AddCommand(command_rconnect);
43 command_rsquit = new cmd_rsquit(ServerInstance, this, Utils);
44 ServerInstance->AddCommand(command_rsquit);
45 if (Utils->EnableTimeSync)
47 SyncTimer = new TimeSyncTimer(ServerInstance, this);
48 ServerInstance->Timers->AddTimer(SyncTimer);
53 RefreshTimer = new CacheRefreshTimer(ServerInstance, Utils);
54 ServerInstance->Timers->AddTimer(RefreshTimer);
57 void ModuleSpanningTree::ShowLinks(TreeServer* Current, User* user, int hops)
59 std::string Parent = Utils->TreeRoot->GetName();
60 if (Current->GetParent())
62 Parent = Current->GetParent()->GetName();
64 for (unsigned int q = 0; q < Current->ChildCount(); q++)
66 if ((Current->GetChild(q)->Hidden) || ((Utils->HideULines) && (ServerInstance->ULine(Current->GetChild(q)->GetName().c_str()))))
70 ShowLinks(Current->GetChild(q),user,hops+1);
75 ShowLinks(Current->GetChild(q),user,hops+1);
78 /* Don't display the line if its a uline, hide ulines is on, and the user isnt an oper */
79 if ((Utils->HideULines) && (ServerInstance->ULine(Current->GetName().c_str())) && (!IS_OPER(user)))
81 /* Or if the server is hidden and they're not an oper */
82 else if ((Current->Hidden) && (!IS_OPER(user)))
85 user->WriteServ("364 %s %s %s :%d %s", user->nick,Current->GetName().c_str(),
86 (Utils->FlatLinks && (!IS_OPER(user))) ? ServerInstance->Config->ServerName : Parent.c_str(),
87 (Utils->FlatLinks && (!IS_OPER(user))) ? 0 : hops,
88 Current->GetDesc().c_str());
91 int ModuleSpanningTree::CountLocalServs()
93 return Utils->TreeRoot->ChildCount();
96 int ModuleSpanningTree::CountServs()
98 return Utils->serverlist.size();
101 void ModuleSpanningTree::HandleLinks(const char** parameters, int pcnt, User* user)
103 ShowLinks(Utils->TreeRoot,user,0);
104 user->WriteServ("365 %s * :End of /LINKS list.",user->nick);
108 void ModuleSpanningTree::HandleLusers(const char** parameters, int pcnt, User* user)
110 unsigned int n_users = ServerInstance->UserCount();
112 /* Only update these when someone wants to see them, more efficient */
113 if ((unsigned int)ServerInstance->LocalUserCount() > max_local)
114 max_local = ServerInstance->LocalUserCount();
115 if (n_users > max_global)
116 max_global = n_users;
118 unsigned int ulined_count = 0;
119 unsigned int ulined_local_count = 0;
121 /* If ulined are hidden and we're not an oper, count the number of ulined servers hidden,
122 * locally and globally (locally means directly connected to us)
124 if ((Utils->HideULines) && (!*user->oper))
126 for (server_hash::iterator q = Utils->serverlist.begin(); q != Utils->serverlist.end(); q++)
128 if (ServerInstance->ULine(q->second->GetName().c_str()))
131 if (q->second->GetParent() == Utils->TreeRoot)
132 ulined_local_count++;
136 user->WriteServ("251 %s :There are %d users and %d invisible on %d servers",user->nick,
137 n_users-ServerInstance->ModeCount('i'),
138 ServerInstance->ModeCount('i'),
139 ulined_count ? this->CountServs() - ulined_count : this->CountServs());
141 if (ServerInstance->OperCount())
142 user->WriteServ("252 %s %d :operator(s) online",user->nick,ServerInstance->OperCount());
144 if (ServerInstance->UnregisteredUserCount())
145 user->WriteServ("253 %s %d :unknown connections",user->nick,ServerInstance->UnregisteredUserCount());
147 if (ServerInstance->ChannelCount())
148 user->WriteServ("254 %s %d :channels formed",user->nick,ServerInstance->ChannelCount());
150 user->WriteServ("255 %s :I have %d clients and %d servers",user->nick,ServerInstance->LocalUserCount(),ulined_local_count ? this->CountLocalServs() - ulined_local_count : this->CountLocalServs());
151 user->WriteServ("265 %s :Current Local Users: %d Max: %d",user->nick,ServerInstance->LocalUserCount(),max_local);
152 user->WriteServ("266 %s :Current Global Users: %d Max: %d",user->nick,n_users,max_global);
156 std::string ModuleSpanningTree::TimeToStr(time_t secs)
158 time_t mins_up = secs / 60;
159 time_t hours_up = mins_up / 60;
160 time_t days_up = hours_up / 24;
162 mins_up = mins_up % 60;
163 hours_up = hours_up % 24;
164 return ((days_up ? (ConvToStr(days_up) + "d") : std::string(""))
165 + (hours_up ? (ConvToStr(hours_up) + "h") : std::string(""))
166 + (mins_up ? (ConvToStr(mins_up) + "m") : std::string(""))
167 + ConvToStr(secs) + "s");
170 void ModuleSpanningTree::DoPingChecks(time_t curtime)
172 for (unsigned int j = 0; j < Utils->TreeRoot->ChildCount(); j++)
174 TreeServer* serv = Utils->TreeRoot->GetChild(j);
175 TreeSocket* sock = serv->GetSocket();
178 if (curtime >= serv->NextPingTime())
180 if (serv->AnsweredLastPing())
182 sock->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" PING "+serv->GetID());
183 serv->SetNextPingTime(curtime + Utils->PingFreq);
184 serv->LastPing = curtime;
186 gettimeofday(&t, NULL);
187 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
188 serv->LastPingMsec = ts;
189 serv->Warned = false;
193 /* they didnt answer, boot them */
194 sock->SendError("Ping timeout");
195 sock->Squit(serv,"Ping timeout");
196 ServerInstance->SE->DelFd(sock);
201 else if ((Utils->PingWarnTime) && (!serv->Warned) && (curtime >= serv->NextPingTime() - (Utils->PingFreq - Utils->PingWarnTime)) && (!serv->AnsweredLastPing()))
203 /* The server hasnt responded, send a warning to opers */
204 ServerInstance->SNO->WriteToSnoMask('l',"Server \002%s\002 has not responded to PING for %d seconds, high latency.", serv->GetName().c_str(), Utils->PingWarnTime);
210 /* Cancel remote burst mode on any servers which still have it enabled due to latency/lack of data.
211 * This prevents lost REMOTECONNECT notices
213 for (server_hash::iterator i = Utils->serverlist.begin(); i != Utils->serverlist.end(); i++)
214 Utils->SetRemoteBursting(i->second, false);
217 void ModuleSpanningTree::ConnectServer(Link* x)
220 QueryType start_type = DNS_QUERY_A;
222 start_type = DNS_QUERY_AAAA;
223 if (strchr(x->IPAddr.c_str(),':'))
226 if (inet_pton(AF_INET6, x->IPAddr.c_str(), &n) < 1)
233 if (inet_aton(x->IPAddr.c_str(),&n) < 1)
237 /* Do we already have an IP? If so, no need to resolve it. */
240 /* Gave a hook, but it wasnt one we know */
241 if ((!x->Hook.empty()) && (Utils->hooks.find(x->Hook.c_str()) == Utils->hooks.end()))
243 TreeSocket* newsocket = new TreeSocket(Utils, ServerInstance, x->IPAddr,x->Port,false,x->Timeout ? x->Timeout : 10,x->Name.c_str(), x->Bind, x->Hook.empty() ? NULL : Utils->hooks[x->Hook.c_str()]);
244 if (newsocket->GetFd() > -1)
246 /* Handled automatically on success */
250 RemoteMessage(NULL, "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(),strerror(errno));
251 if (ServerInstance->SocketCull.find(newsocket) == ServerInstance->SocketCull.end())
252 ServerInstance->SocketCull[newsocket] = newsocket;
253 Utils->DoFailOver(x);
261 ServernameResolver* snr = new ServernameResolver((Module*)this, Utils, ServerInstance,x->IPAddr, *x, cached, start_type);
262 ServerInstance->AddResolver(snr, cached);
264 catch (ModuleException& e)
266 RemoteMessage(NULL, "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(), e.GetReason());
267 Utils->DoFailOver(x);
272 void ModuleSpanningTree::AutoConnectServers(time_t curtime)
274 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
276 if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
278 x->NextConnectTime = curtime + x->AutoConnect;
279 TreeServer* CheckDupe = Utils->FindServer(x->Name.c_str());
280 if (x->FailOver.length())
282 TreeServer* CheckFailOver = Utils->FindServer(x->FailOver.c_str());
285 /* The failover for this server is currently a member of the network.
286 * The failover probably succeeded, where the main link did not.
287 * Don't try the main link until the failover is gone again.
294 // an autoconnected server is not connected. Check if its time to connect it
295 ServerInstance->SNO->WriteToSnoMask('l',"AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
296 this->ConnectServer(&(*x));
302 int ModuleSpanningTree::HandleVersion(const char** parameters, int pcnt, User* user)
304 // we've already checked if pcnt > 0, so this is safe
305 TreeServer* found = Utils->FindServerMask(parameters[0]);
308 std::string Version = found->GetVersion();
309 user->WriteServ("351 %s :%s",user->nick,Version.c_str());
310 if (found == Utils->TreeRoot)
312 ServerInstance->Config->Send005(user);
317 user->WriteServ("402 %s %s :No such server",user->nick,parameters[0]);
322 /* This method will attempt to get a link message out to as many people as is required.
323 * If a user is provided, and that user is local, then the user is sent the message using
324 * WriteServ (they are the local initiator of that message). If the user is remote, they are
325 * sent that message remotely via PUSH.
326 * If the user is NULL, then the notice is sent locally via WriteToSnoMask with snomask 'l',
327 * and remotely via SNONOTICE with mask 'l'.
329 void ModuleSpanningTree::RemoteMessage(User* user, const char* format, ...)
331 /* This could cause an infinite loop, because DoOneToMany() will, on error,
332 * call TreeSocket::OnError(), which in turn will call this function to
333 * notify everyone of the error. So, drop any messages that are generated
334 * during the sending of another message. -Special */
335 static bool SendingRemoteMessage = false;
336 if (SendingRemoteMessage)
338 SendingRemoteMessage = true;
340 std::deque<std::string> params;
344 va_start(argsPtr, format);
345 vsnprintf(text, MAXBUF, format, argsPtr);
350 /* No user, target it generically at everyone */
351 ServerInstance->SNO->WriteToSnoMask('l', "%s", text);
352 params.push_back("l");
353 params.push_back(std::string(":") + text);
354 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "SNONOTICE", params);
359 user->WriteServ("NOTICE %s :%s", user->nick, text);
362 params.push_back(user->uuid);
363 params.push_back(std::string("::") + ServerInstance->Config->ServerName + " NOTICE " + user->nick + " :*** From " +
364 ServerInstance->Config->ServerName+ ": " + text);
365 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "PUSH", params);
369 SendingRemoteMessage = false;
372 int ModuleSpanningTree::HandleConnect(const char** parameters, int pcnt, User* user)
374 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
376 if (ServerInstance->MatchText(x->Name.c_str(),parameters[0]))
378 TreeServer* CheckDupe = Utils->FindServer(x->Name.c_str());
381 RemoteMessage(user, "*** CONNECT: Connecting to server: \002%s\002 (%s:%d)",x->Name.c_str(),(x->HiddenFromStats ? "<hidden>" : x->IPAddr.c_str()),x->Port);
382 ConnectServer(&(*x));
387 RemoteMessage(user, "*** CONNECT: Server \002%s\002 already exists on the network and is connected via \002%s\002",x->Name.c_str(),CheckDupe->GetParent()->GetName().c_str());
392 RemoteMessage(user, "*** CONNECT: No server matching \002%s\002 could be found in the config file.",parameters[0]);
396 void ModuleSpanningTree::BroadcastTimeSync()
398 if (Utils->MasterTime)
400 std::deque<std::string> params;
401 params.push_back(ConvToStr(ServerInstance->Time(false)));
402 params.push_back("FORCE");
403 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "TIMESET", params);
407 void ModuleSpanningTree::OnGetServerDescription(const std::string &servername,std::string &description)
409 TreeServer* s = Utils->FindServer(servername);
412 description = s->GetDesc();
416 void ModuleSpanningTree::OnUserInvite(User* source,User* dest,Channel* channel)
418 if (IS_LOCAL(source))
420 std::deque<std::string> params;
421 params.push_back(dest->uuid);
422 params.push_back(channel->name);
423 Utils->DoOneToMany(source->uuid,"INVITE",params);
427 void ModuleSpanningTree::OnPostLocalTopicChange(User* user, Channel* chan, const std::string &topic)
429 std::deque<std::string> params;
430 params.push_back(chan->name);
431 params.push_back(":"+topic);
432 Utils->DoOneToMany(user->uuid,"TOPIC",params);
435 void ModuleSpanningTree::OnWallops(User* user, const std::string &text)
439 std::deque<std::string> params;
440 params.push_back(":"+text);
441 Utils->DoOneToMany(user->uuid,"WALLOPS",params);
445 void ModuleSpanningTree::OnUserNotice(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
447 if (target_type == TYPE_USER)
449 User* d = (User*)dest;
450 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
452 std::deque<std::string> params;
454 params.push_back(d->uuid);
455 params.push_back(":"+text);
456 Utils->DoOneToOne(user->uuid,"NOTICE",params,d->server);
459 else if (target_type == TYPE_CHANNEL)
463 Channel *c = (Channel*)dest;
466 std::string cname = c->name;
468 cname = status + cname;
470 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
471 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
473 TreeSocket* Sock = i->second->GetSocket();
475 Sock->WriteLine(":"+std::string(user->uuid)+" NOTICE "+cname+" :"+text);
480 else if (target_type == TYPE_SERVER)
484 char* target = (char*)dest;
485 std::deque<std::string> par;
486 par.push_back(target);
487 par.push_back(":"+text);
488 Utils->DoOneToMany(user->uuid,"NOTICE",par);
493 void ModuleSpanningTree::OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
495 if (target_type == TYPE_USER)
497 // route private messages which are targetted at clients only to the server
498 // which needs to receive them
499 User* d = (User*)dest;
500 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
502 std::deque<std::string> params;
504 params.push_back(d->uuid);
505 params.push_back(":"+text);
506 Utils->DoOneToOne(user->uuid,"PRIVMSG",params,d->server);
509 else if (target_type == TYPE_CHANNEL)
513 Channel *c = (Channel*)dest;
516 std::string cname = c->name;
518 cname = status + cname;
520 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
521 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
523 TreeSocket* Sock = i->second->GetSocket();
525 Sock->WriteLine(":"+std::string(user->uuid)+" PRIVMSG "+cname+" :"+text);
530 else if (target_type == TYPE_SERVER)
534 char* target = (char*)dest;
535 std::deque<std::string> par;
536 par.push_back(target);
537 par.push_back(":"+text);
538 Utils->DoOneToMany(user->uuid,"PRIVMSG",par);
543 void ModuleSpanningTree::OnBackgroundTimer(time_t curtime)
545 AutoConnectServers(curtime);
546 DoPingChecks(curtime);
549 void ModuleSpanningTree::OnUserJoin(User* user, Channel* channel, bool &silent)
551 // Only do this for local users
554 if (channel->GetUserCounter() == 1)
556 std::deque<std::string> params;
557 // set up their permissions and the channel TS with FJOIN.
558 // All users are FJOINed now, because a module may specify
559 // new joining permissions for the user.
560 params.push_back(channel->name);
561 params.push_back(ConvToStr(channel->age));
562 params.push_back(std::string(channel->GetAllPrefixChars(user))+","+std::string(user->uuid));
563 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FJOIN",params);
564 /* First user in, sync the modes for the channel */
566 params.push_back(channel->ChanModes(true));
567 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FMODE",params);
571 std::deque<std::string> params;
572 params.push_back(channel->name);
573 params.push_back(ConvToStr(channel->age));
574 Utils->DoOneToMany(user->uuid,"JOIN",params);
579 void ModuleSpanningTree::OnChangeHost(User* user, const std::string &newhost)
581 // only occurs for local clients
582 if (user->registered != REG_ALL)
584 std::deque<std::string> params;
585 params.push_back(newhost);
586 Utils->DoOneToMany(user->uuid,"FHOST",params);
589 void ModuleSpanningTree::OnChangeName(User* user, const std::string &gecos)
591 // only occurs for local clients
592 if (user->registered != REG_ALL)
594 std::deque<std::string> params;
595 params.push_back(gecos);
596 Utils->DoOneToMany(user->uuid,"FNAME",params);
599 void ModuleSpanningTree::OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
603 std::deque<std::string> params;
604 params.push_back(channel->name);
605 if (!partmessage.empty())
606 params.push_back(":"+partmessage);
607 Utils->DoOneToMany(user->uuid,"PART",params);
611 void ModuleSpanningTree::OnUserConnect(User* user)
615 std::deque<std::string> params;
616 params.push_back(user->uuid);
617 params.push_back(ConvToStr(user->age));
618 params.push_back(user->nick);
619 params.push_back(user->host);
620 params.push_back(user->dhost);
621 params.push_back(user->ident);
622 params.push_back("+"+std::string(user->FormatModes()));
623 params.push_back(user->GetIPString());
624 params.push_back(ConvToStr(user->signon));
625 params.push_back(":"+std::string(user->fullname));
626 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "UID", params);
627 // User is Local, change needs to be reflected!
628 TreeServer* SourceServer = Utils->FindServer(user->server);
631 SourceServer->AddUserCount();
636 void ModuleSpanningTree::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
638 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
640 std::deque<std::string> params;
642 if (oper_message != reason)
644 params.push_back(":"+oper_message);
645 Utils->DoOneToMany(user->uuid,"OPERQUIT",params);
648 params.push_back(":"+reason);
649 Utils->DoOneToMany(user->uuid,"QUIT",params);
651 // Regardless, We need to modify the user Counts..
652 TreeServer* SourceServer = Utils->FindServer(user->server);
655 SourceServer->DelUserCount();
659 void ModuleSpanningTree::OnUserPostNick(User* user, const std::string &oldnick)
663 std::deque<std::string> params;
664 params.push_back(user->nick);
666 /** IMPORTANT: We don't update the TS if the oldnick is just a case change of the newnick!
668 if (irc::string(user->nick) != assign(oldnick))
669 user->age = ServerInstance->Time(true);
671 params.push_back(ConvToStr(user->age));
672 Utils->DoOneToMany(user->uuid,"NICK",params);
676 void ModuleSpanningTree::OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
678 if ((source) && (IS_LOCAL(source)))
680 std::deque<std::string> params;
681 params.push_back(chan->name);
682 params.push_back(user->uuid);
683 params.push_back(":"+reason);
684 Utils->DoOneToMany(source->uuid,"KICK",params);
688 std::deque<std::string> params;
689 params.push_back(chan->name);
690 params.push_back(user->uuid);
691 params.push_back(":"+reason);
692 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"KICK",params);
696 void ModuleSpanningTree::OnRemoteKill(User* source, User* dest, const std::string &reason, const std::string &operreason)
698 std::deque<std::string> params;
699 params.push_back(":"+reason);
700 Utils->DoOneToMany(dest->uuid,"OPERQUIT",params);
702 params.push_back(dest->uuid);
703 params.push_back(":"+reason);
704 dest->SetOperQuit(operreason);
705 Utils->DoOneToMany(source->uuid,"KILL",params);
708 void ModuleSpanningTree::OnRehash(User* user, const std::string ¶meter)
710 if (!parameter.empty())
712 std::deque<std::string> params;
713 params.push_back(parameter);
714 Utils->DoOneToMany(user ? user->nick : ServerInstance->Config->GetSID(), "REHASH", params);
716 if (ServerInstance->MatchText(ServerInstance->Config->ServerName,parameter))
718 ServerInstance->WriteOpers("*** Remote rehash initiated locally by \002%s\002", user ? user->nick : ServerInstance->Config->ServerName);
719 ServerInstance->RehashServer();
722 Utils->ReadConfiguration(false);
723 InitializeDisabledCommands(ServerInstance->Config->DisabledCommands, ServerInstance);
726 // note: the protocol does not allow direct umode +o except
727 // via NICK with 8 params. sending OPERTYPE infers +o modechange
729 void ModuleSpanningTree::OnOper(User* user, const std::string &opertype)
733 std::deque<std::string> params;
734 params.push_back(opertype);
735 Utils->DoOneToMany(user->uuid,"OPERTYPE",params);
739 void ModuleSpanningTree::OnAddLine(XLine* line, User* user)
742 snprintf(data,MAXBUF,"%s %s %s %lu %lu :%s", line->type.c_str(), line->Displayable(), ServerInstance->Config->ServerName, line->set_time,
743 line->duration, line->reason);
744 std::deque<std::string> params;
745 params.push_back(data);
749 /* Server-set lines */
750 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "ADDLINE", params);
752 else if (IS_LOCAL(user))
755 Utils->DoOneToMany(user->uuid, "ADDLINE", params);
759 void ModuleSpanningTree::OnDelLine(XLine* line, User* user)
762 snprintf(data,MAXBUF,"%s %s", line->type.c_str(), line->Displayable());
763 std::deque<std::string> params;
764 params.push_back(data);
768 /* Server-unset lines */
769 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "DELLINE", params);
771 else if (IS_LOCAL(user))
773 /* User-unset lines */
774 Utils->DoOneToMany(user->uuid, "DELLINE", params);
778 void ModuleSpanningTree::OnMode(User* user, void* dest, int target_type, const std::string &text)
780 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
782 std::deque<std::string> params;
784 std::string output_text;
786 ServerInstance->Parser->TranslateUIDs(TR_SPACENICKLIST, text, output_text);
788 if (target_type == TYPE_USER)
790 User* u = (User*)dest;
791 params.push_back(u->uuid);
792 params.push_back(output_text);
797 Channel* c = (Channel*)dest;
798 params.push_back(c->name);
799 params.push_back(ConvToStr(c->age));
800 params.push_back(output_text);
804 Utils->DoOneToMany(user->uuid, command, params);
808 void ModuleSpanningTree::OnSetAway(User* user)
812 std::deque<std::string> params;
813 params.push_back(":"+std::string(user->awaymsg));
814 Utils->DoOneToMany(user->uuid,"AWAY",params);
818 void ModuleSpanningTree::OnCancelAway(User* user)
822 std::deque<std::string> params;
824 Utils->DoOneToMany(user->uuid,"AWAY",params);
828 void ModuleSpanningTree::ProtoSendMode(void* opaque, int target_type, void* target, const std::string &modeline)
830 TreeSocket* s = (TreeSocket*)opaque;
831 std::string output_text;
833 ServerInstance->Parser->TranslateUIDs(TR_SPACENICKLIST, modeline, output_text);
837 if (target_type == TYPE_USER)
839 User* u = (User*)target;
840 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" FMODE "+u->uuid+" "+ConvToStr(u->age)+" "+output_text);
844 Channel* c = (Channel*)target;
845 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" FMODE "+c->name+" "+ConvToStr(c->age)+" "+output_text);
850 void ModuleSpanningTree::ProtoSendMetaData(void* opaque, int target_type, void* target, const std::string &extname, const std::string &extdata)
852 TreeSocket* s = (TreeSocket*)opaque;
855 if (target_type == TYPE_USER)
857 User* u = (User*)target;
858 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+u->uuid+" "+extname+" :"+extdata);
860 else if (target_type == TYPE_CHANNEL)
862 Channel* c = (Channel*)target;
863 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+c->name+" "+extname+" :"+extdata);
866 if (target_type == TYPE_OTHER)
868 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA * "+extname+" :"+extdata);
872 void ModuleSpanningTree::OnEvent(Event* event)
874 std::deque<std::string>* params = (std::deque<std::string>*)event->GetData();
875 if (event->GetEventID() == "send_metadata")
877 if (params->size() < 3)
879 (*params)[2] = ":" + (*params)[2];
880 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"METADATA",*params);
882 else if (event->GetEventID() == "send_topic")
884 if (params->size() < 2)
886 (*params)[1] = ":" + (*params)[1];
887 params->insert(params->begin() + 1,ServerInstance->Config->ServerName);
888 params->insert(params->begin() + 1,ConvToStr(ServerInstance->Time(true)));
889 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FTOPIC",*params);
891 else if (event->GetEventID() == "send_mode")
893 if (params->size() < 2)
895 // Insert the TS value of the object, either User or Channel
897 std::string output_text;
899 /* Warning: in-place translation is only safe for type TR_NICK */
900 for (size_t n = 0; n < params->size(); n++)
901 ServerInstance->Parser->TranslateUIDs(TR_NICK, (*params)[n], (*params)[n]);
903 User* a = ServerInstance->FindNick((*params)[0]);
907 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"MODE",*params);
912 Channel* a = ServerInstance->FindChan((*params)[0]);
916 params->insert(params->begin() + 1,ConvToStr(ourTS));
917 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FMODE",*params);
921 else if (event->GetEventID() == "send_mode_explicit")
923 if (params->size() < 2)
925 std::string output_text;
927 /* Warning: in-place translation is only safe for type TR_NICK */
928 for (size_t n = 0; n < params->size(); n++)
929 ServerInstance->Parser->TranslateUIDs(TR_NICK, (*params)[n], (*params)[n]);
931 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"MODE",*params);
933 else if (event->GetEventID() == "send_opers")
935 if (params->size() < 1)
937 (*params)[0] = ":" + (*params)[0];
938 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"OPERNOTICE",*params);
940 else if (event->GetEventID() == "send_modeset")
942 if (params->size() < 2)
944 (*params)[1] = ":" + (*params)[1];
945 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"MODENOTICE",*params);
947 else if (event->GetEventID() == "send_snoset")
949 if (params->size() < 2)
951 (*params)[1] = ":" + (*params)[1];
952 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"SNONOTICE",*params);
954 else if (event->GetEventID() == "send_push")
956 if (params->size() < 2)
959 User *a = ServerInstance->FindNick((*params)[0]);
964 (*params)[0] = a->uuid;
965 (*params)[1] = ":" + (*params)[1];
966 Utils->DoOneToOne(ServerInstance->Config->GetSID(), "PUSH", *params, a->server);
970 ModuleSpanningTree::~ModuleSpanningTree()
972 /* This will also free the listeners */
975 ServerInstance->Timers->DelTimer(SyncTimer);
977 ServerInstance->Timers->DelTimer(RefreshTimer);
979 ServerInstance->Modules->DoneWithInterface("BufferedSocketHook");
982 Version ModuleSpanningTree::GetVersion()
984 return Version(1,1,0,2,VF_VENDOR,API_VERSION);
987 void ModuleSpanningTree::Implements(char* List)
989 List[I_OnPreCommand] = List[I_OnGetServerDescription] = List[I_OnUserInvite] = List[I_OnPostLocalTopicChange] = 1;
990 List[I_OnWallops] = List[I_OnUserNotice] = List[I_OnUserMessage] = List[I_OnBackgroundTimer] = 1;
991 List[I_OnUserJoin] = List[I_OnChangeHost] = List[I_OnChangeName] = List[I_OnUserPart] = List[I_OnUserConnect] = 1;
992 List[I_OnUserQuit] = List[I_OnUserPostNick] = List[I_OnUserKick] = List[I_OnRemoteKill] = List[I_OnRehash] = 1;
993 List[I_OnOper] = List[I_OnAddLine] = List[I_OnDelLine] = List[I_ProtoSendMode] = List[I_OnMode] = 1;
994 List[I_OnStats] = List[I_ProtoSendMetaData] = List[I_OnEvent] = List[I_OnSetAway] = List[I_OnCancelAway] = List[I_OnPostCommand] = 1;
997 /* It is IMPORTANT that m_spanningtree is the last module in the chain
998 * so that any activity it sees is FINAL, e.g. we arent going to send out
999 * a NICK message before m_cloaking has finished putting the +x on the user,
1001 * Therefore, we return PRIORITY_LAST to make sure we end up at the END of
1002 * the module call queue.
1004 Priority ModuleSpanningTree::Prioritize()
1006 return PRIORITY_LAST;
1009 MODULE_INIT(ModuleSpanningTree)