]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hideoper.cpp
6ecfc6074fd7fe6c86fdedd015d5605e9c5ab392
[user/henk/code/inspircd.git] / src / modules / m_hideoper.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23 #include "modules/stats.h"
24
25 /** Handles user mode +H
26  */
27 class HideOper : public SimpleUserModeHandler
28 {
29  public:
30         size_t opercount;
31
32         HideOper(Module* Creator) : SimpleUserModeHandler(Creator, "hideoper", 'H')
33                 , opercount(0)
34         {
35                 oper = true;
36         }
37
38         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
39         {
40                 if (SimpleUserModeHandler::OnModeChange(source, dest, channel, parameter, adding) == MODEACTION_DENY)
41                         return MODEACTION_DENY;
42
43                 if (adding)
44                         opercount++;
45                 else
46                         opercount--;
47
48                 return MODEACTION_ALLOW;
49         }
50 };
51
52 class ModuleHideOper : public Module, public Stats::EventListener, public Whois::LineEventListener
53 {
54         HideOper hm;
55         bool active;
56  public:
57         ModuleHideOper()
58                 : Stats::EventListener(this)
59                 , Whois::LineEventListener(this)
60                 , hm(this)
61                 , active(false)
62         {
63         }
64
65         Version GetVersion() CXX11_OVERRIDE
66         {
67                 return Version("Provides support for hiding oper status with user mode +H", VF_VENDOR);
68         }
69
70         void OnUserQuit(User* user, const std::string&, const std::string&) CXX11_OVERRIDE
71         {
72                 if (user->IsModeSet(hm))
73                         hm.opercount--;
74         }
75
76         ModResult OnNumeric(User* user, const Numeric::Numeric& numeric) CXX11_OVERRIDE
77         {
78                 if (numeric.GetNumeric() != RPL_LUSEROP || active || user->HasPrivPermission("users/auspex"))
79                         return MOD_RES_PASSTHRU;
80
81                 // If there are no visible operators then we shouldn't send the numeric.
82                 size_t opercount = ServerInstance->Users->all_opers.size() - hm.opercount;
83                 if (opercount)
84                 {
85                         active = true;
86                         user->WriteNumeric(RPL_LUSEROP, opercount, "operator(s) online");
87                         active = false;
88                 }
89                 return MOD_RES_DENY;
90         }
91
92         ModResult OnWhoisLine(Whois::Context& whois, Numeric::Numeric& numeric) CXX11_OVERRIDE
93         {
94                 /* Dont display numeric 313 (RPL_WHOISOPER) if they have +H set and the
95                  * person doing the WHOIS is not an oper
96                  */
97                 if (numeric.GetNumeric() != 313)
98                         return MOD_RES_PASSTHRU;
99
100                 if (!whois.GetTarget()->IsModeSet(hm))
101                         return MOD_RES_PASSTHRU;
102
103                 if (!whois.GetSource()->HasPrivPermission("users/auspex"))
104                         return MOD_RES_DENY;
105
106                 return MOD_RES_PASSTHRU;
107         }
108
109         ModResult OnSendWhoLine(User* source, const std::vector<std::string>& params, User* user, Membership* memb, Numeric::Numeric& numeric) CXX11_OVERRIDE
110         {
111                 if (user->IsModeSet(hm) && !source->HasPrivPermission("users/auspex"))
112                 {
113                         // Hide the line completely if doing a "/who * o" query
114                         if ((params.size() > 1) && (params[1].find('o') != std::string::npos))
115                                 return MOD_RES_DENY;
116
117                         // hide the "*" that marks the user as an oper from the /WHO line
118                         // #chan ident localhost insp22.test nick H@ :0 Attila
119                         if (numeric.GetParams().size() < 6)
120                                 return MOD_RES_PASSTHRU;
121
122                         std::string& param = numeric.GetParams()[5];
123                         const std::string::size_type pos = param.find('*');
124                         if (pos != std::string::npos)
125                                 param.erase(pos, 1);
126                 }
127                 return MOD_RES_PASSTHRU;
128         }
129
130         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
131         {
132                 if (stats.GetSymbol() != 'P')
133                         return MOD_RES_PASSTHRU;
134
135                 unsigned int count = 0;
136                 const UserManager::OperList& opers = ServerInstance->Users->all_opers;
137                 for (UserManager::OperList::const_iterator i = opers.begin(); i != opers.end(); ++i)
138                 {
139                         User* oper = *i;
140                         if (!oper->server->IsULine() && (stats.GetSource()->IsOper() || !oper->IsModeSet(hm)))
141                         {
142                                 LocalUser* lu = IS_LOCAL(oper);
143                                 stats.AddRow(249, oper->nick + " (" + oper->ident + "@" + oper->GetDisplayedHost() + ") Idle: " +
144                                                 (lu ? ConvToStr(ServerInstance->Time() - lu->idle_lastmsg) + " secs" : "unavailable"));
145                                 count++;
146                         }
147                 }
148                 stats.AddRow(249, ConvToStr(count)+" OPER(s)");
149
150                 return MOD_RES_DENY;
151         }
152 };
153
154 MODULE_INIT(ModuleHideOper)