]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
WHEEEEE!!!!!
[user/henk/code/inspircd.git] / src / modules / m_timedbans.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 /* $ModDesc: Adds timed bans */
20
21 #include <stdio.h>
22 #include <vector>
23 #include "users.h"
24 #include "channels.h"
25 #include "modules.h"
26 #include "helperfuncs.h"
27 #include "hashcomp.h"
28
29 static Server *Srv;
30          
31 class TimedBan : public classbase
32 {
33  public:
34         std::string channel;
35         std::string mask;
36         time_t expire;
37 };
38
39 typedef std::vector<TimedBan> timedbans;
40 timedbans TimedBanList;
41
42 class cmd_tban : public command_t
43 {
44  public:
45         cmd_tban () : command_t("TBAN", 0, 3)
46         {
47                 this->source = "m_timedbans.so";
48                 syntax = "<channel> <duration> <banmask>";
49         }
50
51         void Handle (const char** parameters, int pcnt, userrec *user)
52         {
53                 chanrec* channel = Srv->FindChannel(parameters[0]);
54                 if (channel)
55                 {
56                         std::string cm = Srv->ChanMode(user,channel);
57                         if ((cm == "%") || (cm == "@"))
58                         {
59                                 if (!Srv->IsValidMask(parameters[2]))
60                                 {
61                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban mask");
62                                         return;
63                                 }
64                                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
65                                 {
66                                         if (!strcasecmp(i->data,parameters[2]))
67                                         {
68                                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :The ban "+std::string(parameters[2])+" is already on the banlist of "+std::string(parameters[0]));
69                                                 return;
70                                         }
71                                 }
72                                 TimedBan T;
73                                 std::string channelname = parameters[0];
74                                 unsigned long expire = Srv->CalcDuration(parameters[1]) + time(NULL);
75                                 if (Srv->CalcDuration(parameters[1]) < 1)
76                                 {
77                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban time");
78                                         return;
79                                 }
80                                 char duration[MAXBUF];
81                                 snprintf(duration,MAXBUF,"%lu",Srv->CalcDuration(parameters[1]));
82                                 std::string mask = parameters[2];
83                                 const char *setban[32];
84                                 setban[0] = parameters[0];
85                                 setban[1] = "+b";
86                                 setban[2] = parameters[2];
87                                 // use CallCommandHandler to make it so that the user sets the mode
88                                 // themselves
89                                 Srv->CallCommandHandler("MODE",setban,3,user);
90                                 /* Check if the ban was actually added (e.g. banlist was NOT full) */
91                                 bool was_added = false;
92                                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
93                                         if (!strcasecmp(i->data,mask.c_str()))
94                                                 was_added = true;
95                                 if (was_added)
96                                 {
97                                         T.channel = channelname;
98                                         T.mask = mask;
99                                         T.expire = expire;
100                                         TimedBanList.push_back(T);
101                                         channel->WriteChannelWithServ(Srv->GetServerName().c_str(), "NOTICE %s :%s added a timed ban on %s lasting for %s seconds.", channel->name, user->nick, mask.c_str(), duration);
102                                 }
103                                 return;
104                         }
105                         else user->WriteServ("482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, channel->name);
106                         return;
107                 }
108                 user->WriteServ("401 %s %s :No such channel",user->nick, parameters[0]);
109         }
110 };
111
112 class ModuleTimedBans : public Module
113 {
114         cmd_tban* mycommand;
115  public:
116         ModuleTimedBans(Server* Me)
117                 : Module::Module(Me)
118         {
119                 Srv = Me;
120                 mycommand = new cmd_tban();
121                 Srv->AddCommand(mycommand);
122                 TimedBanList.clear();
123         }
124         
125         virtual ~ModuleTimedBans()
126         {
127                 TimedBanList.clear();
128         }
129
130         void Implements(char* List)
131         {
132                 List[I_OnDelBan] = List[I_OnBackgroundTimer] = 1;
133         }
134
135         virtual int OnDelBan(userrec* source, chanrec* chan, const std::string &banmask)
136         {
137                 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
138                 {
139                         irc::string listitem = banmask.c_str();
140                         irc::string target = i->mask.c_str();
141                         if (listitem == target)
142                         {
143                                 TimedBanList.erase(i);
144                                 break;
145                         }
146                 }
147                 return 0;
148         }
149
150         virtual void OnBackgroundTimer(time_t curtime)
151         {
152                 bool again = true;
153                 while (again)
154                 {
155                         again = false;
156                         for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
157                         {
158                                 if (curtime > i->expire)
159                                 {
160                                         chanrec* cr = Srv->FindChannel(i->channel);
161                                         again = true;
162                                         if (cr)
163                                         {
164                                                 cr->WriteChannelWithServ(Srv->GetServerName().c_str(), "NOTICE %s :Timed ban on %s expired.", cr->name, i->mask.c_str());
165                                                 const char *setban[3];
166                                                 setban[0] = i->channel.c_str();
167                                                 setban[1] = "-b";
168                                                 setban[2] = i->mask.c_str();
169                                                 // kludge alert!
170                                                 // ::SendMode expects a userrec* to send the numeric replies
171                                                 // back to, so we create it a fake user that isnt in the user
172                                                 // hash and set its descriptor to FD_MAGIC_NUMBER so the data
173                                                 // falls into the abyss :p
174                                                 userrec* temp = new userrec;
175                                                 temp->fd = FD_MAGIC_NUMBER;
176                                                 temp->server = "";
177                                                 Srv->SendMode(setban,3,temp);
178                                                 /* FIX: Send mode remotely*/
179                                                 std::deque<std::string> n;
180                                                 n.push_back(i->channel);
181                                                 n.push_back("-b");
182                                                 n.push_back(i->mask);
183                                                 Event rmode((char *)&n, NULL, "send_mode");
184                                                 rmode.Send();
185                                                 DELETE(temp);
186                                         }
187                                         else
188                                         {
189                                                 /* Where the hell did our channel go?! */
190                                                 TimedBanList.erase(i);
191                                         }
192                                         // we used to delete the item here, but we dont need to as the servermode above does it for us,
193                                         break;
194                                 }
195                         }
196                 }
197         }
198         
199         virtual Version GetVersion()
200         {
201                 return Version(1,0,0,0,VF_VENDOR);
202         }
203 };
204
205
206 class ModuleTimedBansFactory : public ModuleFactory
207 {
208  public:
209         ModuleTimedBansFactory()
210         {
211         }
212         
213         ~ModuleTimedBansFactory()
214         {
215         }
216         
217         virtual Module * CreateModule(Server* Me)
218         {
219                 return new ModuleTimedBans(Me);
220         }
221         
222 };
223
224
225 extern "C" void * init_module( void )
226 {
227         return new ModuleTimedBansFactory;
228 }