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"
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);
56 Implementation eventlist[] =
58 I_OnPreCommand, I_OnGetServerDescription, I_OnUserInvite, I_OnPostLocalTopicChange,
59 I_OnWallops, I_OnUserNotice, I_OnUserMessage, I_OnBackgroundTimer,
60 I_OnUserJoin, I_OnChangeHost, I_OnChangeName, I_OnUserPart, I_OnPostConnect,
61 I_OnUserQuit, I_OnUserPostNick, I_OnUserKick, I_OnRemoteKill, I_OnRehash,
62 I_OnOper, I_OnAddLine, I_OnDelLine, I_ProtoSendMode, I_OnMode,
63 I_OnStats, I_ProtoSendMetaData, I_OnEvent, I_OnSetAway, I_OnCancelAway, I_OnPostCommand
65 ServerInstance->Modules->Attach(eventlist, this, 29);
67 for (std::vector<User*>::const_iterator i = ServerInstance->local_users.begin(); i != ServerInstance->local_users.end(); i++)
69 this->OnPostConnect((*i));
73 void ModuleSpanningTree::ShowLinks(TreeServer* Current, User* user, int hops)
75 std::string Parent = Utils->TreeRoot->GetName();
76 if (Current->GetParent())
78 Parent = Current->GetParent()->GetName();
80 for (unsigned int q = 0; q < Current->ChildCount(); q++)
82 if ((Current->GetChild(q)->Hidden) || ((Utils->HideULines) && (ServerInstance->ULine(Current->GetChild(q)->GetName().c_str()))))
86 ShowLinks(Current->GetChild(q),user,hops+1);
91 ShowLinks(Current->GetChild(q),user,hops+1);
94 /* Don't display the line if its a uline, hide ulines is on, and the user isnt an oper */
95 if ((Utils->HideULines) && (ServerInstance->ULine(Current->GetName().c_str())) && (!IS_OPER(user)))
97 /* Or if the server is hidden and they're not an oper */
98 else if ((Current->Hidden) && (!IS_OPER(user)))
101 user->WriteServ("364 %s %s %s :%d %s", user->nick,Current->GetName().c_str(),
102 (Utils->FlatLinks && (!IS_OPER(user))) ? ServerInstance->Config->ServerName : Parent.c_str(),
103 (Utils->FlatLinks && (!IS_OPER(user))) ? 0 : hops,
104 Current->GetDesc().c_str());
107 int ModuleSpanningTree::CountLocalServs()
109 return Utils->TreeRoot->ChildCount();
112 int ModuleSpanningTree::CountServs()
114 return Utils->serverlist.size();
117 void ModuleSpanningTree::HandleLinks(const char** parameters, int pcnt, User* user)
119 ShowLinks(Utils->TreeRoot,user,0);
120 user->WriteServ("365 %s * :End of /LINKS list.",user->nick);
124 void ModuleSpanningTree::HandleLusers(const char** parameters, int pcnt, User* user)
126 unsigned int n_users = ServerInstance->UserCount();
128 /* Only update these when someone wants to see them, more efficient */
129 if ((unsigned int)ServerInstance->LocalUserCount() > max_local)
130 max_local = ServerInstance->LocalUserCount();
131 if (n_users > max_global)
132 max_global = n_users;
134 unsigned int ulined_count = 0;
135 unsigned int ulined_local_count = 0;
137 /* If ulined are hidden and we're not an oper, count the number of ulined servers hidden,
138 * locally and globally (locally means directly connected to us)
140 if ((Utils->HideULines) && (!*user->oper))
142 for (server_hash::iterator q = Utils->serverlist.begin(); q != Utils->serverlist.end(); q++)
144 if (ServerInstance->ULine(q->second->GetName().c_str()))
147 if (q->second->GetParent() == Utils->TreeRoot)
148 ulined_local_count++;
152 user->WriteServ("251 %s :There are %d users and %d invisible on %d servers",user->nick,
153 n_users-ServerInstance->ModeCount('i'),
154 ServerInstance->ModeCount('i'),
155 ulined_count ? this->CountServs() - ulined_count : this->CountServs());
157 if (ServerInstance->OperCount())
158 user->WriteServ("252 %s %d :operator(s) online",user->nick,ServerInstance->OperCount());
160 if (ServerInstance->UnregisteredUserCount())
161 user->WriteServ("253 %s %d :unknown connections",user->nick,ServerInstance->UnregisteredUserCount());
163 if (ServerInstance->ChannelCount())
164 user->WriteServ("254 %s %d :channels formed",user->nick,ServerInstance->ChannelCount());
166 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());
167 user->WriteServ("265 %s :Current Local Users: %d Max: %d",user->nick,ServerInstance->LocalUserCount(),max_local);
168 user->WriteServ("266 %s :Current Global Users: %d Max: %d",user->nick,n_users,max_global);
172 std::string ModuleSpanningTree::TimeToStr(time_t secs)
174 time_t mins_up = secs / 60;
175 time_t hours_up = mins_up / 60;
176 time_t days_up = hours_up / 24;
178 mins_up = mins_up % 60;
179 hours_up = hours_up % 24;
180 return ((days_up ? (ConvToStr(days_up) + "d") : std::string(""))
181 + (hours_up ? (ConvToStr(hours_up) + "h") : std::string(""))
182 + (mins_up ? (ConvToStr(mins_up) + "m") : std::string(""))
183 + ConvToStr(secs) + "s");
186 void ModuleSpanningTree::DoPingChecks(time_t curtime)
188 for (unsigned int j = 0; j < Utils->TreeRoot->ChildCount(); j++)
190 TreeServer* serv = Utils->TreeRoot->GetChild(j);
191 TreeSocket* sock = serv->GetSocket();
194 if (curtime >= serv->NextPingTime())
196 if (serv->AnsweredLastPing())
198 sock->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" PING "+serv->GetID());
199 serv->SetNextPingTime(curtime + Utils->PingFreq);
200 serv->LastPing = curtime;
202 gettimeofday(&t, NULL);
203 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
204 serv->LastPingMsec = ts;
205 serv->Warned = false;
209 /* they didnt answer, boot them */
210 sock->SendError("Ping timeout");
211 sock->Squit(serv,"Ping timeout");
212 ServerInstance->SE->DelFd(sock);
217 else if ((Utils->PingWarnTime) && (!serv->Warned) && (curtime >= serv->NextPingTime() - (Utils->PingFreq - Utils->PingWarnTime)) && (!serv->AnsweredLastPing()))
219 /* The server hasnt responded, send a warning to opers */
220 ServerInstance->SNO->WriteToSnoMask('l',"Server \002%s\002 has not responded to PING for %d seconds, high latency.", serv->GetName().c_str(), Utils->PingWarnTime);
226 /* Cancel remote burst mode on any servers which still have it enabled due to latency/lack of data.
227 * This prevents lost REMOTECONNECT notices
229 for (server_hash::iterator i = Utils->serverlist.begin(); i != Utils->serverlist.end(); i++)
230 Utils->SetRemoteBursting(i->second, false);
233 void ModuleSpanningTree::ConnectServer(Link* x)
236 QueryType start_type = DNS_QUERY_A;
238 start_type = DNS_QUERY_AAAA;
239 if (strchr(x->IPAddr.c_str(),':'))
242 if (inet_pton(AF_INET6, x->IPAddr.c_str(), &n) < 1)
249 if (inet_aton(x->IPAddr.c_str(),&n) < 1)
253 /* Do we already have an IP? If so, no need to resolve it. */
256 /* Gave a hook, but it wasnt one we know */
257 if ((!x->Hook.empty()) && (Utils->hooks.find(x->Hook.c_str()) == Utils->hooks.end()))
259 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()]);
260 if (newsocket->GetFd() > -1)
262 /* Handled automatically on success */
266 RemoteMessage(NULL, "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(),strerror(errno));
267 if (ServerInstance->SocketCull.find(newsocket) == ServerInstance->SocketCull.end())
268 ServerInstance->SocketCull[newsocket] = newsocket;
269 Utils->DoFailOver(x);
277 ServernameResolver* snr = new ServernameResolver((Module*)this, Utils, ServerInstance,x->IPAddr, *x, cached, start_type);
278 ServerInstance->AddResolver(snr, cached);
280 catch (ModuleException& e)
282 RemoteMessage(NULL, "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(), e.GetReason());
283 Utils->DoFailOver(x);
288 void ModuleSpanningTree::AutoConnectServers(time_t curtime)
290 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
292 if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
294 x->NextConnectTime = curtime + x->AutoConnect;
295 TreeServer* CheckDupe = Utils->FindServer(x->Name.c_str());
296 if (x->FailOver.length())
298 TreeServer* CheckFailOver = Utils->FindServer(x->FailOver.c_str());
301 /* The failover for this server is currently a member of the network.
302 * The failover probably succeeded, where the main link did not.
303 * Don't try the main link until the failover is gone again.
310 // an autoconnected server is not connected. Check if its time to connect it
311 ServerInstance->SNO->WriteToSnoMask('l',"AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
312 this->ConnectServer(&(*x));
318 int ModuleSpanningTree::HandleVersion(const char** parameters, int pcnt, User* user)
320 // we've already checked if pcnt > 0, so this is safe
321 TreeServer* found = Utils->FindServerMask(parameters[0]);
324 std::string Version = found->GetVersion();
325 user->WriteServ("351 %s :%s",user->nick,Version.c_str());
326 if (found == Utils->TreeRoot)
328 ServerInstance->Config->Send005(user);
333 user->WriteServ("402 %s %s :No such server",user->nick,parameters[0]);
338 /* This method will attempt to get a link message out to as many people as is required.
339 * If a user is provided, and that user is local, then the user is sent the message using
340 * WriteServ (they are the local initiator of that message). If the user is remote, they are
341 * sent that message remotely via PUSH.
342 * If the user is NULL, then the notice is sent locally via WriteToSnoMask with snomask 'l',
343 * and remotely via SNONOTICE with mask 'l'.
345 void ModuleSpanningTree::RemoteMessage(User* user, const char* format, ...)
347 /* This could cause an infinite loop, because DoOneToMany() will, on error,
348 * call TreeSocket::OnError(), which in turn will call this function to
349 * notify everyone of the error. So, drop any messages that are generated
350 * during the sending of another message. -Special */
351 static bool SendingRemoteMessage = false;
352 if (SendingRemoteMessage)
354 SendingRemoteMessage = true;
356 std::deque<std::string> params;
360 va_start(argsPtr, format);
361 vsnprintf(text, MAXBUF, format, argsPtr);
366 /* No user, target it generically at everyone */
367 ServerInstance->SNO->WriteToSnoMask('l', "%s", text);
368 params.push_back("l");
369 params.push_back(std::string(":") + text);
370 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "SNONOTICE", params);
375 user->WriteServ("NOTICE %s :%s", user->nick, text);
378 params.push_back(user->uuid);
379 params.push_back(std::string("::") + ServerInstance->Config->ServerName + " NOTICE " + user->nick + " :*** From " +
380 ServerInstance->Config->ServerName+ ": " + text);
381 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "PUSH", params);
385 SendingRemoteMessage = false;
388 int ModuleSpanningTree::HandleConnect(const char** parameters, int pcnt, User* user)
390 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
392 if (ServerInstance->MatchText(x->Name.c_str(),parameters[0]))
394 TreeServer* CheckDupe = Utils->FindServer(x->Name.c_str());
397 RemoteMessage(user, "*** CONNECT: Connecting to server: \002%s\002 (%s:%d)",x->Name.c_str(),(x->HiddenFromStats ? "<hidden>" : x->IPAddr.c_str()),x->Port);
398 ConnectServer(&(*x));
403 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());
408 RemoteMessage(user, "*** CONNECT: No server matching \002%s\002 could be found in the config file.",parameters[0]);
412 void ModuleSpanningTree::BroadcastTimeSync()
414 if (Utils->MasterTime)
416 std::deque<std::string> params;
417 params.push_back(ConvToStr(ServerInstance->Time(false)));
418 params.push_back("FORCE");
419 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "TIMESET", params);
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)
434 if (IS_LOCAL(source))
436 std::deque<std::string> params;
437 params.push_back(dest->uuid);
438 params.push_back(channel->name);
439 Utils->DoOneToMany(source->uuid,"INVITE",params);
443 void ModuleSpanningTree::OnPostLocalTopicChange(User* user, Channel* chan, const std::string &topic)
445 std::deque<std::string> params;
446 params.push_back(chan->name);
447 params.push_back(":"+topic);
448 Utils->DoOneToMany(user->uuid,"TOPIC",params);
451 void ModuleSpanningTree::OnWallops(User* user, const std::string &text)
455 std::deque<std::string> params;
456 params.push_back(":"+text);
457 Utils->DoOneToMany(user->uuid,"WALLOPS",params);
461 void ModuleSpanningTree::OnUserNotice(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
463 if (target_type == TYPE_USER)
465 User* d = (User*)dest;
466 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
468 std::deque<std::string> params;
470 params.push_back(d->uuid);
471 params.push_back(":"+text);
472 Utils->DoOneToOne(user->uuid,"NOTICE",params,d->server);
475 else if (target_type == TYPE_CHANNEL)
479 Channel *c = (Channel*)dest;
482 std::string cname = c->name;
484 cname = status + cname;
486 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
487 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
489 TreeSocket* Sock = i->second->GetSocket();
491 Sock->WriteLine(":"+std::string(user->uuid)+" NOTICE "+cname+" :"+text);
496 else if (target_type == TYPE_SERVER)
500 char* target = (char*)dest;
501 std::deque<std::string> par;
502 par.push_back(target);
503 par.push_back(":"+text);
504 Utils->DoOneToMany(user->uuid,"NOTICE",par);
509 void ModuleSpanningTree::OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
511 if (target_type == TYPE_USER)
513 // route private messages which are targetted at clients only to the server
514 // which needs to receive them
515 User* d = (User*)dest;
516 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
518 std::deque<std::string> params;
520 params.push_back(d->uuid);
521 params.push_back(":"+text);
522 Utils->DoOneToOne(user->uuid,"PRIVMSG",params,d->server);
525 else if (target_type == TYPE_CHANNEL)
529 Channel *c = (Channel*)dest;
532 std::string cname = c->name;
534 cname = status + cname;
536 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
537 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
539 TreeSocket* Sock = i->second->GetSocket();
541 Sock->WriteLine(":"+std::string(user->uuid)+" PRIVMSG "+cname+" :"+text);
546 else if (target_type == TYPE_SERVER)
550 char* target = (char*)dest;
551 std::deque<std::string> par;
552 par.push_back(target);
553 par.push_back(":"+text);
554 Utils->DoOneToMany(user->uuid,"PRIVMSG",par);
559 void ModuleSpanningTree::OnBackgroundTimer(time_t curtime)
561 AutoConnectServers(curtime);
562 DoPingChecks(curtime);
565 void ModuleSpanningTree::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
567 // Only do this for local users
570 if (channel->GetUserCounter() == 1)
572 std::deque<std::string> params;
573 // set up their permissions and the channel TS with FJOIN.
574 // All users are FJOINed now, because a module may specify
575 // new joining permissions for the user.
576 params.push_back(channel->name);
577 params.push_back(ConvToStr(channel->age));
578 params.push_back(std::string(channel->GetAllPrefixChars(user))+","+std::string(user->uuid));
579 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FJOIN",params);
580 /* First user in, sync the modes for the channel */
582 params.push_back(std::string("+") + channel->ChanModes(true));
583 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FMODE",params);
587 std::deque<std::string> params;
588 params.push_back(channel->name);
589 params.push_back(ConvToStr(channel->age));
590 Utils->DoOneToMany(user->uuid,"JOIN",params);
595 void ModuleSpanningTree::OnChangeHost(User* user, const std::string &newhost)
597 // only occurs for local clients
598 if (user->registered != REG_ALL)
600 std::deque<std::string> params;
601 params.push_back(newhost);
602 Utils->DoOneToMany(user->uuid,"FHOST",params);
605 void ModuleSpanningTree::OnChangeName(User* user, const std::string &gecos)
607 // only occurs for local clients
608 if (user->registered != REG_ALL)
610 std::deque<std::string> params;
611 params.push_back(gecos);
612 Utils->DoOneToMany(user->uuid,"FNAME",params);
615 void ModuleSpanningTree::OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
619 std::deque<std::string> params;
620 params.push_back(channel->name);
621 if (!partmessage.empty())
622 params.push_back(":"+partmessage);
623 Utils->DoOneToMany(user->uuid,"PART",params);
627 void ModuleSpanningTree::OnPostConnect(User* user)
631 std::deque<std::string> params;
632 params.push_back(user->uuid);
633 params.push_back(ConvToStr(user->age));
634 params.push_back(user->nick);
635 params.push_back(user->host);
636 params.push_back(user->dhost);
637 params.push_back(user->ident);
638 params.push_back("+"+std::string(user->FormatModes()));
639 params.push_back(user->GetIPString());
640 params.push_back(ConvToStr(user->signon));
641 params.push_back(":"+std::string(user->fullname));
642 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "UID", params);
645 TreeServer* SourceServer = Utils->FindServer(user->server);
648 SourceServer->SetUserCount(1); // increment by 1
652 void ModuleSpanningTree::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
654 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
656 std::deque<std::string> params;
658 if (oper_message != reason)
660 params.push_back(":"+oper_message);
661 Utils->DoOneToMany(user->uuid,"OPERQUIT",params);
664 params.push_back(":"+reason);
665 Utils->DoOneToMany(user->uuid,"QUIT",params);
668 // Regardless, We need to modify the user Counts..
669 TreeServer* SourceServer = Utils->FindServer(user->server);
672 SourceServer->SetUserCount(-1); // decrement by 1
676 void ModuleSpanningTree::OnUserPostNick(User* user, const std::string &oldnick)
680 std::deque<std::string> params;
681 params.push_back(user->nick);
683 /** IMPORTANT: We don't update the TS if the oldnick is just a case change of the newnick!
685 if (irc::string(user->nick) != assign(oldnick))
686 user->age = ServerInstance->Time(true);
688 params.push_back(ConvToStr(user->age));
689 Utils->DoOneToMany(user->uuid,"NICK",params);
693 void ModuleSpanningTree::OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
695 if ((source) && (IS_LOCAL(source)))
697 std::deque<std::string> params;
698 params.push_back(chan->name);
699 params.push_back(user->uuid);
700 params.push_back(":"+reason);
701 Utils->DoOneToMany(source->uuid,"KICK",params);
705 std::deque<std::string> params;
706 params.push_back(chan->name);
707 params.push_back(user->uuid);
708 params.push_back(":"+reason);
709 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"KICK",params);
713 void ModuleSpanningTree::OnRemoteKill(User* source, User* dest, const std::string &reason, const std::string &operreason)
715 std::deque<std::string> params;
716 params.push_back(":"+reason);
717 Utils->DoOneToMany(dest->uuid,"OPERQUIT",params);
719 params.push_back(dest->uuid);
720 params.push_back(":"+reason);
721 dest->SetOperQuit(operreason);
722 Utils->DoOneToMany(source->uuid,"KILL",params);
725 void ModuleSpanningTree::OnRehash(User* user, const std::string ¶meter)
727 if (!parameter.empty())
729 std::deque<std::string> params;
730 params.push_back(parameter);
731 Utils->DoOneToMany(user ? user->nick : ServerInstance->Config->GetSID(), "REHASH", params);
733 if (ServerInstance->MatchText(ServerInstance->Config->ServerName,parameter))
735 ServerInstance->WriteOpers("*** Remote rehash initiated locally by \002%s\002", user ? user->nick : ServerInstance->Config->ServerName);
736 ServerInstance->RehashServer();
739 Utils->ReadConfiguration(true);
740 InitializeDisabledCommands(ServerInstance->Config->DisabledCommands, ServerInstance);
743 // note: the protocol does not allow direct umode +o except
744 // via NICK with 8 params. sending OPERTYPE infers +o modechange
746 void ModuleSpanningTree::OnOper(User* user, const std::string &opertype)
750 std::deque<std::string> params;
751 params.push_back(opertype);
752 Utils->DoOneToMany(user->uuid,"OPERTYPE",params);
756 void ModuleSpanningTree::OnAddLine(XLine* line, User* user)
758 if (line->type == "K")
762 snprintf(data,MAXBUF,"%s %s %s %lu %lu :%s", line->type.c_str(), line->Displayable(), ServerInstance->Config->ServerName, line->set_time,
763 line->duration, line->reason);
764 std::deque<std::string> params;
765 params.push_back(data);
769 /* Server-set lines */
770 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "ADDLINE", params);
772 else if (IS_LOCAL(user))
775 Utils->DoOneToMany(user->uuid, "ADDLINE", params);
779 void ModuleSpanningTree::OnDelLine(XLine* line, User* user)
781 if (line->type == "K")
785 snprintf(data,MAXBUF,"%s %s", line->type.c_str(), line->Displayable());
786 std::deque<std::string> params;
787 params.push_back(data);
791 /* Server-unset lines */
792 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "DELLINE", params);
794 else if (IS_LOCAL(user))
796 /* User-unset lines */
797 Utils->DoOneToMany(user->uuid, "DELLINE", params);
801 void ModuleSpanningTree::OnMode(User* user, void* dest, int target_type, const std::string &text)
803 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
805 std::deque<std::string> params;
807 std::string output_text;
809 ServerInstance->Parser->TranslateUIDs(TR_SPACENICKLIST, text, output_text);
811 if (target_type == TYPE_USER)
813 User* u = (User*)dest;
814 params.push_back(u->uuid);
815 params.push_back(output_text);
820 Channel* c = (Channel*)dest;
821 params.push_back(c->name);
822 params.push_back(ConvToStr(c->age));
823 params.push_back(output_text);
827 Utils->DoOneToMany(user->uuid, command, params);
831 void ModuleSpanningTree::OnSetAway(User* user)
835 std::deque<std::string> params;
836 params.push_back(":"+std::string(user->awaymsg));
837 Utils->DoOneToMany(user->uuid,"AWAY",params);
841 void ModuleSpanningTree::OnCancelAway(User* user)
845 std::deque<std::string> params;
847 Utils->DoOneToMany(user->uuid,"AWAY",params);
851 void ModuleSpanningTree::ProtoSendMode(void* opaque, int target_type, void* target, const std::string &modeline)
853 TreeSocket* s = (TreeSocket*)opaque;
854 std::string output_text;
856 ServerInstance->Parser->TranslateUIDs(TR_SPACENICKLIST, modeline, output_text);
860 if (target_type == TYPE_USER)
862 User* u = (User*)target;
863 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" FMODE "+u->uuid+" "+ConvToStr(u->age)+" "+output_text);
867 Channel* c = (Channel*)target;
868 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" FMODE "+c->name+" "+ConvToStr(c->age)+" "+output_text);
873 void ModuleSpanningTree::ProtoSendMetaData(void* opaque, int target_type, void* target, const std::string &extname, const std::string &extdata)
875 TreeSocket* s = (TreeSocket*)opaque;
878 if (target_type == TYPE_USER)
880 User* u = (User*)target;
881 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+u->uuid+" "+extname+" :"+extdata);
883 else if (target_type == TYPE_CHANNEL)
885 Channel* c = (Channel*)target;
886 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+c->name+" "+extname+" :"+extdata);
889 if (target_type == TYPE_OTHER)
891 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA * "+extname+" :"+extdata);
895 void ModuleSpanningTree::OnEvent(Event* event)
897 std::deque<std::string>* params = (std::deque<std::string>*)event->GetData();
898 if (event->GetEventID() == "send_metadata")
900 if (params->size() < 3)
902 (*params)[2] = ":" + (*params)[2];
903 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"METADATA",*params);
905 else if (event->GetEventID() == "send_topic")
907 if (params->size() < 2)
909 (*params)[1] = ":" + (*params)[1];
910 params->insert(params->begin() + 1,ServerInstance->Config->ServerName);
911 params->insert(params->begin() + 1,ConvToStr(ServerInstance->Time(true)));
912 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FTOPIC",*params);
914 else if (event->GetEventID() == "send_mode")
916 if (params->size() < 2)
918 // Insert the TS value of the object, either User or Channel
920 std::string output_text;
922 /* Warning: in-place translation is only safe for type TR_NICK */
923 for (size_t n = 0; n < params->size(); n++)
924 ServerInstance->Parser->TranslateUIDs(TR_NICK, (*params)[n], (*params)[n]);
926 User* a = ServerInstance->FindNick((*params)[0]);
930 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"MODE",*params);
935 Channel* a = ServerInstance->FindChan((*params)[0]);
939 params->insert(params->begin() + 1,ConvToStr(ourTS));
940 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FMODE",*params);
944 else if (event->GetEventID() == "send_mode_explicit")
946 if (params->size() < 2)
948 std::string output_text;
950 /* Warning: in-place translation is only safe for type TR_NICK */
951 for (size_t n = 0; n < params->size(); n++)
952 ServerInstance->Parser->TranslateUIDs(TR_NICK, (*params)[n], (*params)[n]);
954 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"MODE",*params);
956 else if (event->GetEventID() == "send_opers")
958 if (params->size() < 1)
960 (*params)[0] = ":" + (*params)[0];
961 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"OPERNOTICE",*params);
963 else if (event->GetEventID() == "send_modeset")
965 if (params->size() < 2)
967 (*params)[1] = ":" + (*params)[1];
968 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"MODENOTICE",*params);
970 else if (event->GetEventID() == "send_snoset")
972 if (params->size() < 2)
974 (*params)[1] = ":" + (*params)[1];
975 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"SNONOTICE",*params);
977 else if (event->GetEventID() == "send_push")
979 if (params->size() < 2)
982 User *a = ServerInstance->FindNick((*params)[0]);
987 (*params)[0] = a->uuid;
988 (*params)[1] = ":" + (*params)[1];
989 Utils->DoOneToOne(ServerInstance->Config->GetSID(), "PUSH", *params, a->server);
993 ModuleSpanningTree::~ModuleSpanningTree()
995 /* This will also free the listeners */
998 ServerInstance->Timers->DelTimer(SyncTimer);
1000 ServerInstance->Timers->DelTimer(RefreshTimer);
1002 ServerInstance->Modules->DoneWithInterface("BufferedSocketHook");
1005 Version ModuleSpanningTree::GetVersion()
1007 return Version(1,1,0,2,VF_VENDOR,API_VERSION);
1010 /* It is IMPORTANT that m_spanningtree is the last module in the chain
1011 * so that any activity it sees is FINAL, e.g. we arent going to send out
1012 * a NICK message before m_cloaking has finished putting the +x on the user,
1014 * Therefore, we return PRIORITY_LAST to make sure we end up at the END of
1015 * the module call queue.
1017 void ModuleSpanningTree::Prioritize()
1019 ServerInstance->Modules->SetPriority(this, PRIO_LAST);
1022 MODULE_INIT(ModuleSpanningTree)