]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_samode.cpp
Some more text fixes and improvements (#1618).
[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         bool logged;
30
31  public:
32         bool active;
33         CommandSamode(Module* Creator) : Command(Creator,"SAMODE", 2)
34         {
35                 allow_empty_last_param = false;
36                 flags_needed = 'o'; syntax = "<target> (+|-)<modes> [<mode-parameters>]";
37                 active = false;
38         }
39
40         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
41         {
42                 if (parameters[0].c_str()[0] != '#')
43                 {
44                         User* target = ServerInstance->FindNickOnly(parameters[0]);
45                         if ((!target) || (target->registered != REG_ALL))
46                         {
47                                 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
48                                 return CMD_FAILURE;
49                         }
50
51                         // Changing the modes of another user requires a special permission
52                         if ((target != user) && (!user->HasPrivPermission("users/samode-usermodes")))
53                         {
54                                 user->WriteNotice("*** You are not allowed to /SAMODE other users (the privilege users/samode-usermodes is needed to /SAMODE others).");
55                                 return CMD_FAILURE;
56                         }
57                 }
58
59                 // XXX: Make ModeParser clear LastParse
60                 Modes::ChangeList emptychangelist;
61                 ServerInstance->Modes->ProcessSingle(ServerInstance->FakeClient, NULL, ServerInstance->FakeClient, emptychangelist);
62
63                 logged = false;
64                 this->active = true;
65                 ServerInstance->Parser.CallHandler("MODE", parameters, user);
66                 this->active = false;
67
68                 if (!logged)
69                 {
70                         // If we haven't logged anything yet then the client queried the list of a listmode
71                         // (e.g. /SAMODE #chan b), which was handled internally by the MODE command handler.
72                         //
73                         // Viewing the modes of a user or a channel could also result in this, but
74                         // that is not possible with /SAMODE because we require at least 2 parameters.
75                         LogUsage(user, stdalgo::string::join(parameters));
76                 }
77
78                 return CMD_SUCCESS;
79         }
80
81         void LogUsage(const User* user, const std::string& text)
82         {
83                 logged = true;
84                 ServerInstance->SNO->WriteGlobalSno('a', user->nick + " used SAMODE: " + text);
85         }
86 };
87
88 class ModuleSaMode : public Module
89 {
90         CommandSamode cmd;
91  public:
92         ModuleSaMode()
93                 : cmd(this)
94         {
95         }
96
97         Version GetVersion() CXX11_OVERRIDE
98         {
99                 return Version("Provides the SAMODE command, allows opers to change modes on channels and users", VF_VENDOR);
100         }
101
102         ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes) CXX11_OVERRIDE
103         {
104                 if (cmd.active)
105                         return MOD_RES_ALLOW;
106                 return MOD_RES_PASSTHRU;
107         }
108
109         void OnMode(User* user, User* destuser, Channel* destchan, const Modes::ChangeList& modes, ModeParser::ModeProcessFlag processflags) CXX11_OVERRIDE
110         {
111                 if (!cmd.active)
112                         return;
113
114                 std::string logtext = (destuser ? destuser->nick : destchan->name);
115                 logtext.push_back(' ');
116                 logtext += ClientProtocol::Messages::Mode::ToModeLetters(modes);
117
118                 for (Modes::ChangeList::List::const_iterator i = modes.getlist().begin(); i != modes.getlist().end(); ++i)
119                 {
120                         const Modes::Change& item = *i;
121                         if (!item.param.empty())
122                                 logtext.append(1, ' ').append(item.param);
123                 }
124
125                 cmd.LogUsage(user, logtext);
126         }
127
128         void Prioritize() CXX11_OVERRIDE
129         {
130                 Module* disable = ServerInstance->Modules->Find("m_disable.so");
131                 ServerInstance->Modules->SetPriority(this, I_OnRawMode, PRIORITY_BEFORE, disable);
132
133                 Module *override = ServerInstance->Modules->Find("m_override.so");
134                 ServerInstance->Modules->SetPriority(this, I_OnPreMode, PRIORITY_BEFORE, override);
135         }
136 };
137
138 MODULE_INIT(ModuleSaMode)