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 from executing all except configured commands. */
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;
42 LocalUser* lu = IS_LOCAL(u);
46 if (InspIRCd::Match(u->GetFullHost(), matchtext) || InspIRCd::Match(u->GetFullRealHost(), matchtext) || InspIRCd::Match(u->nick+"!"+u->ident+"@"+u->GetIPString(), matchtext))
52 bool Matches(const std::string &s)
59 const char* Displayable()
61 return matchtext.c_str();
65 /** An XLineFactory specialized to generate shun pointers
67 class ShunFactory : public XLineFactory
70 ShunFactory() : XLineFactory("SHUN") { }
74 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
76 return new Shun(set_time, duration, source, reason, xline_specific_mask);
79 bool AutoApplyToUserList(XLine *x)
85 //typedef std::vector<Shun> shunlist;
87 class CommandShun : public Command
90 CommandShun(Module* Creator) : Command(Creator, "SHUN", 1, 3)
92 flags_needed = 'o'; this->syntax = "<nick!user@hostmask> [<shun-duration>] :<reason>";
95 CmdResult Handle(const std::vector<std::string>& parameters, User *user)
97 /* syntax: SHUN nick!user@host time :reason goes here */
98 /* 'time' is a human-readable timestring, like 2d3h2s. */
100 std::string target = parameters[0];
102 User *find = ServerInstance->FindNick(target);
103 if ((find) && (find->registered == REG_ALL))
104 target = std::string("*!*@") + find->GetIPString();
106 if (parameters.size() == 1)
108 if (ServerInstance->XLines->DelLine(target.c_str(), "SHUN", user))
110 ServerInstance->SNO->WriteToSnoMask('x',"%s removed SHUN on %s",user->nick.c_str(),target.c_str());
114 user->WriteNotice("*** Shun " + target + " not found in list, try /stats H.");
120 // Adding - XXX todo make this respect <insane> tag perhaps..
121 unsigned long duration;
123 if (parameters.size() > 2)
125 duration = InspIRCd::Duration(parameters[1]);
126 expr = parameters[2];
131 expr = parameters[1];
134 Shun* r = new Shun(ServerInstance->Time(), duration, user->nick.c_str(), expr.c_str(), target.c_str());
135 if (ServerInstance->XLines->AddLine(r, user))
139 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent SHUN for %s: %s",
140 user->nick.c_str(), target.c_str(), expr.c_str());
144 time_t c_requires_crap = duration + ServerInstance->Time();
145 std::string timestr = ServerInstance->TimeString(c_requires_crap);
146 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed SHUN for %s to expire on %s: %s",
147 user->nick.c_str(), target.c_str(), timestr.c_str(), expr.c_str());
153 user->WriteNotice("*** Shun for " + target + " already exists");
160 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
163 return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
165 return ROUTE_BROADCAST;
169 class ModuleShun : public Module
173 std::set<std::string> ShunEnabledCommands;
178 ModuleShun() : cmd(this)
184 ServerInstance->XLines->RegisterFactory(&f);
185 ServerInstance->Modules->AddService(cmd);
187 Implementation eventlist[] = { I_OnStats, I_OnPreCommand, I_OnRehash };
188 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
192 virtual ~ModuleShun()
194 ServerInstance->XLines->DelAll("SHUN");
195 ServerInstance->XLines->UnregisterFactory(&f);
200 Module* alias = ServerInstance->Modules->Find("m_alias.so");
201 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, &alias);
204 virtual ModResult OnStats(char symbol, User* user, string_list& out)
207 return MOD_RES_PASSTHRU;
209 ServerInstance->XLines->InvokeStats("SHUN", 223, user, out);
213 virtual void OnRehash(User* user)
215 ConfigTag* tag = ServerInstance->Config->ConfValue("shun");
216 std::string cmds = tag->getString("enabledcommands");
217 std::transform(cmds.begin(), cmds.end(), cmds.begin(), ::toupper);
220 cmds = "PING PONG QUIT";
222 ShunEnabledCommands.clear();
224 std::stringstream dcmds(cmds);
227 while (dcmds >> thiscmd)
229 ShunEnabledCommands.insert(thiscmd);
232 NotifyOfShun = tag->getBool("notifyuser", true);
233 affectopers = tag->getBool("affectopers", false);
236 virtual ModResult OnPreCommand(std::string &command, std::vector<std::string>& parameters, LocalUser* user, bool validated, const std::string &original_line)
239 return MOD_RES_PASSTHRU;
241 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
243 /* Not shunned, don't touch. */
244 return MOD_RES_PASSTHRU;
247 if (!affectopers && user->IsOper())
249 /* Don't do anything if the user is an operator and affectopers isn't set */
250 return MOD_RES_PASSTHRU;
253 std::set<std::string>::iterator i = ShunEnabledCommands.find(command);
255 if (i == ShunEnabledCommands.end())
258 user->WriteNotice("*** Command " + command + " not processed, as you have been blocked from issuing commands (SHUN)");
262 if (command == "QUIT")
264 /* Allow QUIT but dont show any quit message */
267 else if ((command == "PART") && (parameters.size() > 1))
270 parameters[1].clear();
273 /* if we're here, allow the command. */
274 return MOD_RES_PASSTHRU;
277 virtual Version GetVersion()
279 return Version("Provides the /SHUN command, which stops a user from executing all except configured commands.",VF_VENDOR|VF_COMMON);
283 MODULE_INIT(ModuleShun)