]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
3602469bd61a052170c27ef456699d1b2c89089b
[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         }
49
50         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
51         {
52                 Channel* channel = ServerInstance->FindChan(parameters[0]);
53                 if (!channel)
54                 {
55                         user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
56                         return CMD_FAILURE;
57                 }
58                 int cm = channel->GetPrefixValue(user);
59                 if (cm < HALFOP_VALUE)
60                 {
61                         user->WriteNumeric(482, "%s %s :You do not have permission to set bans on this channel",
62                                 user->nick.c_str(), channel->name.c_str());
63                         return CMD_FAILURE;
64                 }
65
66                 TimedBan T;
67                 std::string channelname = parameters[0];
68                 unsigned long duration = InspIRCd::Duration(parameters[1]);
69                 unsigned long expire = duration + ServerInstance->Time();
70                 if (duration < 1)
71                 {
72                         user->WriteNotice("Invalid ban time");
73                         return CMD_FAILURE;
74                 }
75                 std::string mask = parameters[2];
76                 std::vector<std::string> setban;
77                 setban.push_back(parameters[0]);
78                 setban.push_back("+b");
79                 bool isextban = ((mask.size() > 2) && (mask[1] == ':'));
80                 if (!isextban && !ServerInstance->IsValidMask(mask))
81                         mask.append("!*@*");
82
83                 setban.push_back(mask);
84                 // use CallHandler to make it so that the user sets the mode
85                 // themselves
86                 ServerInstance->Parser->CallHandler("MODE",setban,user);
87                 if (ServerInstance->Modes->GetLastParse().empty())
88                 {
89                         user->WriteNotice("Invalid ban mask");
90                         return CMD_FAILURE;
91                 }
92
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
99                 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
100                 ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
101                 char pfxchar = (mh && mh->name == "halfop") ? '%' : '@';
102
103                 channel->WriteAllExcept(ServerInstance->FakeClient, true, pfxchar, 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);
104                 return CMD_SUCCESS;
105         }
106
107         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
108         {
109                 return ROUTE_BROADCAST;
110         }
111 };
112
113 class BanWatcher : public ModeWatcher
114 {
115  public:
116         BanWatcher(Module* parent)
117                 : ModeWatcher(parent, "ban", MODETYPE_CHANNEL)
118         {
119         }
120
121         void AfterMode(User* source, User* dest, Channel* chan, const std::string& banmask, bool adding)
122         {
123                 if (adding)
124                         return;
125
126                 irc::string listitem = banmask.c_str();
127                 irc::string thischan = chan->name.c_str();
128                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); ++i)
129                 {
130                         irc::string target = i->mask.c_str();
131                         irc::string tchan = i->channel.c_str();
132                         if ((listitem == target) && (tchan == thischan))
133                         {
134                                 TimedBanList.erase(i);
135                                 break;
136                         }
137                 }
138         }
139 };
140
141 class ModuleTimedBans : public Module
142 {
143         CommandTban cmd;
144         BanWatcher banwatcher;
145
146  public:
147         ModuleTimedBans()
148                 : cmd(this)
149                 , banwatcher(this)
150         {
151         }
152
153         void init() CXX11_OVERRIDE
154         {
155                 ServerInstance->Modules->AddService(cmd);
156                 Implementation eventlist[] = { I_OnBackgroundTimer };
157                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
158                 ServerInstance->Modes->AddModeWatcher(&banwatcher);
159         }
160
161         ~ModuleTimedBans()
162         {
163                 ServerInstance->Modes->DelModeWatcher(&banwatcher);
164         }
165
166         void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
167         {
168                 timedbans expired;
169                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
170                 {
171                         if (curtime > i->expire)
172                         {
173                                 expired.push_back(*i);
174                                 i = TimedBanList.erase(i);
175                         }
176                         else
177                                 ++i;
178                 }
179
180                 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
181                 {
182                         std::string chan = i->channel;
183                         std::string mask = i->mask;
184                         Channel* cr = ServerInstance->FindChan(chan);
185                         if (cr)
186                         {
187                                 std::vector<std::string> setban;
188                                 setban.push_back(chan);
189                                 setban.push_back("-b");
190                                 setban.push_back(mask);
191
192                                 CUList empty;
193                                 std::string expiry = "*** Timed ban on " + chan + " expired.";
194                                 cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
195                                 ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
196
197                                 ServerInstance->Modes->Process(setban, ServerInstance->FakeClient);
198                         }
199                 }
200         }
201
202         Version GetVersion() CXX11_OVERRIDE
203         {
204                 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
205         }
206 };
207
208 MODULE_INIT(ModuleTimedBans)