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 */
28 #include "configreader.h"
34 class TimedBan : public classbase
42 typedef std::vector<TimedBan> timedbans;
43 timedbans TimedBanList;
45 class cmd_tban : public command_t
48 cmd_tban (InspIRCd* Instance) : command_t(Instance,"TBAN", 0, 3)
50 this->source = "m_timedbans.so";
51 syntax = "<channel> <duration> <banmask>";
54 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
56 chanrec* channel = ServerInstance->FindChan(parameters[0]);
59 int cm = channel->GetStatus(user);
60 if ((cm == STATUS_HOP) || (cm == STATUS_OP))
62 if (!ServerInstance->IsValidMask(parameters[2]))
64 user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban mask");
67 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
69 if (!strcasecmp(i->data,parameters[2]))
71 user->WriteServ("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 = ServerInstance->Duration(parameters[1]) + time(NULL);
78 if (ServerInstance->Duration(parameters[1]) < 1)
80 user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban time");
83 char duration[MAXBUF];
84 snprintf(duration,MAXBUF,"%lu",ServerInstance->Duration(parameters[1]));
85 std::string mask = parameters[2];
86 const char *setban[32];
87 setban[0] = parameters[0];
89 setban[2] = parameters[2];
90 // use CallCommandHandler to make it so that the user sets the mode
92 ServerInstance->CallCommandHandler("MODE",setban,3,user);
93 /* Check if the ban was actually added (e.g. banlist was NOT full) */
94 bool was_added = false;
95 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
96 if (!strcasecmp(i->data,mask.c_str()))
100 T.channel = channelname;
103 TimedBanList.push_back(T);
104 channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s added a timed ban on %s lasting for %s seconds.", channel->name, user->nick, mask.c_str(), duration);
109 else user->WriteServ("482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, channel->name);
112 user->WriteServ("401 %s %s :No such channel",user->nick, parameters[0]);
117 class ModuleTimedBans : public Module
121 ModuleTimedBans(InspIRCd* Me)
125 mycommand = new cmd_tban(ServerInstance);
126 ServerInstance->AddCommand(mycommand);
127 TimedBanList.clear();
130 virtual ~ModuleTimedBans()
132 TimedBanList.clear();
135 void Implements(char* List)
137 List[I_OnDelBan] = List[I_OnBackgroundTimer] = 1;
140 virtual int OnDelBan(userrec* source, chanrec* chan, const std::string &banmask)
142 irc::string listitem = banmask.c_str();
143 irc::string thischan = chan->name;
144 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
146 irc::string target = i->mask.c_str();
147 irc::string tchan = i->channel.c_str();
148 if ((listitem == target) && (tchan == thischan))
150 TimedBanList.erase(i);
157 virtual void OnBackgroundTimer(time_t curtime)
163 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
165 if (curtime > i->expire)
167 chanrec* cr = ServerInstance->FindChan(i->channel);
171 cr->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :Timed ban on %s expired.", cr->name, i->mask.c_str());
172 const char *setban[3];
173 setban[0] = i->channel.c_str();
175 setban[2] = i->mask.c_str();
177 // ::SendMode expects a userrec* to send the numeric replies
178 // back to, so we create it a fake user that isnt in the user
179 // hash and set its descriptor to FD_MAGIC_NUMBER so the data
180 // falls into the abyss :p
181 userrec* temp = new userrec(ServerInstance);
182 temp->SetFd(FD_MAGIC_NUMBER);
183 /* FIX: Send mode remotely*/
184 std::deque<std::string> n;
185 n.push_back(setban[0]);
187 n.push_back(setban[2]);
188 ServerInstance->SendMode(setban,3,temp);
189 Event rmode((char *)&n, NULL, "send_mode");
190 rmode.Send(ServerInstance);
195 /* Where the hell did our channel go?! */
196 TimedBanList.erase(i);
198 // we used to delete the item here, but we dont need to as the servermode above does it for us,
205 virtual Version GetVersion()
207 return Version(1,0,0,0,VF_VENDOR);
212 class ModuleTimedBansFactory : public ModuleFactory
215 ModuleTimedBansFactory()
219 ~ModuleTimedBansFactory()
223 virtual Module * CreateModule(InspIRCd* Me)
225 return new ModuleTimedBans(Me);
231 extern "C" void * init_module( void )
233 return new ModuleTimedBansFactory;