1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $ModDesc: Adds timed bans */
20 class TimedBan : public classbase
28 typedef std::vector<TimedBan> timedbans;
29 timedbans TimedBanList;
33 class CommandTban : public Command
36 CommandTban (InspIRCd* Instance) : Command(Instance,"TBAN", 0, 3)
38 this->source = "m_timedbans.so";
39 syntax = "<channel> <duration> <banmask>";
40 TRANSLATE4(TR_TEXT, TR_TEXT, TR_TEXT, TR_END);
43 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
45 Channel* channel = ServerInstance->FindChan(parameters[0]);
48 int cm = channel->GetStatus(user);
49 if ((cm == STATUS_HOP) || (cm == STATUS_OP))
51 if (!ServerInstance->IsValidMask(parameters[2]))
53 user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban mask");
56 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
58 if (!strcasecmp(i->data.c_str(), parameters[2].c_str()))
60 user->WriteServ("NOTICE "+std::string(user->nick)+" :The ban "+parameters[2]+" is already on the banlist of "+parameters[0]);
65 std::string channelname = parameters[0];
66 long duration = ServerInstance->Duration(parameters[1]);
67 unsigned long expire = duration + ServerInstance->Time();
70 user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban time");
73 std::string mask = parameters[2];
74 std::vector<std::string> setban;
75 setban.push_back(parameters[0]);
76 setban.push_back("+b");
77 setban.push_back(parameters[2]);
78 // use CallCommandHandler to make it so that the user sets the mode
80 ServerInstance->CallCommandHandler("MODE",setban,user);
81 /* Check if the ban was actually added (e.g. banlist was NOT full) */
82 bool was_added = false;
83 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
84 if (!strcasecmp(i->data.c_str(), mask.c_str()))
89 T.channel = channelname;
92 TimedBanList.push_back(T);
93 channel->WriteAllExcept(user, true, '@', 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);
94 ServerInstance->PI->SendChannelNotice(channel, '@', user->nick + " added a timed ban on " + mask + " lasting for " + ConvToStr(duration) + " seconds.");
95 if (ServerInstance->Config->AllowHalfop)
97 channel->WriteAllExcept(user, true, '%', 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);
98 ServerInstance->PI->SendChannelNotice(channel, '%', user->nick + " added a timed ban on " + mask + " lasting for " + ConvToStr(duration) + " seconds.");
104 else user->WriteNumeric(482, "%s %s :You must be at least a%soperator to change modes on this channel",user->nick.c_str(), channel->name.c_str(),
105 ServerInstance->Config->AllowHalfop ? " half-" : " channel ");
108 user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
113 class ModuleTimedBans : public Module
115 CommandTban* mycommand;
117 ModuleTimedBans(InspIRCd* Me)
121 mycommand = new CommandTban(ServerInstance);
122 ServerInstance->AddCommand(mycommand);
123 TimedBanList.clear();
124 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
125 ServerInstance->Modules->Attach(eventlist, this, 2);
128 virtual ~ModuleTimedBans()
130 TimedBanList.clear();
133 virtual int 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);
150 virtual void OnBackgroundTimer(time_t curtime)
152 timedbans::iterator safei;
153 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end();)
155 /* Safe copy of iterator, so we can erase as we iterate */
159 if (curtime > safei->expire)
161 Channel* cr = ServerInstance->FindChan(safei->channel);
164 std::string mask = safei->mask;
165 std::vector<std::string> setban;
166 setban.push_back(safei->channel);
167 setban.push_back("-b");
168 setban.push_back(mask);
171 cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :*** Timed ban on %s expired.", cr->name.c_str(), safei->mask.c_str());
172 ServerInstance->PI->SendChannelNotice(cr, '@', "*** Timed ban on " + safei->mask + " expired.");
173 if (ServerInstance->Config->AllowHalfop)
175 cr->WriteAllExcept(ServerInstance->FakeClient, true, '%', empty, "NOTICE %s :*** Timed ban on %s expired.", cr->name.c_str(), safei->mask.c_str());
176 ServerInstance->PI->SendChannelNotice(cr, '%', "*** Timed ban on " + safei->mask + " expired.");
179 /* Removes the ban item for us, no ::erase() needed */
180 ServerInstance->PI->SendModeStr(safei->channel, std::string("-b ") + setban[2]);
181 ServerInstance->SendMode(setban, ServerInstance->FakeClient);
183 if (ServerInstance->Modes->GetLastParse().empty())
184 TimedBanList.erase(safei);
188 /* Where the hell did our channel go?! */
189 TimedBanList.erase(safei);
195 virtual Version GetVersion()
197 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
201 MODULE_INIT(ModuleTimedBans)