]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
663a43f2fec560958eb6dea3180b0136ecc0894f
[user/henk/code/inspircd.git] / src / modules / m_timedbans.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2005-2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2006 Robin Burchell <robin+git@viroteck.net>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 /* $ModDesc: Adds timed bans */
24
25 #include "inspircd.h"
26
27 /** Holds a timed ban
28  */
29 class TimedBan
30 {
31  public:
32         std::string channel;
33         std::string mask;
34         time_t expire;
35 };
36
37 typedef std::vector<TimedBan> timedbans;
38 timedbans TimedBanList;
39
40 /** Handle /TBAN
41  */
42 class CommandTban : public Command
43 {
44  public:
45         CommandTban(Module* Creator) : Command(Creator,"TBAN", 3)
46         {
47                 syntax = "<channel> <duration> <banmask>";
48                 TRANSLATE4(TR_TEXT, TR_TEXT, TR_TEXT, TR_END);
49         }
50
51         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
52         {
53                 Channel* channel = ServerInstance->FindChan(parameters[0]);
54                 if (!channel)
55                 {
56                         user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
57                         return CMD_FAILURE;
58                 }
59                 int cm = channel->GetPrefixValue(user);
60                 if (cm < HALFOP_VALUE)
61                 {
62                         user->WriteNumeric(482, "%s %s :You do not have permission to set bans on this channel",
63                                 user->nick.c_str(), channel->name.c_str());
64                         return CMD_FAILURE;
65                 }
66                 if (!ServerInstance->IsValidMask(parameters[2]))
67                 {
68                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban mask");
69                         return CMD_FAILURE;
70                 }
71                 TimedBan T;
72                 std::string channelname = parameters[0];
73                 long duration = ServerInstance->Duration(parameters[1]);
74                 unsigned long expire = duration + ServerInstance->Time();
75                 if (duration < 1)
76                 {
77                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban time");
78                         return CMD_FAILURE;
79                 }
80                 std::string mask = parameters[2];
81                 std::vector<std::string> setban;
82                 setban.push_back(parameters[0]);
83                 setban.push_back("+b");
84                 setban.push_back(parameters[2]);
85                 // use CallCommandHandler to make it so that the user sets the mode
86                 // themselves
87                 ServerInstance->CallCommandHandler("MODE",setban,user);
88                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
89                         if (!strcasecmp(i->data.c_str(), mask.c_str()))
90                                 goto found;
91                 return CMD_FAILURE;
92 found:
93                 CUList tmp;
94                 T.channel = channelname;
95                 T.mask = mask;
96                 T.expire = expire + (IS_REMOTE(user) ? 5 : 0);
97                 TimedBanList.push_back(T);
98                 channel->WriteAllExcept(ServerInstance->FakeClient, true, '@', tmp, "NOTICE %s :%s added a timed ban on %s lasting for %ld seconds.", channel->name.c_str(), user->nick.c_str(), mask.c_str(), duration);
99                 return CMD_SUCCESS;
100         }
101
102         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
103         {
104                 return ROUTE_BROADCAST;
105         }
106 };
107
108 class ModuleTimedBans : public Module
109 {
110         CommandTban cmd;
111  public:
112         ModuleTimedBans()
113                 : cmd(this)
114         {
115                 ServerInstance->AddCommand(&cmd);
116                 TimedBanList.clear();
117                 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
118                 ServerInstance->Modules->Attach(eventlist, this, 2);
119         }
120
121         virtual ~ModuleTimedBans()
122         {
123                 TimedBanList.clear();
124         }
125
126         virtual ModResult OnDelBan(User* source, Channel* chan, const std::string &banmask)
127         {
128                 irc::string listitem = banmask.c_str();
129                 irc::string thischan = chan->name.c_str();
130                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); i++)
131                 {
132                         irc::string target = i->mask.c_str();
133                         irc::string tchan = i->channel.c_str();
134                         if ((listitem == target) && (tchan == thischan))
135                         {
136                                 TimedBanList.erase(i);
137                                 break;
138                         }
139                 }
140                 return MOD_RES_PASSTHRU;
141         }
142
143         virtual void OnBackgroundTimer(time_t curtime)
144         {
145                 timedbans expired;
146                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
147                 {
148                         if (curtime > i->expire)
149                         {
150                                 expired.push_back(*i);
151                                 i = TimedBanList.erase(i);
152                         }
153                         else
154                                 ++i;
155                 }
156
157                 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
158                 {
159                         std::string chan = i->channel;
160                         std::string mask = i->mask;
161                         Channel* cr = ServerInstance->FindChan(chan);
162                         if (cr)
163                         {
164                                 std::vector<std::string> setban;
165                                 setban.push_back(chan);
166                                 setban.push_back("-b");
167                                 setban.push_back(mask);
168
169                                 CUList empty;
170                                 std::string expiry = "*** Timed ban on " + chan + " expired.";
171                                 cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
172                                 ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
173
174                                 ServerInstance->SendGlobalMode(setban, ServerInstance->FakeClient);
175                         }
176                 }
177         }
178
179         virtual Version GetVersion()
180         {
181                 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
182         }
183 };
184
185 MODULE_INIT(ModuleTimedBans)
186