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/>.
36 typedef std::vector<TimedBan> timedbans;
37 timedbans TimedBanList;
41 class CommandTban : public Command
43 ChanModeReference banmode;
45 bool IsBanSet(Channel* chan, const std::string& mask)
47 ListModeBase* banlm = static_cast<ListModeBase*>(*banmode);
48 const ListModeBase::ModeList* bans = banlm->GetList(chan);
51 for (ListModeBase::ModeList::const_iterator i = bans->begin(); i != bans->end(); ++i)
53 const ListModeBase::ListItem& ban = *i;
54 if (!strcasecmp(ban.mask.c_str(), mask.c_str()))
63 CommandTban(Module* Creator) : Command(Creator,"TBAN", 3)
64 , banmode(Creator, "ban")
66 syntax = "<channel> <duration> <banmask>";
69 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
71 Channel* channel = ServerInstance->FindChan(parameters[0]);
74 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
77 int cm = channel->GetPrefixValue(user);
78 if (cm < HALFOP_VALUE)
80 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, "You do not have permission to set bans on this channel");
85 unsigned long duration = InspIRCd::Duration(parameters[1]);
86 unsigned long expire = duration + ServerInstance->Time();
89 user->WriteNotice("Invalid ban time");
92 std::string mask = parameters[2];
93 bool isextban = ((mask.size() > 2) && (mask[1] == ':'));
94 if (!isextban && !InspIRCd::IsValidMask(mask))
97 if (IsBanSet(channel, mask))
99 user->WriteNotice("Ban already set");
103 Modes::ChangeList setban;
104 setban.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
105 // Pass the user (instead of ServerInstance->FakeClient) to ModeHandler::Process() to
106 // make it so that the user sets the mode themselves
107 ServerInstance->Modes->Process(user, channel, NULL, setban);
108 if (ServerInstance->Modes->GetLastParse().empty())
110 user->WriteNotice("Invalid ban mask");
116 T.expire = expire + (IS_REMOTE(user) ? 5 : 0);
118 TimedBanList.push_back(T);
120 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
121 ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
122 char pfxchar = (mh && mh->name == "halfop") ? '%' : '@';
124 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);
128 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
130 return ROUTE_BROADCAST;
134 class BanWatcher : public ModeWatcher
137 BanWatcher(Module* parent)
138 : ModeWatcher(parent, "ban", MODETYPE_CHANNEL)
142 void AfterMode(User* source, User* dest, Channel* chan, const std::string& banmask, bool adding)
147 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); ++i)
152 const std::string& target = i->mask;
153 if (irc::equals(banmask, target))
155 TimedBanList.erase(i);
167 ChannelMatcher(Channel* ch)
172 bool operator()(const TimedBan& tb) const
174 return (tb.chan == chan);
178 class ModuleTimedBans : public Module
181 BanWatcher banwatcher;
190 void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
193 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
195 if (curtime > i->expire)
197 expired.push_back(*i);
198 i = TimedBanList.erase(i);
204 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
206 std::string mask = i->mask;
207 Channel* cr = i->chan;
210 std::string expiry = "*** Timed ban on " + cr->name + " expired.";
211 cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
212 ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
214 Modes::ChangeList setban;
215 setban.push_remove(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
216 ServerInstance->Modes->Process(ServerInstance->FakeClient, cr, NULL, setban);
221 void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
223 // Remove all timed bans affecting the channel from internal bookkeeping
224 TimedBanList.erase(std::remove_if(TimedBanList.begin(), TimedBanList.end(), ChannelMatcher(chan)), TimedBanList.end());
227 Version GetVersion() CXX11_OVERRIDE
229 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
233 MODULE_INIT(ModuleTimedBans)