2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
26 /* $ModDesc: Provides the /shun command, which stops a user executing all commands except PING and PONG. */
28 class Shun : public XLine
31 std::string matchtext;
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")
36 this->matchtext = shunmask;
49 if (InspIRCd::Match(u->GetFullHost(), matchtext) || InspIRCd::Match(u->GetFullRealHost(), matchtext) || InspIRCd::Match(u->nick+"!"+u->ident+"@"+u->GetIPString(), matchtext))
55 bool Matches(const std::string &s)
69 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired shun %s (set by %s %ld seconds ago)",
70 this->matchtext.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
73 const char* Displayable()
75 return matchtext.c_str();
79 /** An XLineFactory specialized to generate shun pointers
81 class ShunFactory : public XLineFactory
84 ShunFactory() : XLineFactory("SHUN") { }
88 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
90 return new Shun(set_time, duration, source, reason, xline_specific_mask);
94 //typedef std::vector<Shun> shunlist;
96 class CommandShun : public Command
99 CommandShun(Module* Creator) : Command(Creator, "SHUN", 1, 3)
101 flags_needed = 'o'; this->syntax = "<nick!user@hostmask> [<shun-duration>] :<reason>";
104 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
106 /* syntax: SHUN nick!user@host time :reason goes here */
107 /* 'time' is a human-readable timestring, like 2d3h2s. */
109 std::string target = parameters[0];
111 User *find = ServerInstance->FindNick(target.c_str());
113 target = std::string("*!*@") + find->GetIPString();
115 if (parameters.size() == 1)
117 if (ServerInstance->XLines->DelLine(target.c_str(), "SHUN", user))
119 ServerInstance->SNO->WriteToSnoMask('x',"%s removed SHUN on %s",user->nick.c_str(),target.c_str());
123 user->WriteServ("NOTICE %s :*** Shun %s not found in list, try /stats H.",user->nick.c_str(),target.c_str());
128 else if (parameters.size() >= 2)
130 // Adding - XXX todo make this respect <insane> tag perhaps..
133 if (parameters.size() > 2)
135 duration = ServerInstance->Duration(parameters[1]);
136 expr = parameters[2];
141 expr = parameters[1];
147 r = new Shun(ServerInstance->Time(), duration, user->nick.c_str(), expr.c_str(), target.c_str());
151 ; // Do nothing. If we get here, the regex was fucked up, and they already got told it fucked up.
156 if (ServerInstance->XLines->AddLine(r, user))
160 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent SHUN for %s: %s",
161 user->nick.c_str(), target.c_str(), expr.c_str());
165 time_t c_requires_crap = duration + ServerInstance->Time();
166 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed SHUN for %s to expire on %s: %s",
167 user->nick.c_str(), target.c_str(), ServerInstance->TimeString(c_requires_crap).c_str(), expr.c_str());
170 ServerInstance->XLines->ApplyLines();
175 user->WriteServ("NOTICE %s :*** Shun for %s already exists", user->nick.c_str(), expr.c_str());
183 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
185 return ROUTE_BROADCAST;
189 class ModuleShun : public Module
193 std::set<std::string> ShunEnabledCommands;
198 ModuleShun() : cmd(this)
200 ServerInstance->XLines->RegisterFactory(&f);
201 ServerInstance->AddCommand(&cmd);
203 Implementation eventlist[] = { I_OnStats, I_OnPreCommand, I_OnUserConnect, I_OnRehash };
204 ServerInstance->Modules->Attach(eventlist, this, 4);
208 virtual ~ModuleShun()
210 ServerInstance->XLines->DelAll("SHUN");
211 ServerInstance->XLines->UnregisterFactory(&f);
216 Module* alias = ServerInstance->Modules->Find("m_alias.so");
217 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, &alias);
220 virtual ModResult OnStats(char symbol, User* user, string_list& out)
223 return MOD_RES_PASSTHRU;
225 ServerInstance->XLines->InvokeStats("SHUN", 223, user, out);
229 virtual void OnRehash(User* user)
232 std::string cmds = MyConf.ReadValue("shun", "enabledcommands", 0);
235 cmds = "PING PONG QUIT";
237 ShunEnabledCommands.clear();
241 std::stringstream dcmds(cmds);
244 while (dcmds >> thiscmd)
246 ShunEnabledCommands.insert(thiscmd);
249 NotifyOfShun = MyConf.ReadFlag("shun", "notifyuser", "yes", 0);
250 affectopers = MyConf.ReadFlag("shun", "affectopers", "no", 0);
253 virtual void OnUserConnect(LocalUser* user)
258 // Apply lines on user connect
259 XLine *rl = ServerInstance->XLines->MatchesLine("SHUN", user);
268 virtual ModResult OnPreCommand(std::string &command, std::vector<std::string>& parameters, LocalUser* user, bool validated, const std::string &original_line)
271 return MOD_RES_PASSTHRU;
273 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
275 /* Not shunned, don't touch. */
276 return MOD_RES_PASSTHRU;
279 if (!affectopers && IS_OPER(user))
281 /* Don't do anything if the user is an operator and affectopers isn't set */
282 return MOD_RES_PASSTHRU;
285 std::set<std::string>::iterator i = ShunEnabledCommands.find(command);
287 if (i == ShunEnabledCommands.end())
290 user->WriteServ("NOTICE %s :*** Command %s not processed, as you have been blocked from issuing commands (SHUN)", user->nick.c_str(), command.c_str());
294 if (command == "QUIT")
296 /* Allow QUIT but dont show any quit message */
299 else if (command == "PART")
305 /* if we're here, allow the command. */
306 return MOD_RES_PASSTHRU;
309 virtual Version GetVersion()
311 return Version("Provides the /shun command, which stops a user executing all commands except PING and PONG.",VF_VENDOR|VF_COMMON);
315 MODULE_INIT(ModuleShun)