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