]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
Update copyright headers.
[user/henk/code/inspircd.git] / src / modules / m_timedbans.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
5  *   Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
6  *   Copyright (C) 2016, 2019 Matt Schatz <genius3000@g3k.solutions>
7  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
8  *   Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
9  *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
10  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
11  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
12  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
13  *   Copyright (C) 2009 John Brooks <special@inspircd.org>
14  *   Copyright (C) 2007-2008, 2010 Craig Edwards <brain@inspircd.org>
15  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
16  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
17  *
18  * This file is part of InspIRCd.  InspIRCd is free software: you can
19  * redistribute it and/or modify it under the terms of the GNU General Public
20  * License as published by the Free Software Foundation, version 2.
21  *
22  * This program is distributed in the hope that it will be useful, but WITHOUT
23  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
25  * details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  */
30
31
32 #include "inspircd.h"
33 #include "listmode.h"
34
35 // Holds a timed ban
36 class TimedBan
37 {
38  public:
39         std::string mask;
40         std::string setter;
41         time_t expire;
42         Channel* chan;
43 };
44
45 typedef std::vector<TimedBan> timedbans;
46 timedbans TimedBanList;
47
48 // Handle /TBAN
49 class CommandTban : public Command
50 {
51         ChanModeReference banmode;
52
53         bool IsBanSet(Channel* chan, const std::string& mask)
54         {
55                 ListModeBase* banlm = static_cast<ListModeBase*>(*banmode);
56                 if (!banlm)
57                         return false;
58
59                 const ListModeBase::ModeList* bans = banlm->GetList(chan);
60                 if (bans)
61                 {
62                         for (ListModeBase::ModeList::const_iterator i = bans->begin(); i != bans->end(); ++i)
63                         {
64                                 const ListModeBase::ListItem& ban = *i;
65                                 if (!strcasecmp(ban.mask.c_str(), mask.c_str()))
66                                         return true;
67                         }
68                 }
69
70                 return false;
71         }
72
73  public:
74         CommandTban(Module* Creator)
75                 : Command(Creator,"TBAN", 3)
76                 , banmode(Creator, "ban")
77         {
78                 syntax = "<channel> <duration> <banmask>";
79         }
80
81         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
82         {
83                 Channel* channel = ServerInstance->FindChan(parameters[0]);
84                 if (!channel)
85                 {
86                         user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
87                         return CMD_FAILURE;
88                 }
89
90                 unsigned int cm = channel->GetPrefixValue(user);
91                 if (cm < HALFOP_VALUE)
92                 {
93                         user->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, "You do not have permission to set bans on this channel");
94                         return CMD_FAILURE;
95                 }
96
97                 TimedBan T;
98                 unsigned long duration;
99                 if (!InspIRCd::Duration(parameters[1], duration))
100                 {
101                         user->WriteNotice("Invalid ban time");
102                         return CMD_FAILURE;
103                 }
104                 unsigned long expire = duration + ServerInstance->Time();
105
106                 std::string mask = parameters[2];
107                 bool isextban = ((mask.size() > 2) && (mask[1] == ':'));
108                 if (!isextban && !InspIRCd::IsValidMask(mask))
109                         mask.append("!*@*");
110
111                 if (IsBanSet(channel, mask))
112                 {
113                         user->WriteNotice("Ban already set");
114                         return CMD_FAILURE;
115                 }
116
117                 Modes::ChangeList setban;
118                 setban.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
119                 // Pass the user (instead of ServerInstance->FakeClient) to ModeHandler::Process() to
120                 // make it so that the user sets the mode themselves
121                 ServerInstance->Modes->Process(user, channel, NULL, setban);
122                 if (ServerInstance->Modes->GetLastChangeList().empty())
123                 {
124                         user->WriteNotice("Invalid ban mask");
125                         return CMD_FAILURE;
126                 }
127
128                 T.mask = mask;
129                 T.setter = user->nick;
130                 T.expire = expire + (IS_REMOTE(user) ? 5 : 0);
131                 T.chan = channel;
132                 TimedBanList.push_back(T);
133
134                 const std::string message = InspIRCd::Format("Timed ban %s added by %s on %s lasting for %s.",
135                         mask.c_str(), user->nick.c_str(), channel->name.c_str(), InspIRCd::DurationString(duration).c_str());
136                 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
137                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
138                 char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
139
140                 channel->WriteNotice(message, pfxchar);
141                 return CMD_SUCCESS;
142         }
143
144         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
145         {
146                 return ROUTE_BROADCAST;
147         }
148 };
149
150 class BanWatcher : public ModeWatcher
151 {
152  public:
153         BanWatcher(Module* parent)
154                 : ModeWatcher(parent, "ban", MODETYPE_CHANNEL)
155         {
156         }
157
158         void AfterMode(User* source, User* dest, Channel* chan, const std::string& banmask, bool adding) CXX11_OVERRIDE
159         {
160                 if (adding)
161                         return;
162
163                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); ++i)
164                 {
165                         if (i->chan != chan)
166                                 continue;
167
168                         const std::string& target = i->mask;
169                         if (irc::equals(banmask, target))
170                         {
171                                 TimedBanList.erase(i);
172                                 break;
173                         }
174                 }
175         }
176 };
177
178 class ChannelMatcher
179 {
180         Channel* const chan;
181
182  public:
183         ChannelMatcher(Channel* ch)
184                 : chan(ch)
185         {
186         }
187
188         bool operator()(const TimedBan& tb) const
189         {
190                 return (tb.chan == chan);
191         }
192 };
193
194 class ModuleTimedBans : public Module
195 {
196         CommandTban cmd;
197         BanWatcher banwatcher;
198
199  public:
200         ModuleTimedBans()
201                 : cmd(this)
202                 , banwatcher(this)
203         {
204         }
205
206         void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
207         {
208                 timedbans expired;
209                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
210                 {
211                         if (curtime > i->expire)
212                         {
213                                 expired.push_back(*i);
214                                 i = TimedBanList.erase(i);
215                         }
216                         else
217                                 ++i;
218                 }
219
220                 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
221                 {
222                         const std::string mask = i->mask;
223                         Channel* cr = i->chan;
224
225                         const std::string message = InspIRCd::Format("Timed ban %s set by %s on %s has expired.",
226                                 mask.c_str(), i->setter.c_str(), cr->name.c_str());
227                         // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
228                         PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
229                         char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
230
231                         cr->WriteNotice(message, pfxchar);
232
233                         Modes::ChangeList setban;
234                         setban.push_remove(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
235                         ServerInstance->Modes->Process(ServerInstance->FakeClient, cr, NULL, setban);
236                 }
237         }
238
239         void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
240         {
241                 // Remove all timed bans affecting the channel from internal bookkeeping
242                 TimedBanList.erase(std::remove_if(TimedBanList.begin(), TimedBanList.end(), ChannelMatcher(chan)), TimedBanList.end());
243         }
244
245         Version GetVersion() CXX11_OVERRIDE
246         {
247                 return Version("Provides the TBAN command, timed channel bans", VF_COMMON | VF_VENDOR);
248         }
249 };
250
251 MODULE_INIT(ModuleTimedBans)