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