]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
307e6a69496852e63bd19284cf7c2c2c054731fb
[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-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 class CommandShun : public Command
61 {
62  public:
63         CommandShun(Module* Creator) : Command(Creator, "SHUN", 1, 3)
64         {
65                 flags_needed = 'o';
66                 syntax = "<nick!user@host> [<duration> :<reason>]";
67         }
68
69         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
70         {
71                 /* syntax: SHUN nick!user@host time :reason goes here */
72                 /* 'time' is a human-readable timestring, like 2d3h2s. */
73
74                 std::string target = parameters[0];
75
76                 User *find = ServerInstance->FindNick(target);
77                 if ((find) && (find->registered == REG_ALL))
78                         target = std::string("*!*@") + find->GetIPString();
79
80                 if (parameters.size() == 1)
81                 {
82                         std::string reason;
83
84                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SHUN", reason, user))
85                         {
86                                 ServerInstance->SNO->WriteToSnoMask('x', "%s removed SHUN on %s: %s", user->nick.c_str(), parameters[0].c_str(), reason.c_str());
87                         }
88                         else if (ServerInstance->XLines->DelLine(target.c_str(), "SHUN", reason, user))
89                         {
90                                 ServerInstance->SNO->WriteToSnoMask('x', "%s removed SHUN on %s: %s", user->nick.c_str(), target.c_str(), reason.c_str());
91                         }
92                         else
93                         {
94                                 user->WriteNotice("*** Shun " + parameters[0] + " not found on the list.");
95                                 return CMD_FAILURE;
96                         }
97                 }
98                 else
99                 {
100                         // Adding - XXX todo make this respect <insane> tag perhaps..
101                         unsigned long duration;
102                         std::string expr;
103                         if (parameters.size() > 2)
104                         {
105                                 if (!InspIRCd::Duration(parameters[1], duration))
106                                 {
107                                         user->WriteNotice("*** Invalid duration for SHUN.");
108                                         return CMD_FAILURE;
109                                 }
110                                 expr = parameters[2];
111                         }
112                         else
113                         {
114                                 duration = 0;
115                                 expr = parameters[1];
116                         }
117
118                         Shun* r = new Shun(ServerInstance->Time(), duration, user->nick.c_str(), expr.c_str(), target.c_str());
119                         if (ServerInstance->XLines->AddLine(r, user))
120                         {
121                                 if (!duration)
122                                 {
123                                         ServerInstance->SNO->WriteToSnoMask('x', "%s added permanent SHUN for %s: %s",
124                                                 user->nick.c_str(), target.c_str(), expr.c_str());
125                                 }
126                                 else
127                                 {
128                                         ServerInstance->SNO->WriteToSnoMask('x', "%s added timed SHUN for %s, expires in %s (on %s): %s",
129                                                 user->nick.c_str(), target.c_str(), InspIRCd::DurationString(duration).c_str(),
130                                                 InspIRCd::TimeString(ServerInstance->Time() + duration).c_str(), expr.c_str());
131                                 }
132                         }
133                         else
134                         {
135                                 delete r;
136                                 user->WriteNotice("*** Shun for " + target + " already exists.");
137                                 return CMD_FAILURE;
138                         }
139                 }
140                 return CMD_SUCCESS;
141         }
142
143         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
144         {
145                 if (IS_LOCAL(user))
146                         return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
147
148                 return ROUTE_BROADCAST;
149         }
150 };
151
152 class ModuleShun : public Module, public Stats::EventListener
153 {
154  private:
155         CommandShun cmd;
156         ShunFactory shun;
157         insp::flat_set<std::string, irc::insensitive_swo> cleanedcommands;
158         insp::flat_set<std::string, irc::insensitive_swo> enabledcommands;
159         bool affectopers;
160         bool allowtags;
161         bool notifyuser;
162
163         bool IsShunned(LocalUser* user)
164         {
165                 // Exempt the user from shuns if they are an oper and affectopers is disabled.
166                 if (!affectopers && user->IsOper())
167                         return false;
168
169                 // Exempt the user from shuns if they are an oper with the servers/ignore-shun privilege.
170                 if (user->HasPrivPermission("servers/ignore-shun"))
171                         return false;
172
173                 // Check whether the user is actually shunned.
174                 return ServerInstance->XLines->MatchesLine("SHUN", user);
175         }
176
177  public:
178         ModuleShun()
179                 : Stats::EventListener(this)
180                 , cmd(this)
181         {
182         }
183
184         void init() CXX11_OVERRIDE
185         {
186                 ServerInstance->XLines->RegisterFactory(&shun);
187         }
188
189         ~ModuleShun()
190         {
191                 ServerInstance->XLines->DelAll("SHUN");
192                 ServerInstance->XLines->UnregisterFactory(&shun);
193         }
194
195         void Prioritize() CXX11_OVERRIDE
196         {
197                 Module* alias = ServerInstance->Modules->Find("m_alias.so");
198                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, alias);
199         }
200
201         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
202         {
203                 if (stats.GetSymbol() != 'H')
204                         return MOD_RES_PASSTHRU;
205
206                 ServerInstance->XLines->InvokeStats("SHUN", stats);
207                 return MOD_RES_DENY;
208         }
209
210         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
211         {
212                 ConfigTag* tag = ServerInstance->Config->ConfValue("shun");
213
214                 cleanedcommands.clear();
215                 irc::spacesepstream cleanedcmds(tag->getString("cleanedcommands", "AWAY PART QUIT"));
216                 for (std::string cleanedcmd; cleanedcmds.GetToken(cleanedcmd); )
217                         cleanedcommands.insert(cleanedcmd);
218
219                 enabledcommands.clear();
220                 irc::spacesepstream enabledcmds(tag->getString("enabledcommands", "ADMIN OPER PING PONG QUIT", 1));
221                 for (std::string enabledcmd; enabledcmds.GetToken(enabledcmd); )
222                         enabledcommands.insert(enabledcmd);
223
224                 affectopers = tag->getBool("affectopers", false);
225                 allowtags = tag->getBool("allowtags");
226                 notifyuser = tag->getBool("notifyuser", true);
227         }
228
229         ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
230         {
231                 if (validated || !IsShunned(user))
232                         return MOD_RES_PASSTHRU;
233
234                 if (!enabledcommands.count(command))
235                 {
236                         if (notifyuser)
237                                 user->WriteNotice("*** " + command + " command not processed as you have been blocked from issuing commands.");
238                         return MOD_RES_DENY;
239                 }
240
241                 if (!allowtags)
242                 {
243                         // Remove all client tags.
244                         ClientProtocol::TagMap& tags = parameters.GetTags();
245                         for (ClientProtocol::TagMap::iterator tag = tags.begin(); tag != tags.end(); )
246                         {
247                                 if (tag->first[0] == '+')
248                                         tag = tags.erase(tag);
249                                 else
250                                         tag++;
251                         }
252                 }
253
254                 if (cleanedcommands.count(command))
255                 {
256                         if (command == "AWAY" && !parameters.empty())
257                         {
258                                 // Allow away but only for unsetting.
259                                 parameters.clear();
260                         }
261                         else if (command == "PART" && parameters.size() > 1)
262                         {
263                                 // Allow part but strip the message.
264                                 parameters.pop_back();
265                         }
266                         else if (command == "QUIT" && !parameters.empty())
267                         {
268                                 // Allow quit but strip the message.
269                                 parameters.clear();
270                         }
271                 }
272
273                 return MOD_RES_PASSTHRU;
274         }
275
276         Version GetVersion() CXX11_OVERRIDE
277         {
278                 return Version("Adds the /SHUN command which allows server operators to prevent users from executing commands.", VF_VENDOR|VF_COMMON);
279         }
280 };
281
282 MODULE_INIT(ModuleShun)