]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_samode.cpp
Fix being able to see the modes of private/secret channels.
[user/henk/code/inspircd.git] / src / modules / m_samode.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
6  *   Copyright (C) 2012-2016, 2018 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
10  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11  *   Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28
29 /** Handle /SAMODE
30  */
31 class CommandSamode : public Command
32 {
33         bool logged;
34
35  public:
36         bool active;
37         CommandSamode(Module* Creator) : Command(Creator,"SAMODE", 2)
38         {
39                 allow_empty_last_param = false;
40                 flags_needed = 'o'; syntax = "<target> (+|-)<modes> [<mode-parameters>]";
41                 active = false;
42         }
43
44         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
45         {
46                 if (parameters[0].c_str()[0] != '#')
47                 {
48                         User* target = ServerInstance->FindNickOnly(parameters[0]);
49                         if ((!target) || (target->registered != REG_ALL))
50                         {
51                                 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
52                                 return CMD_FAILURE;
53                         }
54
55                         // Changing the modes of another user requires a special permission
56                         if ((target != user) && (!user->HasPrivPermission("users/samode-usermodes")))
57                         {
58                                 user->WriteNotice("*** You are not allowed to /SAMODE other users (the privilege users/samode-usermodes is needed to /SAMODE others).");
59                                 return CMD_FAILURE;
60                         }
61                 }
62
63                 // XXX: Make ModeParser clear LastParse
64                 Modes::ChangeList emptychangelist;
65                 ServerInstance->Modes->ProcessSingle(ServerInstance->FakeClient, NULL, ServerInstance->FakeClient, emptychangelist);
66
67                 logged = false;
68                 this->active = true;
69                 ServerInstance->Parser.CallHandler("MODE", parameters, user);
70                 this->active = false;
71
72                 if (!logged)
73                 {
74                         // If we haven't logged anything yet then the client queried the list of a listmode
75                         // (e.g. /SAMODE #chan b), which was handled internally by the MODE command handler.
76                         //
77                         // Viewing the modes of a user or a channel could also result in this, but
78                         // that is not possible with /SAMODE because we require at least 2 parameters.
79                         LogUsage(user, stdalgo::string::join(parameters));
80                 }
81
82                 return CMD_SUCCESS;
83         }
84
85         void LogUsage(const User* user, const std::string& text)
86         {
87                 logged = true;
88                 ServerInstance->SNO->WriteGlobalSno('a', user->nick + " used SAMODE: " + text);
89         }
90 };
91
92 class ModuleSaMode : public Module
93 {
94         CommandSamode cmd;
95  public:
96         ModuleSaMode()
97                 : cmd(this)
98         {
99         }
100
101         Version GetVersion() CXX11_OVERRIDE
102         {
103                 return Version("Provides the SAMODE command, allows opers to change modes on channels and users", VF_VENDOR);
104         }
105
106         ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes) CXX11_OVERRIDE
107         {
108                 if (cmd.active)
109                         return MOD_RES_ALLOW;
110                 return MOD_RES_PASSTHRU;
111         }
112
113         void OnMode(User* user, User* destuser, Channel* destchan, const Modes::ChangeList& modes, ModeParser::ModeProcessFlag processflags) CXX11_OVERRIDE
114         {
115                 if (!cmd.active)
116                         return;
117
118                 std::string logtext = (destuser ? destuser->nick : destchan->name);
119                 logtext.push_back(' ');
120                 logtext += ClientProtocol::Messages::Mode::ToModeLetters(modes);
121
122                 for (Modes::ChangeList::List::const_iterator i = modes.getlist().begin(); i != modes.getlist().end(); ++i)
123                 {
124                         const Modes::Change& item = *i;
125                         if (!item.param.empty())
126                                 logtext.append(1, ' ').append(item.param);
127                 }
128
129                 cmd.LogUsage(user, logtext);
130         }
131
132         void Prioritize() CXX11_OVERRIDE
133         {
134                 Module* disable = ServerInstance->Modules->Find("m_disable.so");
135                 ServerInstance->Modules->SetPriority(this, I_OnRawMode, PRIORITY_BEFORE, disable);
136
137                 Module *override = ServerInstance->Modules->Find("m_override.so");
138                 ServerInstance->Modules->SetPriority(this, I_OnPreMode, PRIORITY_BEFORE, override);
139         }
140 };
141
142 MODULE_INIT(ModuleSaMode)