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 */
37 typedef std::vector<TimedBan> timedbans;
38 timedbans TimedBanList;
42 class CommandTban : public Command
45 CommandTban(Module* Creator) : Command(Creator,"TBAN", 3)
47 syntax = "<channel> <duration> <banmask>";
48 TRANSLATE4(TR_TEXT, TR_TEXT, TR_TEXT, TR_END);
51 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
53 Channel* channel = ServerInstance->FindChan(parameters[0]);
56 user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
59 int cm = channel->GetPrefixValue(user);
60 if (cm < HALFOP_VALUE)
62 user->WriteNumeric(482, "%s %s :You do not have permission to set bans on this channel",
63 user->nick.c_str(), channel->name.c_str());
68 std::string channelname = parameters[0];
69 long duration = ServerInstance->Duration(parameters[1]);
70 unsigned long expire = duration + ServerInstance->Time();
73 user->WriteServ("NOTICE "+user->nick+" :Invalid ban time");
76 std::string mask = parameters[2];
77 std::vector<std::string> setban;
78 setban.push_back(parameters[0]);
79 setban.push_back("+b");
80 bool isextban = ((mask.size() > 2) && (mask[1] == ':'));
81 if (!isextban && !ServerInstance->IsValidMask(mask))
83 if ((mask.length() > 250) || (!ServerInstance->IsValidMask(mask) && !isextban))
85 user->WriteServ("NOTICE "+user->nick+" :Invalid ban mask");
88 setban.push_back(mask);
89 // use CallHandler to make it so that the user sets the mode
91 ServerInstance->Parser->CallHandler("MODE",setban,user);
92 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
93 if (!strcasecmp(i->data.c_str(), mask.c_str()))
98 T.channel = channelname;
100 T.expire = expire + (IS_REMOTE(user) ? 5 : 0);
101 TimedBanList.push_back(T);
103 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
104 ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
105 char pfxchar = (mh && mh->name == "halfop") ? '%' : '@';
107 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);
111 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
113 return ROUTE_BROADCAST;
117 class ModuleTimedBans : public Module
128 ServerInstance->Modules->AddService(cmd);
129 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
130 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
133 virtual ModResult OnDelBan(User* source, Channel* chan, const std::string &banmask)
135 irc::string listitem = banmask.c_str();
136 irc::string thischan = chan->name.c_str();
137 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); i++)
139 irc::string target = i->mask.c_str();
140 irc::string tchan = i->channel.c_str();
141 if ((listitem == target) && (tchan == thischan))
143 TimedBanList.erase(i);
147 return MOD_RES_PASSTHRU;
150 virtual void OnBackgroundTimer(time_t curtime)
153 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
155 if (curtime > i->expire)
157 expired.push_back(*i);
158 i = TimedBanList.erase(i);
164 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
166 std::string chan = i->channel;
167 std::string mask = i->mask;
168 Channel* cr = ServerInstance->FindChan(chan);
171 std::vector<std::string> setban;
172 setban.push_back(chan);
173 setban.push_back("-b");
174 setban.push_back(mask);
177 std::string expiry = "*** Timed ban on " + chan + " expired.";
178 cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
179 ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
181 ServerInstance->SendGlobalMode(setban, ServerInstance->FakeClient);
186 virtual Version GetVersion()
188 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
192 MODULE_INIT(ModuleTimedBans)