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