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