]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_lusers.cpp
cmd_lusers Use of size() instead of counting elements
[user/henk/code/inspircd.git] / src / commands / cmd_lusers.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #include "inspircd.h"
22
23 struct LusersCounters
24 {
25         unsigned int max_local;
26         unsigned int max_global;
27
28         LusersCounters()
29                 : max_local(ServerInstance->Users->LocalUserCount() - ServerInstance->Users->UnregisteredUserCount())
30                 , max_global(ServerInstance->Users->RegisteredUserCount())
31         {
32         }
33
34         inline void UpdateMaxUsers()
35         {
36                 unsigned int current = ServerInstance->Users->LocalUserCount() - ServerInstance->Users->UnregisteredUserCount();
37                 if (current > max_local)
38                         max_local = current;
39
40                 current = ServerInstance->Users->RegisteredUserCount();
41                 if (current > max_global)
42                         max_global = current;
43         }
44 };
45
46 /** Handle /LUSERS. These command handlers can be reloaded by the core,
47  * and handle basic RFC1459 commands. Commands within modules work
48  * the same way, however, they can be fully unloaded, where these
49  * may not.
50  */
51 class CommandLusers : public Command
52 {
53         LusersCounters& counters;
54  public:
55         /** Constructor for lusers.
56          */
57         CommandLusers(Module* parent, LusersCounters& Counters)
58                 : Command(parent,"LUSERS",0,0), counters(Counters)
59         { }
60         /** Handle command.
61          * @param parameters The parameters to the comamnd
62          * @param pcnt The number of parameters passed to teh command
63          * @param user The user issuing the command
64          * @return A value from CmdResult to indicate command success or failure.
65          */
66         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
67 };
68
69 /** Handle /LUSERS
70  */
71 CmdResult CommandLusers::Handle (const std::vector<std::string>&, User *user)
72 {
73         unsigned int n_users = ServerInstance->Users->UserCount();
74         unsigned int n_invis = ServerInstance->Users->ModeCount('i');
75         ProtoServerList serverlist;
76         ServerInstance->PI->GetServerList(serverlist);
77         unsigned int n_serv = serverlist.size();
78         unsigned int n_local_servs = 0;
79         for(ProtoServerList::iterator i = serverlist.begin(); i != serverlist.end(); ++i)
80         {
81                 if (i->parentname == ServerInstance->Config->ServerName)
82                         n_local_servs++;
83         }
84         // fix for default GetServerList not returning us
85         if (!n_serv)
86                 n_serv = 1;
87
88         counters.UpdateMaxUsers();
89
90         user->WriteNumeric(251, "%s :There are %d users and %d invisible on %d servers",user->nick.c_str(),
91                         n_users-n_invis, n_invis, n_serv);
92
93         if (ServerInstance->Users->OperCount())
94                 user->WriteNumeric(252, "%s %d :operator(s) online",user->nick.c_str(),ServerInstance->Users->OperCount());
95
96         if (ServerInstance->Users->UnregisteredUserCount())
97                 user->WriteNumeric(253, "%s %d :unknown connections",user->nick.c_str(),ServerInstance->Users->UnregisteredUserCount());
98
99         user->WriteNumeric(254, "%s %ld :channels formed",user->nick.c_str(),ServerInstance->ChannelCount());
100         user->WriteNumeric(255, "%s :I have %d clients and %d servers",user->nick.c_str(),ServerInstance->Users->LocalUserCount(),n_local_servs);
101         user->WriteNumeric(265, "%s :Current Local Users: %d  Max: %d", user->nick.c_str(), ServerInstance->Users->LocalUserCount(), counters.max_local);
102         user->WriteNumeric(266, "%s :Current Global Users: %d  Max: %d", user->nick.c_str(), n_users, counters.max_global);
103
104         return CMD_SUCCESS;
105 }
106
107 class ModuleLusers : public Module
108 {
109         LusersCounters counters;
110         CommandLusers cmd;
111
112  public:
113         ModuleLusers()
114                 : cmd(this, counters)
115         {
116         }
117
118         void init()
119         {
120                 ServerInstance->Modules->AddService(cmd);
121                 Implementation events[] = { I_OnPostConnect };
122                 ServerInstance->Modules->Attach(events, this, sizeof(events)/sizeof(Implementation));
123         }
124
125         void OnPostConnect(User* user)
126         {
127                 counters.UpdateMaxUsers();
128         }
129
130         Version GetVersion()
131         {
132                 return Version("LUSERS", VF_VENDOR | VF_CORE);
133         }
134 };
135
136 MODULE_INIT(ModuleLusers)