]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
a778ab4c6848866ead67e7111ca7b8acf0b57196
[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 #include "configreader.h"
29 #include "inspircd.h"
30
31
32 extern InspIRCd* ServerInstance;
33
34 class TimedBan : public classbase
35 {
36  public:
37         std::string channel;
38         std::string mask;
39         time_t expire;
40 };
41
42 typedef std::vector<TimedBan> timedbans;
43 timedbans TimedBanList;
44
45 class cmd_tban : public command_t
46 {
47  public:
48         cmd_tban () : command_t("TBAN", 0, 3)
49         {
50                 this->source = "m_timedbans.so";
51                 syntax = "<channel> <duration> <banmask>";
52         }
53
54         void Handle (const char** parameters, int pcnt, userrec *user)
55         {
56                 chanrec* channel = ServerInstance->FindChan(parameters[0]);
57                 if (channel)
58                 {
59                         int cm = channel->GetStatus(user);
60                         if ((cm == STATUS_HOP) || (cm == STATUS_OP))
61                         {
62                                 if (!ServerInstance->IsValidMask(parameters[2]))
63                                 {
64                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban mask");
65                                         return;
66                                 }
67                                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
68                                 {
69                                         if (!strcasecmp(i->data,parameters[2]))
70                                         {
71                                                 user->WriteServ("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 = ServerInstance->CalcDuration(parameters[1]) + time(NULL);
78                                 if (ServerInstance->CalcDuration(parameters[1]) < 1)
79                                 {
80                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban time");
81                                         return;
82                                 }
83                                 char duration[MAXBUF];
84                                 snprintf(duration,MAXBUF,"%lu",ServerInstance->CalcDuration(parameters[1]));
85                                 std::string mask = parameters[2];
86                                 const 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                                 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()))
97                                                 was_added = true;
98                                 if (was_added)
99                                 {
100                                         T.channel = channelname;
101                                         T.mask = mask;
102                                         T.expire = expire;
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);
105                                 }
106                                 return;
107                         }
108                         else user->WriteServ("482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, channel->name);
109                         return;
110                 }
111                 user->WriteServ("401 %s %s :No such channel",user->nick, parameters[0]);
112         }
113 };
114
115 class ModuleTimedBans : public Module
116 {
117         cmd_tban* mycommand;
118  public:
119         ModuleTimedBans(InspIRCd* Me)
120                 : Module::Module(Me)
121         {
122                 
123                 mycommand = new cmd_tban();
124                 ServerInstance->AddCommand(mycommand);
125                 TimedBanList.clear();
126         }
127         
128         virtual ~ModuleTimedBans()
129         {
130                 TimedBanList.clear();
131         }
132
133         void Implements(char* List)
134         {
135                 List[I_OnDelBan] = List[I_OnBackgroundTimer] = 1;
136         }
137
138         virtual int OnDelBan(userrec* source, chanrec* chan, const std::string &banmask)
139         {
140                 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
141                 {
142                         irc::string listitem = banmask.c_str();
143                         irc::string target = i->mask.c_str();
144                         if (listitem == target)
145                         {
146                                 TimedBanList.erase(i);
147                                 break;
148                         }
149                 }
150                 return 0;
151         }
152
153         virtual void OnBackgroundTimer(time_t curtime)
154         {
155                 bool again = true;
156                 while (again)
157                 {
158                         again = false;
159                         for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
160                         {
161                                 if (curtime > i->expire)
162                                 {
163                                         chanrec* cr = ServerInstance->FindChan(i->channel);
164                                         again = true;
165                                         if (cr)
166                                         {
167                                                 cr->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :Timed ban on %s expired.", cr->name, i->mask.c_str());
168                                                 const char *setban[3];
169                                                 setban[0] = i->channel.c_str();
170                                                 setban[1] = "-b";
171                                                 setban[2] = i->mask.c_str();
172                                                 // kludge alert!
173                                                 // ::SendMode expects a userrec* to send the numeric replies
174                                                 // back to, so we create it a fake user that isnt in the user
175                                                 // hash and set its descriptor to FD_MAGIC_NUMBER so the data
176                                                 // falls into the abyss :p
177                                                 userrec* temp = new userrec(ServerInstance);
178                                                 temp->fd = FD_MAGIC_NUMBER;
179                                                 temp->server = "";
180                                                 ServerInstance->SendMode(setban,3,temp);
181                                                 /* FIX: Send mode remotely*/
182                                                 std::deque<std::string> n;
183                                                 n.push_back(i->channel);
184                                                 n.push_back("-b");
185                                                 n.push_back(i->mask);
186                                                 Event rmode((char *)&n, NULL, "send_mode");
187                                                 rmode.Send();
188                                                 DELETE(temp);
189                                         }
190                                         else
191                                         {
192                                                 /* Where the hell did our channel go?! */
193                                                 TimedBanList.erase(i);
194                                         }
195                                         // we used to delete the item here, but we dont need to as the servermode above does it for us,
196                                         break;
197                                 }
198                         }
199                 }
200         }
201         
202         virtual Version GetVersion()
203         {
204                 return Version(1,0,0,0,VF_VENDOR);
205         }
206 };
207
208
209 class ModuleTimedBansFactory : public ModuleFactory
210 {
211  public:
212         ModuleTimedBansFactory()
213         {
214         }
215         
216         ~ModuleTimedBansFactory()
217         {
218         }
219         
220         virtual Module * CreateModule(InspIRCd* Me)
221         {
222                 return new ModuleTimedBans(Me);
223         }
224         
225 };
226
227
228 extern "C" void * init_module( void )
229 {
230         return new ModuleTimedBansFactory;
231 }