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, Module* Creator) : Command(Instance, Creator,"TBAN", 0, 3)
38 syntax = "<channel> <duration> <banmask>";
39 TRANSLATE4(TR_TEXT, TR_TEXT, TR_TEXT, TR_END);
42 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
44 Channel* channel = ServerInstance->FindChan(parameters[0]);
47 int cm = channel->GetStatus(user);
48 if ((cm == STATUS_HOP) || (cm == STATUS_OP))
50 if (!ServerInstance->IsValidMask(parameters[2]))
52 user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban mask");
55 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
57 if (!strcasecmp(i->data.c_str(), parameters[2].c_str()))
59 user->WriteServ("NOTICE "+std::string(user->nick)+" :The ban "+parameters[2]+" is already on the banlist of "+parameters[0]);
64 std::string channelname = parameters[0];
65 long duration = ServerInstance->Duration(parameters[1]);
66 unsigned long expire = duration + ServerInstance->Time();
69 user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban time");
72 std::string mask = parameters[2];
73 std::vector<std::string> setban;
74 setban.push_back(parameters[0]);
75 setban.push_back("+b");
76 setban.push_back(parameters[2]);
77 // use CallCommandHandler to make it so that the user sets the mode
79 ServerInstance->CallCommandHandler("MODE",setban,user);
80 /* Check if the ban was actually added (e.g. banlist was NOT full) */
81 bool was_added = false;
82 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
83 if (!strcasecmp(i->data.c_str(), mask.c_str()))
88 T.channel = channelname;
91 TimedBanList.push_back(T);
92 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);
93 ServerInstance->PI->SendChannelNotice(channel, '@', user->nick + " added a timed ban on " + mask + " lasting for " + ConvToStr(duration) + " seconds.");
94 if (ServerInstance->Config->AllowHalfop)
96 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);
97 ServerInstance->PI->SendChannelNotice(channel, '%', user->nick + " added a timed ban on " + mask + " lasting for " + ConvToStr(duration) + " seconds.");
103 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(),
104 ServerInstance->Config->AllowHalfop ? " half-" : " channel ");
107 user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
112 class ModuleTimedBans : public Module
116 ModuleTimedBans(InspIRCd* Me)
117 : Module(Me), cmd(Me, this)
119 ServerInstance->AddCommand(&cmd);
120 TimedBanList.clear();
121 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
122 ServerInstance->Modules->Attach(eventlist, this, 2);
125 virtual ~ModuleTimedBans()
127 TimedBanList.clear();
130 virtual int OnDelBan(User* source, Channel* chan, const std::string &banmask)
132 irc::string listitem = banmask.c_str();
133 irc::string thischan = chan->name.c_str();
134 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); i++)
136 irc::string target = i->mask.c_str();
137 irc::string tchan = i->channel.c_str();
138 if ((listitem == target) && (tchan == thischan))
140 TimedBanList.erase(i);
147 virtual void OnBackgroundTimer(time_t curtime)
149 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
151 if (curtime > i->expire)
153 std::string chan = i->channel;
154 std::string mask = i->mask;
155 Channel* cr = ServerInstance->FindChan(chan);
156 i = TimedBanList.erase(i);
159 std::vector<std::string> setban;
160 setban.push_back(chan);
161 setban.push_back("-b");
162 setban.push_back(mask);
165 std::string expiry = "*** Timed ban on " + chan + " expired.";
166 cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
167 ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
168 if (ServerInstance->Config->AllowHalfop)
170 cr->WriteAllExcept(ServerInstance->FakeClient, true, '%', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
171 ServerInstance->PI->SendChannelNotice(cr, '%', expiry);
174 ServerInstance->SendMode(setban, ServerInstance->FakeClient);
175 ServerInstance->PI->SendMode(chan, ServerInstance->Modes->GetLastParseParams(), ServerInstance->Modes->GetLastParseTranslate());
183 virtual Version GetVersion()
185 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
189 MODULE_INIT(ModuleTimedBans)