]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_hideoper.cpp
Release v2.0.21
[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 /* $ModDesc: Provides support for hiding oper status with user mode +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)
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
54 {
55         HideOper hm;
56         bool active;
57  public:
58         ModuleHideOper()
59                 : hm(this)
60                 , active(false)
61         {
62         }
63
64         void init()
65         {
66                 ServerInstance->Modules->AddService(hm);
67                 Implementation eventlist[] = { I_OnWhoisLine, I_OnSendWhoLine, I_OnStats, I_OnNumeric, I_OnUserQuit };
68                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
69         }
70
71
72         virtual ~ModuleHideOper()
73         {
74         }
75
76         virtual Version GetVersion()
77         {
78                 return Version("Provides support for hiding oper status with user mode +H", VF_VENDOR);
79         }
80
81         void OnUserQuit(User* user, const std::string&, const std::string&)
82         {
83                 if (user->IsModeSet('H'))
84                         hm.opercount--;
85         }
86
87         ModResult OnNumeric(User* user, unsigned int numeric, const std::string& text)
88         {
89                 if (numeric != 252 || active || user->HasPrivPermission("users/auspex"))
90                         return MOD_RES_PASSTHRU;
91
92                 // If there are no visible operators then we shouldn't send the numeric.
93                 size_t opercount = ServerInstance->Users->all_opers.size() - hm.opercount;
94                 if (opercount)
95                 {
96                         active = true;
97                         user->WriteNumeric(252, "%s %lu :operator(s) online", user->nick.c_str(),  opercount);
98                         active = false;
99                 }
100                 return MOD_RES_DENY;
101         }
102
103         ModResult OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
104         {
105                 /* Dont display numeric 313 (RPL_WHOISOPER) if they have +H set and the
106                  * person doing the WHOIS is not an oper
107                  */
108                 if (numeric != 313)
109                         return MOD_RES_PASSTHRU;
110
111                 if (!dest->IsModeSet('H'))
112                         return MOD_RES_PASSTHRU;
113
114                 if (!user->HasPrivPermission("users/auspex"))
115                         return MOD_RES_DENY;
116
117                 return MOD_RES_PASSTHRU;
118         }
119
120         void OnSendWhoLine(User* source, const std::vector<std::string>& params, User* user, std::string& line)
121         {
122                 if (user->IsModeSet('H') && !source->HasPrivPermission("users/auspex"))
123                 {
124                         // hide the "*" that marks the user as an oper from the /WHO line
125                         std::string::size_type spcolon = line.find(" :");
126                         if (spcolon == std::string::npos)
127                                 return; // Another module hid the user completely
128                         std::string::size_type sp = line.rfind(' ', spcolon-1);
129                         std::string::size_type pos = line.find('*', sp);
130                         if (pos != std::string::npos)
131                                 line.erase(pos, 1);
132                         // hide the line completely if doing a "/who * o" query
133                         if (params.size() > 1 && params[1].find('o') != std::string::npos)
134                                 line.clear();
135                 }
136         }
137
138         ModResult OnStats(char symbol, User* user, string_list &results)
139         {
140                 if (symbol != 'P')
141                         return MOD_RES_PASSTHRU;
142
143                 unsigned int count = 0;
144                 for (std::list<User*>::const_iterator i = ServerInstance->Users->all_opers.begin(); i != ServerInstance->Users->all_opers.end(); ++i)
145                 {
146                         User* oper = *i;
147                         if (!ServerInstance->ULine(oper->server) && (IS_OPER(user) || !oper->IsModeSet('H')))
148                         {
149                                 results.push_back(ServerInstance->Config->ServerName+" 249 " + user->nick + " :" + oper->nick + " (" + oper->ident + "@" + oper->dhost + ") Idle: " +
150                                                 (IS_LOCAL(oper) ? ConvToStr(ServerInstance->Time() - oper->idle_lastmsg) + " secs" : "unavailable"));
151                                 count++;
152                         }
153                 }
154                 results.push_back(ServerInstance->Config->ServerName+" 249 "+user->nick+" :"+ConvToStr(count)+" OPER(s)");
155
156                 return MOD_RES_DENY;
157         }
158 };
159
160
161 MODULE_INIT(ModuleHideOper)