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