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