2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2005-2008 Craig Edwards <craigedwards@brainbox.cc>
6 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7 * Copyright (C) 2006 Robin Burchell <robin+git@viroteck.net>
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/>.
35 typedef std::vector<TimedBan> timedbans;
36 timedbans TimedBanList;
40 class CommandTban : public Command
43 CommandTban(Module* Creator) : Command(Creator,"TBAN", 3)
45 syntax = "<channel> <duration> <banmask>";
48 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
50 Channel* channel = ServerInstance->FindChan(parameters[0]);
53 user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such channel", parameters[0].c_str());
56 int cm = channel->GetPrefixValue(user);
57 if (cm < HALFOP_VALUE)
59 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s :You do not have permission to set bans on this channel",
60 channel->name.c_str());
65 std::string channelname = parameters[0];
66 unsigned long duration = InspIRCd::Duration(parameters[1]);
67 unsigned long expire = duration + ServerInstance->Time();
70 user->WriteNotice("Invalid ban time");
73 std::string mask = parameters[2];
74 bool isextban = ((mask.size() > 2) && (mask[1] == ':'));
75 if (!isextban && !InspIRCd::IsValidMask(mask))
78 Modes::ChangeList setban;
79 setban.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
80 // Pass the user (instead of ServerInstance->FakeClient) to ModeHandler::Process() to
81 // make it so that the user sets the mode themselves
82 ServerInstance->Modes->Process(user, channel, NULL, setban);
83 if (ServerInstance->Modes->GetLastParse().empty())
85 user->WriteNotice("Invalid ban mask");
90 T.channel = channelname;
92 T.expire = expire + (IS_REMOTE(user) ? 5 : 0);
93 TimedBanList.push_back(T);
95 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
96 ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
97 char pfxchar = (mh && mh->name == "halfop") ? '%' : '@';
99 channel->WriteAllExcept(ServerInstance->FakeClient, true, pfxchar, tmp, "NOTICE %s :%s added a timed ban on %s lasting for %ld seconds.", channel->name.c_str(), user->nick.c_str(), mask.c_str(), duration);
103 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
105 return ROUTE_BROADCAST;
109 class BanWatcher : public ModeWatcher
112 BanWatcher(Module* parent)
113 : ModeWatcher(parent, "ban", MODETYPE_CHANNEL)
117 void AfterMode(User* source, User* dest, Channel* chan, const std::string& banmask, bool adding)
122 irc::string listitem = banmask.c_str();
123 irc::string thischan = chan->name.c_str();
124 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); ++i)
126 irc::string target = i->mask.c_str();
127 irc::string tchan = i->channel.c_str();
128 if ((listitem == target) && (tchan == thischan))
130 TimedBanList.erase(i);
137 class ModuleTimedBans : public Module
140 BanWatcher banwatcher;
149 void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
152 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
154 if (curtime > i->expire)
156 expired.push_back(*i);
157 i = TimedBanList.erase(i);
163 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
165 std::string chan = i->channel;
166 std::string mask = i->mask;
167 Channel* cr = ServerInstance->FindChan(chan);
171 std::string expiry = "*** Timed ban on " + chan + " expired.";
172 cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
173 ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
175 Modes::ChangeList setban;
176 setban.push_remove(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
177 ServerInstance->Modes->Process(ServerInstance->FakeClient, cr, NULL, setban);
182 Version GetVersion() CXX11_OVERRIDE
184 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
188 MODULE_INIT(ModuleTimedBans)