]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_samode.cpp
8b06118ee536f05b9a152879193cca98634209cc
[user/henk/code/inspircd.git] / src / modules / m_samode.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2006-2007 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2004-2005 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24
25 /** Handle /SAMODE
26  */
27 class CommandSamode : public Command
28 {
29  public:
30         bool active;
31         CommandSamode(Module* Creator) : Command(Creator,"SAMODE", 2)
32         {
33                 allow_empty_last_param = false;
34                 flags_needed = 'o'; Penalty = 0; syntax = "<target> <modes> {<mode-parameters>}";
35                 active = false;
36         }
37
38         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
39         {
40                 if (parameters[0].c_str()[0] != '#')
41                 {
42                         User* target = ServerInstance->FindNickOnly(parameters[0]);
43                         if ((!target) || (target->registered != REG_ALL))
44                         {
45                                 user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameters[0].c_str());
46                                 return CMD_FAILURE;
47                         }
48
49                         // Changing the modes of another user requires a special permission
50                         if ((target != user) && (!user->HasPrivPermission("users/samode-usermodes", true)))
51                                 return CMD_FAILURE;
52                 }
53
54                 // XXX: Make ModeParser clear LastParse
55                 Modes::ChangeList emptychangelist;
56                 ServerInstance->Modes->ProcessSingle(ServerInstance->FakeClient, NULL, ServerInstance->FakeClient, emptychangelist);
57
58                 this->active = true;
59                 CmdResult result = ServerInstance->Parser.CallHandler("MODE", parameters, user);
60                 this->active = false;
61
62                 if (result == CMD_SUCCESS)
63                 {
64                         // If lastparse is empty and the MODE command handler returned CMD_SUCCESS then
65                         // the client queried the list of a listmode (e.g. /SAMODE #chan b), which was
66                         // handled internally by the MODE command handler.
67                         //
68                         // Viewing the modes of a user or a channel can also result in CMD_SUCCESS, but
69                         // that is not possible with /SAMODE because we require at least 2 parameters.
70                         const std::string& lastparse = ServerInstance->Modes.GetLastParse();
71                         ServerInstance->SNO->WriteGlobalSno('a', user->nick + " used SAMODE: " + (lastparse.empty() ? irc::stringjoiner(parameters) : lastparse));
72                 }
73
74                 return CMD_SUCCESS;
75         }
76 };
77
78 class ModuleSaMode : public Module
79 {
80         CommandSamode cmd;
81  public:
82         ModuleSaMode()
83                 : cmd(this)
84         {
85         }
86
87         Version GetVersion() CXX11_OVERRIDE
88         {
89                 return Version("Provides command SAMODE to allow opers to change modes on channels and users", VF_VENDOR);
90         }
91
92         ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes) CXX11_OVERRIDE
93         {
94                 if (cmd.active)
95                         return MOD_RES_ALLOW;
96                 return MOD_RES_PASSTHRU;
97         }
98
99         void Prioritize()
100         {
101                 Module *override = ServerInstance->Modules->Find("m_override.so");
102                 ServerInstance->Modules->SetPriority(this, I_OnPreMode, PRIORITY_BEFORE, override);
103         }
104 };
105
106 MODULE_INIT(ModuleSaMode)