1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 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.so";
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 void Implements(char* List)
127 List[I_OnDelBan] = List[I_OnBackgroundTimer] = 1;
130 virtual int OnDelBan(userrec* source, chanrec* chan, const std::string &banmask)
132 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
134 irc::string listitem = banmask.c_str();
135 irc::string target = i->mask.c_str();
136 if (listitem == target)
138 TimedBanList.erase(i);
145 virtual void OnBackgroundTimer(time_t curtime)
151 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
153 if (curtime > i->expire)
155 chanrec* cr = Srv->FindChannel(i->channel);
159 Srv->SendChannelServerNotice(Srv->GetServerName(),cr,"NOTICE "+std::string(cr->name)+" :Timed ban on "+i->mask+" expired.");
161 setban[0] = (char*)i->channel.c_str();
163 setban[2] = (char*)i->mask.c_str();
165 // ::SendMode expects a userrec* to send the numeric replies
166 // back to, so we create it a fake user that isnt in the user
167 // hash and set its descriptor to FD_MAGIC_NUMBER so the data
168 // falls into the abyss :p
169 userrec* temp = new userrec;
170 temp->fd = FD_MAGIC_NUMBER;
172 Srv->SendMode(setban,3,temp);
175 // we used to delete the item here, but we dont need to as the servermode above does it for us,
182 virtual Version GetVersion()
184 return Version(1,0,0,0,VF_VENDOR);
189 class ModuleTimedBansFactory : public ModuleFactory
192 ModuleTimedBansFactory()
196 ~ModuleTimedBansFactory()
200 virtual Module * CreateModule(Server* Me)
202 return new ModuleTimedBans(Me);
208 extern "C" void * init_module( void )
210 return new ModuleTimedBansFactory;