2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2020 iwalkalone <iwalkalone69@gmail.com>
5 * Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
6 * Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
7 * Copyright (C) 2016, 2019 Matt Schatz <genius3000@g3k.solutions>
8 * Copyright (C) 2013, 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
9 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
10 * Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
11 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
12 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
13 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
14 * Copyright (C) 2009 John Brooks <special@inspircd.org>
15 * Copyright (C) 2007-2008, 2010 Craig Edwards <brain@inspircd.org>
16 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
17 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
19 * This file is part of InspIRCd. InspIRCd is free software: you can
20 * redistribute it and/or modify it under the terms of the GNU General Public
21 * License as published by the Free Software Foundation, version 2.
23 * This program is distributed in the hope that it will be useful, but WITHOUT
24 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
46 typedef std::vector<TimedBan> timedbans;
47 timedbans TimedBanList;
50 class CommandTban : public Command
52 ChanModeReference banmode;
54 bool IsBanSet(Channel* chan, const std::string& mask)
56 ListModeBase* banlm = static_cast<ListModeBase*>(*banmode);
60 const ListModeBase::ModeList* bans = banlm->GetList(chan);
63 for (ListModeBase::ModeList::const_iterator i = bans->begin(); i != bans->end(); ++i)
65 const ListModeBase::ListItem& ban = *i;
66 if (irc::equals(ban.mask, mask))
77 CommandTban(Module* Creator)
78 : Command(Creator,"TBAN", 3)
79 , banmode(Creator, "ban")
81 syntax = "<channel> <duration> <banmask>";
84 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
86 Channel* channel = ServerInstance->FindChan(parameters[0]);
89 user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
93 unsigned int cm = channel->GetPrefixValue(user);
94 if (cm < HALFOP_VALUE)
96 user->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, "You do not have permission to set bans on this channel");
101 unsigned long duration;
102 if (!InspIRCd::Duration(parameters[1], duration))
104 user->WriteNotice("Invalid ban time");
107 unsigned long expire = duration + ServerInstance->Time();
109 std::string mask = parameters[2];
110 bool isextban = ((mask.size() > 2) && (mask[1] == ':'));
111 if (!isextban && !InspIRCd::IsValidMask(mask))
114 if (IsBanSet(channel, mask))
116 user->WriteNotice("Ban already set");
120 Modes::ChangeList setban;
121 setban.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
122 // Pass the user (instead of ServerInstance->FakeClient) to ModeHandler::Process() to
123 // make it so that the user sets the mode themselves
124 ServerInstance->Modes->Process(user, channel, NULL, setban);
125 if (ServerInstance->Modes->GetLastChangeList().empty())
127 user->WriteNotice("Invalid ban mask");
132 T.setter = user->nick;
133 T.expire = expire + (IS_REMOTE(user) ? 5 : 0);
135 TimedBanList.push_back(T);
139 const std::string message = InspIRCd::Format("Timed ban %s added by %s on %s lasting for %s.",
140 mask.c_str(), user->nick.c_str(), channel->name.c_str(), InspIRCd::DurationString(duration).c_str());
141 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
142 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
143 char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
145 channel->WriteRemoteNotice(message, pfxchar);
151 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
153 return ROUTE_BROADCAST;
157 class BanWatcher : public ModeWatcher
160 BanWatcher(Module* parent)
161 : ModeWatcher(parent, "ban", MODETYPE_CHANNEL)
165 void AfterMode(User* source, User* dest, Channel* chan, const std::string& banmask, bool adding) CXX11_OVERRIDE
170 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); ++i)
175 const std::string& target = i->mask;
176 if (irc::equals(banmask, target))
178 TimedBanList.erase(i);
190 ChannelMatcher(Channel* ch)
195 bool operator()(const TimedBan& tb) const
197 return (tb.chan == chan);
201 class ModuleTimedBans : public Module
204 BanWatcher banwatcher;
213 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
215 ConfigTag* tag = ServerInstance->Config->ConfValue("timedbans");
216 cmd.sendnotice = tag->getBool("sendnotice", true);
219 void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
222 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
224 if (curtime > i->expire)
226 expired.push_back(*i);
227 i = TimedBanList.erase(i);
233 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
235 const std::string mask = i->mask;
236 Channel* cr = i->chan;
240 const std::string message = InspIRCd::Format("Timed ban %s set by %s on %s has expired.",
241 mask.c_str(), i->setter.c_str(), cr->name.c_str());
242 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
243 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
244 char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
246 cr->WriteRemoteNotice(message, pfxchar);
249 Modes::ChangeList setban;
250 setban.push_remove(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
251 ServerInstance->Modes->Process(ServerInstance->FakeClient, cr, NULL, setban);
255 void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
257 // Remove all timed bans affecting the channel from internal bookkeeping
258 TimedBanList.erase(std::remove_if(TimedBanList.begin(), TimedBanList.end(), ChannelMatcher(chan)), TimedBanList.end());
261 Version GetVersion() CXX11_OVERRIDE
263 return Version("Adds the /TBAN command which allows channel operators to add bans which will be expired after the specified period.", VF_COMMON | VF_VENDOR);
267 MODULE_INIT(ModuleTimedBans)