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