]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
Windows support. Tested and working to compile on freebsd and linux. Next step is...
[user/henk/code/inspircd.git] / src / modules / m_timedbans.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $ModDesc: Adds timed bans */
15
16 #include <stdio.h>
17 #include <vector>
18 #include "users.h"
19 #include "channels.h"
20 #include "modules.h"
21 #include "hashcomp.h"
22 #include "configreader.h"
23 #include "inspircd.h"
24
25
26 /** Holds a timed ban
27  */
28 class TimedBan : public classbase
29 {
30  public:
31         std::string channel;
32         std::string mask;
33         time_t expire;
34 };
35
36 typedef std::vector<TimedBan> timedbans;
37 timedbans TimedBanList;
38
39 /** Handle /TBAN
40  */
41 class cmd_tban : public command_t
42 {
43  public:
44  cmd_tban (InspIRCd* Instance) : command_t(Instance,"TBAN", 0, 3)
45         {
46                 this->source = "m_timedbans.so";
47                 syntax = "<channel> <duration> <banmask>";
48         }
49
50         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
51         {
52                 chanrec* channel = ServerInstance->FindChan(parameters[0]);
53                 if (channel)
54                 {
55                         int cm = channel->GetStatus(user);
56                         if ((cm == STATUS_HOP) || (cm == STATUS_OP))
57                         {
58                                 if (!ServerInstance->IsValidMask(parameters[2]))
59                                 {
60                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban mask");
61                                         return CMD_FAILURE;
62                                 }
63                                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
64                                 {
65                                         if (!strcasecmp(i->data,parameters[2]))
66                                         {
67                                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :The ban "+std::string(parameters[2])+" is already on the banlist of "+std::string(parameters[0]));
68                                                 return CMD_FAILURE;
69                                         }
70                                 }
71                                 TimedBan T;
72                                 std::string channelname = parameters[0];
73                                 unsigned long expire = ServerInstance->Duration(parameters[1]) + time(NULL);
74                                 if (ServerInstance->Duration(parameters[1]) < 1)
75                                 {
76                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban time");
77                                         return CMD_FAILURE;
78                                 }
79                                 char duration[MAXBUF];
80                                 snprintf(duration,MAXBUF,"%lu",ServerInstance->Duration(parameters[1]));
81                                 std::string mask = parameters[2];
82                                 const char *setban[32];
83                                 setban[0] = parameters[0];
84                                 setban[1] = "+b";
85                                 setban[2] = parameters[2];
86                                 // use CallCommandHandler to make it so that the user sets the mode
87                                 // themselves
88                                 ServerInstance->CallCommandHandler("MODE",setban,3,user);
89                                 /* Check if the ban was actually added (e.g. banlist was NOT full) */
90                                 bool was_added = false;
91                                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
92                                         if (!strcasecmp(i->data,mask.c_str()))
93                                                 was_added = true;
94                                 if (was_added)
95                                 {
96                                         T.channel = channelname;
97                                         T.mask = mask;
98                                         T.expire = expire;
99                                         TimedBanList.push_back(T);
100                                         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);
101                                         return CMD_SUCCESS;
102                                 }
103                                 return CMD_FAILURE;
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 CMD_FAILURE;
107                 }
108                 user->WriteServ("401 %s %s :No such channel",user->nick, parameters[0]);
109                 return CMD_FAILURE;
110         }
111 };
112
113 class ModuleTimedBans : public Module
114 {
115         cmd_tban* mycommand;
116  public:
117         ModuleTimedBans(InspIRCd* Me)
118                 : Module(Me)
119         {
120                 
121                 mycommand = new cmd_tban(ServerInstance);
122                 ServerInstance->AddCommand(mycommand);
123                 TimedBanList.clear();
124         }
125         
126         virtual ~ModuleTimedBans()
127         {
128                 TimedBanList.clear();
129         }
130
131         void Implements(char* List)
132         {
133                 List[I_OnDelBan] = List[I_OnBackgroundTimer] = 1;
134         }
135
136         virtual int OnDelBan(userrec* source, chanrec* chan, const std::string &banmask)
137         {
138                 irc::string listitem = banmask.c_str();
139                 irc::string thischan = chan->name;
140                 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
141                 {
142                         irc::string target = i->mask.c_str();
143                         irc::string tchan = i->channel.c_str();
144                         if ((listitem == target) && (tchan == thischan))
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->SetFd(FD_MAGIC_NUMBER);
179                                                 /* FIX: Send mode remotely*/
180                                                 std::deque<std::string> n;
181                                                 n.push_back(setban[0]);
182                                                 n.push_back("-b");
183                                                 n.push_back(setban[2]);
184                                                 ServerInstance->SendMode(setban,3,temp);
185                                                 Event rmode((char *)&n, NULL, "send_mode");
186                                                 rmode.Send(ServerInstance);
187                                                 DELETE(temp);
188                                         }
189                                         else
190                                         {
191                                                 /* Where the hell did our channel go?! */
192                                                 TimedBanList.erase(i);
193                                         }
194                                         // we used to delete the item here, but we dont need to as the servermode above does it for us,
195                                         break;
196                                 }
197                         }
198                 }
199         }
200         
201         virtual Version GetVersion()
202         {
203                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
204         }
205 };
206
207
208 class ModuleTimedBansFactory : public ModuleFactory
209 {
210  public:
211         ModuleTimedBansFactory()
212         {
213         }
214         
215         ~ModuleTimedBansFactory()
216         {
217         }
218         
219         virtual Module * CreateModule(InspIRCd* Me)
220         {
221                 return new ModuleTimedBans(Me);
222         }
223         
224 };
225
226
227 extern "C" DllExport void * init_module( void )
228 {
229         return new ModuleTimedBansFactory;
230 }