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