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 class cmd_tban : public command_t
45 cmd_tban () : command_t("TBAN", 0, 3)
47 this->source = "m_timedbans.cpp";
50 void Handle (char **parameters, int pcnt, userrec *user)
52 chanrec* channel = Srv->FindChannel(parameters[0]);
55 std::string cm = Srv->ChanMode(user,channel);
56 if ((cm == "%") || (cm == "@"))
58 if (!Srv->IsValidMask(parameters[2]))
60 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban mask");
63 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
65 irc::string listitem = i->mask.c_str();
66 irc::string target = parameters[2];
67 irc::string listchan = i->channel.c_str();
68 irc::string targetchan = parameters[0];
69 if ((listitem == target) && (listchan == targetchan))
71 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]));
76 std::string channelname = parameters[0];
77 unsigned long expire = Srv->CalcDuration(parameters[1]) + time(NULL);
78 if (Srv->CalcDuration(parameters[1]) < 1)
80 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban time");
83 char duration[MAXBUF];
84 snprintf(duration,MAXBUF,"%lu",Srv->CalcDuration(parameters[1]));
85 std::string mask = parameters[2];
87 setban[0] = parameters[0];
89 setban[2] = parameters[2];
90 // use CallCommandHandler to make it so that the user sets the mode
92 Srv->CallCommandHandler("MODE",setban,3,user);
93 T.channel = channelname;
96 TimedBanList.push_back(T);
97 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.");
100 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);
103 WriteServ(user->fd,"401 %s %s :No such channel",user->nick, parameters[0]);
107 class ModuleTimedBans : public Module
111 ModuleTimedBans(Server* Me)
115 mycommand = new cmd_tban();
116 Srv->AddCommand(mycommand);
117 TimedBanList.clear();
120 virtual ~ModuleTimedBans()
122 TimedBanList.clear();
125 virtual int OnDelBan(userrec* source, chanrec* chan, std::string banmask)
127 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
129 irc::string listitem = banmask.c_str();
130 irc::string target = i->mask.c_str();
131 if (listitem == target)
133 TimedBanList.erase(i);
140 virtual void OnBackgroundTimer(time_t curtime)
146 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
148 if (curtime > i->expire)
150 chanrec* cr = Srv->FindChannel(i->channel);
154 Srv->SendChannelServerNotice(Srv->GetServerName(),cr,"NOTICE "+std::string(cr->name)+" :Timed ban on "+i->mask+" expired.");
156 setban[0] = (char*)i->channel.c_str();
158 setban[2] = (char*)i->mask.c_str();
160 // ::SendMode expects a userrec* to send the numeric replies
161 // back to, so we create it a fake user that isnt in the user
162 // hash and set its descriptor to FD_MAGIC_NUMBER so the data
163 // falls into the abyss :p
164 userrec* temp = new userrec;
165 temp->fd = FD_MAGIC_NUMBER;
167 Srv->SendMode(setban,3,temp);
170 // we used to delete the item here, but we dont need to as the servermode above does it for us,
177 virtual Version GetVersion()
179 return Version(1,0,0,0,VF_VENDOR);
184 class ModuleTimedBansFactory : public ModuleFactory
187 ModuleTimedBansFactory()
191 ~ModuleTimedBansFactory()
195 virtual Module * CreateModule(Server* Me)
197 return new ModuleTimedBans(Me);
203 extern "C" void * init_module( void )
205 return new ModuleTimedBansFactory;