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