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
46 CommandTban(Module* Creator) : Command(Creator,"TBAN", 3)
48 syntax = "<channel> <duration> <banmask>";
49 TRANSLATE4(TR_TEXT, TR_TEXT, TR_TEXT, TR_END);
52 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
54 Channel* channel = ServerInstance->FindChan(parameters[0]);
57 user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
60 int cm = channel->GetPrefixValue(user);
61 if (cm < HALFOP_VALUE)
63 user->WriteNumeric(482, "%s %s :You do not have permission to set bans on this channel",
64 user->nick.c_str(), channel->name.c_str());
69 std::string channelname = parameters[0];
70 long duration = ServerInstance->Duration(parameters[1]);
71 unsigned long expire = duration + ServerInstance->Time();
74 user->WriteServ("NOTICE "+user->nick+" :Invalid ban time");
77 std::string mask = parameters[2];
78 std::vector<std::string> setban;
79 setban.push_back(parameters[0]);
80 setban.push_back("+b");
81 bool isextban = ((mask.size() > 2) && (mask[1] == ':'));
82 if (!isextban && !ServerInstance->IsValidMask(mask))
84 if ((mask.length() > 250) || (!ServerInstance->IsValidMask(mask) && !isextban))
86 user->WriteServ("NOTICE "+user->nick+" :Invalid ban mask");
89 setban.push_back(mask);
90 // use CallHandler to make it so that the user sets the mode
92 ServerInstance->Parser->CallHandler("MODE",setban,user);
93 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
94 if (!strcasecmp(i->data.c_str(), mask.c_str()))
99 T.channel = channelname;
101 T.expire = expire + (IS_REMOTE(user) ? 5 : 0);
103 TimedBanList.push_back(T);
105 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
106 ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
107 char pfxchar = (mh && mh->name == "halfop") ? '%' : '@';
109 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);
113 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
115 return ROUTE_BROADCAST;
124 ChannelMatcher(Channel* ch)
129 bool operator()(const TimedBan& tb) const
131 return (tb.chan == chan);
135 class ModuleTimedBans : public Module
146 ServerInstance->Modules->AddService(cmd);
147 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer, I_OnChannelDelete };
148 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
151 virtual ModResult OnDelBan(User* source, Channel* chan, const std::string &banmask)
153 irc::string listitem = banmask.c_str();
154 irc::string thischan = chan->name.c_str();
155 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); i++)
157 irc::string target = i->mask.c_str();
158 irc::string tchan = i->channel.c_str();
159 if ((listitem == target) && (tchan == thischan))
161 TimedBanList.erase(i);
165 return MOD_RES_PASSTHRU;
168 virtual void OnBackgroundTimer(time_t curtime)
171 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
173 if (curtime > i->expire)
175 expired.push_back(*i);
176 i = TimedBanList.erase(i);
182 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
184 std::string chan = i->channel;
185 std::string mask = i->mask;
186 Channel* cr = ServerInstance->FindChan(chan);
189 std::vector<std::string> setban;
190 setban.push_back(chan);
191 setban.push_back("-b");
192 setban.push_back(mask);
195 std::string expiry = "*** Timed ban on " + chan + " expired.";
196 cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
197 ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
199 ServerInstance->SendGlobalMode(setban, ServerInstance->FakeClient);
204 void OnChannelDelete(Channel* chan)
206 // Remove all timed bans affecting the channel from internal bookkeeping
207 TimedBanList.erase(std::remove_if(TimedBanList.begin(), TimedBanList.end(), ChannelMatcher(chan)), TimedBanList.end());
210 virtual Version GetVersion()
212 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
216 MODULE_INIT(ModuleTimedBans)