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