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 */
28 typedef std::vector<TimedBan> timedbans;
29 timedbans TimedBanList;
33 class CommandTban : public Command
36 CommandTban(Module* Creator) : Command(Creator,"TBAN", 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->GetPrefixValue(user);
48 if ((cm == HALFOP_VALUE) || (cm == OP_VALUE))
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());
111 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
113 return ROUTE_BROADCAST;
117 class ModuleTimedBans : public Module
124 ServerInstance->AddCommand(&cmd);
125 TimedBanList.clear();
126 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
127 ServerInstance->Modules->Attach(eventlist, this, 2);
130 virtual ~ModuleTimedBans()
132 TimedBanList.clear();
135 virtual ModResult OnDelBan(User* source, Channel* chan, const std::string &banmask)
137 irc::string listitem = banmask.c_str();
138 irc::string thischan = chan->name.c_str();
139 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); i++)
141 irc::string target = i->mask.c_str();
142 irc::string tchan = i->channel.c_str();
143 if ((listitem == target) && (tchan == thischan))
145 TimedBanList.erase(i);
149 return MOD_RES_PASSTHRU;
152 virtual void OnBackgroundTimer(time_t curtime)
154 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
156 if (curtime > i->expire)
158 std::string chan = i->channel;
159 std::string mask = i->mask;
160 Channel* cr = ServerInstance->FindChan(chan);
161 i = TimedBanList.erase(i);
164 std::vector<std::string> setban;
165 setban.push_back(chan);
166 setban.push_back("-b");
167 setban.push_back(mask);
170 std::string expiry = "*** Timed ban on " + chan + " expired.";
171 cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
172 ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
173 if (ServerInstance->Config->AllowHalfop)
175 cr->WriteAllExcept(ServerInstance->FakeClient, true, '%', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
176 ServerInstance->PI->SendChannelNotice(cr, '%', expiry);
179 ServerInstance->SendMode(setban, ServerInstance->FakeClient);
180 ServerInstance->PI->SendMode(chan, ServerInstance->Modes->GetLastParseParams(), ServerInstance->Modes->GetLastParseTranslate());
188 virtual Version GetVersion()
190 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
194 MODULE_INIT(ModuleTimedBans)