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 class Shun : public XLine
29 std::string matchtext;
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")
40 LocalUser* lu = IS_LOCAL(u);
44 if (InspIRCd::Match(u->GetFullHost(), matchtext) || InspIRCd::Match(u->GetFullRealHost(), matchtext) || InspIRCd::Match(u->nick+"!"+u->ident+"@"+u->GetIPString(), matchtext))
50 bool Matches(const std::string &s)
57 const std::string& Displayable()
63 /** An XLineFactory specialized to generate shun pointers
65 class ShunFactory : public XLineFactory
68 ShunFactory() : XLineFactory("SHUN") { }
72 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
74 return new Shun(set_time, duration, source, reason, xline_specific_mask);
77 bool AutoApplyToUserList(XLine *x)
83 //typedef std::vector<Shun> shunlist;
85 class CommandShun : public Command
88 CommandShun(Module* Creator) : Command(Creator, "SHUN", 1, 3)
90 flags_needed = 'o'; this->syntax = "<nick!user@hostmask> [<shun-duration>] :<reason>";
93 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
95 /* syntax: SHUN nick!user@host time :reason goes here */
96 /* 'time' is a human-readable timestring, like 2d3h2s. */
98 std::string target = parameters[0];
100 User *find = ServerInstance->FindNick(target);
101 if ((find) && (find->registered == REG_ALL))
102 target = std::string("*!*@") + find->GetIPString();
104 if (parameters.size() == 1)
106 if (ServerInstance->XLines->DelLine(target.c_str(), "SHUN", user))
108 ServerInstance->SNO->WriteToSnoMask('x',"%s removed SHUN on %s",user->nick.c_str(),target.c_str());
112 user->WriteNotice("*** Shun " + target + " not found in list, try /stats H.");
118 // Adding - XXX todo make this respect <insane> tag perhaps..
119 unsigned long duration;
121 if (parameters.size() > 2)
123 duration = InspIRCd::Duration(parameters[1]);
124 expr = parameters[2];
129 expr = parameters[1];
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))
137 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent SHUN for %s: %s",
138 user->nick.c_str(), target.c_str(), expr.c_str());
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());
151 user->WriteNotice("*** Shun for " + target + " already exists");
158 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
161 return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
163 return ROUTE_BROADCAST;
167 class ModuleShun : public Module
171 std::set<std::string> ShunEnabledCommands;
176 ModuleShun() : cmd(this)
180 void init() CXX11_OVERRIDE
182 ServerInstance->XLines->RegisterFactory(&f);
183 ServerInstance->Modules->AddService(cmd);
190 ServerInstance->XLines->DelAll("SHUN");
191 ServerInstance->XLines->UnregisterFactory(&f);
196 Module* alias = ServerInstance->Modules->Find("m_alias.so");
197 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, &alias);
200 ModResult OnStats(char symbol, User* user, string_list& out) CXX11_OVERRIDE
203 return MOD_RES_PASSTHRU;
205 ServerInstance->XLines->InvokeStats("SHUN", 223, user, out);
209 void OnRehash(User* user) CXX11_OVERRIDE
211 ConfigTag* tag = ServerInstance->Config->ConfValue("shun");
212 std::string cmds = tag->getString("enabledcommands");
213 std::transform(cmds.begin(), cmds.end(), cmds.begin(), ::toupper);
216 cmds = "PING PONG QUIT";
218 ShunEnabledCommands.clear();
220 std::stringstream dcmds(cmds);
223 while (dcmds >> thiscmd)
225 ShunEnabledCommands.insert(thiscmd);
228 NotifyOfShun = tag->getBool("notifyuser", true);
229 affectopers = tag->getBool("affectopers", false);
232 ModResult OnPreCommand(std::string &command, std::vector<std::string>& parameters, LocalUser* user, bool validated, const std::string &original_line) CXX11_OVERRIDE
235 return MOD_RES_PASSTHRU;
237 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
239 /* Not shunned, don't touch. */
240 return MOD_RES_PASSTHRU;
243 if (!affectopers && user->IsOper())
245 /* Don't do anything if the user is an operator and affectopers isn't set */
246 return MOD_RES_PASSTHRU;
249 std::set<std::string>::iterator i = ShunEnabledCommands.find(command);
251 if (i == ShunEnabledCommands.end())
254 user->WriteNotice("*** Command " + command + " not processed, as you have been blocked from issuing commands (SHUN)");
258 if (command == "QUIT")
260 /* Allow QUIT but dont show any quit message */
263 else if ((command == "PART") && (parameters.size() > 1))
266 parameters[1].clear();
269 /* if we're here, allow the command. */
270 return MOD_RES_PASSTHRU;
273 Version GetVersion() CXX11_OVERRIDE
275 return Version("Provides the /SHUN command, which stops a user from executing all except configured commands.",VF_VENDOR|VF_COMMON);
279 MODULE_INIT(ModuleShun)