]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
Add CXX11_OVERRIDE to overridden members that lack it.
[user/henk/code/inspircd.git] / src / modules / m_shun.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
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 #include "xline.h"
25
26 class Shun : public XLine
27 {
28 public:
29         std::string matchtext;
30
31         Shun(time_t s_time, long d, const std::string& src, const std::string& re, const std::string& shunmask)
32                 : XLine(s_time, d, src, re, "SHUN")
33                 , matchtext(shunmask)
34         {
35         }
36
37         bool Matches(User *u)
38         {
39                 // E: overrides shun
40                 LocalUser* lu = IS_LOCAL(u);
41                 if (lu && lu->exempt)
42                         return false;
43
44                 if (InspIRCd::Match(u->GetFullHost(), matchtext) || InspIRCd::Match(u->GetFullRealHost(), matchtext) || InspIRCd::Match(u->nick+"!"+u->ident+"@"+u->GetIPString(), matchtext))
45                         return true;
46
47                 if (InspIRCd::MatchCIDR(u->GetIPString(), matchtext, ascii_case_insensitive_map))
48                         return true;
49
50                 return false;
51         }
52
53         bool Matches(const std::string &s)
54         {
55                 if (matchtext == s)
56                         return true;
57                 return false;
58         }
59
60         const std::string& Displayable()
61         {
62                 return matchtext;
63         }
64 };
65
66 /** An XLineFactory specialized to generate shun pointers
67  */
68 class ShunFactory : public XLineFactory
69 {
70  public:
71         ShunFactory() : XLineFactory("SHUN") { }
72
73         /** Generate a shun
74         */
75         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
76         {
77                 return new Shun(set_time, duration, source, reason, xline_specific_mask);
78         }
79
80         bool AutoApplyToUserList(XLine *x)
81         {
82                 return false;
83         }
84 };
85
86 //typedef std::vector<Shun> shunlist;
87
88 class CommandShun : public Command
89 {
90  public:
91         CommandShun(Module* Creator) : Command(Creator, "SHUN", 1, 3)
92         {
93                 flags_needed = 'o'; this->syntax = "<nick!user@hostmask> [<shun-duration>] :<reason>";
94         }
95
96         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
97         {
98                 /* syntax: SHUN nick!user@host time :reason goes here */
99                 /* 'time' is a human-readable timestring, like 2d3h2s. */
100
101                 std::string target = parameters[0];
102
103                 User *find = ServerInstance->FindNick(target);
104                 if ((find) && (find->registered == REG_ALL))
105                         target = std::string("*!*@") + find->GetIPString();
106
107                 if (parameters.size() == 1)
108                 {
109                         if (ServerInstance->XLines->DelLine(target.c_str(), "SHUN", user))
110                         {
111                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed SHUN on %s",user->nick.c_str(),target.c_str());
112                         }
113                         else
114                         {
115                                 user->WriteNotice("*** Shun " + target + " not found in list, try /stats H.");
116                                 return CMD_FAILURE;
117                         }
118                 }
119                 else
120                 {
121                         // Adding - XXX todo make this respect <insane> tag perhaps..
122                         unsigned long duration;
123                         std::string expr;
124                         if (parameters.size() > 2)
125                         {
126                                 duration = InspIRCd::Duration(parameters[1]);
127                                 expr = parameters[2];
128                         }
129                         else
130                         {
131                                 duration = 0;
132                                 expr = parameters[1];
133                         }
134
135                         Shun* r = new Shun(ServerInstance->Time(), duration, user->nick.c_str(), expr.c_str(), target.c_str());
136                         if (ServerInstance->XLines->AddLine(r, user))
137                         {
138                                 if (!duration)
139                                 {
140                                         ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent SHUN for %s: %s",
141                                                 user->nick.c_str(), target.c_str(), expr.c_str());
142                                 }
143                                 else
144                                 {
145                                         time_t c_requires_crap = duration + ServerInstance->Time();
146                                         std::string timestr = InspIRCd::TimeString(c_requires_crap);
147                                         ServerInstance->SNO->WriteToSnoMask('x', "%s added timed SHUN for %s to expire on %s: %s",
148                                                 user->nick.c_str(), target.c_str(), timestr.c_str(), expr.c_str());
149                                 }
150                         }
151                         else
152                         {
153                                 delete r;
154                                 user->WriteNotice("*** Shun for " + target + " already exists");
155                                 return CMD_FAILURE;
156                         }
157                 }
158                 return CMD_SUCCESS;
159         }
160
161         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
162         {
163                 if (IS_LOCAL(user))
164                         return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
165
166                 return ROUTE_BROADCAST;
167         }
168 };
169
170 class ModuleShun : public Module
171 {
172         CommandShun cmd;
173         ShunFactory f;
174         insp::flat_set<std::string> ShunEnabledCommands;
175         bool NotifyOfShun;
176         bool affectopers;
177
178  public:
179         ModuleShun() : cmd(this)
180         {
181         }
182
183         void init() CXX11_OVERRIDE
184         {
185                 ServerInstance->XLines->RegisterFactory(&f);
186         }
187
188         ~ModuleShun()
189         {
190                 ServerInstance->XLines->DelAll("SHUN");
191                 ServerInstance->XLines->UnregisterFactory(&f);
192         }
193
194         void Prioritize() CXX11_OVERRIDE
195         {
196                 Module* alias = ServerInstance->Modules->Find("m_alias.so");
197                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, alias);
198         }
199
200         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
201         {
202                 if (stats.GetSymbol() != 'H')
203                         return MOD_RES_PASSTHRU;
204
205                 ServerInstance->XLines->InvokeStats("SHUN", 223, stats);
206                 return MOD_RES_DENY;
207         }
208
209         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
210         {
211                 ConfigTag* tag = ServerInstance->Config->ConfValue("shun");
212                 std::string cmds = tag->getString("enabledcommands");
213                 std::transform(cmds.begin(), cmds.end(), cmds.begin(), ::toupper);
214
215                 if (cmds.empty())
216                         cmds = "PING PONG QUIT";
217
218                 ShunEnabledCommands.clear();
219
220                 std::stringstream dcmds(cmds);
221                 std::string thiscmd;
222
223                 while (dcmds >> thiscmd)
224                 {
225                         ShunEnabledCommands.insert(thiscmd);
226                 }
227
228                 NotifyOfShun = tag->getBool("notifyuser", true);
229                 affectopers = tag->getBool("affectopers", false);
230         }
231
232         ModResult OnPreCommand(std::string &command, std::vector<std::string>& parameters, LocalUser* user, bool validated, const std::string &original_line) CXX11_OVERRIDE
233         {
234                 if (validated)
235                         return MOD_RES_PASSTHRU;
236
237                 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
238                 {
239                         /* Not shunned, don't touch. */
240                         return MOD_RES_PASSTHRU;
241                 }
242
243                 if (!affectopers && user->IsOper())
244                 {
245                         /* Don't do anything if the user is an operator and affectopers isn't set */
246                         return MOD_RES_PASSTHRU;
247                 }
248
249                 if (!ShunEnabledCommands.count(command))
250                 {
251                         if (NotifyOfShun)
252                                 user->WriteNotice("*** Command " + command + " not processed, as you have been blocked from issuing commands (SHUN)");
253                         return MOD_RES_DENY;
254                 }
255
256                 if (command == "QUIT")
257                 {
258                         /* Allow QUIT but dont show any quit message */
259                         parameters.clear();
260                 }
261                 else if ((command == "PART") && (parameters.size() > 1))
262                 {
263                         /* same for PART */
264                         parameters[1].clear();
265                 }
266
267                 /* if we're here, allow the command. */
268                 return MOD_RES_PASSTHRU;
269         }
270
271         Version GetVersion() CXX11_OVERRIDE
272         {
273                 return Version("Provides the /SHUN command, which stops a user from executing all except configured commands.",VF_VENDOR|VF_COMMON);
274         }
275 };
276
277 MODULE_INIT(ModuleShun)