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