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 * ---------------------------------------------------
17 /* $ModDesc: Adds timed bans */
21 * Err... not a lot really.
29 #include "helperfuncs.h"
41 typedef std::vector<TimedBan> timedbans;
42 timedbans TimedBanList;
44 void handle_tban(char **parameters, int pcnt, userrec *user)
46 chanrec* channel = Srv->FindChannel(parameters[0]);
49 std::string cm = Srv->ChanMode(user,channel);
50 if ((cm == "%") || (cm == "@"))
52 if (!Srv->IsValidMask(parameters[2]))
54 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban mask");
57 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
59 if ((!strcasecmp(i->mask.c_str(),parameters[2])) && (!strcasecmp(i->channel.c_str(),parameters[0])))
61 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]));
66 std::string channelname = parameters[0];
67 unsigned long expire = Srv->CalcDuration(parameters[1]) + time(NULL);
68 if (Srv->CalcDuration(parameters[1]) < 1)
70 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban time");
73 char duration[MAXBUF];
74 snprintf(duration,MAXBUF,"%lu",Srv->CalcDuration(parameters[1]));
75 std::string mask = parameters[2];
77 setban[0] = parameters[0];
79 setban[2] = parameters[2];
80 // use CallCommandHandler to make it so that the user sets the mode
82 Srv->CallCommandHandler("MODE",setban,3,user);
83 T.channel = channelname;
86 TimedBanList.push_back(T);
87 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.");
90 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);
93 WriteServ(user->fd,"401 %s %s :No such channel",user->nick, parameters[0]);
96 class ModuleTimedBans : public Module
102 Srv->AddCommand("TBAN",handle_tban,0,3,"m_timedbans.so");
103 TimedBanList.clear();
106 virtual ~ModuleTimedBans()
109 TimedBanList.clear();
112 virtual int OnDelBan(userrec* source, chanrec* chan, std::string banmask)
114 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
116 if (!strcasecmp(banmask.c_str(),i->mask.c_str()))
118 TimedBanList.erase(i);
125 virtual void OnBackgroundTimer(time_t curtime)
131 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
133 if (curtime > i->expire)
135 chanrec* cr = Srv->FindChannel(i->channel);
139 Srv->SendChannelServerNotice(Srv->GetServerName(),cr,"NOTICE "+std::string(cr->name)+" :Timed ban on "+i->mask+" expired.");
141 setban[0] = (char*)i->channel.c_str();
143 setban[2] = (char*)i->mask.c_str();
145 // ::SendMode expects a userrec* to send the numeric replies
146 // back to, so we create it a fake user that isnt in the user
147 // hash and set its descriptor to FD_MAGIC_NUMBER so the data
148 // falls into the abyss :p
149 userrec* temp = new userrec;
150 temp->fd = FD_MAGIC_NUMBER;
151 Srv->SendMode(setban,3,temp);
154 // we used to delete the item here, but we dont need to as the servermode above does it for us,
161 virtual Version GetVersion()
163 return Version(1,0,0,0,VF_VENDOR);
168 class ModuleTimedBansFactory : public ModuleFactory
171 ModuleTimedBansFactory()
175 ~ModuleTimedBansFactory()
179 virtual Module * CreateModule()
181 return new ModuleTimedBans;
187 extern "C" void * init_module( void )
189 return new ModuleTimedBansFactory;