]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
m_mlock Remove unnecessary iteration
[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                 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.c_str());
112                 if (find)
113                         target = std::string("*!*@") + find->GetIPString();
114
115                 if (parameters.size() == 1)
116                 {
117                         if (ServerInstance->XLines->DelLine(target.c_str(), "SHUN", user))
118                         {
119                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed SHUN on %s",user->nick.c_str(),target.c_str());
120                         }
121                         else
122                         {
123                                 user->WriteServ("NOTICE %s :*** Shun %s not found in list, try /stats H.",user->nick.c_str(),target.c_str());
124                                 return CMD_FAILURE;
125                         }
126                 }
127                 else
128                 {
129                         // Adding - XXX todo make this respect <insane> tag perhaps..
130                         long duration;
131                         std::string expr;
132                         if (parameters.size() > 2)
133                         {
134                                 duration = ServerInstance->Duration(parameters[1]);
135                                 expr = parameters[2];
136                         }
137                         else
138                         {
139                                 duration = 0;
140                                 expr = parameters[1];
141                         }
142
143                         Shun* r = new Shun(ServerInstance->Time(), duration, user->nick.c_str(), expr.c_str(), target.c_str());
144                         if (ServerInstance->XLines->AddLine(r, user))
145                         {
146                                 if (!duration)
147                                 {
148                                         ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent SHUN for %s: %s",
149                                                 user->nick.c_str(), target.c_str(), expr.c_str());
150                                 }
151                                 else
152                                 {
153                                         time_t c_requires_crap = duration + ServerInstance->Time();
154                                         ServerInstance->SNO->WriteToSnoMask('x', "%s added timed SHUN for %s to expire on %s: %s",
155                                                 user->nick.c_str(), target.c_str(), ServerInstance->TimeString(c_requires_crap).c_str(), expr.c_str());
156                                 }
157                         }
158                         else
159                         {
160                                 delete r;
161                                 user->WriteServ("NOTICE %s :*** Shun for %s already exists", user->nick.c_str(), expr.c_str());
162                                 return CMD_FAILURE;
163                         }
164                 }
165                 return CMD_SUCCESS;
166         }
167
168         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
169         {
170                 return ROUTE_LOCALONLY;
171         }
172 };
173
174 class ModuleShun : public Module
175 {
176         CommandShun cmd;
177         ShunFactory f;
178         std::set<std::string> ShunEnabledCommands;
179         bool NotifyOfShun;
180         bool affectopers;
181
182  public:
183         ModuleShun() : cmd(this)
184         {
185                 ServerInstance->XLines->RegisterFactory(&f);
186                 ServerInstance->AddCommand(&cmd);
187
188                 Implementation eventlist[] = { I_OnStats, I_OnPreCommand, I_OnRehash };
189                 ServerInstance->Modules->Attach(eventlist, this, 3);
190                 OnRehash(NULL);
191         }
192
193         virtual ~ModuleShun()
194         {
195                 ServerInstance->XLines->DelAll("SHUN");
196                 ServerInstance->XLines->UnregisterFactory(&f);
197         }
198
199         void Prioritize()
200         {
201                 Module* alias = ServerInstance->Modules->Find("m_alias.so");
202                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, &alias);
203         }
204
205         virtual ModResult OnStats(char symbol, User* user, string_list& out)
206         {
207                 if (symbol != 'H')
208                         return MOD_RES_PASSTHRU;
209
210                 ServerInstance->XLines->InvokeStats("SHUN", 223, user, out);
211                 return MOD_RES_DENY;
212         }
213
214         virtual void OnRehash(User* user)
215         {
216                 ConfigReader MyConf;
217                 std::string cmds = MyConf.ReadValue("shun", "enabledcommands", 0);
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 = MyConf.ReadFlag("shun", "notifyuser", "yes", 0);
233                 affectopers = MyConf.ReadFlag("shun", "affectopers", "no", 0);
234         }
235
236         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string>& parameters, LocalUser* user, bool validated, const std::string &original_line)
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 && IS_OPER(user))
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->WriteServ("NOTICE %s :*** Command %s not processed, as you have been blocked from issuing commands (SHUN)", user->nick.c_str(), command.c_str());
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         virtual Version GetVersion()
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)
284