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;
40 class CommandTban : public Command
42 ChanModeReference banmode;
44 bool IsBanSet(Channel* chan, const std::string& mask)
46 ListModeBase* banlm = static_cast<ListModeBase*>(*banmode);
50 const ListModeBase::ModeList* bans = banlm->GetList(chan);
53 for (ListModeBase::ModeList::const_iterator i = bans->begin(); i != bans->end(); ++i)
55 const ListModeBase::ListItem& ban = *i;
56 if (!strcasecmp(ban.mask.c_str(), mask.c_str()))
65 CommandTban(Module* Creator)
66 : Command(Creator,"TBAN", 3)
67 , banmode(Creator, "ban")
69 syntax = "<channel> <duration> <banmask>";
72 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
74 Channel* channel = ServerInstance->FindChan(parameters[0]);
77 user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
81 unsigned int cm = channel->GetPrefixValue(user);
82 if (cm < HALFOP_VALUE)
84 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, "You do not have permission to set bans on this channel");
89 unsigned long duration;
90 if (!InspIRCd::Duration(parameters[1], duration))
92 user->WriteNotice("Invalid ban time");
95 unsigned long expire = duration + ServerInstance->Time();
97 std::string mask = parameters[2];
98 bool isextban = ((mask.size() > 2) && (mask[1] == ':'));
99 if (!isextban && !InspIRCd::IsValidMask(mask))
102 if (IsBanSet(channel, mask))
104 user->WriteNotice("Ban already set");
108 Modes::ChangeList setban;
109 setban.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
110 // Pass the user (instead of ServerInstance->FakeClient) to ModeHandler::Process() to
111 // make it so that the user sets the mode themselves
112 ServerInstance->Modes->Process(user, channel, NULL, setban);
113 if (ServerInstance->Modes->GetLastChangeList().empty())
115 user->WriteNotice("Invalid ban mask");
120 T.setter = user->nick;
121 T.expire = expire + (IS_REMOTE(user) ? 5 : 0);
123 TimedBanList.push_back(T);
125 const std::string message = InspIRCd::Format("Timed ban %s added by %s on %s lasting for %s.",
126 mask.c_str(), user->nick.c_str(), channel->name.c_str(), InspIRCd::DurationString(duration).c_str());
127 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
128 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
129 char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
131 channel->WriteNotice(message, pfxchar);
135 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
137 return ROUTE_BROADCAST;
141 class BanWatcher : public ModeWatcher
144 BanWatcher(Module* parent)
145 : ModeWatcher(parent, "ban", MODETYPE_CHANNEL)
149 void AfterMode(User* source, User* dest, Channel* chan, const std::string& banmask, bool adding) CXX11_OVERRIDE
154 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); ++i)
159 const std::string& target = i->mask;
160 if (irc::equals(banmask, target))
162 TimedBanList.erase(i);
174 ChannelMatcher(Channel* ch)
179 bool operator()(const TimedBan& tb) const
181 return (tb.chan == chan);
185 class ModuleTimedBans : public Module
188 BanWatcher banwatcher;
197 void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
200 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
202 if (curtime > i->expire)
204 expired.push_back(*i);
205 i = TimedBanList.erase(i);
211 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
213 const std::string mask = i->mask;
214 Channel* cr = i->chan;
216 const std::string message = InspIRCd::Format("Timed ban %s set by %s on %s has expired.",
217 mask.c_str(), i->setter.c_str(), cr->name.c_str());
218 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
219 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
220 char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
222 cr->WriteNotice(message, pfxchar);
224 Modes::ChangeList setban;
225 setban.push_remove(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
226 ServerInstance->Modes->Process(ServerInstance->FakeClient, cr, NULL, setban);
230 void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
232 // Remove all timed bans affecting the channel from internal bookkeeping
233 TimedBanList.erase(std::remove_if(TimedBanList.begin(), TimedBanList.end(), ChannelMatcher(chan)), TimedBanList.end());
236 Version GetVersion() CXX11_OVERRIDE
238 return Version("Provides the TBAN command, timed channel bans", VF_COMMON | VF_VENDOR);
242 MODULE_INIT(ModuleTimedBans)