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