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.
40 typedef std::vector<TimedBan> timedbans;
41 timedbans TimedBanList;
43 void handle_tban(char **parameters, int pcnt, userrec *user)
45 chanrec* channel = Srv->FindChannel(parameters[0]);
48 std::string cm = Srv->ChanMode(user,channel);
49 if ((cm == "%") || (cm == "@"))
51 if (!Srv->IsValidMask(parameters[2]))
53 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban mask");
56 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
58 if ((!strcasecmp(i->mask.c_str(),parameters[2])) && (!strcasecmp(i->channel.c_str(),parameters[0])))
60 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]));
65 std::string channelname = parameters[0];
66 unsigned long expire = Srv->CalcDuration(parameters[1]) + time(NULL);
67 if (Srv->CalcDuration(parameters[1]) < 1)
69 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban time");
72 char duration[MAXBUF];
73 snprintf(duration,MAXBUF,"%lu",Srv->CalcDuration(parameters[1]));
74 std::string mask = parameters[2];
76 setban[0] = parameters[0];
78 setban[2] = parameters[2];
79 // use CallCommandHandler to make it so that the user sets the mode
81 Srv->CallCommandHandler("MODE",setban,3,user);
82 T.channel = channelname;
85 TimedBanList.push_back(T);
86 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.");
89 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);
92 WriteServ(user->fd,"401 %s %s :No such channel",user->nick, parameters[0]);
95 class ModuleTimedBans : public Module
101 Srv->AddCommand("TBAN",handle_tban,0,3,"m_timedbans.so");
102 TimedBanList.clear();
105 virtual ~ModuleTimedBans()
108 TimedBanList.clear();
111 virtual int OnDelBan(userrec* source, chanrec* chan, std::string banmask)
113 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
115 if (!strcasecmp(banmask.c_str(),i->mask.c_str()))
117 TimedBanList.erase(i);
124 virtual void OnBackgroundTimer(time_t curtime)
130 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
132 if (curtime > i->expire)
134 chanrec* cr = Srv->FindChannel(i->channel);
138 Srv->SendChannelServerNotice(Srv->GetServerName(),cr,"NOTICE "+std::string(cr->name)+" :Timed ban on "+i->mask+" expired.");
140 setban[0] = (char*)i->channel.c_str();
142 setban[2] = (char*)i->mask.c_str();
144 // ::SendMode expects a userrec* to send the numeric replies
145 // back to, so we create it a fake user that isnt in the user
146 // hash and set its descriptor to FD_MAGIC_NUMBER so the data
147 // falls into the abyss :p
148 userrec* temp = new userrec;
149 temp->fd = FD_MAGIC_NUMBER;
150 Srv->SendMode(setban,3,temp);
153 // we used to delete the item here, but we dont need to as the servermode above does it for us,
160 virtual Version GetVersion()
162 return Version(1,0,0,0,VF_VENDOR);
167 class ModuleTimedBansFactory : public ModuleFactory
170 ModuleTimedBansFactory()
174 ~ModuleTimedBansFactory()
178 virtual Module * CreateModule()
180 return new ModuleTimedBans;
186 extern "C" void * init_module( void )
188 return new ModuleTimedBansFactory;