]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
b05cbf8f77a2f890c647f500c1a75782438b3c1d
[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                                         Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban mask");
62                                         return;
63                                 }
64                                 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
65                                 {
66                                         irc::string listitem = i->mask.c_str();
67                                         irc::string target = parameters[2];
68                                         irc::string listchan = i->channel.c_str();
69                                         irc::string targetchan = parameters[0];
70                                         if ((listitem == target) && (listchan == targetchan))
71                                         {
72                                                 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]));
73                                                 return;
74                                         }
75                                 }
76                                 TimedBan T;
77                                 std::string channelname = parameters[0];
78                                 unsigned long expire = Srv->CalcDuration(parameters[1]) + time(NULL);
79                                 if (Srv->CalcDuration(parameters[1]) < 1)
80                                 {
81                                         Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :Invalid ban time");
82                                         return;
83                                 }
84                                 char duration[MAXBUF];
85                                 snprintf(duration,MAXBUF,"%lu",Srv->CalcDuration(parameters[1]));
86                                 std::string mask = parameters[2];
87                                 const char *setban[32];
88                                 setban[0] = parameters[0];
89                                 setban[1] = "+b";
90                                 setban[2] = parameters[2];
91                                 // use CallCommandHandler to make it so that the user sets the mode
92                                 // themselves
93                                 Srv->CallCommandHandler("MODE",setban,3,user);
94                                 T.channel = channelname;
95                                 T.mask = mask;
96                                 T.expire = expire;
97                                 TimedBanList.push_back(T);
98                                 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.");
99                                 return;
100                         }
101                         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);
102                         return;
103                 }
104                 WriteServ(user->fd,"401 %s %s :No such channel",user->nick, parameters[0]);
105         }
106 };
107
108 class ModuleTimedBans : public Module
109 {
110         cmd_tban* mycommand;
111  public:
112         ModuleTimedBans(Server* Me)
113                 : Module::Module(Me)
114         {
115                 Srv = Me;
116                 mycommand = new cmd_tban();
117                 Srv->AddCommand(mycommand);
118                 TimedBanList.clear();
119         }
120         
121         virtual ~ModuleTimedBans()
122         {
123                 TimedBanList.clear();
124         }
125
126         void Implements(char* List)
127         {
128                 List[I_OnDelBan] = List[I_OnBackgroundTimer] = 1;
129         }
130
131         virtual int OnDelBan(userrec* source, chanrec* chan, const std::string &banmask)
132         {
133                 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
134                 {
135                         irc::string listitem = banmask.c_str();
136                         irc::string target = i->mask.c_str();
137                         if (listitem == target)
138                         {
139                                 TimedBanList.erase(i);
140                                 break;
141                         }
142                 }
143                 return 0;
144         }
145
146         virtual void OnBackgroundTimer(time_t curtime)
147         {
148                 bool again = true;
149                 while (again)
150                 {
151                         again = false;
152                         for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
153                         {
154                                 if (curtime > i->expire)
155                                 {
156                                         chanrec* cr = Srv->FindChannel(i->channel);
157                                         again = true;
158                                         if (cr)
159                                         {
160                                                 Srv->SendChannelServerNotice(Srv->GetServerName(),cr,"NOTICE "+std::string(cr->name)+" :Timed ban on "+i->mask+" expired.");
161                                                 const char *setban[3];
162                                                 setban[0] = i->channel.c_str();
163                                                 setban[1] = "-b";
164                                                 setban[2] = i->mask.c_str();
165                                                 // kludge alert!
166                                                 // ::SendMode expects a userrec* to send the numeric replies
167                                                 // back to, so we create it a fake user that isnt in the user
168                                                 // hash and set its descriptor to FD_MAGIC_NUMBER so the data
169                                                 // falls into the abyss :p
170                                                 userrec* temp = new userrec;
171                                                 temp->fd = FD_MAGIC_NUMBER;
172                                                 temp->server = "";
173                                                 Srv->SendMode(setban,3,temp);
174                                                 DELETE(temp);
175                                         }
176                                         // we used to delete the item here, but we dont need to as the servermode above does it for us,
177                                         break;
178                                 }
179                         }
180                 }
181         }
182         
183         virtual Version GetVersion()
184         {
185                 return Version(1,0,0,0,VF_VENDOR);
186         }
187 };
188
189
190 class ModuleTimedBansFactory : public ModuleFactory
191 {
192  public:
193         ModuleTimedBansFactory()
194         {
195         }
196         
197         ~ModuleTimedBansFactory()
198         {
199         }
200         
201         virtual Module * CreateModule(Server* Me)
202         {
203                 return new ModuleTimedBans(Me);
204         }
205         
206 };
207
208
209 extern "C" void * init_module( void )
210 {
211         return new ModuleTimedBansFactory;
212 }