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"
42 typedef std::vector<TimedBan> timedbans;
43 timedbans TimedBanList;
45 void handle_tban(char **parameters, int pcnt, userrec *user)
47 chanrec* channel = Srv->FindChannel(parameters[0]);
50 std::string cm = Srv->ChanMode(user,channel);
51 if ((cm == "%") || (cm == "@"))
53 if (!Srv->IsValidMask(parameters[2]))
55 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban mask");
58 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
60 irc::string listitem = i->mask.c_str();
61 irc::string target = parameters[2];
62 irc::string listchan = i->channel.c_str();
63 irc::string targetchan = parameters[0];
64 if ((listitem == target) && (listchan == targetchan))
66 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]));
71 std::string channelname = parameters[0];
72 unsigned long expire = Srv->CalcDuration(parameters[1]) + time(NULL);
73 if (Srv->CalcDuration(parameters[1]) < 1)
75 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban time");
78 char duration[MAXBUF];
79 snprintf(duration,MAXBUF,"%lu",Srv->CalcDuration(parameters[1]));
80 std::string mask = parameters[2];
82 setban[0] = parameters[0];
84 setban[2] = parameters[2];
85 // use CallCommandHandler to make it so that the user sets the mode
87 Srv->CallCommandHandler("MODE",setban,3,user);
88 T.channel = channelname;
91 TimedBanList.push_back(T);
92 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.");
95 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);
98 WriteServ(user->fd,"401 %s %s :No such channel",user->nick, parameters[0]);
101 class ModuleTimedBans : public Module
107 Srv->AddCommand("TBAN",handle_tban,0,3,"m_timedbans.so");
108 TimedBanList.clear();
111 virtual ~ModuleTimedBans()
114 TimedBanList.clear();
117 virtual int OnDelBan(userrec* source, chanrec* chan, std::string banmask)
119 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
121 irc::string listitem = banmask.c_str();
122 irc::string target = i->mask.c_str();
123 if (listitem == target)
125 TimedBanList.erase(i);
132 virtual void OnBackgroundTimer(time_t curtime)
138 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
140 if (curtime > i->expire)
142 chanrec* cr = Srv->FindChannel(i->channel);
146 Srv->SendChannelServerNotice(Srv->GetServerName(),cr,"NOTICE "+std::string(cr->name)+" :Timed ban on "+i->mask+" expired.");
148 setban[0] = (char*)i->channel.c_str();
150 setban[2] = (char*)i->mask.c_str();
152 // ::SendMode expects a userrec* to send the numeric replies
153 // back to, so we create it a fake user that isnt in the user
154 // hash and set its descriptor to FD_MAGIC_NUMBER so the data
155 // falls into the abyss :p
156 userrec* temp = new userrec;
157 temp->fd = FD_MAGIC_NUMBER;
158 Srv->SendMode(setban,3,temp);
161 // we used to delete the item here, but we dont need to as the servermode above does it for us,
168 virtual Version GetVersion()
170 return Version(1,0,0,0,VF_VENDOR);
175 class ModuleTimedBansFactory : public ModuleFactory
178 ModuleTimedBansFactory()
182 ~ModuleTimedBansFactory()
186 virtual Module * CreateModule()
188 return new ModuleTimedBans;
194 extern "C" void * init_module( void )
196 return new ModuleTimedBansFactory;