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 */
17 #include "commands/cmd_whois.h"
18 #include "commands/cmd_stats.h"
21 #include "../transport.h"
23 #include "cachetimer.h"
24 #include "resolvers.h"
27 #include "treeserver.h"
29 #include "treesocket.h"
32 #include "protocolinterface.h"
34 /* $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 */
36 ModuleSpanningTree::ModuleSpanningTree(InspIRCd* Me)
37 : Module(Me), max_local(0), max_global(0)
39 ServerInstance->Modules->UseInterface("BufferedSocketHook");
40 Utils = new SpanningTreeUtilities(ServerInstance, this);
41 command_rconnect = new CommandRConnect(ServerInstance, this, Utils);
42 ServerInstance->AddCommand(command_rconnect);
43 command_rsquit = new CommandRSQuit(ServerInstance, this, Utils);
44 ServerInstance->AddCommand(command_rsquit);
45 RefreshTimer = new CacheRefreshTimer(ServerInstance, Utils);
46 ServerInstance->Timers->AddTimer(RefreshTimer);
48 Implementation eventlist[] =
50 I_OnPreCommand, I_OnGetServerDescription, I_OnUserInvite, I_OnPostLocalTopicChange,
51 I_OnWallops, I_OnUserNotice, I_OnUserMessage, I_OnBackgroundTimer,
52 I_OnUserJoin, I_OnChangeLocalUserHost, I_OnChangeName, I_OnUserPart, I_OnUnloadModule,
53 I_OnUserQuit, I_OnUserPostNick, I_OnUserKick, I_OnRemoteKill, I_OnRehash, I_OnPreRehash,
54 I_OnOper, I_OnAddLine, I_OnDelLine, I_ProtoSendMode, I_OnMode, I_OnLoadModule,
55 I_OnStats, I_ProtoSendMetaData, I_OnEvent, I_OnSetAway, I_OnPostCommand
57 ServerInstance->Modules->Attach(eventlist, this, 30);
59 delete ServerInstance->PI;
60 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 ServerInstance->PI->Introduce(*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.c_str(),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 std::vector<std::string>& parameters, User* user)
115 ShowLinks(Utils->TreeRoot,user,0);
116 user->WriteNumeric(365, "%s * :End of /LINKS list.",user->nick.c_str());
120 void ModuleSpanningTree::HandleLusers(const std::vector<std::string>& parameters, 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) && (!IS_OPER(user)))
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.c_str(),
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.c_str(),ServerInstance->Users->OperCount());
156 if (ServerInstance->Users->UnregisteredUserCount())
157 user->WriteNumeric(253, "%s %d :unknown connections",user->nick.c_str(),ServerInstance->Users->UnregisteredUserCount());
159 if (ServerInstance->ChannelCount())
160 user->WriteNumeric(254, "%s %ld :channels formed",user->nick.c_str(),ServerInstance->ChannelCount());
162 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());
163 user->WriteNumeric(265, "%s :Current Local Users: %d Max: %d",user->nick.c_str(),ServerInstance->Users->LocalUserCount(),max_local);
164 user->WriteNumeric(266, "%s :Current Global Users: %d Max: %d",user->nick.c_str(),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)
185 * Cancel remote burst mode on any servers which still have it enabled due to latency/lack of data.
186 * This prevents lost REMOTECONNECT notices
189 gettimeofday(&t, NULL);
190 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
192 for (server_hash::iterator i = Utils->serverlist.begin(); i != Utils->serverlist.end(); i++)
194 TreeServer *s = i->second;
196 // Fix for bug #792, do not ping servers that are not connected yet!
197 // Remote servers have Socket == NULL and local connected servers have
198 // Socket->LinkState == CONNECTED
199 if (s->GetSocket() && s->GetSocket()->GetLinkState() != CONNECTED)
202 // Now do PING checks on all servers
203 TreeServer *mts = Utils->BestRouteTo(s->GetID());
207 // Only ping if this server needs one
208 if (curtime >= s->NextPingTime())
210 // And if they answered the last
211 if (s->AnsweredLastPing())
213 // They did, send a ping to them
214 s->SetNextPingTime(curtime + Utils->PingFreq);
215 TreeSocket *tsock = mts->GetSocket();
217 // ... if we can find a proper route to them
220 tsock->WriteLine(std::string(":") + ServerInstance->Config->GetSID() + " PING " +
221 ServerInstance->Config->GetSID() + " " + s->GetID());
222 s->LastPingMsec = ts;
227 // They didn't answer the last ping, if they are locally connected, get rid of them.
228 TreeSocket *sock = s->GetSocket();
231 sock->SendError("Ping timeout");
232 sock->Squit(s,"Ping timeout");
233 ServerInstance->SE->DelFd(sock);
240 // If warn on ping enabled and not warned and the difference is sufficient and they didn't answer the last ping...
241 if ((Utils->PingWarnTime) && (!s->Warned) && (curtime >= s->NextPingTime() - (Utils->PingFreq - Utils->PingWarnTime)) && (!s->AnsweredLastPing()))
243 /* The server hasnt responded, send a warning to opers */
244 ServerInstance->SNO->WriteToSnoMask('l',"Server \002%s\002 has not responded to PING for %d seconds, high latency.", s->GetName().c_str(), Utils->PingWarnTime);
251 void ModuleSpanningTree::ConnectServer(Link* x)
255 if (InspIRCd::Match(ServerInstance->Config->ServerName, assign(x->Name)))
257 this->ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Not connecting to myself.");
261 QueryType start_type = DNS_QUERY_A;
263 start_type = DNS_QUERY_AAAA;
264 if (strchr(x->IPAddr.c_str(),':'))
267 if (inet_pton(AF_INET6, x->IPAddr.c_str(), &n) < 1)
274 if (inet_aton(x->IPAddr.c_str(),&n) < 1)
278 /* Do we already have an IP? If so, no need to resolve it. */
281 /* Gave a hook, but it wasnt one we know */
282 if ((!x->Hook.empty()) && (Utils->hooks.find(x->Hook.c_str()) == Utils->hooks.end()))
284 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()]);
285 if (newsocket->GetFd() > -1)
287 /* Handled automatically on success */
291 this->ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(),strerror(errno));
292 if (ServerInstance->SocketCull.find(newsocket) == ServerInstance->SocketCull.end())
293 ServerInstance->SocketCull[newsocket] = newsocket;
294 Utils->DoFailOver(x);
302 ServernameResolver* snr = new ServernameResolver((Module*)this, Utils, ServerInstance,x->IPAddr, *x, cached, start_type);
303 ServerInstance->AddResolver(snr, cached);
305 catch (ModuleException& e)
307 this->ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(), e.GetReason());
308 Utils->DoFailOver(x);
313 void ModuleSpanningTree::AutoConnectServers(time_t curtime)
315 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
317 if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
319 x->NextConnectTime = curtime + x->AutoConnect;
320 TreeServer* CheckDupe = Utils->FindServer(x->Name.c_str());
321 if (x->FailOver.length())
323 TreeServer* CheckFailOver = Utils->FindServer(x->FailOver.c_str());
326 /* The failover for this server is currently a member of the network.
327 * The failover probably succeeded, where the main link did not.
328 * Don't try the main link until the failover is gone again.
335 // an autoconnected server is not connected. Check if its time to connect it
336 ServerInstance->SNO->WriteToSnoMask('l',"AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
337 this->ConnectServer(&(*x));
343 void ModuleSpanningTree::DoConnectTimeout(time_t curtime)
345 std::vector<Link*> failovers;
346 for (std::map<TreeSocket*, std::pair<std::string, int> >::iterator i = Utils->timeoutlist.begin(); i != Utils->timeoutlist.end(); i++)
348 TreeSocket* s = i->first;
349 std::pair<std::string, int> p = i->second;
350 if (curtime > s->age + p.second)
352 ServerInstance->SNO->WriteToSnoMask('l',"CONNECT: Error connecting \002%s\002 (timeout of %d seconds)",p.first.c_str(),p.second);
353 ServerInstance->SE->DelFd(s);
355 Link* MyLink = Utils->FindLink(p.first);
357 failovers.push_back(MyLink);
360 /* Trigger failover for each timed out socket */
361 for (std::vector<Link*>::const_iterator n = failovers.begin(); n != failovers.end(); ++n)
363 Utils->DoFailOver(*n);
367 int ModuleSpanningTree::HandleVersion(const std::vector<std::string>& parameters, User* user)
369 // we've already checked if pcnt > 0, so this is safe
370 TreeServer* found = Utils->FindServerMask(parameters[0]);
373 std::string Version = found->GetVersion();
374 user->WriteNumeric(351, "%s :%s",user->nick.c_str(),Version.c_str());
375 if (found == Utils->TreeRoot)
377 ServerInstance->Config->Send005(user);
382 user->WriteNumeric(402, "%s %s :No such server",user->nick.c_str(),parameters[0].c_str());
387 /* This method will attempt to get a message to a remote user.
389 void ModuleSpanningTree::RemoteMessage(User* user, const char* format, ...)
394 va_start(argsPtr, format);
395 vsnprintf(text, MAXBUF, format, argsPtr);
399 user->WriteServ("NOTICE %s :%s", user->nick.c_str(), text);
401 ServerInstance->PI->SendUserNotice(user, text);
404 int ModuleSpanningTree::HandleConnect(const std::vector<std::string>& parameters, User* user)
406 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
408 if (InspIRCd::Match(x->Name.c_str(),parameters[0]))
410 if (InspIRCd::Match(ServerInstance->Config->ServerName, assign(x->Name)))
412 RemoteMessage(user, "*** CONNECT: Server \002%s\002 is ME, not connecting.",x->Name.c_str());
416 TreeServer* CheckDupe = Utils->FindServer(x->Name.c_str());
419 RemoteMessage(user, "*** CONNECT: Connecting to server: \002%s\002 (%s:%d)",x->Name.c_str(),(x->HiddenFromStats ? "<hidden>" : x->IPAddr.c_str()),x->Port);
420 ConnectServer(&(*x));
425 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());
430 RemoteMessage(user, "*** CONNECT: No server matching \002%s\002 could be found in the config file.",parameters[0].c_str());
434 void ModuleSpanningTree::OnGetServerDescription(const std::string &servername,std::string &description)
436 TreeServer* s = Utils->FindServer(servername);
439 description = s->GetDesc();
443 void ModuleSpanningTree::OnUserInvite(User* source,User* dest,Channel* channel, time_t expiry)
445 if (IS_LOCAL(source))
447 std::deque<std::string> params;
448 params.push_back(dest->uuid);
449 params.push_back(channel->name);
450 params.push_back(ConvToStr(expiry));
451 Utils->DoOneToMany(source->uuid,"INVITE",params);
455 void ModuleSpanningTree::OnPostLocalTopicChange(User* user, Channel* chan, const std::string &topic)
457 std::deque<std::string> params;
458 params.push_back(chan->name);
459 params.push_back(":"+topic);
460 Utils->DoOneToMany(user->uuid,"TOPIC",params);
463 void ModuleSpanningTree::OnWallops(User* user, const std::string &text)
467 std::deque<std::string> params;
468 params.push_back(":"+text);
469 Utils->DoOneToMany(user->uuid,"WALLOPS",params);
473 void ModuleSpanningTree::OnUserNotice(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
479 if (target_type == TYPE_USER)
481 User* d = (User*)dest;
482 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
484 std::deque<std::string> params;
486 params.push_back(d->uuid);
487 params.push_back(":"+text);
488 Utils->DoOneToOne(user->uuid,"NOTICE",params,d->server);
491 else if (target_type == TYPE_CHANNEL)
495 Channel *c = (Channel*)dest;
498 std::string cname = c->name;
500 cname = status + cname;
502 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
503 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
505 TreeSocket* Sock = i->second->GetSocket();
507 Sock->WriteLine(":"+std::string(user->uuid)+" NOTICE "+cname+" :"+text);
512 else if (target_type == TYPE_SERVER)
516 char* target = (char*)dest;
517 std::deque<std::string> par;
518 par.push_back(target);
519 par.push_back(":"+text);
520 Utils->DoOneToMany(user->uuid,"NOTICE",par);
525 void ModuleSpanningTree::OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
531 if (target_type == TYPE_USER)
533 // route private messages which are targetted at clients only to the server
534 // which needs to receive them
535 User* d = (User*)dest;
536 if ((d->GetFd() < 0) && (IS_LOCAL(user)))
538 std::deque<std::string> params;
540 params.push_back(d->uuid);
541 params.push_back(":"+text);
542 Utils->DoOneToOne(user->uuid,"PRIVMSG",params,d->server);
545 else if (target_type == TYPE_CHANNEL)
549 Channel *c = (Channel*)dest;
552 std::string cname = c->name;
554 cname = status + cname;
556 Utils->GetListOfServersForChannel(c,list,status,exempt_list);
557 for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
559 TreeSocket* Sock = i->second->GetSocket();
561 Sock->WriteLine(":"+std::string(user->uuid)+" PRIVMSG "+cname+" :"+text);
566 else if (target_type == TYPE_SERVER)
570 char* target = (char*)dest;
571 std::deque<std::string> par;
572 par.push_back(target);
573 par.push_back(":"+text);
574 Utils->DoOneToMany(user->uuid,"PRIVMSG",par);
579 void ModuleSpanningTree::OnBackgroundTimer(time_t curtime)
581 AutoConnectServers(curtime);
582 DoPingChecks(curtime);
583 DoConnectTimeout(curtime);
586 void ModuleSpanningTree::OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
588 // Only do this for local users
591 std::deque<std::string> params;
592 // set up their permissions and the channel TS with FJOIN.
593 // All users are FJOINed now, because a module may specify
594 // new joining permissions for the user.
595 params.push_back(channel->name);
596 params.push_back(ConvToStr(channel->age));
597 params.push_back(std::string("+") + channel->ChanModes(true));
598 params.push_back(ServerInstance->Modes->ModeString(user, channel, false)+","+std::string(user->uuid));
599 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FJOIN",params);
603 int ModuleSpanningTree::OnChangeLocalUserHost(User* user, const std::string &newhost)
605 if (user->registered != REG_ALL)
608 std::deque<std::string> params;
609 params.push_back(newhost);
610 Utils->DoOneToMany(user->uuid,"FHOST",params);
614 void ModuleSpanningTree::OnChangeName(User* user, const std::string &gecos)
616 // only occurs for local clients
617 if (user->registered != REG_ALL)
620 std::deque<std::string> params;
621 params.push_back(gecos);
622 Utils->DoOneToMany(user->uuid,"FNAME",params);
625 void ModuleSpanningTree::OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
629 std::deque<std::string> params;
630 params.push_back(channel->name);
631 if (!partmessage.empty())
632 params.push_back(":"+partmessage);
633 Utils->DoOneToMany(user->uuid,"PART",params);
637 void ModuleSpanningTree::OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
639 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
641 std::deque<std::string> params;
643 if (oper_message != reason)
645 params.push_back(":"+oper_message);
646 Utils->DoOneToMany(user->uuid,"OPERQUIT",params);
649 params.push_back(":"+reason);
650 Utils->DoOneToMany(user->uuid,"QUIT",params);
653 // Regardless, We need to modify the user Counts..
654 TreeServer* SourceServer = Utils->FindServer(user->server);
657 SourceServer->SetUserCount(-1); // decrement by 1
661 void ModuleSpanningTree::OnUserPostNick(User* user, const std::string &oldnick)
665 std::deque<std::string> params;
666 params.push_back(user->nick);
668 /** IMPORTANT: We don't update the TS if the oldnick is just a case change of the newnick!
670 if (irc::string(user->nick.c_str()) != assign(oldnick))
671 user->age = ServerInstance->Time();
673 params.push_back(ConvToStr(user->age));
674 Utils->DoOneToMany(user->uuid,"NICK",params);
678 void ModuleSpanningTree::OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
680 std::deque<std::string> params;
681 params.push_back(chan->name);
682 params.push_back(user->uuid);
683 params.push_back(":"+reason);
684 if (IS_LOCAL(source))
686 Utils->DoOneToMany(source->uuid,"KICK",params);
688 else if (IS_FAKE(source) && source != Utils->ServerUser)
690 Utils->DoOneToMany(ServerInstance->Config->GetSID(),"KICK",params);
694 void ModuleSpanningTree::OnRemoteKill(User* source, User* dest, const std::string &reason, const std::string &operreason)
696 if (!IS_LOCAL(source))
697 return; // Only start routing if we're origin.
699 std::deque<std::string> params;
700 params.push_back(":"+reason);
701 Utils->DoOneToMany(dest->uuid,"OPERQUIT",params);
703 params.push_back(dest->uuid);
704 params.push_back(":"+reason);
705 dest->SetOperQuit(operreason);
706 Utils->DoOneToMany(source->uuid,"KILL",params);
709 void ModuleSpanningTree::OnPreRehash(User* user, const std::string ¶meter)
711 ServerInstance->Logs->Log("remoterehash", DEBUG, "called with param %s", parameter.c_str());
713 // Send out to other servers
714 if (!parameter.empty() && parameter[0] != '-')
716 std::deque<std::string> params;
717 params.push_back(parameter);
718 Utils->DoOneToAllButSender(user ? user->uuid : ServerInstance->Config->GetSID(), "REHASH", params, user ? user->server : ServerInstance->Config->ServerName);
722 void ModuleSpanningTree::OnRehash(User* user)
724 // Re-read config stuff
725 Utils->ReadConfiguration(true);
728 void ModuleSpanningTree::OnLoadModule(Module* mod, const std::string &name)
730 this->RedoConfig(mod, name);
733 void ModuleSpanningTree::OnUnloadModule(Module* mod, const std::string &name)
735 this->RedoConfig(mod, name);
738 void ModuleSpanningTree::RedoConfig(Module* mod, const std::string &name)
740 /* If m_sha256.so is loaded (we use this for HMAC) or any module implementing a BufferedSocket interface is loaded,
741 * then we need to re-read our config again taking this into account.
743 modulelist* ml = ServerInstance->Modules->FindInterface("BufferedSocketHook");
744 bool IsBufferSocketModule = false;
746 /* Did we find any modules? */
747 if (ml && std::find(ml->begin(), ml->end(), mod) != ml->end())
748 IsBufferSocketModule = true;
750 if (name == "m_sha256.so" || IsBufferSocketModule)
752 Utils->ReadConfiguration(true);
756 // note: the protocol does not allow direct umode +o except
757 // via NICK with 8 params. sending OPERTYPE infers +o modechange
759 void ModuleSpanningTree::OnOper(User* user, const std::string &opertype)
763 std::deque<std::string> params;
764 params.push_back(opertype);
765 Utils->DoOneToMany(user->uuid,"OPERTYPE",params);
769 void ModuleSpanningTree::OnAddLine(User* user, XLine *x)
771 if (!x->IsBurstable() || loopCall)
775 snprintf(data,MAXBUF,"%s %s %s %lu %lu :%s", x->type.c_str(), x->Displayable(),
776 ServerInstance->Config->ServerName, (unsigned long)x->set_time, (unsigned long)x->duration, x->reason);
777 std::deque<std::string> params;
778 params.push_back(data);
782 /* Server-set lines */
783 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "ADDLINE", params);
785 else if (IS_LOCAL(user))
788 Utils->DoOneToMany(user->uuid, "ADDLINE", params);
792 void ModuleSpanningTree::OnDelLine(User* user, XLine *x)
798 snprintf(data,MAXBUF,"%s %s", x->type.c_str(), x->Displayable());
799 std::deque<std::string> params;
800 params.push_back(data);
804 /* Server-unset lines */
805 Utils->DoOneToMany(ServerInstance->Config->GetSID(), "DELLINE", params);
807 else if (IS_LOCAL(user))
809 /* User-unset lines */
810 Utils->DoOneToMany(user->uuid, "DELLINE", params);
814 void ModuleSpanningTree::OnMode(User* user, void* dest, int target_type, const std::deque<std::string> &text, const std::deque<TranslateType> &translate)
816 if ((IS_LOCAL(user)) && (user->registered == REG_ALL))
818 std::deque<std::string> params;
820 std::string output_text;
822 ServerInstance->Parser->TranslateUIDs(translate, text, output_text);
824 if (target_type == TYPE_USER)
826 User* u = (User*)dest;
827 params.push_back(u->uuid);
828 params.push_back(output_text);
833 Channel* c = (Channel*)dest;
834 params.push_back(c->name);
835 params.push_back(ConvToStr(c->age));
836 params.push_back(output_text);
840 Utils->DoOneToMany(user->uuid, command, params);
844 int ModuleSpanningTree::OnSetAway(User* user, const std::string &awaymsg)
850 std::deque<std::string> params;
852 Utils->DoOneToMany(user->uuid,"AWAY",params);
856 std::deque<std::string> params;
857 params.push_back(":" + awaymsg);
858 Utils->DoOneToMany(user->uuid,"AWAY",params);
865 void ModuleSpanningTree::ProtoSendMode(void* opaque, TargetTypeFlags target_type, void* target, const std::deque<std::string> &modeline, const std::deque<TranslateType> &translate)
867 TreeSocket* s = (TreeSocket*)opaque;
868 std::string output_text;
870 ServerInstance->Parser->TranslateUIDs(translate, modeline, output_text);
874 if (target_type == TYPE_USER)
876 User* u = (User*)target;
877 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" MODE "+u->uuid+" "+output_text);
879 else if (target_type == TYPE_CHANNEL)
881 Channel* c = (Channel*)target;
882 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" FMODE "+c->name+" "+ConvToStr(c->age)+" "+output_text);
887 void ModuleSpanningTree::ProtoSendMetaData(void* opaque, TargetTypeFlags target_type, void* target, const std::string &extname, const std::string &extdata)
889 TreeSocket* s = (TreeSocket*)opaque;
892 if (target_type == TYPE_USER)
894 User* u = (User*)target;
895 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+u->uuid+" "+extname+" :"+extdata);
897 else if (target_type == TYPE_CHANNEL)
899 Channel* c = (Channel*)target;
900 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA "+c->name+" "+extname+" :"+extdata);
903 if (target_type == TYPE_OTHER)
905 s->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" METADATA * "+extname+" :"+extdata);
909 void ModuleSpanningTree::OnEvent(Event* event)
911 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")
912 || (event->GetEventID() == "send_modeset") || (event->GetEventID() == "send_snoset") || (event->GetEventID() == "send_push"))
914 ServerInstance->Logs->Log("m_spanningtree", DEBUG, "WARNING: Deprecated use of old 1.1 style m_spanningtree event ignored, type '"+event->GetEventID()+"'!");
918 ModuleSpanningTree::~ModuleSpanningTree()
920 delete ServerInstance->PI;
921 ServerInstance->PI = new ProtocolInterface(ServerInstance);
923 /* This will also free the listeners */
926 ServerInstance->Timers->DelTimer(RefreshTimer);
928 ServerInstance->Modules->DoneWithInterface("BufferedSocketHook");
931 Version ModuleSpanningTree::GetVersion()
933 return Version("$Id$", VF_VENDOR, API_VERSION);
936 /* It is IMPORTANT that m_spanningtree is the last module in the chain
937 * so that any activity it sees is FINAL, e.g. we arent going to send out
938 * a NICK message before m_cloaking has finished putting the +x on the user,
940 * Therefore, we return PRIORITY_LAST to make sure we end up at the END of
941 * the module call queue.
943 void ModuleSpanningTree::Prioritize()
945 ServerInstance->Modules->SetPriority(this, PRIORITY_LAST);
948 MODULE_INIT(ModuleSpanningTree)