]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
Move OnStats from the core to a cross-module event.
[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, long duration, std::string source, std::string reason, 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@hostmask> [<shun-duration>] :<reason>";
57         }
58
59         CmdResult Handle(const std::vector<std::string>& parameters, User* user) 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                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SHUN", user))
73                         {
74                                 ServerInstance->SNO->WriteToSnoMask('x', "%s removed SHUN on %s", user->nick.c_str(), parameters[0].c_str());
75                         }
76                         else if (ServerInstance->XLines->DelLine(target.c_str(), "SHUN", user))
77                         {
78                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed SHUN on %s", user->nick.c_str(), target.c_str());
79                         }
80                         else
81                         {
82                                 user->WriteNotice("*** Shun " + parameters[0] + " not found in list, try /stats H.");
83                                 return CMD_FAILURE;
84                         }
85                 }
86                 else
87                 {
88                         // Adding - XXX todo make this respect <insane> tag perhaps..
89                         unsigned long duration;
90                         std::string expr;
91                         if (parameters.size() > 2)
92                         {
93                                 duration = InspIRCd::Duration(parameters[1]);
94                                 expr = parameters[2];
95                         }
96                         else
97                         {
98                                 duration = 0;
99                                 expr = parameters[1];
100                         }
101
102                         Shun* r = new Shun(ServerInstance->Time(), duration, user->nick.c_str(), expr.c_str(), target.c_str());
103                         if (ServerInstance->XLines->AddLine(r, user))
104                         {
105                                 if (!duration)
106                                 {
107                                         ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent SHUN for %s: %s",
108                                                 user->nick.c_str(), target.c_str(), expr.c_str());
109                                 }
110                                 else
111                                 {
112                                         time_t c_requires_crap = duration + ServerInstance->Time();
113                                         std::string timestr = InspIRCd::TimeString(c_requires_crap);
114                                         ServerInstance->SNO->WriteToSnoMask('x', "%s added timed SHUN for %s to expire on %s: %s",
115                                                 user->nick.c_str(), target.c_str(), timestr.c_str(), expr.c_str());
116                                 }
117                         }
118                         else
119                         {
120                                 delete r;
121                                 user->WriteNotice("*** Shun for " + target + " already exists");
122                                 return CMD_FAILURE;
123                         }
124                 }
125                 return CMD_SUCCESS;
126         }
127
128         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters) CXX11_OVERRIDE
129         {
130                 if (IS_LOCAL(user))
131                         return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
132
133                 return ROUTE_BROADCAST;
134         }
135 };
136
137 class ModuleShun : public Module, public Stats::EventListener
138 {
139         CommandShun cmd;
140         ShunFactory f;
141         insp::flat_set<std::string> ShunEnabledCommands;
142         bool NotifyOfShun;
143         bool affectopers;
144
145  public:
146         ModuleShun()
147                 : Stats::EventListener(this)
148                 , cmd(this)
149         {
150         }
151
152         void init() CXX11_OVERRIDE
153         {
154                 ServerInstance->XLines->RegisterFactory(&f);
155         }
156
157         ~ModuleShun()
158         {
159                 ServerInstance->XLines->DelAll("SHUN");
160                 ServerInstance->XLines->UnregisterFactory(&f);
161         }
162
163         void Prioritize() CXX11_OVERRIDE
164         {
165                 Module* alias = ServerInstance->Modules->Find("m_alias.so");
166                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, alias);
167         }
168
169         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE
170         {
171                 if (stats.GetSymbol() != 'H')
172                         return MOD_RES_PASSTHRU;
173
174                 ServerInstance->XLines->InvokeStats("SHUN", 223, stats);
175                 return MOD_RES_DENY;
176         }
177
178         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
179         {
180                 ConfigTag* tag = ServerInstance->Config->ConfValue("shun");
181                 std::string cmds = tag->getString("enabledcommands");
182                 std::transform(cmds.begin(), cmds.end(), cmds.begin(), ::toupper);
183
184                 if (cmds.empty())
185                         cmds = "PING PONG QUIT";
186
187                 ShunEnabledCommands.clear();
188
189                 irc::spacesepstream dcmds(cmds);
190                 std::string thiscmd;
191
192                 while (dcmds.GetToken(thiscmd))
193                 {
194                         ShunEnabledCommands.insert(thiscmd);
195                 }
196
197                 NotifyOfShun = tag->getBool("notifyuser", true);
198                 affectopers = tag->getBool("affectopers", false);
199         }
200
201         ModResult OnPreCommand(std::string &command, std::vector<std::string>& parameters, LocalUser* user, bool validated, const std::string &original_line) CXX11_OVERRIDE
202         {
203                 if (validated)
204                         return MOD_RES_PASSTHRU;
205
206                 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
207                 {
208                         /* Not shunned, don't touch. */
209                         return MOD_RES_PASSTHRU;
210                 }
211
212                 if (!affectopers && user->IsOper())
213                 {
214                         /* Don't do anything if the user is an operator and affectopers isn't set */
215                         return MOD_RES_PASSTHRU;
216                 }
217
218                 if (!ShunEnabledCommands.count(command))
219                 {
220                         if (NotifyOfShun)
221                                 user->WriteNotice("*** Command " + command + " not processed, as you have been blocked from issuing commands (SHUN)");
222                         return MOD_RES_DENY;
223                 }
224
225                 if (command == "QUIT")
226                 {
227                         /* Allow QUIT but dont show any quit message */
228                         parameters.clear();
229                 }
230                 else if ((command == "PART") && (parameters.size() > 1))
231                 {
232                         /* same for PART */
233                         parameters[1].clear();
234                 }
235
236                 /* if we're here, allow the command. */
237                 return MOD_RES_PASSTHRU;
238         }
239
240         Version GetVersion() CXX11_OVERRIDE
241         {
242                 return Version("Provides the /SHUN command, which stops a user from executing all except configured commands.",VF_VENDOR|VF_COMMON);
243         }
244 };
245
246 MODULE_INIT(ModuleShun)