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