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