1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 /* $ModDesc: Adds timed bans */
26 #include "helperfuncs.h"
39 typedef std::vector<TimedBan> timedbans;
40 timedbans TimedBanList;
42 void handle_tban(char **parameters, int pcnt, userrec *user)
44 chanrec* channel = Srv->FindChannel(parameters[0]);
47 std::string cm = Srv->ChanMode(user,channel);
48 if ((cm == "%") || (cm == "@"))
50 if (!Srv->IsValidMask(parameters[2]))
52 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban mask");
55 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
57 irc::string listitem = i->mask.c_str();
58 irc::string target = parameters[2];
59 irc::string listchan = i->channel.c_str();
60 irc::string targetchan = parameters[0];
61 if ((listitem == target) && (listchan == targetchan))
63 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :The ban "+std::string(parameters[2])+" is already on the banlist of "+std::string(parameters[0]));
68 std::string channelname = parameters[0];
69 unsigned long expire = Srv->CalcDuration(parameters[1]) + time(NULL);
70 if (Srv->CalcDuration(parameters[1]) < 1)
72 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban time");
75 char duration[MAXBUF];
76 snprintf(duration,MAXBUF,"%lu",Srv->CalcDuration(parameters[1]));
77 std::string mask = parameters[2];
79 setban[0] = parameters[0];
81 setban[2] = parameters[2];
82 // use CallCommandHandler to make it so that the user sets the mode
84 Srv->CallCommandHandler("MODE",setban,3,user);
85 T.channel = channelname;
88 TimedBanList.push_back(T);
89 Srv->SendChannelServerNotice(Srv->GetServerName(),channel,"NOTICE "+std::string(channel->name)+" :"+std::string(user->nick)+" added a timed ban on "+mask+" lasting for "+std::string(duration)+" seconds.");
92 else WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, channel->name);
95 WriteServ(user->fd,"401 %s %s :No such channel",user->nick, parameters[0]);
98 class ModuleTimedBans : public Module
104 Srv->AddCommand("TBAN",handle_tban,0,3,"m_timedbans.so");
105 TimedBanList.clear();
108 virtual ~ModuleTimedBans()
111 TimedBanList.clear();
114 virtual int OnDelBan(userrec* source, chanrec* chan, std::string banmask)
116 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
118 irc::string listitem = banmask.c_str();
119 irc::string target = i->mask.c_str();
120 if (listitem == target)
122 TimedBanList.erase(i);
129 virtual void OnBackgroundTimer(time_t curtime)
135 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
137 if (curtime > i->expire)
139 chanrec* cr = Srv->FindChannel(i->channel);
143 Srv->SendChannelServerNotice(Srv->GetServerName(),cr,"NOTICE "+std::string(cr->name)+" :Timed ban on "+i->mask+" expired.");
145 setban[0] = (char*)i->channel.c_str();
147 setban[2] = (char*)i->mask.c_str();
149 // ::SendMode expects a userrec* to send the numeric replies
150 // back to, so we create it a fake user that isnt in the user
151 // hash and set its descriptor to FD_MAGIC_NUMBER so the data
152 // falls into the abyss :p
153 userrec* temp = new userrec;
154 temp->fd = FD_MAGIC_NUMBER;
156 Srv->SendMode(setban,3,temp);
159 // we used to delete the item here, but we dont need to as the servermode above does it for us,
166 virtual Version GetVersion()
168 return Version(1,0,0,0,VF_VENDOR);
173 class ModuleTimedBansFactory : public ModuleFactory
176 ModuleTimedBansFactory()
180 ~ModuleTimedBansFactory()
184 virtual Module * CreateModule()
186 return new ModuleTimedBans;
192 extern "C" void * init_module( void )
194 return new ModuleTimedBansFactory;