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