]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
Add a shun option to only apply when users are fully connected.
[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, 2020 Sadie Powell <sadie@witchery.services>
8  *   Copyright (C) 2012-2013, 2015-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 John Brooks <special@inspircd.org>
13  *   Copyright (C) 2009 Dennis Friis <peavey@inspircd.org>
14  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
15  *   Copyright (C) 2008-2010 Craig Edwards <brain@inspircd.org>
16  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
17  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
18  *
19  * This file is part of InspIRCd.  InspIRCd is free software: you can
20  * redistribute it and/or modify it under the terms of the GNU General Public
21  * License as published by the Free Software Foundation, version 2.
22  *
23  * This program is distributed in the hope that it will be useful, but WITHOUT
24  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
26  * details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30  */
31
32
33 #include "inspircd.h"
34 #include "xline.h"
35 #include "modules/shun.h"
36 #include "modules/stats.h"
37
38
39 /** An XLineFactory specialized to generate shun pointers
40  */
41 class ShunFactory : public XLineFactory
42 {
43  public:
44         ShunFactory() : XLineFactory("SHUN") { }
45
46         /** Generate a shun
47         */
48         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
49         {
50                 return new Shun(set_time, duration, source, reason, xline_specific_mask);
51         }
52
53         bool AutoApplyToUserList(XLine* x) CXX11_OVERRIDE
54         {
55                 return false;
56         }
57 };
58
59 class CommandShun : public Command
60 {
61  public:
62         CommandShun(Module* Creator) : Command(Creator, "SHUN", 1, 3)
63         {
64                 flags_needed = 'o';
65                 syntax = "<nick!user@host> [<duration> :<reason>]";
66         }
67
68         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
69         {
70                 /* syntax: SHUN nick!user@host time :reason goes here */
71                 /* 'time' is a human-readable timestring, like 2d3h2s. */
72
73                 std::string target = parameters[0];
74
75                 User *find = ServerInstance->FindNick(target);
76                 if ((find) && (find->registered == REG_ALL))
77                         target = std::string("*!*@") + find->GetIPString();
78
79                 if (parameters.size() == 1)
80                 {
81                         std::string reason;
82
83                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SHUN", reason, user))
84                         {
85                                 ServerInstance->SNO->WriteToSnoMask('x', "%s removed SHUN on %s: %s", user->nick.c_str(), parameters[0].c_str(), reason.c_str());
86                         }
87                         else if (ServerInstance->XLines->DelLine(target.c_str(), "SHUN", reason, user))
88                         {
89                                 ServerInstance->SNO->WriteToSnoMask('x', "%s removed SHUN on %s: %s", user->nick.c_str(), target.c_str(), reason.c_str());
90                         }
91                         else
92                         {
93                                 user->WriteNotice("*** Shun " + parameters[0] + " not found on the list.");
94                                 return CMD_FAILURE;
95                         }
96                 }
97                 else
98                 {
99                         // Adding - XXX todo make this respect <insane> tag perhaps..
100                         unsigned long duration;
101                         std::string expr;
102                         if (parameters.size() > 2)
103                         {
104                                 if (!InspIRCd::Duration(parameters[1], duration))
105                                 {
106                                         user->WriteNotice("*** Invalid duration for SHUN.");
107                                         return CMD_FAILURE;
108                                 }
109                                 expr = parameters[2];
110                         }
111                         else
112                         {
113                                 duration = 0;
114                                 expr = parameters[1];
115                         }
116
117                         Shun* r = new Shun(ServerInstance->Time(), duration, user->nick.c_str(), expr.c_str(), target.c_str());
118                         if (ServerInstance->XLines->AddLine(r, user))
119                         {
120                                 if (!duration)
121                                 {
122                                         ServerInstance->SNO->WriteToSnoMask('x', "%s added permanent SHUN for %s: %s",
123                                                 user->nick.c_str(), target.c_str(), expr.c_str());
124                                 }
125                                 else
126                                 {
127                                         ServerInstance->SNO->WriteToSnoMask('x', "%s added timed SHUN for %s, expires in %s (on %s): %s",
128                                                 user->nick.c_str(), target.c_str(), InspIRCd::DurationString(duration).c_str(),
129                                                 InspIRCd::TimeString(ServerInstance->Time() + duration).c_str(), expr.c_str());
130                                 }
131                         }
132                         else
133                         {
134                                 delete r;
135                                 user->WriteNotice("*** Shun for " + target + " already exists.");
136                                 return CMD_FAILURE;
137                         }
138                 }
139                 return CMD_SUCCESS;
140         }
141
142         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
143         {
144                 if (IS_LOCAL(user))
145                         return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
146
147                 return ROUTE_BROADCAST;
148         }
149 };
150
151 class ModuleShun : public Module, public Stats::EventListener
152 {
153  private:
154         CommandShun cmd;
155         ShunFactory shun;
156         insp::flat_set<std::string, irc::insensitive_swo> cleanedcommands;
157         insp::flat_set<std::string, irc::insensitive_swo> enabledcommands;
158         bool affectopers;
159         bool allowconnect;
160         bool allowtags;
161         bool notifyuser;
162
163         bool IsShunned(LocalUser* user)
164         {
165                 // Exempt the user if they are not fully connected and allowconnect is enabled.
166                 if (allowconnect && user->registered != REG_ALL)
167                         return false;
168
169                 // Exempt the user from shuns if they are an oper and affectopers is disabled.
170                 if (!affectopers && user->IsOper())
171                         return false;
172
173                 // Exempt the user from shuns if they are an oper with the servers/ignore-shun privilege.
174                 if (user->HasPrivPermission("servers/ignore-shun"))
175                         return false;
176
177                 // Check whether the user is actually shunned.
178                 return ServerInstance->XLines->MatchesLine("SHUN", user);
179         }
180
181  public:
182         ModuleShun()
183                 : Stats::EventListener(this)
184                 , cmd(this)
185         {
186         }
187
188         void init() CXX11_OVERRIDE
189         {
190                 ServerInstance->XLines->RegisterFactory(&shun);
191         }
192
193         ~ModuleShun()
194         {
195                 ServerInstance->XLines->DelAll("SHUN");
196                 ServerInstance->XLines->UnregisterFactory(&shun);
197         }
198
199         void Prioritize() CXX11_OVERRIDE
200         {
201                 Module* alias = ServerInstance->Modules->Find("m_alias.so");
202                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, alias);
203         }
204
205         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
206         {
207                 if (stats.GetSymbol() != 'H')
208                         return MOD_RES_PASSTHRU;
209
210                 ServerInstance->XLines->InvokeStats("SHUN", stats);
211                 return MOD_RES_DENY;
212         }
213
214         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
215         {
216                 ConfigTag* tag = ServerInstance->Config->ConfValue("shun");
217
218                 cleanedcommands.clear();
219                 irc::spacesepstream cleanedcmds(tag->getString("cleanedcommands", "AWAY PART QUIT"));
220                 for (std::string cleanedcmd; cleanedcmds.GetToken(cleanedcmd); )
221                         cleanedcommands.insert(cleanedcmd);
222
223                 enabledcommands.clear();
224                 irc::spacesepstream enabledcmds(tag->getString("enabledcommands", "ADMIN OPER PING PONG QUIT", 1));
225                 for (std::string enabledcmd; enabledcmds.GetToken(enabledcmd); )
226                         enabledcommands.insert(enabledcmd);
227
228                 affectopers = tag->getBool("affectopers", false);
229                 allowtags = tag->getBool("allowtags");
230                 allowconnect = tag->getBool("allowconnect");
231                 notifyuser = tag->getBool("notifyuser", true);
232         }
233
234         ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
235         {
236                 if (validated || !IsShunned(user))
237                         return MOD_RES_PASSTHRU;
238
239                 if (!enabledcommands.count(command))
240                 {
241                         if (notifyuser)
242                                 user->WriteNotice("*** " + command + " command not processed as you have been blocked from issuing commands.");
243                         return MOD_RES_DENY;
244                 }
245
246                 if (!allowtags)
247                 {
248                         // Remove all client tags.
249                         ClientProtocol::TagMap& tags = parameters.GetTags();
250                         for (ClientProtocol::TagMap::iterator tag = tags.begin(); tag != tags.end(); )
251                         {
252                                 if (tag->first[0] == '+')
253                                         tag = tags.erase(tag);
254                                 else
255                                         tag++;
256                         }
257                 }
258
259                 if (cleanedcommands.count(command))
260                 {
261                         if (command == "AWAY" && !parameters.empty())
262                         {
263                                 // Allow away but only for unsetting.
264                                 parameters.clear();
265                         }
266                         else if (command == "PART" && parameters.size() > 1)
267                         {
268                                 // Allow part but strip the message.
269                                 parameters.pop_back();
270                         }
271                         else if (command == "QUIT" && !parameters.empty())
272                         {
273                                 // Allow quit but strip the message.
274                                 parameters.clear();
275                         }
276                 }
277
278                 return MOD_RES_PASSTHRU;
279         }
280
281         Version GetVersion() CXX11_OVERRIDE
282         {
283                 return Version("Adds the /SHUN command which allows server operators to prevent users from executing commands.", VF_VENDOR|VF_COMMON);
284         }
285 };
286
287 MODULE_INIT(ModuleShun)