]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
Include ADMIN and OPER in the default for <shun:enabledcmds>.
[user/henk/code/inspircd.git] / src / modules / m_shun.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5  *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
6  *   Copyright (C) 2017-2018 B00mX0r <b00mx0r@aureus.pw>
7  *   Copyright (C) 2013, 2017-2018 Sadie Powell <sadie@witchery.services>
8  *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
9  *   Copyright (C) 2012, 2018-2019 Robby <robby@chatbelgie.be>
10  *   Copyright (C) 2012 Jens Voss <DukePyrolator@anope.org>
11  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
12  *   Copyright (C) 2009 Matt Smith <dz@inspircd.org>
13  *   Copyright (C) 2009 John Brooks <special@inspircd.org>
14  *   Copyright (C) 2009 Dennis Friis <peavey@inspircd.org>
15  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
16  *   Copyright (C) 2008-2010 Craig Edwards <brain@inspircd.org>
17  *   Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
18  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
19  *
20  * This file is part of InspIRCd.  InspIRCd is free software: you can
21  * redistribute it and/or modify it under the terms of the GNU General Public
22  * License as published by the Free Software Foundation, version 2.
23  *
24  * This program is distributed in the hope that it will be useful, but WITHOUT
25  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
27  * details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31  */
32
33
34 #include "inspircd.h"
35 #include "xline.h"
36 #include "modules/shun.h"
37 #include "modules/stats.h"
38
39
40 /** An XLineFactory specialized to generate shun pointers
41  */
42 class ShunFactory : public XLineFactory
43 {
44  public:
45         ShunFactory() : XLineFactory("SHUN") { }
46
47         /** Generate a shun
48         */
49         XLine* Generate(time_t set_time, unsigned long duration, const std::string& source, const std::string& reason, const std::string& xline_specific_mask) CXX11_OVERRIDE
50         {
51                 return new Shun(set_time, duration, source, reason, xline_specific_mask);
52         }
53
54         bool AutoApplyToUserList(XLine* x) CXX11_OVERRIDE
55         {
56                 return false;
57         }
58 };
59
60 //typedef std::vector<Shun> shunlist;
61
62 class CommandShun : public Command
63 {
64  public:
65         CommandShun(Module* Creator) : Command(Creator, "SHUN", 1, 3)
66         {
67                 flags_needed = 'o'; this->syntax = "<nick!user@host> [<duration> :<reason>]";
68         }
69
70         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
71         {
72                 /* syntax: SHUN nick!user@host time :reason goes here */
73                 /* 'time' is a human-readable timestring, like 2d3h2s. */
74
75                 std::string target = parameters[0];
76
77                 User *find = ServerInstance->FindNick(target);
78                 if ((find) && (find->registered == REG_ALL))
79                         target = std::string("*!*@") + find->GetIPString();
80
81                 if (parameters.size() == 1)
82                 {
83                         std::string reason;
84
85                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SHUN", reason, user))
86                         {
87                                 ServerInstance->SNO->WriteToSnoMask('x', "%s removed SHUN on %s: %s", user->nick.c_str(), parameters[0].c_str(), reason.c_str());
88                         }
89                         else if (ServerInstance->XLines->DelLine(target.c_str(), "SHUN", reason, user))
90                         {
91                                 ServerInstance->SNO->WriteToSnoMask('x', "%s removed SHUN on %s: %s", user->nick.c_str(), target.c_str(), reason.c_str());
92                         }
93                         else
94                         {
95                                 user->WriteNotice("*** Shun " + parameters[0] + " not found on the list.");
96                                 return CMD_FAILURE;
97                         }
98                 }
99                 else
100                 {
101                         // Adding - XXX todo make this respect <insane> tag perhaps..
102                         unsigned long duration;
103                         std::string expr;
104                         if (parameters.size() > 2)
105                         {
106                                 if (!InspIRCd::Duration(parameters[1], duration))
107                                 {
108                                         user->WriteNotice("*** Invalid duration for SHUN.");
109                                         return CMD_FAILURE;
110                                 }
111                                 expr = parameters[2];
112                         }
113                         else
114                         {
115                                 duration = 0;
116                                 expr = parameters[1];
117                         }
118
119                         Shun* r = new Shun(ServerInstance->Time(), duration, user->nick.c_str(), expr.c_str(), target.c_str());
120                         if (ServerInstance->XLines->AddLine(r, user))
121                         {
122                                 if (!duration)
123                                 {
124                                         ServerInstance->SNO->WriteToSnoMask('x', "%s added permanent SHUN for %s: %s",
125                                                 user->nick.c_str(), target.c_str(), expr.c_str());
126                                 }
127                                 else
128                                 {
129                                         ServerInstance->SNO->WriteToSnoMask('x', "%s added timed SHUN for %s, expires in %s (on %s): %s",
130                                                 user->nick.c_str(), target.c_str(), InspIRCd::DurationString(duration).c_str(),
131                                                 InspIRCd::TimeString(ServerInstance->Time() + duration).c_str(), expr.c_str());
132                                 }
133                         }
134                         else
135                         {
136                                 delete r;
137                                 user->WriteNotice("*** Shun for " + target + " already exists.");
138                                 return CMD_FAILURE;
139                         }
140                 }
141                 return CMD_SUCCESS;
142         }
143
144         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
145         {
146                 if (IS_LOCAL(user))
147                         return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
148
149                 return ROUTE_BROADCAST;
150         }
151 };
152
153 class ModuleShun : public Module, public Stats::EventListener
154 {
155         CommandShun cmd;
156         ShunFactory f;
157         insp::flat_set<std::string> ShunEnabledCommands;
158         bool NotifyOfShun;
159         bool affectopers;
160
161  public:
162         ModuleShun()
163                 : Stats::EventListener(this)
164                 , cmd(this)
165         {
166         }
167
168         void init() CXX11_OVERRIDE
169         {
170                 ServerInstance->XLines->RegisterFactory(&f);
171         }
172
173         ~ModuleShun()
174         {
175                 ServerInstance->XLines->DelAll("SHUN");
176                 ServerInstance->XLines->UnregisterFactory(&f);
177         }
178
179         void Prioritize() CXX11_OVERRIDE
180         {
181                 Module* alias = ServerInstance->Modules->Find("m_alias.so");
182                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, alias);
183         }
184
185         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
186         {
187                 if (stats.GetSymbol() != 'H')
188                         return MOD_RES_PASSTHRU;
189
190                 ServerInstance->XLines->InvokeStats("SHUN", stats);
191                 return MOD_RES_DENY;
192         }
193
194         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
195         {
196                 ConfigTag* tag = ServerInstance->Config->ConfValue("shun");
197
198                 ShunEnabledCommands.clear();
199                 irc::spacesepstream enabledcmds(tag->getString("enabledcommands", "ADMIN OPER PING PONG QUIT", 1));
200                 for (std::string enabledcmd; enabledcmds.GetToken(enabledcmd); )
201                 {
202                         std::transform(enabledcmd.begin(), enabledcmd.end(), enabledcmd.begin(), ::toupper);
203                         ShunEnabledCommands.insert(enabledcmd);
204                 }
205
206                 NotifyOfShun = tag->getBool("notifyuser", true);
207                 affectopers = tag->getBool("affectopers", false);
208         }
209
210         ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
211         {
212                 if (validated)
213                         return MOD_RES_PASSTHRU;
214
215                 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
216                 {
217                         /* Not shunned, don't touch. */
218                         return MOD_RES_PASSTHRU;
219                 }
220
221                 if (!affectopers && user->IsOper())
222                 {
223                         /* Don't do anything if the user is an operator and affectopers isn't set */
224                         return MOD_RES_PASSTHRU;
225                 }
226
227                 if (!ShunEnabledCommands.count(command))
228                 {
229                         if (NotifyOfShun)
230                                 user->WriteNotice("*** Command " + command + " not processed, as you have been blocked from issuing commands (SHUN)");
231                         return MOD_RES_DENY;
232                 }
233
234                 if (command == "QUIT")
235                 {
236                         /* Allow QUIT but dont show any quit message */
237                         parameters.clear();
238                 }
239                 else if ((command == "PART") && (parameters.size() > 1))
240                 {
241                         /* same for PART */
242                         parameters.pop_back();
243                 }
244
245                 /* if we're here, allow the command. */
246                 return MOD_RES_PASSTHRU;
247         }
248
249         Version GetVersion() CXX11_OVERRIDE
250         {
251                 return Version("Provides the SHUN command, which stops a user from executing all except configured commands", VF_VENDOR|VF_COMMON);
252         }
253 };
254
255 MODULE_INIT(ModuleShun)