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