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