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