]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
Merge tag 'v2.0.25' into master.
[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 #include "listmode.h"
25
26 /** Holds a timed ban
27  */
28 class TimedBan
29 {
30  public:
31         std::string mask;
32         time_t expire;
33         Channel* chan;
34 };
35
36 typedef std::vector<TimedBan> timedbans;
37 timedbans TimedBanList;
38
39 /** Handle /TBAN
40  */
41 class CommandTban : public Command
42 {
43         ChanModeReference banmode;
44
45         bool IsBanSet(Channel* chan, const std::string& mask)
46         {
47                 ListModeBase* banlm = static_cast<ListModeBase*>(*banmode);
48                 const ListModeBase::ModeList* bans = banlm->GetList(chan);
49                 if (bans)
50                 {
51                         for (ListModeBase::ModeList::const_iterator i = bans->begin(); i != bans->end(); ++i)
52                         {
53                                 const ListModeBase::ListItem& ban = *i;
54                                 if (!strcasecmp(ban.mask.c_str(), mask.c_str()))
55                                         return true;
56                         }
57                 }
58
59                 return false;
60         }
61
62  public:
63         CommandTban(Module* Creator) : Command(Creator,"TBAN", 3)
64                 , banmode(Creator, "ban")
65         {
66                 syntax = "<channel> <duration> <banmask>";
67         }
68
69         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
70         {
71                 Channel* channel = ServerInstance->FindChan(parameters[0]);
72                 if (!channel)
73                 {
74                         user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
75                         return CMD_FAILURE;
76                 }
77                 int cm = channel->GetPrefixValue(user);
78                 if (cm < HALFOP_VALUE)
79                 {
80                         user->WriteNumeric(ERR_CHANOPRIVSNEEDED, channel->name, "You do not have permission to set bans on this channel");
81                         return CMD_FAILURE;
82                 }
83
84                 TimedBan T;
85                 unsigned long duration = InspIRCd::Duration(parameters[1]);
86                 unsigned long expire = duration + ServerInstance->Time();
87                 if (duration < 1)
88                 {
89                         user->WriteNotice("Invalid ban time");
90                         return CMD_FAILURE;
91                 }
92                 std::string mask = parameters[2];
93                 bool isextban = ((mask.size() > 2) && (mask[1] == ':'));
94                 if (!isextban && !InspIRCd::IsValidMask(mask))
95                         mask.append("!*@*");
96
97                 if (IsBanSet(channel, mask))
98                 {
99                         user->WriteNotice("Ban already set");
100                         return CMD_FAILURE;
101                 }
102
103                 Modes::ChangeList setban;
104                 setban.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
105                 // Pass the user (instead of ServerInstance->FakeClient) to ModeHandler::Process() to
106                 // make it so that the user sets the mode themselves
107                 ServerInstance->Modes->Process(user, channel, NULL, setban);
108                 if (ServerInstance->Modes->GetLastParse().empty())
109                 {
110                         user->WriteNotice("Invalid ban mask");
111                         return CMD_FAILURE;
112                 }
113
114                 CUList tmp;
115                 T.mask = mask;
116                 T.expire = expire + (IS_REMOTE(user) ? 5 : 0);
117                 T.chan = channel;
118                 TimedBanList.push_back(T);
119
120                 const std::string addban = user->nick + " added a timed ban on " + mask + " lasting for " + ConvToStr(duration) + " seconds.";
121                 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
122                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
123                 char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
124
125                 channel->WriteAllExcept(ServerInstance->FakeClient, true, pfxchar, tmp, "NOTICE %s :%s", channel->name.c_str(), addban.c_str());
126                 ServerInstance->PI->SendChannelNotice(channel, pfxchar, addban);
127                 return CMD_SUCCESS;
128         }
129
130         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
131         {
132                 return ROUTE_BROADCAST;
133         }
134 };
135
136 class BanWatcher : public ModeWatcher
137 {
138  public:
139         BanWatcher(Module* parent)
140                 : ModeWatcher(parent, "ban", MODETYPE_CHANNEL)
141         {
142         }
143
144         void AfterMode(User* source, User* dest, Channel* chan, const std::string& banmask, bool adding)
145         {
146                 if (adding)
147                         return;
148
149                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); ++i)
150                 {
151                         if (i->chan != chan)
152                                 continue;
153
154                         const std::string& target = i->mask;
155                         if (irc::equals(banmask, target))
156                         {
157                                 TimedBanList.erase(i);
158                                 break;
159                         }
160                 }
161         }
162 };
163
164 class ChannelMatcher
165 {
166         Channel* const chan;
167
168  public:
169         ChannelMatcher(Channel* ch)
170                 : chan(ch)
171         {
172         }
173
174         bool operator()(const TimedBan& tb) const
175         {
176                 return (tb.chan == chan);
177         }
178 };
179
180 class ModuleTimedBans : public Module
181 {
182         CommandTban cmd;
183         BanWatcher banwatcher;
184
185  public:
186         ModuleTimedBans()
187                 : cmd(this)
188                 , banwatcher(this)
189         {
190         }
191
192         void OnBackgroundTimer(time_t curtime) CXX11_OVERRIDE
193         {
194                 timedbans expired;
195                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
196                 {
197                         if (curtime > i->expire)
198                         {
199                                 expired.push_back(*i);
200                                 i = TimedBanList.erase(i);
201                         }
202                         else
203                                 ++i;
204                 }
205
206                 for (timedbans::iterator i = expired.begin(); i != expired.end(); i++)
207                 {
208                         std::string mask = i->mask;
209                         Channel* cr = i->chan;
210                         {
211                                 CUList empty;
212                                 const std::string expiry = "*** Timed ban on " + cr->name + " expired.";
213                                 // If halfop is loaded, send notice to halfops and above, otherwise send to ops and above
214                                 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode('h');
215                                 char pfxchar = (mh && mh->name == "halfop") ? mh->GetPrefix() : '@';
216
217                                 cr->WriteAllExcept(ServerInstance->FakeClient, true, pfxchar, empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
218                                 ServerInstance->PI->SendChannelNotice(cr, pfxchar, expiry);
219
220                                 Modes::ChangeList setban;
221                                 setban.push_remove(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), mask);
222                                 ServerInstance->Modes->Process(ServerInstance->FakeClient, cr, NULL, setban);
223                         }
224                 }
225         }
226
227         void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
228         {
229                 // Remove all timed bans affecting the channel from internal bookkeeping
230                 TimedBanList.erase(std::remove_if(TimedBanList.begin(), TimedBanList.end(), ChannelMatcher(chan)), TimedBanList.end());
231         }
232
233         Version GetVersion() CXX11_OVERRIDE
234         {
235                 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
236         }
237 };
238
239 MODULE_INIT(ModuleTimedBans)