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 */
20 #include "cachetimer.h"
21 #include "resolvers.h"
24 #include "treeserver.h"
26 #include "treesocket.h"
29 #include "protocolinterface.h"
31 /* $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 */
33 ModuleSpanningTree::ModuleSpanningTree()
34 : max_local(0), max_global(0)
36 ServerInstance->Modules->UseInterface("BufferedSocketHook");
37 Utils = new SpanningTreeUtilities(this);
38 command_rconnect = new CommandRConnect(this, Utils);
39 ServerInstance->AddCommand(command_rconnect);
40 command_rsquit = new CommandRSQuit(this, Utils);
41 ServerInstance->AddCommand(command_rsquit);
42 RefreshTimer = new CacheRefreshTimer(Utils);
43 ServerInstance->Timers->AddTimer(RefreshTimer);
45 Implementation eventlist[] =
47 I_OnPreCommand, I_OnGetServerDescription, I_OnUserInvite, I_OnPostTopicChange,
48 I_OnWallops, I_OnUserNotice, I_OnUserMessage, I_OnBackgroundTimer, I_OnUserJoin,
49 I_OnChangeLocalUserHost, I_OnChangeName, I_OnChangeIdent, I_OnUserPart, I_OnUnloadModule,
50 I_OnUserQuit, I_OnUserPostNick, I_OnUserKick, I_OnRemoteKill, I_OnRehash, I_OnPreRehash,
51 I_OnOper, I_OnAddLine, I_OnDelLine, I_OnMode, I_OnLoadModule, I_OnStats,
52 I_OnSetAway, I_OnPostCommand, I_OnUserConnect
54 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
56 delete ServerInstance->PI;
57 ServerInstance->PI = new SpanningTreeProtocolInterface(this, Utils);
60 // update our local user count
61 Utils->TreeRoot->SetUserCount(ServerInstance->Users->local_users.size());
64 void ModuleSpanningTree::ShowLinks(TreeServer* Current, User* user, int hops)
66 std::string Parent = Utils->TreeRoot->GetName();
67 if (Current->GetParent())
69 Parent = Current->GetParent()->GetName();
71 for (unsigned int q = 0; q < Current->ChildCount(); q++)
73 if ((Current->GetChild(q)->Hidden) || ((Utils->HideULines) && (ServerInstance->ULine(Current->GetChild(q)->GetName().c_str()))))
77 ShowLinks(Current->GetChild(q),user,hops+1);
82 ShowLinks(Current->GetChild(q),user,hops+1);
85 /* Don't display the line if its a uline, hide ulines is on, and the user isnt an oper */
86 if ((Utils->HideULines) && (ServerInstance->ULine(Current->GetName().c_str())) && (!IS_OPER(user)))
88 /* Or if the server is hidden and they're not an oper */
89 else if ((Current->Hidden) && (!IS_OPER(user)))
92 user->WriteNumeric(364, "%s %s %s :%d %s", user->nick.c_str(),Current->GetName().c_str(),
93 (Utils->FlatLinks && (!IS_OPER(user))) ? ServerInstance->Config->ServerName.c_str() : Parent.c_str(),
94 (Utils->FlatLinks && (!IS_OPER(user))) ? 0 : hops,
95 Current->GetDesc().c_str());
98 int ModuleSpanningTree::CountLocalServs()
100 return Utils->TreeRoot->ChildCount();
103 int ModuleSpanningTree::CountServs()
105 return Utils->serverlist.size();
108 void ModuleSpanningTree::HandleLinks(const std::vector<std::string>& parameters, User* user)
110 ShowLinks(Utils->TreeRoot,user,0);
111 user->WriteNumeric(365, "%s * :End of /LINKS list.",user->nick.c_str());
115 void ModuleSpanningTree::HandleLusers(const std::vector<std::string>& parameters, User* user)
117 unsigned int n_users = ServerInstance->Users->UserCount();
119 /* Only update these when someone wants to see them, more efficient */
120 if ((unsigned int)ServerInstance->Users->LocalUserCount() > max_local)
121 max_local = ServerInstance->Users->LocalUserCount();
122 if (n_users > max_global)
123 max_global = n_users;
125 unsigned int ulined_count = 0;
126 unsigned int ulined_local_count = 0;
128 /* If ulined are hidden and we're not an oper, count the number of ulined servers hidden,
129 * locally and globally (locally means directly connected to us)
131 if ((Utils->HideULines) && (!IS_OPER(user)))
133 for (server_hash::iterator q = Utils->serverlist.begin(); q != Utils->serverlist.end(); q++)
135 if (ServerInstance->ULine(q->second->GetName().c_str()))
138 if (q->second->GetParent() == Utils->TreeRoot)
139 ulined_local_count++;
143 user->WriteNumeric(251, "%s :There are %d users and %d invisible on %d servers",user->nick.c_str(),
144 n_users-ServerInstance->Users->ModeCount('i'),
145 ServerInstance->Users->ModeCount('i'),
146 ulined_count ? this->CountServs() - ulined_count : this->CountServs());
148 if (ServerInstance->Users->OperCount())
149 user->WriteNumeric(252, "%s %d :operator(s) online",user->nick.c_str(),ServerInstance->Users->OperCount());
151 if (ServerInstance->Users->UnregisteredUserCount())
152 user->WriteNumeric(253, "%s %d :unknown connections",user->nick.c_str(),ServerInstance->Users->UnregisteredUserCount());
154 if (ServerInstance->ChannelCount())
155 user->WriteNumeric(254, "%s %ld :channels formed",user->nick.c_str(),ServerInstance->ChannelCount());
157 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());
158 user->WriteNumeric(265, "%s :Current Local Users: %d Max: %d",user->nick.c_str(),ServerInstance->Users->LocalUserCount(),max_local);
159 user->WriteNumeric(266, "%s :Current Global Users: %d Max: %d",user->nick.c_str(),n_users,max_global);
163 std::string ModuleSpanningTree::TimeToStr(time_t secs)
165 time_t mins_up = secs / 60;
166 time_t hours_up = mins_up / 60;
167 time_t days_up = hours_up / 24;
169 mins_up = mins_up % 60;
170 hours_up = hours_up % 24;
171 return ((days_up ? (ConvToStr(days_up) + "d") : std::string(""))
172 + (hours_up ? (ConvToStr(hours_up) + "h") : std::string(""))
173 + (mins_up ? (ConvToStr(mins_up) + "m") : std::string(""))
174 + ConvToStr(secs) + "s");
177 void ModuleSpanningTree::DoPingChecks(time_t curtime)
180 * Cancel remote burst mode on any servers which still have it enabled due to latency/lack of data.
181 * This prevents lost REMOTECONNECT notices
184 gettimeofday(&t, NULL);
185 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
187 for (server_hash::iterator i = Utils->serverlist.begin(); i != Utils->serverlist.end(); i++)
189 TreeServer *s = i->second;
191 // Fix for bug #792, do not ping servers that are not connected yet!
192 // Remote servers have Socket == NULL and local connected servers have
193 // Socket->LinkState == CONNECTED
194 if (s->GetSocket() && s->GetSocket()->GetLinkState() != CONNECTED)
197 // Now do PING checks on all servers
198 TreeServer *mts = Utils->BestRouteTo(s->GetID());
202 // Only ping if this server needs one
203 if (curtime >= s->NextPingTime())
205 // And if they answered the last
206 if (s->AnsweredLastPing())
208 // They did, send a ping to them
209 s->SetNextPingTime(curtime + Utils->PingFreq);
210 TreeSocket *tsock = mts->GetSocket();
212 // ... if we can find a proper route to them
215 tsock->WriteLine(std::string(":") + ServerInstance->Config->GetSID() + " PING " +
216 ServerInstance->Config->GetSID() + " " + s->GetID());
217 s->LastPingMsec = ts;
222 // They didn't answer the last ping, if they are locally connected, get rid of them.
223 TreeSocket *sock = s->GetSocket();
226 sock->SendError("Ping timeout");
227 sock->Squit(s,"Ping timeout");
228 ServerInstance->SE->DelFd(sock);
235 // If warn on ping enabled and not warned and the difference is sufficient and they didn't answer the last ping...
236 if ((Utils->PingWarnTime) && (!s->Warned) && (curtime >= s->NextPingTime() - (Utils->PingFreq - Utils->PingWarnTime)) && (!s->AnsweredLastPing()))
238 /* The server hasnt responded, send a warning to opers */
239 ServerInstance->SNO->WriteToSnoMask('l',"Server \002%s\002 has not responded to PING for %d seconds, high latency.", s->GetName().c_str(), Utils->PingWarnTime);
246 void ModuleSpanningTree::ConnectServer(Autoconnect* a, bool on_timer)
250 for(unsigned int j=0; j < a->servers.size(); j++)
252 if (Utils->FindServer(a->servers[j]))
254 // found something in this block. Should the server fail,
255 // we want to start at the start of the list, not in the
256 // middle where we left off
261 if (on_timer && a->position >= 0)
263 if (!on_timer && a->position < 0)
267 while (a->position < (int)a->servers.size())
269 Link* x = Utils->FindLink(a->servers[a->position]);
272 ServerInstance->SNO->WriteToSnoMask('l', "AUTOCONNECT: Auto-connecting server \002%s\002", x->Name.c_str());
278 // Autoconnect chain has been fully iterated; start at the beginning on the
279 // next AutoConnectServers run
283 void ModuleSpanningTree::ConnectServer(Link* x, Autoconnect* y)
287 if (InspIRCd::Match(ServerInstance->Config->ServerName, assign(x->Name)))
289 ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Not connecting to myself.");
293 QueryType start_type = DNS_QUERY_A;
294 start_type = DNS_QUERY_AAAA;
295 if (strchr(x->IPAddr.c_str(),':'))
298 if (inet_pton(AF_INET6, x->IPAddr.c_str(), &n) < 1)
304 if (inet_aton(x->IPAddr.c_str(),&n) < 1)
308 /* Do we already have an IP? If so, no need to resolve it. */
311 /* Gave a hook, but it wasnt one we know */
312 TreeSocket* newsocket = new TreeSocket(Utils, x->IPAddr, x->Port, x->Timeout ? x->Timeout : 10,
313 x->Name.c_str(), x->Bind, y, x->Hook);
314 if (newsocket->GetFd() > -1)
316 /* Handled automatically on success */
320 ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",
321 x->Name.c_str(), newsocket->getError().c_str());
322 ServerInstance->GlobalCulls.AddItem(newsocket);
330 ServernameResolver* snr = new ServernameResolver(Utils, x->IPAddr, x, cached, start_type, y);
331 ServerInstance->AddResolver(snr, cached);
333 catch (ModuleException& e)
335 ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(), e.GetReason());
336 ConnectServer(y, false);
341 void ModuleSpanningTree::AutoConnectServers(time_t curtime)
343 for (std::vector<reference<Autoconnect> >::iterator i = Utils->AutoconnectBlocks.begin(); i < Utils->AutoconnectBlocks.end(); ++i)
346 if (curtime >= x->NextConnectTime)
348 x->NextConnectTime = curtime + x->Period;
349 ConnectServer(x, true);
354 void ModuleSpanningTree::DoConnectTimeout(time_t curtime)
356 std::map<TreeSocket*, std::pair<std::string, int> >::iterator i = Utils->timeoutlist.begin();
357 while (i != Utils->timeoutlist.end())
359 TreeSocket* s = i->first;
360 std::pair<std::string, int> p = i->second;
361 std::map<TreeSocket*, std::pair<std::string, int> >::iterator me = i;
363 if (curtime > s->age + p.second)
365 ServerInstance->SNO->WriteToSnoMask('l',"CONNECT: Error connecting \002%s\002 (timeout of %d seconds)",p.first.c_str(),p.second);
366 Utils->timeoutlist.erase(me);
368 ServerInstance->GlobalCulls.AddItem(s);
373 ModResult ModuleSpanningTree::HandleVersion(const std::vector<std::string>& parameters, User* user)
375 // we've already checked if pcnt > 0, so this is safe
376 TreeServer* found = Utils->FindServerMask(parameters[0]);
379 std::string Version = found->GetVersion();
380 user->WriteNumeric(351, "%s :%s",user->nick.c_str(),Version.c_str());
381 if (found == Utils->TreeRoot)
383 ServerInstance->Config->Send005(user);
388 user->WriteNumeric(402, "%s %s :No such server",user->nick.c_str(),parameters[0].c_str());
393 /* This method will attempt to get a message to a remote user.
395 void ModuleSpanningTree::RemoteMessage(User* user, const char* format, ...)
400 va_start(argsPtr, format);
401 vsnprintf(text, MAXBUF, format, argsPtr);
405 user->WriteServ("NOTICE %s :%s", user->nick.c_str(), text);
407 ServerInstance->PI->SendUserNotice(user, text);
410 ModResult ModuleSpanningTree::HandleConnect(const std::vector<std::string>& parameters, User* user)
412 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
415 if (InspIRCd::Match(x->Name.c_str(),parameters[0]))
417 if (InspIRCd::Match(ServerInstance->Config->ServerName, assign(x->Name)))
419 RemoteMessage(user, "*** CONNECT: Server \002%s\002 is ME, not connecting.",x->Name.c_str());
423 TreeServer* CheckDupe = Utils->FindServer(x->Name.c_str());
426 RemoteMessage(user, "*** CONNECT: Connecting to server: \002%s\002 (%s:%d)",x->Name.c_str(),(x->HiddenFromStats ? "<hidden>" : x->IPAddr.c_str()),x->Port);
432 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());
437 RemoteMessage(user, "*** CONNECT: No server matching \002%s\002 could be found in the config file.",parameters[0].c_str());
441 void ModuleSpanningTree::OnGetServerDescription(const std::string &servername,std::string &description)
443 TreeServer* s = Utils->FindServer(servername);
446 description = s->GetDesc();
450 void ModuleSpanningTree::OnUserInvite(User* source,User* dest,Channel* channel, time_t expiry)
452 if (IS_LOCAL(source))
454 parameterlist params;
455 params.push_back(dest->uuid);
456 params.push_back(channel->name);
457 params.push_back(ConvToStr(expiry));
458 Utils->DoOneToMany(source->uuid,"INVITE",params);
462 void ModuleSpanningTree::OnPostTopicChange(User* user, Channel* chan, const std::string &topic)
464 // Drop remote events on the floor.
468 parameterlist params;
469 params.push_back(chan->name);
470 params.push_back(":"+topic);
471 Utils->DoOneToMany(user->uuid,"TOPIC",params);
474 void ModuleSpanningTree::OnWallops(User* user, const std::string &text)
478 parameterlist params;
479 params.push_back(":"+text);
480 Utils->DoOneToMany(user->uuid,"WALLOPS",params);
484 void ModuleSpanningTree::OnUserNotice(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
490 if (target_type == TYPE_USER)
492 User* d = (User*)dest;
493 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
495 parameterlist params;
496 params.push_back(d->uuid);
497 params.push_back(":"+text);
498 Utils->DoOneToOne(user->uuid,"NOTICE",params,d->server);
501 else if (target_type == TYPE_CHANNEL)
505 Channel *c = (Channel*)dest;
508 std::string cname = c->name;
510 cname = status + cname;
512 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
513 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
515 TreeSocket* Sock = i->second->GetSocket();
517 Sock->WriteLine(":"+std::string(user->uuid)+" NOTICE "+cname+" :"+text);
522 else if (target_type == TYPE_SERVER)
526 char* target = (char*)dest;
528 par.push_back(target);
529 par.push_back(":"+text);
530 Utils->DoOneToMany(user->uuid,"NOTICE",par);
535 void ModuleSpanningTree::OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
541 if (target_type == TYPE_USER)
543 // route private messages which are targetted at clients only to the server
544 // which needs to receive them
545 User* d = (User*)dest;
546 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
548 parameterlist params;
549 params.push_back(d->uuid);
550 params.push_back(":"+text);
551 Utils->DoOneToOne(user->uuid,"PRIVMSG",params,d->server);
554 else if (target_type == TYPE_CHANNEL)
558 Channel *c = (Channel*)dest;
561 std::string cname = c->name;
563 cname = status + cname;
565 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
566 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
568 TreeSocket* Sock = i->second->GetSocket();
570 Sock->WriteLine(":"+std::string(user->uuid)+" PRIVMSG "+cname+" :"+text);
575 else if (target_type == TYPE_SERVER)
579 char* target = (char*)dest;
581 par.push_back(target);
582 par.push_back(":"+text);
583 Utils->DoOneToMany(user->uuid,"PRIVMSG",par);
588 void ModuleSpanningTree::OnBackgroundTimer(time_t curtime)
590 AutoConnectServers(curtime);
591 DoPingChecks(curtime);
592 DoConnectTimeout(curtime);
595 void ModuleSpanningTree::OnUserConnect(User* user)
600 parameterlist params;
601 params.push_back(user->uuid);
602 params.push_back(ConvToStr(user->age));
603 params.push_back(user->nick);
604 params.push_back(user->host);
605 params.push_back(user->dhost);
606 params.push_back(user->ident);
607 params.push_back(user->GetIPString());
608 params.push_back(ConvToStr(user->signon));
609 params.push_back("+"+std::string(user->FormatModes(true)));
610 params.push_back(":"+std::string(user->fullname));
611 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "UID", params);
613 Utils->TreeRoot->SetUserCount(1); // increment by 1
616 void ModuleSpanningTree::OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
618 // Only do this for local users
619 if (IS_LOCAL(memb->user))
621 parameterlist params;
622 // set up their permissions and the channel TS with FJOIN.
623 // All users are FJOINed now, because a module may specify
624 // new joining permissions for the user.
625 params.push_back(memb->chan->name);
626 params.push_back(ConvToStr(memb->chan->age));
627 params.push_back(std::string("+") + memb->chan->ChanModes(true));
628 params.push_back(memb->modes+","+std::string(memb->user->uuid));
629 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FJOIN",params);
633 ModResult ModuleSpanningTree::OnChangeLocalUserHost(User* user, const std::string &newhost)
635 if (user->registered != REG_ALL)
636 return MOD_RES_PASSTHRU;
638 parameterlist params;
639 params.push_back(newhost);
640 Utils->DoOneToMany(user->uuid,"FHOST",params);
641 return MOD_RES_PASSTHRU;
644 void ModuleSpanningTree::OnChangeName(User* user, const std::string &gecos)
646 // only occurs for local clients
647 if (user->registered != REG_ALL)
650 parameterlist params;
651 params.push_back(gecos);
652 Utils->DoOneToMany(user->uuid,"FNAME",params);
655 void ModuleSpanningTree::OnChangeIdent(User* user, const std::string &ident)
657 // only occurs for local clients
658 if (user->registered != REG_ALL)
661 parameterlist params;
662 params.push_back(ident);
663 Utils->DoOneToMany(user->uuid,"FIDENT",params);
666 void ModuleSpanningTree::OnUserPart(Membership* memb, std::string &partmessage, CUList& excepts)
668 if (IS_LOCAL(memb->user))
670 parameterlist params;
671 params.push_back(memb->chan->name);
672 if (!partmessage.empty())
673 params.push_back(":"+partmessage);
674 Utils->DoOneToMany(memb->user->uuid,"PART",params);
678 void ModuleSpanningTree::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
680 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
682 parameterlist params;
684 if (oper_message != reason)
686 params.push_back(":"+oper_message);
687 Utils->DoOneToMany(user->uuid,"OPERQUIT",params);
690 params.push_back(":"+reason);
691 Utils->DoOneToMany(user->uuid,"QUIT",params);
694 // Regardless, We need to modify the user Counts..
695 TreeServer* SourceServer = Utils->FindServer(user->server);
698 SourceServer->SetUserCount(-1); // decrement by 1
702 void ModuleSpanningTree::OnUserPostNick(User* user, const std::string &oldnick)
706 parameterlist params;
707 params.push_back(user->nick);
709 /** IMPORTANT: We don't update the TS if the oldnick is just a case change of the newnick!
711 if (irc::string(user->nick.c_str()) != assign(oldnick))
712 user->age = ServerInstance->Time();
714 params.push_back(ConvToStr(user->age));
715 Utils->DoOneToMany(user->uuid,"NICK",params);
717 else if (!loopCall && user->nick == user->uuid)
719 parameterlist params;
720 params.push_back(user->uuid);
721 params.push_back(ConvToStr(user->age));
722 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"SAVE",params);
726 void ModuleSpanningTree::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts)
728 parameterlist params;
729 params.push_back(memb->chan->name);
730 params.push_back(memb->user->uuid);
731 params.push_back(":"+reason);
732 if (IS_LOCAL(source))
734 Utils->DoOneToMany(source->uuid,"KICK",params);
736 else if (IS_SERVER(source) && source != Utils->ServerUser)
738 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"KICK",params);
742 void ModuleSpanningTree::OnRemoteKill(User* source, User* dest, const std::string &reason, const std::string &operreason)
744 if (!IS_LOCAL(source))
745 return; // Only start routing if we're origin.
747 ServerInstance->OperQuit.set(dest, operreason);
748 parameterlist params;
749 params.push_back(":"+operreason);
750 Utils->DoOneToMany(dest->uuid,"OPERQUIT",params);
752 params.push_back(dest->uuid);
753 params.push_back(":"+reason);
754 Utils->DoOneToMany(source->uuid,"KILL",params);
757 void ModuleSpanningTree::OnPreRehash(User* user, const std::string ¶meter)
759 ServerInstance->Logs->Log("remoterehash", DEBUG, "called with param %s", parameter.c_str());
761 // Send out to other servers
762 if (!parameter.empty() && parameter[0] != '-')
764 parameterlist params;
765 params.push_back(parameter);
766 Utils->DoOneToAllButSender(user ? user->uuid : ServerInstance->Config->GetSID(), "REHASH", params, user ? user->server : ServerInstance->Config->ServerName);
770 void ModuleSpanningTree::OnRehash(User* user)
772 // Re-read config stuff
773 Utils->ReadConfiguration(true);
776 void ModuleSpanningTree::OnLoadModule(Module* mod)
778 this->RedoConfig(mod);
781 void ModuleSpanningTree::OnUnloadModule(Module* mod)
783 this->RedoConfig(mod);
786 void ModuleSpanningTree::RedoConfig(Module* mod)
788 /* If m_sha256.so is loaded (we use this for HMAC) or any module implementing a BufferedSocket interface is loaded,
789 * then we need to re-read our config again taking this into account.
791 modulelist* ml = ServerInstance->Modules->FindInterface("BufferedSocketHook");
792 bool IsBufferSocketModule = false;
794 /* Did we find any modules? */
795 if (ml && std::find(ml->begin(), ml->end(), mod) != ml->end())
796 IsBufferSocketModule = true;
798 if (mod->ModuleSourceFile == "m_sha256.so" || IsBufferSocketModule)
800 Utils->ReadConfiguration(true);
804 // note: the protocol does not allow direct umode +o except
805 // via NICK with 8 params. sending OPERTYPE infers +o modechange
807 void ModuleSpanningTree::OnOper(User* user, const std::string &opertype)
811 parameterlist params;
812 params.push_back(opertype);
813 Utils->DoOneToMany(user->uuid,"OPERTYPE",params);
817 void ModuleSpanningTree::OnAddLine(User* user, XLine *x)
819 if (!x->IsBurstable() || loopCall)
823 snprintf(data,MAXBUF,"%s %s %s %lu %lu :%s", x->type.c_str(), x->Displayable(),
824 ServerInstance->Config->ServerName.c_str(), (unsigned long)x->set_time, (unsigned long)x->duration, x->reason.c_str());
825 parameterlist params;
826 params.push_back(data);
830 /* Server-set lines */
831 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "ADDLINE", params);
833 else if (IS_LOCAL(user))
836 Utils->DoOneToMany(user->uuid, "ADDLINE", params);
840 void ModuleSpanningTree::OnDelLine(User* user, XLine *x)
846 snprintf(data,MAXBUF,"%s %s", x->type.c_str(), x->Displayable());
847 parameterlist params;
848 params.push_back(data);
852 /* Server-unset lines */
853 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "DELLINE", params);
855 else if (IS_LOCAL(user))
857 /* User-unset lines */
858 Utils->DoOneToMany(user->uuid, "DELLINE", params);
862 void ModuleSpanningTree::OnMode(User* user, void* dest, int target_type, const parameterlist &text, const std::vector<TranslateType> &translate)
864 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
866 parameterlist params;
868 std::string output_text;
870 ServerInstance->Parser->TranslateUIDs(translate, text, output_text);
872 if (target_type == TYPE_USER)
874 User* u = (User*)dest;
875 params.push_back(u->uuid);
876 params.push_back(output_text);
881 Channel* c = (Channel*)dest;
882 params.push_back(c->name);
883 params.push_back(ConvToStr(c->age));
884 params.push_back(output_text);
888 Utils->DoOneToMany(user->uuid, command, params);
892 ModResult ModuleSpanningTree::OnSetAway(User* user, const std::string &awaymsg)
898 parameterlist params;
899 Utils->DoOneToMany(user->uuid,"AWAY",params);
903 parameterlist params;
904 params.push_back(ConvToStr(user->awaytime));
905 params.push_back(":" + awaymsg);
906 Utils->DoOneToMany(user->uuid,"AWAY",params);
910 return MOD_RES_PASSTHRU;
913 void ModuleSpanningTree::ProtoSendMode(void* opaque, TargetTypeFlags target_type, void* target, const parameterlist &modeline, const std::vector<TranslateType> &translate)
915 TreeSocket* s = (TreeSocket*)opaque;
916 std::string output_text;
918 ServerInstance->Parser->TranslateUIDs(translate, modeline, output_text);
922 if (target_type == TYPE_USER)
924 User* u = (User*)target;
925 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" MODE "+u->uuid+" "+output_text);
927 else if (target_type == TYPE_CHANNEL)
929 Channel* c = (Channel*)target;
930 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" FMODE "+c->name+" "+ConvToStr(c->age)+" "+output_text);
935 void ModuleSpanningTree::ProtoSendMetaData(void* opaque, Extensible* target, const std::string &extname, const std::string &extdata)
937 TreeSocket* s = static_cast<TreeSocket*>(opaque);
938 User* u = dynamic_cast<User*>(target);
939 Channel* c = dynamic_cast<Channel*>(target);
941 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+u->uuid+" "+extname+" :"+extdata);
943 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+c->name+" "+extname+" :"+extdata);
945 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA * "+extname+" :"+extdata);
948 CullResult ModuleSpanningTree::cull()
951 ServerInstance->Timers->DelTimer(RefreshTimer);
952 ServerInstance->Modules->DoneWithInterface("BufferedSocketHook");
953 return this->Module::cull();
956 ModuleSpanningTree::~ModuleSpanningTree()
958 delete ServerInstance->PI;
959 ServerInstance->PI = new ProtocolInterface;
961 /* This will also free the listeners */
964 delete command_rconnect;
965 delete command_rsquit;
968 Version ModuleSpanningTree::GetVersion()
970 return Version("Allows servers to be linked", VF_VENDOR);
973 /* It is IMPORTANT that m_spanningtree is the last module in the chain
974 * so that any activity it sees is FINAL, e.g. we arent going to send out
975 * a NICK message before m_cloaking has finished putting the +x on the user,
977 * Therefore, we return PRIORITY_LAST to make sure we end up at the END of
978 * the module call queue.
980 void ModuleSpanningTree::Prioritize()
982 ServerInstance->Modules->SetPriority(this, PRIORITY_LAST);
985 MODULE_INIT(ModuleSpanningTree)