]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hideoper.cpp
Merge branch 'insp20' into master.
[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 #include "modules/whois.h"
25
26 /** Handles user mode +H
27  */
28 class HideOper : public SimpleUserModeHandler
29 {
30  public:
31         size_t opercount;
32
33         HideOper(Module* Creator) : SimpleUserModeHandler(Creator, "hideoper", 'H')
34                 , opercount(0)
35         {
36                 oper = true;
37         }
38
39         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE
40         {
41                 if (SimpleUserModeHandler::OnModeChange(source, dest, channel, parameter, adding) == MODEACTION_DENY)
42                         return MODEACTION_DENY;
43
44                 if (adding)
45                         opercount++;
46                 else
47                         opercount--;
48
49                 return MODEACTION_ALLOW;
50         }
51 };
52
53 class ModuleHideOper : public Module, public Stats::EventListener, public Whois::LineEventListener
54 {
55         HideOper hm;
56         bool active;
57  public:
58         ModuleHideOper()
59                 : Stats::EventListener(this)
60                 , Whois::LineEventListener(this)
61                 , hm(this)
62                 , active(false)
63         {
64         }
65
66         Version GetVersion() CXX11_OVERRIDE
67         {
68                 return Version("Provides support for hiding oper status with user mode +H", VF_VENDOR);
69         }
70
71         void OnUserQuit(User* user, const std::string&, const std::string&) CXX11_OVERRIDE
72         {
73                 if (user->IsModeSet(hm))
74                         hm.opercount--;
75         }
76
77         ModResult OnNumeric(User* user, const Numeric::Numeric& numeric) CXX11_OVERRIDE
78         {
79                 if (numeric.GetNumeric() != RPL_LUSEROP || active || user->HasPrivPermission("users/auspex"))
80                         return MOD_RES_PASSTHRU;
81
82                 // If there are no visible operators then we shouldn't send the numeric.
83                 size_t opercount = ServerInstance->Users->all_opers.size() - hm.opercount;
84                 if (opercount)
85                 {
86                         active = true;
87                         user->WriteNumeric(RPL_LUSEROP, opercount, "operator(s) online");
88                         active = false;
89                 }
90                 return MOD_RES_DENY;
91         }
92
93         ModResult OnWhoisLine(Whois::Context& whois, Numeric::Numeric& numeric) CXX11_OVERRIDE
94         {
95                 /* Dont display numeric 313 (RPL_WHOISOPER) if they have +H set and the
96                  * person doing the WHOIS is not an oper
97                  */
98                 if (numeric.GetNumeric() != 313)
99                         return MOD_RES_PASSTHRU;
100
101                 if (!whois.GetTarget()->IsModeSet(hm))
102                         return MOD_RES_PASSTHRU;
103
104                 if (!whois.GetSource()->HasPrivPermission("users/auspex"))
105                         return MOD_RES_DENY;
106
107                 return MOD_RES_PASSTHRU;
108         }
109
110         ModResult OnSendWhoLine(User* source, const std::vector<std::string>& params, User* user, Membership* memb, Numeric::Numeric& numeric) CXX11_OVERRIDE
111         {
112                 if (user->IsModeSet(hm) && !source->HasPrivPermission("users/auspex"))
113                 {
114                         // Hide the line completely if doing a "/who * o" query
115                         if ((params.size() > 1) && (params[1].find('o') != std::string::npos))
116                                 return MOD_RES_DENY;
117
118                         // hide the "*" that marks the user as an oper from the /WHO line
119                         // #chan ident localhost insp22.test nick H@ :0 Attila
120                         if (numeric.GetParams().size() < 6)
121                                 return MOD_RES_PASSTHRU;
122
123                         std::string& param = numeric.GetParams()[5];
124                         const std::string::size_type pos = param.find('*');
125                         if (pos != std::string::npos)
126                                 param.erase(pos, 1);
127                 }
128                 return MOD_RES_PASSTHRU;
129         }
130
131         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
132         {
133                 if (stats.GetSymbol() != 'P')
134                         return MOD_RES_PASSTHRU;
135
136                 unsigned int count = 0;
137                 const UserManager::OperList& opers = ServerInstance->Users->all_opers;
138                 for (UserManager::OperList::const_iterator i = opers.begin(); i != opers.end(); ++i)
139                 {
140                         User* oper = *i;
141                         if (!oper->server->IsULine() && (stats.GetSource()->IsOper() || !oper->IsModeSet(hm)))
142                         {
143                                 LocalUser* lu = IS_LOCAL(oper);
144                                 stats.AddRow(249, oper->nick + " (" + oper->ident + "@" + oper->GetDisplayedHost() + ") Idle: " +
145                                                 (lu ? ConvToStr(ServerInstance->Time() - lu->idle_lastmsg) + " secs" : "unavailable"));
146                                 count++;
147                         }
148                 }
149                 stats.AddRow(249, ConvToStr(count)+" OPER(s)");
150
151                 return MOD_RES_DENY;
152         }
153 };
154
155 MODULE_INIT(ModuleHideOper)