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