1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $ModDesc: Provides a spanning tree server link protocol */
19 #include "../transport.h"
21 #include "cachetimer.h"
22 #include "resolvers.h"
25 #include "treeserver.h"
27 #include "treesocket.h"
30 #include "protocolinterface.h"
32 /* $ModDep: m_spanningtree/cachetimer.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 */
34 ModuleSpanningTree::ModuleSpanningTree(InspIRCd* Me)
35 : Module(Me), max_local(0), max_global(0)
37 ServerInstance->Modules->UseInterface("BufferedSocketHook");
38 Utils = new SpanningTreeUtilities(ServerInstance, this);
39 command_rconnect = new CommandRConnect(ServerInstance, this, Utils);
40 ServerInstance->AddCommand(command_rconnect);
41 command_rsquit = new CommandRSQuit(ServerInstance, this, Utils);
42 ServerInstance->AddCommand(command_rsquit);
43 RefreshTimer = new CacheRefreshTimer(ServerInstance, Utils);
44 ServerInstance->Timers->AddTimer(RefreshTimer);
46 Implementation eventlist[] =
48 I_OnPreCommand, I_OnGetServerDescription, I_OnUserInvite, I_OnPostTopicChange,
49 I_OnWallops, I_OnUserNotice, I_OnUserMessage, I_OnBackgroundTimer, I_OnUserJoin,
50 I_OnChangeLocalUserHost, I_OnChangeName, I_OnChangeIdent, I_OnUserPart, I_OnUnloadModule,
51 I_OnUserQuit, I_OnUserPostNick, I_OnUserKick, I_OnRemoteKill, I_OnRehash, I_OnPreRehash,
52 I_OnOper, I_OnAddLine, I_OnDelLine, I_OnMode, I_OnLoadModule, I_OnStats, I_OnEvent,
53 I_OnSetAway, I_OnPostCommand, I_OnUserConnect
55 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
57 delete ServerInstance->PI;
58 ServerInstance->PI = new SpanningTreeProtocolInterface(this, Utils, ServerInstance);
61 // update our local user count
62 Utils->TreeRoot->SetUserCount(ServerInstance->Users->local_users.size());
65 void ModuleSpanningTree::ShowLinks(TreeServer* Current, User* user, int hops)
67 std::string Parent = Utils->TreeRoot->GetName();
68 if (Current->GetParent())
70 Parent = Current->GetParent()->GetName();
72 for (unsigned int q = 0; q < Current->ChildCount(); q++)
74 if ((Current->GetChild(q)->Hidden) || ((Utils->HideULines) && (ServerInstance->ULine(Current->GetChild(q)->GetName().c_str()))))
78 ShowLinks(Current->GetChild(q),user,hops+1);
83 ShowLinks(Current->GetChild(q),user,hops+1);
86 /* Don't display the line if its a uline, hide ulines is on, and the user isnt an oper */
87 if ((Utils->HideULines) && (ServerInstance->ULine(Current->GetName().c_str())) && (!IS_OPER(user)))
89 /* Or if the server is hidden and they're not an oper */
90 else if ((Current->Hidden) && (!IS_OPER(user)))
93 user->WriteNumeric(364, "%s %s %s :%d %s", user->nick.c_str(),Current->GetName().c_str(),
94 (Utils->FlatLinks && (!IS_OPER(user))) ? ServerInstance->Config->ServerName : Parent.c_str(),
95 (Utils->FlatLinks && (!IS_OPER(user))) ? 0 : hops,
96 Current->GetDesc().c_str());
99 int ModuleSpanningTree::CountLocalServs()
101 return Utils->TreeRoot->ChildCount();
104 int ModuleSpanningTree::CountServs()
106 return Utils->serverlist.size();
109 void ModuleSpanningTree::HandleLinks(const std::vector<std::string>& parameters, User* user)
111 ShowLinks(Utils->TreeRoot,user,0);
112 user->WriteNumeric(365, "%s * :End of /LINKS list.",user->nick.c_str());
116 void ModuleSpanningTree::HandleLusers(const std::vector<std::string>& parameters, User* user)
118 unsigned int n_users = ServerInstance->Users->UserCount();
120 /* Only update these when someone wants to see them, more efficient */
121 if ((unsigned int)ServerInstance->Users->LocalUserCount() > max_local)
122 max_local = ServerInstance->Users->LocalUserCount();
123 if (n_users > max_global)
124 max_global = n_users;
126 unsigned int ulined_count = 0;
127 unsigned int ulined_local_count = 0;
129 /* If ulined are hidden and we're not an oper, count the number of ulined servers hidden,
130 * locally and globally (locally means directly connected to us)
132 if ((Utils->HideULines) && (!IS_OPER(user)))
134 for (server_hash::iterator q = Utils->serverlist.begin(); q != Utils->serverlist.end(); q++)
136 if (ServerInstance->ULine(q->second->GetName().c_str()))
139 if (q->second->GetParent() == Utils->TreeRoot)
140 ulined_local_count++;
144 user->WriteNumeric(251, "%s :There are %d users and %d invisible on %d servers",user->nick.c_str(),
145 n_users-ServerInstance->Users->ModeCount('i'),
146 ServerInstance->Users->ModeCount('i'),
147 ulined_count ? this->CountServs() - ulined_count : this->CountServs());
149 if (ServerInstance->Users->OperCount())
150 user->WriteNumeric(252, "%s %d :operator(s) online",user->nick.c_str(),ServerInstance->Users->OperCount());
152 if (ServerInstance->Users->UnregisteredUserCount())
153 user->WriteNumeric(253, "%s %d :unknown connections",user->nick.c_str(),ServerInstance->Users->UnregisteredUserCount());
155 if (ServerInstance->ChannelCount())
156 user->WriteNumeric(254, "%s %ld :channels formed",user->nick.c_str(),ServerInstance->ChannelCount());
158 user->WriteNumeric(255, "%s :I have %d clients and %d servers",user->nick.c_str(),ServerInstance->Users->LocalUserCount(),ulined_local_count ? this->CountLocalServs() - ulined_local_count : this->CountLocalServs());
159 user->WriteNumeric(265, "%s :Current Local Users: %d Max: %d",user->nick.c_str(),ServerInstance->Users->LocalUserCount(),max_local);
160 user->WriteNumeric(266, "%s :Current Global Users: %d Max: %d",user->nick.c_str(),n_users,max_global);
164 std::string ModuleSpanningTree::TimeToStr(time_t secs)
166 time_t mins_up = secs / 60;
167 time_t hours_up = mins_up / 60;
168 time_t days_up = hours_up / 24;
170 mins_up = mins_up % 60;
171 hours_up = hours_up % 24;
172 return ((days_up ? (ConvToStr(days_up) + "d") : std::string(""))
173 + (hours_up ? (ConvToStr(hours_up) + "h") : std::string(""))
174 + (mins_up ? (ConvToStr(mins_up) + "m") : std::string(""))
175 + ConvToStr(secs) + "s");
178 void ModuleSpanningTree::DoPingChecks(time_t curtime)
181 * Cancel remote burst mode on any servers which still have it enabled due to latency/lack of data.
182 * This prevents lost REMOTECONNECT notices
185 gettimeofday(&t, NULL);
186 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
188 for (server_hash::iterator i = Utils->serverlist.begin(); i != Utils->serverlist.end(); i++)
190 TreeServer *s = i->second;
192 // Fix for bug #792, do not ping servers that are not connected yet!
193 // Remote servers have Socket == NULL and local connected servers have
194 // Socket->LinkState == CONNECTED
195 if (s->GetSocket() && s->GetSocket()->GetLinkState() != CONNECTED)
198 // Now do PING checks on all servers
199 TreeServer *mts = Utils->BestRouteTo(s->GetID());
203 // Only ping if this server needs one
204 if (curtime >= s->NextPingTime())
206 // And if they answered the last
207 if (s->AnsweredLastPing())
209 // They did, send a ping to them
210 s->SetNextPingTime(curtime + Utils->PingFreq);
211 TreeSocket *tsock = mts->GetSocket();
213 // ... if we can find a proper route to them
216 tsock->WriteLine(std::string(":") + ServerInstance->Config->GetSID() + " PING " +
217 ServerInstance->Config->GetSID() + " " + s->GetID());
218 s->LastPingMsec = ts;
223 // They didn't answer the last ping, if they are locally connected, get rid of them.
224 TreeSocket *sock = s->GetSocket();
227 sock->SendError("Ping timeout");
228 sock->Squit(s,"Ping timeout");
229 ServerInstance->SE->DelFd(sock);
236 // If warn on ping enabled and not warned and the difference is sufficient and they didn't answer the last ping...
237 if ((Utils->PingWarnTime) && (!s->Warned) && (curtime >= s->NextPingTime() - (Utils->PingFreq - Utils->PingWarnTime)) && (!s->AnsweredLastPing()))
239 /* The server hasnt responded, send a warning to opers */
240 ServerInstance->SNO->WriteToSnoMask('l',"Server \002%s\002 has not responded to PING for %d seconds, high latency.", s->GetName().c_str(), Utils->PingWarnTime);
247 void ModuleSpanningTree::ConnectServer(Link* x)
251 if (InspIRCd::Match(ServerInstance->Config->ServerName, assign(x->Name)))
253 ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Not connecting to myself.");
257 QueryType start_type = DNS_QUERY_A;
258 start_type = DNS_QUERY_AAAA;
259 if (strchr(x->IPAddr.c_str(),':'))
262 if (inet_pton(AF_INET6, x->IPAddr.c_str(), &n) < 1)
268 if (inet_aton(x->IPAddr.c_str(),&n) < 1)
272 /* Do we already have an IP? If so, no need to resolve it. */
275 /* Gave a hook, but it wasnt one we know */
276 if ((!x->Hook.empty()) && (Utils->hooks.find(x->Hook.c_str()) == Utils->hooks.end()))
278 TreeSocket* newsocket = new TreeSocket(Utils, ServerInstance, x->IPAddr,x->Port, x->Timeout ? x->Timeout : 10,x->Name.c_str(), x->Bind, x->Hook.empty() ? NULL : Utils->hooks[x->Hook.c_str()]);
279 if (newsocket->GetFd() > -1)
281 /* Handled automatically on success */
285 ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(),strerror(errno));
286 if (ServerInstance->SocketCull.find(newsocket) == ServerInstance->SocketCull.end())
287 ServerInstance->SocketCull[newsocket] = newsocket;
288 Utils->DoFailOver(x);
296 ServernameResolver* snr = new ServernameResolver((Module*)this, Utils, ServerInstance,x->IPAddr, *x, cached, start_type);
297 ServerInstance->AddResolver(snr, cached);
299 catch (ModuleException& e)
301 ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(), e.GetReason());
302 Utils->DoFailOver(x);
307 void ModuleSpanningTree::AutoConnectServers(time_t curtime)
309 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
311 if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
313 x->NextConnectTime = curtime + x->AutoConnect;
314 TreeServer* CheckDupe = Utils->FindServer(x->Name.c_str());
315 if (x->FailOver.length())
317 TreeServer* CheckFailOver = Utils->FindServer(x->FailOver.c_str());
320 /* The failover for this server is currently a member of the network.
321 * The failover probably succeeded, where the main link did not.
322 * Don't try the main link until the failover is gone again.
329 // an autoconnected server is not connected. Check if its time to connect it
330 ServerInstance->SNO->WriteToSnoMask('l',"AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
331 this->ConnectServer(&(*x));
337 void ModuleSpanningTree::DoConnectTimeout(time_t curtime)
339 std::vector<Link*> failovers;
340 for (std::map<TreeSocket*, std::pair<std::string, int> >::iterator i = Utils->timeoutlist.begin(); i != Utils->timeoutlist.end(); i++)
342 TreeSocket* s = i->first;
343 std::pair<std::string, int> p = i->second;
344 if (curtime > s->age + p.second)
346 ServerInstance->SNO->WriteToSnoMask('l',"CONNECT: Error connecting \002%s\002 (timeout of %d seconds)",p.first.c_str(),p.second);
347 ServerInstance->SE->DelFd(s);
349 Link* MyLink = Utils->FindLink(p.first);
351 failovers.push_back(MyLink);
354 /* Trigger failover for each timed out socket */
355 for (std::vector<Link*>::const_iterator n = failovers.begin(); n != failovers.end(); ++n)
357 Utils->DoFailOver(*n);
361 ModResult ModuleSpanningTree::HandleVersion(const std::vector<std::string>& parameters, User* user)
363 // we've already checked if pcnt > 0, so this is safe
364 TreeServer* found = Utils->FindServerMask(parameters[0]);
367 std::string Version = found->GetVersion();
368 user->WriteNumeric(351, "%s :%s",user->nick.c_str(),Version.c_str());
369 if (found == Utils->TreeRoot)
371 ServerInstance->Config->Send005(user);
376 user->WriteNumeric(402, "%s %s :No such server",user->nick.c_str(),parameters[0].c_str());
381 /* This method will attempt to get a message to a remote user.
383 void ModuleSpanningTree::RemoteMessage(User* user, const char* format, ...)
388 va_start(argsPtr, format);
389 vsnprintf(text, MAXBUF, format, argsPtr);
393 user->WriteServ("NOTICE %s :%s", user->nick.c_str(), text);
395 ServerInstance->PI->SendUserNotice(user, text);
398 ModResult ModuleSpanningTree::HandleConnect(const std::vector<std::string>& parameters, User* user)
400 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
402 if (InspIRCd::Match(x->Name.c_str(),parameters[0]))
404 if (InspIRCd::Match(ServerInstance->Config->ServerName, assign(x->Name)))
406 RemoteMessage(user, "*** CONNECT: Server \002%s\002 is ME, not connecting.",x->Name.c_str());
410 TreeServer* CheckDupe = Utils->FindServer(x->Name.c_str());
413 RemoteMessage(user, "*** CONNECT: Connecting to server: \002%s\002 (%s:%d)",x->Name.c_str(),(x->HiddenFromStats ? "<hidden>" : x->IPAddr.c_str()),x->Port);
414 ConnectServer(&(*x));
419 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());
424 RemoteMessage(user, "*** CONNECT: No server matching \002%s\002 could be found in the config file.",parameters[0].c_str());
428 void ModuleSpanningTree::OnGetServerDescription(const std::string &servername,std::string &description)
430 TreeServer* s = Utils->FindServer(servername);
433 description = s->GetDesc();
437 void ModuleSpanningTree::OnUserInvite(User* source,User* dest,Channel* channel, time_t expiry)
439 if (IS_LOCAL(source))
441 parameterlist params;
442 params.push_back(dest->uuid);
443 params.push_back(channel->name);
444 params.push_back(ConvToStr(expiry));
445 Utils->DoOneToMany(source->uuid,"INVITE",params);
449 void ModuleSpanningTree::OnPostTopicChange(User* user, Channel* chan, const std::string &topic)
451 // Drop remote events on the floor.
455 parameterlist params;
456 params.push_back(chan->name);
457 params.push_back(":"+topic);
458 Utils->DoOneToMany(user->uuid,"TOPIC",params);
461 void ModuleSpanningTree::OnWallops(User* user, const std::string &text)
465 parameterlist params;
466 params.push_back(":"+text);
467 Utils->DoOneToMany(user->uuid,"WALLOPS",params);
471 void ModuleSpanningTree::OnUserNotice(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
477 if (target_type == TYPE_USER)
479 User* d = (User*)dest;
480 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
482 parameterlist params;
483 params.push_back(d->uuid);
484 params.push_back(":"+text);
485 Utils->DoOneToOne(user->uuid,"NOTICE",params,d->server);
488 else if (target_type == TYPE_CHANNEL)
492 Channel *c = (Channel*)dest;
495 std::string cname = c->name;
497 cname = status + cname;
499 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
500 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
502 TreeSocket* Sock = i->second->GetSocket();
504 Sock->WriteLine(":"+std::string(user->uuid)+" NOTICE "+cname+" :"+text);
509 else if (target_type == TYPE_SERVER)
513 char* target = (char*)dest;
515 par.push_back(target);
516 par.push_back(":"+text);
517 Utils->DoOneToMany(user->uuid,"NOTICE",par);
522 void ModuleSpanningTree::OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
528 if (target_type == TYPE_USER)
530 // route private messages which are targetted at clients only to the server
531 // which needs to receive them
532 User* d = (User*)dest;
533 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
535 parameterlist params;
536 params.push_back(d->uuid);
537 params.push_back(":"+text);
538 Utils->DoOneToOne(user->uuid,"PRIVMSG",params,d->server);
541 else if (target_type == TYPE_CHANNEL)
545 Channel *c = (Channel*)dest;
548 std::string cname = c->name;
550 cname = status + cname;
552 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
553 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
555 TreeSocket* Sock = i->second->GetSocket();
557 Sock->WriteLine(":"+std::string(user->uuid)+" PRIVMSG "+cname+" :"+text);
562 else if (target_type == TYPE_SERVER)
566 char* target = (char*)dest;
568 par.push_back(target);
569 par.push_back(":"+text);
570 Utils->DoOneToMany(user->uuid,"PRIVMSG",par);
575 void ModuleSpanningTree::OnBackgroundTimer(time_t curtime)
577 AutoConnectServers(curtime);
578 DoPingChecks(curtime);
579 DoConnectTimeout(curtime);
582 void ModuleSpanningTree::OnUserConnect(User* user)
587 parameterlist params;
588 params.push_back(user->uuid);
589 params.push_back(ConvToStr(user->age));
590 params.push_back(user->nick);
591 params.push_back(user->host);
592 params.push_back(user->dhost);
593 params.push_back(user->ident);
594 params.push_back(user->GetIPString());
595 params.push_back(ConvToStr(user->signon));
596 params.push_back("+"+std::string(user->FormatModes(true)));
597 params.push_back(":"+std::string(user->fullname));
598 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "UID", params);
600 Utils->TreeRoot->SetUserCount(1); // increment by 1
603 void ModuleSpanningTree::OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
605 // Only do this for local users
606 if (IS_LOCAL(memb->user))
608 parameterlist params;
609 // set up their permissions and the channel TS with FJOIN.
610 // All users are FJOINed now, because a module may specify
611 // new joining permissions for the user.
612 params.push_back(memb->chan->name);
613 params.push_back(ConvToStr(memb->chan->age));
614 params.push_back(std::string("+") + memb->chan->ChanModes(true));
615 params.push_back(memb->modes+","+std::string(memb->user->uuid));
616 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FJOIN",params);
620 ModResult ModuleSpanningTree::OnChangeLocalUserHost(User* user, const std::string &newhost)
622 if (user->registered != REG_ALL)
623 return MOD_RES_PASSTHRU;
625 parameterlist params;
626 params.push_back(newhost);
627 Utils->DoOneToMany(user->uuid,"FHOST",params);
628 return MOD_RES_PASSTHRU;
631 void ModuleSpanningTree::OnChangeName(User* user, const std::string &gecos)
633 // only occurs for local clients
634 if (user->registered != REG_ALL)
637 parameterlist params;
638 params.push_back(gecos);
639 Utils->DoOneToMany(user->uuid,"FNAME",params);
642 void ModuleSpanningTree::OnChangeIdent(User* user, const std::string &ident)
644 // only occurs for local clients
645 if (user->registered != REG_ALL)
648 parameterlist params;
649 params.push_back(ident);
650 Utils->DoOneToMany(user->uuid,"FIDENT",params);
653 void ModuleSpanningTree::OnUserPart(Membership* memb, std::string &partmessage, CUList& excepts)
655 if (IS_LOCAL(memb->user))
657 parameterlist params;
658 params.push_back(memb->chan->name);
659 if (!partmessage.empty())
660 params.push_back(":"+partmessage);
661 Utils->DoOneToMany(memb->user->uuid,"PART",params);
665 void ModuleSpanningTree::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
667 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
669 parameterlist params;
671 if (oper_message != reason)
673 params.push_back(":"+oper_message);
674 Utils->DoOneToMany(user->uuid,"OPERQUIT",params);
677 params.push_back(":"+reason);
678 Utils->DoOneToMany(user->uuid,"QUIT",params);
681 // Regardless, We need to modify the user Counts..
682 TreeServer* SourceServer = Utils->FindServer(user->server);
685 SourceServer->SetUserCount(-1); // decrement by 1
689 void ModuleSpanningTree::OnUserPostNick(User* user, const std::string &oldnick)
693 parameterlist params;
694 params.push_back(user->nick);
696 /** IMPORTANT: We don't update the TS if the oldnick is just a case change of the newnick!
698 if (irc::string(user->nick.c_str()) != assign(oldnick))
699 user->age = ServerInstance->Time();
701 params.push_back(ConvToStr(user->age));
702 Utils->DoOneToMany(user->uuid,"NICK",params);
704 else if (!loopCall && user->nick == user->uuid)
706 parameterlist params;
707 params.push_back(user->uuid);
708 params.push_back(ConvToStr(user->age));
709 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"SAVE",params);
713 void ModuleSpanningTree::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts)
715 parameterlist params;
716 params.push_back(memb->chan->name);
717 params.push_back(memb->user->uuid);
718 params.push_back(":"+reason);
719 if (IS_LOCAL(source))
721 Utils->DoOneToMany(source->uuid,"KICK",params);
723 else if (IS_FAKE(source) && source != Utils->ServerUser)
725 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"KICK",params);
729 void ModuleSpanningTree::OnRemoteKill(User* source, User* dest, const std::string &reason, const std::string &operreason)
731 if (!IS_LOCAL(source))
732 return; // Only start routing if we're origin.
734 User::OperQuit.set(dest, operreason);
735 parameterlist params;
736 params.push_back(":"+operreason);
737 Utils->DoOneToMany(dest->uuid,"OPERQUIT",params);
739 params.push_back(dest->uuid);
740 params.push_back(":"+reason);
741 Utils->DoOneToMany(source->uuid,"KILL",params);
744 void ModuleSpanningTree::OnPreRehash(User* user, const std::string ¶meter)
746 ServerInstance->Logs->Log("remoterehash", DEBUG, "called with param %s", parameter.c_str());
748 // Send out to other servers
749 if (!parameter.empty() && parameter[0] != '-')
751 parameterlist params;
752 params.push_back(parameter);
753 Utils->DoOneToAllButSender(user ? user->uuid : ServerInstance->Config->GetSID(), "REHASH", params, user ? user->server : ServerInstance->Config->ServerName);
757 void ModuleSpanningTree::OnRehash(User* user)
759 // Re-read config stuff
760 Utils->ReadConfiguration(true);
763 void ModuleSpanningTree::OnLoadModule(Module* mod, const std::string &name)
765 this->RedoConfig(mod, name);
768 void ModuleSpanningTree::OnUnloadModule(Module* mod, const std::string &name)
770 this->RedoConfig(mod, name);
773 void ModuleSpanningTree::RedoConfig(Module* mod, const std::string &name)
775 /* If m_sha256.so is loaded (we use this for HMAC) or any module implementing a BufferedSocket interface is loaded,
776 * then we need to re-read our config again taking this into account.
778 modulelist* ml = ServerInstance->Modules->FindInterface("BufferedSocketHook");
779 bool IsBufferSocketModule = false;
781 /* Did we find any modules? */
782 if (ml && std::find(ml->begin(), ml->end(), mod) != ml->end())
783 IsBufferSocketModule = true;
785 if (name == "m_sha256.so" || IsBufferSocketModule)
787 Utils->ReadConfiguration(true);
791 // note: the protocol does not allow direct umode +o except
792 // via NICK with 8 params. sending OPERTYPE infers +o modechange
794 void ModuleSpanningTree::OnOper(User* user, const std::string &opertype)
798 parameterlist params;
799 params.push_back(opertype);
800 Utils->DoOneToMany(user->uuid,"OPERTYPE",params);
804 void ModuleSpanningTree::OnAddLine(User* user, XLine *x)
806 if (!x->IsBurstable() || loopCall)
810 snprintf(data,MAXBUF,"%s %s %s %lu %lu :%s", x->type.c_str(), x->Displayable(),
811 ServerInstance->Config->ServerName, (unsigned long)x->set_time, (unsigned long)x->duration, x->reason.c_str());
812 parameterlist params;
813 params.push_back(data);
817 /* Server-set lines */
818 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "ADDLINE", params);
820 else if (IS_LOCAL(user))
823 Utils->DoOneToMany(user->uuid, "ADDLINE", params);
827 void ModuleSpanningTree::OnDelLine(User* user, XLine *x)
833 snprintf(data,MAXBUF,"%s %s", x->type.c_str(), x->Displayable());
834 parameterlist params;
835 params.push_back(data);
839 /* Server-unset lines */
840 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "DELLINE", params);
842 else if (IS_LOCAL(user))
844 /* User-unset lines */
845 Utils->DoOneToMany(user->uuid, "DELLINE", params);
849 void ModuleSpanningTree::OnMode(User* user, void* dest, int target_type, const parameterlist &text, const std::vector<TranslateType> &translate)
851 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
853 parameterlist params;
855 std::string output_text;
857 ServerInstance->Parser->TranslateUIDs(translate, text, output_text);
859 if (target_type == TYPE_USER)
861 User* u = (User*)dest;
862 params.push_back(u->uuid);
863 params.push_back(output_text);
868 Channel* c = (Channel*)dest;
869 params.push_back(c->name);
870 params.push_back(ConvToStr(c->age));
871 params.push_back(output_text);
875 Utils->DoOneToMany(user->uuid, command, params);
879 ModResult ModuleSpanningTree::OnSetAway(User* user, const std::string &awaymsg)
885 parameterlist params;
886 Utils->DoOneToMany(user->uuid,"AWAY",params);
890 parameterlist params;
891 params.push_back(ConvToStr(user->awaytime));
892 params.push_back(":" + awaymsg);
893 Utils->DoOneToMany(user->uuid,"AWAY",params);
897 return MOD_RES_PASSTHRU;
900 void ModuleSpanningTree::ProtoSendMode(void* opaque, TargetTypeFlags target_type, void* target, const parameterlist &modeline, const std::vector<TranslateType> &translate)
902 TreeSocket* s = (TreeSocket*)opaque;
903 std::string output_text;
905 ServerInstance->Parser->TranslateUIDs(translate, modeline, output_text);
909 if (target_type == TYPE_USER)
911 User* u = (User*)target;
912 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" MODE "+u->uuid+" "+output_text);
914 else if (target_type == TYPE_CHANNEL)
916 Channel* c = (Channel*)target;
917 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" FMODE "+c->name+" "+ConvToStr(c->age)+" "+output_text);
922 void ModuleSpanningTree::ProtoSendMetaData(void* opaque, Extensible* target, const std::string &extname, const std::string &extdata)
924 TreeSocket* s = static_cast<TreeSocket*>(opaque);
925 User* u = dynamic_cast<User*>(target);
926 Channel* c = dynamic_cast<Channel*>(target);
928 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+u->uuid+" "+extname+" :"+extdata);
930 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+c->name+" "+extname+" :"+extdata);
932 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA * "+extname+" :"+extdata);
935 void ModuleSpanningTree::OnEvent(Event* event)
937 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")
938 || (event->GetEventID() == "send_modeset") || (event->GetEventID() == "send_snoset") || (event->GetEventID() == "send_push"))
940 ServerInstance->Logs->Log("m_spanningtree", DEBUG, "WARNING: Deprecated use of old 1.1 style m_spanningtree event ignored, type '"+event->GetEventID()+"'!");
944 ModuleSpanningTree::~ModuleSpanningTree()
946 delete ServerInstance->PI;
947 ServerInstance->PI = new ProtocolInterface(ServerInstance);
949 /* This will also free the listeners */
952 delete command_rconnect;
953 delete command_rsquit;
955 ServerInstance->Timers->DelTimer(RefreshTimer);
957 ServerInstance->Modules->DoneWithInterface("BufferedSocketHook");
960 Version ModuleSpanningTree::GetVersion()
962 return Version("$Id$", VF_VENDOR, API_VERSION);
965 /* It is IMPORTANT that m_spanningtree is the last module in the chain
966 * so that any activity it sees is FINAL, e.g. we arent going to send out
967 * a NICK message before m_cloaking has finished putting the +x on the user,
969 * Therefore, we return PRIORITY_LAST to make sure we end up at the END of
970 * the module call queue.
972 void ModuleSpanningTree::Prioritize()
974 ServerInstance->Modules->SetPriority(this, PRIORITY_LAST);
977 MODULE_INIT(ModuleSpanningTree)