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