2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 unsigned int max_local;
26 unsigned int max_global;
27 unsigned int invisible;
30 : max_local(ServerInstance->Users->LocalUserCount())
31 , max_global(ServerInstance->Users->RegisteredUserCount())
32 , invisible(ServerInstance->Users->ModeCount('i'))
36 inline void UpdateMaxUsers()
38 unsigned int current = ServerInstance->Users->LocalUserCount();
39 if (current > max_local)
42 current = ServerInstance->Users->RegisteredUserCount();
43 if (current > max_global)
48 /** Handle /LUSERS. These command handlers can be reloaded by the core,
49 * and handle basic RFC1459 commands. Commands within modules work
50 * the same way, however, they can be fully unloaded, where these
53 class CommandLusers : public Command
55 LusersCounters& counters;
57 /** Constructor for lusers.
59 CommandLusers(Module* parent, LusersCounters& Counters)
60 : Command(parent,"LUSERS",0,0), counters(Counters)
63 * @param parameters The parameters to the comamnd
64 * @param pcnt The number of parameters passed to teh command
65 * @param user The user issuing the command
66 * @return A value from CmdResult to indicate command success or failure.
68 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
73 CmdResult CommandLusers::Handle (const std::vector<std::string>&, User *user)
75 unsigned int n_users = ServerInstance->Users->RegisteredUserCount();
76 ProtoServerList serverlist;
77 ServerInstance->PI->GetServerList(serverlist);
78 unsigned int n_serv = serverlist.size();
79 unsigned int n_local_servs = 0;
80 for(ProtoServerList::iterator i = serverlist.begin(); i != serverlist.end(); ++i)
82 if (i->parentname == ServerInstance->Config->ServerName)
85 // fix for default GetServerList not returning us
89 counters.UpdateMaxUsers();
91 user->WriteNumeric(251, "%s :There are %d users and %d invisible on %d servers",user->nick.c_str(),
92 n_users - counters.invisible, counters.invisible, n_serv);
94 if (ServerInstance->Users->OperCount())
95 user->WriteNumeric(252, "%s %d :operator(s) online",user->nick.c_str(),ServerInstance->Users->OperCount());
97 if (ServerInstance->Users->UnregisteredUserCount())
98 user->WriteNumeric(253, "%s %d :unknown connections",user->nick.c_str(),ServerInstance->Users->UnregisteredUserCount());
100 user->WriteNumeric(254, "%s %ld :channels formed",user->nick.c_str(),ServerInstance->ChannelCount());
101 user->WriteNumeric(255, "%s :I have %d clients and %d servers",user->nick.c_str(),ServerInstance->Users->LocalUserCount(),n_local_servs);
102 user->WriteNumeric(265, "%s :Current Local Users: %d Max: %d", user->nick.c_str(), ServerInstance->Users->LocalUserCount(), counters.max_local);
103 user->WriteNumeric(266, "%s :Current Global Users: %d Max: %d", user->nick.c_str(), n_users, counters.max_global);
108 class InvisibleWatcher : public ModeWatcher
110 unsigned int& invisible;
112 InvisibleWatcher(Module* mod, unsigned int& Invisible)
113 : ModeWatcher(mod, 'i', MODETYPE_USER), invisible(Invisible)
117 void AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding, ModeType type)
119 if (dest->registered != REG_ALL)
129 class ModuleLusers : public Module
131 LusersCounters counters;
137 : cmd(this, counters), mw(this, counters.invisible)
143 ServerInstance->Modules->AddService(cmd);
144 Implementation events[] = { I_OnPostConnect, I_OnUserQuit };
145 ServerInstance->Modules->Attach(events, this, sizeof(events)/sizeof(Implementation));
146 ServerInstance->Modes->AddModeWatcher(&mw);
149 void OnPostConnect(User* user)
151 counters.UpdateMaxUsers();
152 if (user->IsModeSet('i'))
153 counters.invisible++;
156 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
158 if (user->IsModeSet('i'))
159 counters.invisible--;
164 ServerInstance->Modes->DelModeWatcher(&mw);
169 return Version("LUSERS", VF_VENDOR | VF_CORE);
173 MODULE_INIT(ModuleLusers)