1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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"
33 #include "m_spanningtree/protocolinterface.h"
35 /* $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 m_spanningtree/protocolinterface.h */
37 ModuleSpanningTree::ModuleSpanningTree(InspIRCd* Me)
38 : Module(Me), max_local(0), max_global(0)
40 ServerInstance->Modules->UseInterface("BufferedSocketHook");
41 Utils = new SpanningTreeUtilities(ServerInstance, this);
42 command_rconnect = new cmd_rconnect(ServerInstance, this, Utils);
43 ServerInstance->AddCommand(command_rconnect);
44 command_rsquit = new cmd_rsquit(ServerInstance, this, Utils);
45 ServerInstance->AddCommand(command_rsquit);
46 RefreshTimer = new CacheRefreshTimer(ServerInstance, Utils);
47 ServerInstance->Timers->AddTimer(RefreshTimer);
49 Implementation eventlist[] =
51 I_OnPreCommand, I_OnGetServerDescription, I_OnUserInvite, I_OnPostLocalTopicChange,
52 I_OnWallops, I_OnUserNotice, I_OnUserMessage, I_OnBackgroundTimer,
53 I_OnUserJoin, I_OnChangeHost, I_OnChangeName, I_OnUserPart, I_OnPostConnect,
54 I_OnUserQuit, I_OnUserPostNick, I_OnUserKick, I_OnRemoteKill, I_OnRehash,
55 I_OnOper, I_OnAddLine, I_OnDelLine, I_ProtoSendMode, I_OnMode,
56 I_OnStats, I_ProtoSendMetaData, I_OnEvent, I_OnSetAway, I_OnPostCommand
58 ServerInstance->Modules->Attach(eventlist, this, 28);
60 delete ServerInstance->PI;
61 ServerInstance->PI = new SpanningTreeProtocolInterface(this, Utils, ServerInstance);
63 for (std::vector<User*>::const_iterator i = ServerInstance->Users->local_users.begin(); i != ServerInstance->Users->local_users.end(); i++)
65 this->OnPostConnect((*i));
69 void ModuleSpanningTree::ShowLinks(TreeServer* Current, User* user, int hops)
71 std::string Parent = Utils->TreeRoot->GetName();
72 if (Current->GetParent())
74 Parent = Current->GetParent()->GetName();
76 for (unsigned int q = 0; q < Current->ChildCount(); q++)
78 if ((Current->GetChild(q)->Hidden) || ((Utils->HideULines) && (ServerInstance->ULine(Current->GetChild(q)->GetName().c_str()))))
82 ShowLinks(Current->GetChild(q),user,hops+1);
87 ShowLinks(Current->GetChild(q),user,hops+1);
90 /* Don't display the line if its a uline, hide ulines is on, and the user isnt an oper */
91 if ((Utils->HideULines) && (ServerInstance->ULine(Current->GetName().c_str())) && (!IS_OPER(user)))
93 /* Or if the server is hidden and they're not an oper */
94 else if ((Current->Hidden) && (!IS_OPER(user)))
97 user->WriteNumeric(364, "%s %s %s :%d %s", user->nick,Current->GetName().c_str(),
98 (Utils->FlatLinks && (!IS_OPER(user))) ? ServerInstance->Config->ServerName : Parent.c_str(),
99 (Utils->FlatLinks && (!IS_OPER(user))) ? 0 : hops,
100 Current->GetDesc().c_str());
103 int ModuleSpanningTree::CountLocalServs()
105 return Utils->TreeRoot->ChildCount();
108 int ModuleSpanningTree::CountServs()
110 return Utils->serverlist.size();
113 void ModuleSpanningTree::HandleLinks(const char* const* parameters, int pcnt, User* user)
115 ShowLinks(Utils->TreeRoot,user,0);
116 user->WriteNumeric(365, "%s * :End of /LINKS list.",user->nick);
120 void ModuleSpanningTree::HandleLusers(const char* const* parameters, int pcnt, User* user)
122 unsigned int n_users = ServerInstance->Users->UserCount();
124 /* Only update these when someone wants to see them, more efficient */
125 if ((unsigned int)ServerInstance->Users->LocalUserCount() > max_local)
126 max_local = ServerInstance->Users->LocalUserCount();
127 if (n_users > max_global)
128 max_global = n_users;
130 unsigned int ulined_count = 0;
131 unsigned int ulined_local_count = 0;
133 /* If ulined are hidden and we're not an oper, count the number of ulined servers hidden,
134 * locally and globally (locally means directly connected to us)
136 if ((Utils->HideULines) && (!*user->oper))
138 for (server_hash::iterator q = Utils->serverlist.begin(); q != Utils->serverlist.end(); q++)
140 if (ServerInstance->ULine(q->second->GetName().c_str()))
143 if (q->second->GetParent() == Utils->TreeRoot)
144 ulined_local_count++;
148 user->WriteNumeric(251, "%s :There are %d users and %d invisible on %d servers",user->nick,
149 n_users-ServerInstance->Users->ModeCount('i'),
150 ServerInstance->Users->ModeCount('i'),
151 ulined_count ? this->CountServs() - ulined_count : this->CountServs());
153 if (ServerInstance->Users->OperCount())
154 user->WriteNumeric(252, "%s %d :operator(s) online",user->nick,ServerInstance->Users->OperCount());
156 if (ServerInstance->Users->UnregisteredUserCount())
157 user->WriteNumeric(253, "%s %d :unknown connections",user->nick,ServerInstance->Users->UnregisteredUserCount());
159 if (ServerInstance->ChannelCount())
160 user->WriteNumeric(254, "%s %ld :channels formed",user->nick,ServerInstance->ChannelCount());
162 user->WriteNumeric(255, "%s :I have %d clients and %d servers",user->nick,ServerInstance->Users->LocalUserCount(),ulined_local_count ? this->CountLocalServs() - ulined_local_count : this->CountLocalServs());
163 user->WriteNumeric(265, "%s :Current Local Users: %d Max: %d",user->nick,ServerInstance->Users->LocalUserCount(),max_local);
164 user->WriteNumeric(266, "%s :Current Global Users: %d Max: %d",user->nick,n_users,max_global);
168 std::string ModuleSpanningTree::TimeToStr(time_t secs)
170 time_t mins_up = secs / 60;
171 time_t hours_up = mins_up / 60;
172 time_t days_up = hours_up / 24;
174 mins_up = mins_up % 60;
175 hours_up = hours_up % 24;
176 return ((days_up ? (ConvToStr(days_up) + "d") : std::string(""))
177 + (hours_up ? (ConvToStr(hours_up) + "h") : std::string(""))
178 + (mins_up ? (ConvToStr(mins_up) + "m") : std::string(""))
179 + ConvToStr(secs) + "s");
182 void ModuleSpanningTree::DoPingChecks(time_t curtime)
184 for (unsigned int j = 0; j < Utils->TreeRoot->ChildCount(); j++)
186 TreeServer* serv = Utils->TreeRoot->GetChild(j);
187 TreeSocket* sock = serv->GetSocket();
190 if (curtime >= serv->NextPingTime())
192 if (serv->AnsweredLastPing())
194 sock->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" PING "+serv->GetID());
195 serv->SetNextPingTime(curtime + Utils->PingFreq);
197 gettimeofday(&t, NULL);
198 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
199 serv->LastPingMsec = ts;
200 serv->Warned = false;
204 /* they didnt answer, boot them */
205 sock->SendError("Ping timeout");
206 sock->Squit(serv,"Ping timeout");
207 ServerInstance->SE->DelFd(sock);
212 else if ((Utils->PingWarnTime) && (!serv->Warned) && (curtime >= serv->NextPingTime() - (Utils->PingFreq - Utils->PingWarnTime)) && (!serv->AnsweredLastPing()))
214 /* The server hasnt responded, send a warning to opers */
215 ServerInstance->SNO->WriteToSnoMask('l',"Server \002%s\002 has not responded to PING for %d seconds, high latency.", serv->GetName().c_str(), Utils->PingWarnTime);
223 * Cancel remote burst mode on any servers which still have it enabled due to latency/lack of data.
224 * This prevents lost REMOTECONNECT notices
227 gettimeofday(&t, NULL);
228 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
230 for (server_hash::iterator i = Utils->serverlist.begin(); i != Utils->serverlist.end(); i++)
232 if (i->second->bursting)
234 unsigned long bursttime = ts - i->second->StartBurst;
235 if (bursttime > 60000) // A minute
237 ServerInstance->SNO->WriteToSnoMask('l',"Server \002%s\002 has not finished burst, forcing end of burst.", i->second->GetName().c_str());
238 i->second->FinishBurst();
244 void ModuleSpanningTree::ConnectServer(Link* x)
247 QueryType start_type = DNS_QUERY_A;
249 start_type = DNS_QUERY_AAAA;
250 if (strchr(x->IPAddr.c_str(),':'))
253 if (inet_pton(AF_INET6, x->IPAddr.c_str(), &n) < 1)
260 if (inet_aton(x->IPAddr.c_str(),&n) < 1)
264 /* Do we already have an IP? If so, no need to resolve it. */
267 /* Gave a hook, but it wasnt one we know */
268 if ((!x->Hook.empty()) && (Utils->hooks.find(x->Hook.c_str()) == Utils->hooks.end()))
270 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()]);
271 if (newsocket->GetFd() > -1)
273 /* Handled automatically on success */
277 RemoteMessage(NULL, "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(),strerror(errno));
278 if (ServerInstance->SocketCull.find(newsocket) == ServerInstance->SocketCull.end())
279 ServerInstance->SocketCull[newsocket] = newsocket;
280 Utils->DoFailOver(x);
288 ServernameResolver* snr = new ServernameResolver((Module*)this, Utils, ServerInstance,x->IPAddr, *x, cached, start_type);
289 ServerInstance->AddResolver(snr, cached);
291 catch (ModuleException& e)
293 RemoteMessage(NULL, "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(), e.GetReason());
294 Utils->DoFailOver(x);
299 void ModuleSpanningTree::AutoConnectServers(time_t curtime)
301 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
303 if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
305 x->NextConnectTime = curtime + x->AutoConnect;
306 TreeServer* CheckDupe = Utils->FindServer(x->Name.c_str());
307 if (x->FailOver.length())
309 TreeServer* CheckFailOver = Utils->FindServer(x->FailOver.c_str());
312 /* The failover for this server is currently a member of the network.
313 * The failover probably succeeded, where the main link did not.
314 * Don't try the main link until the failover is gone again.
321 // an autoconnected server is not connected. Check if its time to connect it
322 ServerInstance->SNO->WriteToSnoMask('l',"AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
323 this->ConnectServer(&(*x));
329 int ModuleSpanningTree::HandleVersion(const char* const* parameters, int pcnt, User* user)
331 // we've already checked if pcnt > 0, so this is safe
332 TreeServer* found = Utils->FindServerMask(parameters[0]);
335 std::string Version = found->GetVersion();
336 user->WriteNumeric(351, "%s :%s",user->nick,Version.c_str());
337 if (found == Utils->TreeRoot)
339 ServerInstance->Config->Send005(user);
344 user->WriteNumeric(402, "%s %s :No such server",user->nick,parameters[0]);
349 /* This method will attempt to get a link message out to as many people as is required.
350 * If a user is provided, and that user is local, then the user is sent the message using
351 * WriteServ (they are the local initiator of that message). If the user is remote, they are
352 * sent that message remotely via PUSH.
353 * If the user is NULL, then the notice is sent locally via WriteToSnoMask with snomask 'l',
354 * and remotely via SNONOTICE with mask 'l'.
356 void ModuleSpanningTree::RemoteMessage(User* user, const char* format, ...)
358 /* This could cause an infinite loop, because DoOneToMany() will, on error,
359 * call TreeSocket::OnError(), which in turn will call this function to
360 * notify everyone of the error. So, drop any messages that are generated
361 * during the sending of another message. -Special */
362 static bool SendingRemoteMessage = false;
363 if (SendingRemoteMessage)
365 SendingRemoteMessage = true;
367 std::deque<std::string> params;
371 va_start(argsPtr, format);
372 vsnprintf(text, MAXBUF, format, argsPtr);
377 /* No user, target it generically at everyone */
378 ServerInstance->SNO->WriteToSnoMask('l', "%s", text);
379 params.push_back("l");
380 params.push_back(std::string(":") + text);
381 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "SNONOTICE", params);
386 user->WriteServ("NOTICE %s :%s", user->nick, text);
389 params.push_back(user->uuid);
390 params.push_back(std::string("::") + ServerInstance->Config->ServerName + " NOTICE " + user->nick + " :*** From " +
391 ServerInstance->Config->ServerName+ ": " + text);
392 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "PUSH", params);
396 SendingRemoteMessage = false;
399 int ModuleSpanningTree::HandleConnect(const char* const* parameters, int pcnt, User* user)
401 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
403 if (ServerInstance->MatchText(x->Name.c_str(),parameters[0]))
405 TreeServer* CheckDupe = Utils->FindServer(x->Name.c_str());
408 RemoteMessage(user, "*** CONNECT: Connecting to server: \002%s\002 (%s:%d)",x->Name.c_str(),(x->HiddenFromStats ? "<hidden>" : x->IPAddr.c_str()),x->Port);
409 ConnectServer(&(*x));
414 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());
419 RemoteMessage(user, "*** CONNECT: No server matching \002%s\002 could be found in the config file.",parameters[0]);
423 void ModuleSpanningTree::OnGetServerDescription(const std::string &servername,std::string &description)
425 TreeServer* s = Utils->FindServer(servername);
428 description = s->GetDesc();
432 void ModuleSpanningTree::OnUserInvite(User* source,User* dest,Channel* channel, time_t expiry)
434 if (IS_LOCAL(source))
436 std::deque<std::string> params;
437 params.push_back(dest->uuid);
438 params.push_back(channel->name);
439 params.push_back(ConvToStr(expiry));
440 Utils->DoOneToMany(source->uuid,"INVITE",params);
444 void ModuleSpanningTree::OnPostLocalTopicChange(User* user, Channel* chan, const std::string &topic)
446 std::deque<std::string> params;
447 params.push_back(chan->name);
448 params.push_back(":"+topic);
449 Utils->DoOneToMany(user->uuid,"TOPIC",params);
452 void ModuleSpanningTree::OnWallops(User* user, const std::string &text)
456 std::deque<std::string> params;
457 params.push_back(":"+text);
458 Utils->DoOneToMany(user->uuid,"WALLOPS",params);
462 void ModuleSpanningTree::OnUserNotice(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
468 if (target_type == TYPE_USER)
470 User* d = (User*)dest;
471 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
473 std::deque<std::string> params;
475 params.push_back(d->uuid);
476 params.push_back(":"+text);
477 Utils->DoOneToOne(user->uuid,"NOTICE",params,d->server);
480 else if (target_type == TYPE_CHANNEL)
484 Channel *c = (Channel*)dest;
487 std::string cname = c->name;
489 cname = status + cname;
491 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
492 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
494 TreeSocket* Sock = i->second->GetSocket();
496 Sock->WriteLine(":"+std::string(user->uuid)+" NOTICE "+cname+" :"+text);
501 else if (target_type == TYPE_SERVER)
505 char* target = (char*)dest;
506 std::deque<std::string> par;
507 par.push_back(target);
508 par.push_back(":"+text);
509 Utils->DoOneToMany(user->uuid,"NOTICE",par);
514 void ModuleSpanningTree::OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
520 if (target_type == TYPE_USER)
522 // route private messages which are targetted at clients only to the server
523 // which needs to receive them
524 User* d = (User*)dest;
525 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
527 std::deque<std::string> params;
529 params.push_back(d->uuid);
530 params.push_back(":"+text);
531 Utils->DoOneToOne(user->uuid,"PRIVMSG",params,d->server);
534 else if (target_type == TYPE_CHANNEL)
538 Channel *c = (Channel*)dest;
541 std::string cname = c->name;
543 cname = status + cname;
545 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
546 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
548 TreeSocket* Sock = i->second->GetSocket();
550 Sock->WriteLine(":"+std::string(user->uuid)+" PRIVMSG "+cname+" :"+text);
555 else if (target_type == TYPE_SERVER)
559 char* target = (char*)dest;
560 std::deque<std::string> par;
561 par.push_back(target);
562 par.push_back(":"+text);
563 Utils->DoOneToMany(user->uuid,"PRIVMSG",par);
568 void ModuleSpanningTree::OnBackgroundTimer(time_t curtime)
570 AutoConnectServers(curtime);
571 DoPingChecks(curtime);
574 void ModuleSpanningTree::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
576 // Only do this for local users
579 if (channel->GetUserCounter() == 1)
581 std::deque<std::string> params;
582 // set up their permissions and the channel TS with FJOIN.
583 // All users are FJOINed now, because a module may specify
584 // new joining permissions for the user.
585 params.push_back(channel->name);
586 params.push_back(ConvToStr(channel->age));
587 params.push_back(std::string("+") + channel->ChanModes(true));
588 params.push_back(ServerInstance->Modes->ModeString(user, channel, false)+","+std::string(user->uuid));
589 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FJOIN",params);
593 std::deque<std::string> params;
594 params.push_back(channel->name);
595 params.push_back(ConvToStr(channel->age));
596 Utils->DoOneToMany(user->uuid,"JOIN",params);
601 void ModuleSpanningTree::OnChangeHost(User* user, const std::string &newhost)
603 // only occurs for local clients
604 if (user->registered != REG_ALL)
606 std::deque<std::string> params;
607 params.push_back(newhost);
608 Utils->DoOneToMany(user->uuid,"FHOST",params);
611 void ModuleSpanningTree::OnChangeName(User* user, const std::string &gecos)
613 // only occurs for local clients
614 if (user->registered != REG_ALL)
616 std::deque<std::string> params;
617 params.push_back(gecos);
618 Utils->DoOneToMany(user->uuid,"FNAME",params);
621 void ModuleSpanningTree::OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
625 std::deque<std::string> params;
626 params.push_back(channel->name);
627 if (!partmessage.empty())
628 params.push_back(":"+partmessage);
629 Utils->DoOneToMany(user->uuid,"PART",params);
633 void ModuleSpanningTree::OnPostConnect(User* user)
637 std::deque<std::string> params;
638 params.push_back(user->uuid);
639 params.push_back(ConvToStr(user->age));
640 params.push_back(user->nick);
641 params.push_back(user->host);
642 params.push_back(user->dhost);
643 params.push_back(user->ident);
644 params.push_back("+"+std::string(user->FormatModes()));
645 params.push_back(user->GetIPString());
646 params.push_back(ConvToStr(user->signon));
647 params.push_back(":"+std::string(user->fullname));
648 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "UID", params);
651 TreeServer* SourceServer = Utils->FindServer(user->server);
654 SourceServer->SetUserCount(1); // increment by 1
658 void ModuleSpanningTree::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
660 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
662 std::deque<std::string> params;
664 if (oper_message != reason)
666 params.push_back(":"+oper_message);
667 Utils->DoOneToMany(user->uuid,"OPERQUIT",params);
670 params.push_back(":"+reason);
671 Utils->DoOneToMany(user->uuid,"QUIT",params);
674 // Regardless, We need to modify the user Counts..
675 TreeServer* SourceServer = Utils->FindServer(user->server);
678 SourceServer->SetUserCount(-1); // decrement by 1
682 void ModuleSpanningTree::OnUserPostNick(User* user, const std::string &oldnick)
686 std::deque<std::string> params;
687 params.push_back(user->nick);
689 /** IMPORTANT: We don't update the TS if the oldnick is just a case change of the newnick!
691 if (irc::string(user->nick) != assign(oldnick))
692 user->age = ServerInstance->Time();
694 params.push_back(ConvToStr(user->age));
695 Utils->DoOneToMany(user->uuid,"NICK",params);
699 void ModuleSpanningTree::OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
701 if ((source) && (IS_LOCAL(source)))
703 std::deque<std::string> params;
704 params.push_back(chan->name);
705 params.push_back(user->uuid);
706 params.push_back(":"+reason);
707 Utils->DoOneToMany(source->uuid,"KICK",params);
711 std::deque<std::string> params;
712 params.push_back(chan->name);
713 params.push_back(user->uuid);
714 params.push_back(":"+reason);
715 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"KICK",params);
719 void ModuleSpanningTree::OnRemoteKill(User* source, User* dest, const std::string &reason, const std::string &operreason)
721 std::deque<std::string> params;
722 params.push_back(":"+reason);
723 Utils->DoOneToMany(dest->uuid,"OPERQUIT",params);
725 params.push_back(dest->uuid);
726 params.push_back(":"+reason);
727 dest->SetOperQuit(operreason);
728 Utils->DoOneToMany(source->uuid,"KILL",params);
731 void ModuleSpanningTree::OnRehash(User* user, const std::string ¶meter)
733 if (!parameter.empty())
735 std::deque<std::string> params;
736 params.push_back(parameter);
737 Utils->DoOneToMany(user ? user->nick : ServerInstance->Config->GetSID(), "REHASH", params);
739 if (ServerInstance->MatchText(ServerInstance->Config->ServerName,parameter))
741 ServerInstance->SNO->WriteToSnoMask('A', "Remote rehash initiated locally by \002%s\002", user ? user->nick : ServerInstance->Config->ServerName);
742 ServerInstance->RehashServer();
745 Utils->ReadConfiguration(true);
746 InitializeDisabledCommands(ServerInstance->Config->DisabledCommands, ServerInstance);
749 // note: the protocol does not allow direct umode +o except
750 // via NICK with 8 params. sending OPERTYPE infers +o modechange
752 void ModuleSpanningTree::OnOper(User* user, const std::string &opertype)
756 std::deque<std::string> params;
757 params.push_back(opertype);
758 Utils->DoOneToMany(user->uuid,"OPERTYPE",params);
762 void ModuleSpanningTree::OnAddLine(User* user, XLine *x)
764 if (!x->IsBurstable())
768 snprintf(data,MAXBUF,"%s %s %s %lu %lu :%s", x->type.c_str(), x->Displayable(),
769 ServerInstance->Config->ServerName, (unsigned long)x->set_time, (unsigned long)x->duration, x->reason);
770 std::deque<std::string> params;
771 params.push_back(data);
775 /* Server-set lines */
776 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "ADDLINE", params);
778 else if (IS_LOCAL(user))
781 Utils->DoOneToMany(user->uuid, "ADDLINE", params);
785 void ModuleSpanningTree::OnDelLine(User* user, XLine *x)
791 snprintf(data,MAXBUF,"%s %s", x->type.c_str(), x->Displayable());
792 std::deque<std::string> params;
793 params.push_back(data);
797 /* Server-unset lines */
798 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "DELLINE", params);
800 else if (IS_LOCAL(user))
802 /* User-unset lines */
803 Utils->DoOneToMany(user->uuid, "DELLINE", params);
807 void ModuleSpanningTree::OnMode(User* user, void* dest, int target_type, const std::string &text)
809 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
811 std::deque<std::string> params;
813 std::string output_text;
815 ServerInstance->Parser->TranslateUIDs(TR_SPACENICKLIST, text, output_text);
817 if (target_type == TYPE_USER)
819 User* u = (User*)dest;
820 params.push_back(u->uuid);
821 params.push_back(output_text);
826 Channel* c = (Channel*)dest;
827 params.push_back(c->name);
828 params.push_back(ConvToStr(c->age));
829 params.push_back(output_text);
833 Utils->DoOneToMany(user->uuid, command, params);
837 int ModuleSpanningTree::OnSetAway(User* user, const std::string &awaymsg)
843 std::deque<std::string> params;
845 Utils->DoOneToMany(user->uuid,"AWAY",params);
849 std::deque<std::string> params;
850 params.push_back(":" + awaymsg);
851 Utils->DoOneToMany(user->uuid,"AWAY",params);
858 void ModuleSpanningTree::ProtoSendMode(void* opaque, int target_type, void* target, const std::string &modeline)
860 TreeSocket* s = (TreeSocket*)opaque;
861 std::string output_text;
863 ServerInstance->Parser->TranslateUIDs(TR_SPACENICKLIST, modeline, output_text);
867 if (target_type == TYPE_USER)
869 User* u = (User*)target;
870 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" FMODE "+u->uuid+" "+ConvToStr(u->age)+" "+output_text);
874 Channel* c = (Channel*)target;
875 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" FMODE "+c->name+" "+ConvToStr(c->age)+" "+output_text);
880 void ModuleSpanningTree::ProtoSendMetaData(void* opaque, int target_type, void* target, const std::string &extname, const std::string &extdata)
882 TreeSocket* s = (TreeSocket*)opaque;
885 if (target_type == TYPE_USER)
887 User* u = (User*)target;
888 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+u->uuid+" "+extname+" :"+extdata);
890 else if (target_type == TYPE_CHANNEL)
892 Channel* c = (Channel*)target;
893 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+c->name+" "+extname+" :"+extdata);
896 if (target_type == TYPE_OTHER)
898 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA * "+extname+" :"+extdata);
902 void ModuleSpanningTree::OnEvent(Event* event)
904 if ((event->GetEventID() == "send_encap") || (event->GetEventID() == "send_metadata") || (event->GetEventID() == "send_topic") || (event->GetEventID() == "send_mode") || (event->GetEventID() == "send_mode_explicit") || (event->GetEventID() == "send_opers")
905 || (event->GetEventID() == "send_modeset") || (event->GetEventID() == "send_snoset") || (event->GetEventID() == "send_push"))
907 ServerInstance->Logs->Log("m_spanningtree", DEBUG, "WARNING: Deprecated use of old 1.1 style m_spanningtree event ignored, type '"+event->GetEventID()+"'!");
911 ModuleSpanningTree::~ModuleSpanningTree()
913 /* This will also free the listeners */
914 delete ServerInstance->PI;
915 ServerInstance->PI = new ProtocolInterface(ServerInstance);
919 ServerInstance->Timers->DelTimer(RefreshTimer);
921 ServerInstance->Modules->DoneWithInterface("BufferedSocketHook");
924 Version ModuleSpanningTree::GetVersion()
926 return Version(1,2,0,2,VF_VENDOR,API_VERSION);
929 /* It is IMPORTANT that m_spanningtree is the last module in the chain
930 * so that any activity it sees is FINAL, e.g. we arent going to send out
931 * a NICK message before m_cloaking has finished putting the +x on the user,
933 * Therefore, we return PRIORITY_LAST to make sure we end up at the END of
934 * the module call queue.
936 void ModuleSpanningTree::Prioritize()
938 ServerInstance->Modules->SetPriority(this, PRIO_LAST);
941 MODULE_INIT(ModuleSpanningTree)