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