2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
6 * Copyright (C) 2016, 2019 Matt Schatz <genius3000@g3k.solutions>
7 * Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
8 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
9 * Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
10 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
11 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
12 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
13 * Copyright (C) 2009 John Brooks <special@inspircd.org>
14 * Copyright (C) 2007-2008, 2010 Craig Edwards <brain@inspircd.org>
15 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
16 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
18 * This file is part of InspIRCd. InspIRCd is free software: you can
19 * redistribute it and/or modify it under the terms of the GNU General Public
20 * License as published by the Free Software Foundation, version 2.
22 * This program is distributed in the hope that it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
45 typedef std::vector<TimedBan> timedbans;
46 timedbans TimedBanList;
49 class CommandTban : public Command
51 ChanModeReference banmode;
53 bool IsBanSet(Channel* chan, const std::string& mask)
55 ListModeBase* banlm = static_cast<ListModeBase*>(*banmode);
59 const ListModeBase::ModeList* bans = banlm->GetList(chan);
62 for (ListModeBase::ModeList::const_iterator i = bans->begin(); i != bans->end(); ++i)
64 const ListModeBase::ListItem& ban = *i;
65 if (irc::equals(ban.mask, mask))
74 CommandTban(Module* Creator)
75 : Command(Creator,"TBAN", 3)
76 , banmode(Creator, "ban")
78 syntax = "<channel> <duration> <banmask>";
81 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
83 Channel* channel = ServerInstance->FindChan(parameters[0]);
86 user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
90 unsigned int cm = channel->GetPrefixValue(user);
91 if (cm < HALFOP_VALUE)
93 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, "You do not have permission to set bans on this channel");
98 unsigned long duration;
99 if (!InspIRCd::Duration(parameters[1], duration))
101 user->WriteNotice("Invalid ban time");
104 unsigned long expire = duration + ServerInstance->Time();
106 std::string mask = parameters[2];
107 bool isextban = ((mask.size() > 2) && (mask[1] == ':'));
108 if (!isextban && !InspIRCd::IsValidMask(mask))
111 if (IsBanSet(channel, mask))
113 user->WriteNotice("Ban already set");
117 Modes::ChangeList setban;
118 setban.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
119 // Pass the user (instead of ServerInstance->FakeClient) to ModeHandler::Process() to
120 // make it so that the user sets the mode themselves
121 ServerInstance->Modes->Process(user, channel, NULL, setban);
122 if (ServerInstance->Modes->GetLastChangeList().empty())
124 user->WriteNotice("Invalid ban mask");
129 T.setter = user->nick;
130 T.expire = expire + (IS_REMOTE(user) ? 5 : 0);
132 TimedBanList.push_back(T);
134 const std::string message = InspIRCd::Format("Timed ban %s added by %s on %s lasting for %s.",
135 mask.c_str(), user->nick.c_str(), channel->name.c_str(), InspIRCd::DurationString(duration).c_str());
136 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
137 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
138 char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
140 channel->WriteNotice(message, pfxchar);
144 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
146 return ROUTE_BROADCAST;
150 class BanWatcher : public ModeWatcher
153 BanWatcher(Module* parent)
154 : ModeWatcher(parent, "ban", MODETYPE_CHANNEL)
158 void AfterMode(User* source, User* dest, Channel* chan, const std::string& banmask, bool adding) CXX11_OVERRIDE
163 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); ++i)
168 const std::string& target = i->mask;
169 if (irc::equals(banmask, target))
171 TimedBanList.erase(i);
183 ChannelMatcher(Channel* ch)
188 bool operator()(const TimedBan& tb) const
190 return (tb.chan == chan);
194 class ModuleTimedBans : public Module
197 BanWatcher banwatcher;
206 void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
209 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
211 if (curtime > i->expire)
213 expired.push_back(*i);
214 i = TimedBanList.erase(i);
220 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
222 const std::string mask = i->mask;
223 Channel* cr = i->chan;
225 const std::string message = InspIRCd::Format("Timed ban %s set by %s on %s has expired.",
226 mask.c_str(), i->setter.c_str(), cr->name.c_str());
227 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
228 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
229 char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
231 cr->WriteNotice(message, pfxchar);
233 Modes::ChangeList setban;
234 setban.push_remove(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
235 ServerInstance->Modes->Process(ServerInstance->FakeClient, cr, NULL, setban);
239 void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
241 // Remove all timed bans affecting the channel from internal bookkeeping
242 TimedBanList.erase(std::remove_if(TimedBanList.begin(), TimedBanList.end(), ChannelMatcher(chan)), TimedBanList.end());
245 Version GetVersion() CXX11_OVERRIDE
247 return Version("Provides the TBAN command, timed channel bans", VF_COMMON | VF_VENDOR);
251 MODULE_INIT(ModuleTimedBans)