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/>.
23 /* $ModDesc: Adds timed bans */
38 typedef std::vector<TimedBan> timedbans;
39 timedbans TimedBanList;
43 class CommandTban : public Command
45 static bool IsBanSet(Channel* chan, const std::string& mask)
47 for (BanList::const_iterator i = chan->bans.begin(); i != chan->bans.end(); ++i)
49 if (!strcasecmp(i->data.c_str(), mask.c_str()))
56 CommandTban(Module* Creator) : Command(Creator,"TBAN", 3)
58 syntax = "<channel> <duration> <banmask>";
59 TRANSLATE4(TR_TEXT, TR_TEXT, TR_TEXT, TR_END);
62 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
64 Channel* channel = ServerInstance->FindChan(parameters[0]);
67 user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
70 int cm = channel->GetPrefixValue(user);
71 if (cm < HALFOP_VALUE)
73 user->WriteNumeric(482, "%s %s :You do not have permission to set bans on this channel",
74 user->nick.c_str(), channel->name.c_str());
79 std::string channelname = parameters[0];
80 long duration = ServerInstance->Duration(parameters[1]);
81 unsigned long expire = duration + ServerInstance->Time();
84 user->WriteServ("NOTICE "+user->nick+" :Invalid ban time");
87 std::string mask = parameters[2];
88 std::vector<std::string> setban;
89 setban.push_back(parameters[0]);
90 setban.push_back("+b");
91 bool isextban = ((mask.size() > 2) && (mask[1] == ':'));
92 if (!isextban && !ServerInstance->IsValidMask(mask))
94 if ((mask.length() > 250) || (!ServerInstance->IsValidMask(mask) && !isextban))
96 user->WriteServ("NOTICE "+user->nick+" :Invalid ban mask");
100 if (IsBanSet(channel, mask))
102 user->WriteServ("NOTICE %s :Ban already set", user->nick.c_str());
106 setban.push_back(mask);
107 // use CallHandler to make it so that the user sets the mode
109 ServerInstance->Parser->CallHandler("MODE",setban,user);
110 if (!IsBanSet(channel, mask))
114 T.channel = channelname;
116 T.expire = expire + (IS_REMOTE(user) ? 5 : 0);
118 TimedBanList.push_back(T);
120 const std::string addban = user->nick + " added a timed ban on " + mask + " lasting for " + ConvToStr(duration) + " seconds.";
121 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
122 ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
123 char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
125 channel->WriteAllExcept(ServerInstance->FakeClient, true, pfxchar, tmp, "NOTICE %s :%s", channel->name.c_str(), addban.c_str());
126 ServerInstance->PI->SendChannelNotice(channel, pfxchar, addban);
130 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
132 return ROUTE_BROADCAST;
141 ChannelMatcher(Channel* ch)
146 bool operator()(const TimedBan& tb) const
148 return (tb.chan == chan);
152 class ModuleTimedBans : public Module
163 ServerInstance->Modules->AddService(cmd);
164 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer, I_OnChannelDelete };
165 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
168 virtual ModResult OnDelBan(User* source, Channel* chan, const std::string &banmask)
170 irc::string listitem = banmask.c_str();
171 irc::string thischan = chan->name.c_str();
172 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); i++)
174 irc::string target = i->mask.c_str();
175 irc::string tchan = i->channel.c_str();
176 if ((listitem == target) && (tchan == thischan))
178 TimedBanList.erase(i);
182 return MOD_RES_PASSTHRU;
185 virtual void OnBackgroundTimer(time_t curtime)
188 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
190 if (curtime > i->expire)
192 expired.push_back(*i);
193 i = TimedBanList.erase(i);
199 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
201 std::string chan = i->channel;
202 std::string mask = i->mask;
203 Channel* cr = ServerInstance->FindChan(chan);
206 std::vector<std::string> setban;
207 setban.push_back(chan);
208 setban.push_back("-b");
209 setban.push_back(mask);
212 const std::string expiry = "*** Timed ban on " + chan + " expired.";
213 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
214 ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
215 char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
217 cr->WriteAllExcept(ServerInstance->FakeClient, true, pfxchar, empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
218 ServerInstance->PI->SendChannelNotice(cr, pfxchar, expiry);
220 ServerInstance->SendGlobalMode(setban, ServerInstance->FakeClient);
225 void OnChannelDelete(Channel* chan)
227 // Remove all timed bans affecting the channel from internal bookkeeping
228 TimedBanList.erase(std::remove_if(TimedBanList.begin(), TimedBanList.end(), ChannelMatcher(chan)), TimedBanList.end());
231 virtual Version GetVersion()
233 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
237 MODULE_INIT(ModuleTimedBans)