]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
Make classbase and refcountbase uncopyable; expand comments on their indended uses
[user/henk/code/inspircd.git] / src / modules / m_timedbans.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $ModDesc: Adds timed bans */
15
16 #include "inspircd.h"
17
18 /** Holds a timed ban
19  */
20 class TimedBan
21 {
22  public:
23         std::string channel;
24         std::string mask;
25         time_t expire;
26 };
27
28 typedef std::vector<TimedBan> timedbans;
29 timedbans TimedBanList;
30
31 /** Handle /TBAN
32  */
33 class CommandTban : public Command
34 {
35  public:
36         CommandTban(Module* Creator) : Command(Creator,"TBAN", 3)
37         {
38                 syntax = "<channel> <duration> <banmask>";
39                 TRANSLATE4(TR_TEXT, TR_TEXT, TR_TEXT, TR_END);
40         }
41
42         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
43         {
44                 Channel* channel = ServerInstance->FindChan(parameters[0]);
45                 if (channel)
46                 {
47                         int cm = channel->GetPrefixValue(user);
48                         if ((cm == HALFOP_VALUE) || (cm == OP_VALUE))
49                         {
50                                 if (!ServerInstance->IsValidMask(parameters[2]))
51                                 {
52                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban mask");
53                                         return CMD_FAILURE;
54                                 }
55                                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
56                                 {
57                                         if (!strcasecmp(i->data.c_str(), parameters[2].c_str()))
58                                         {
59                                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :The ban "+parameters[2]+" is already on the banlist of "+parameters[0]);
60                                                 return CMD_FAILURE;
61                                         }
62                                 }
63                                 TimedBan T;
64                                 std::string channelname = parameters[0];
65                                 long duration = ServerInstance->Duration(parameters[1]);
66                                 unsigned long expire = duration + ServerInstance->Time();
67                                 if (duration < 1)
68                                 {
69                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban time");
70                                         return CMD_FAILURE;
71                                 }
72                                 std::string mask = parameters[2];
73                                 std::vector<std::string> setban;
74                                 setban.push_back(parameters[0]);
75                                 setban.push_back("+b");
76                                 setban.push_back(parameters[2]);
77                                 // use CallCommandHandler to make it so that the user sets the mode
78                                 // themselves
79                                 ServerInstance->CallCommandHandler("MODE",setban,user);
80                                 /* Check if the ban was actually added (e.g. banlist was NOT full) */
81                                 bool was_added = false;
82                                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
83                                         if (!strcasecmp(i->data.c_str(), mask.c_str()))
84                                                 was_added = true;
85                                 if (was_added)
86                                 {
87                                         CUList tmp;
88                                         T.channel = channelname;
89                                         T.mask = mask;
90                                         T.expire = expire;
91                                         TimedBanList.push_back(T);
92                                         channel->WriteAllExcept(user, true, '@', 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);
93                                         ServerInstance->PI->SendChannelNotice(channel, '@', user->nick + " added a timed ban on " + mask + " lasting for " + ConvToStr(duration) + " seconds.");
94                                         if (ServerInstance->Config->AllowHalfop)
95                                         {
96                                                 channel->WriteAllExcept(user, true, '%', 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);
97                                                 ServerInstance->PI->SendChannelNotice(channel, '%', user->nick + " added a timed ban on " + mask + " lasting for " + ConvToStr(duration) + " seconds.");
98                                         }
99                                         return CMD_SUCCESS;
100                                 }
101                                 return CMD_FAILURE;
102                         }
103                         else user->WriteNumeric(482, "%s %s :You must be at least a%soperator to change modes on this channel",user->nick.c_str(), channel->name.c_str(),
104                                         ServerInstance->Config->AllowHalfop ? " half-" : " channel ");
105                         return CMD_FAILURE;
106                 }
107                 user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
108                 return CMD_FAILURE;
109         }
110
111         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
112         {
113                 return ROUTE_BROADCAST;
114         }
115 };
116
117 class ModuleTimedBans : public Module
118 {
119         CommandTban cmd;
120  public:
121         ModuleTimedBans()
122                 : cmd(this)
123         {
124                 ServerInstance->AddCommand(&cmd);
125                 TimedBanList.clear();
126                 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
127                 ServerInstance->Modules->Attach(eventlist, this, 2);
128         }
129
130         virtual ~ModuleTimedBans()
131         {
132                 TimedBanList.clear();
133         }
134
135         virtual ModResult OnDelBan(User* source, Channel* chan, const std::string &banmask)
136         {
137                 irc::string listitem = banmask.c_str();
138                 irc::string thischan = chan->name.c_str();
139                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); i++)
140                 {
141                         irc::string target = i->mask.c_str();
142                         irc::string tchan = i->channel.c_str();
143                         if ((listitem == target) && (tchan == thischan))
144                         {
145                                 TimedBanList.erase(i);
146                                 break;
147                         }
148                 }
149                 return MOD_RES_PASSTHRU;
150         }
151
152         virtual void OnBackgroundTimer(time_t curtime)
153         {
154                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
155                 {
156                         if (curtime > i->expire)
157                         {
158                                 std::string chan = i->channel;
159                                 std::string mask = i->mask;
160                                 Channel* cr = ServerInstance->FindChan(chan);
161                                 i = TimedBanList.erase(i);
162                                 if (cr)
163                                 {
164                                         std::vector<std::string> setban;
165                                         setban.push_back(chan);
166                                         setban.push_back("-b");
167                                         setban.push_back(mask);
168
169                                         CUList empty;
170                                         std::string expiry = "*** Timed ban on " + chan + " expired.";
171                                         cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
172                                         ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
173                                         if (ServerInstance->Config->AllowHalfop)
174                                         {
175                                                 cr->WriteAllExcept(ServerInstance->FakeClient, true, '%', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
176                                                 ServerInstance->PI->SendChannelNotice(cr, '%', expiry);
177                                         }
178
179                                         ServerInstance->SendMode(setban, ServerInstance->FakeClient);
180                                         ServerInstance->PI->SendMode(chan, ServerInstance->Modes->GetLastParseParams(), ServerInstance->Modes->GetLastParseTranslate());
181                                 }
182                         }
183                         else
184                                 ++i;
185                 }
186         }
187
188         virtual Version GetVersion()
189         {
190                 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
191         }
192 };
193
194 MODULE_INIT(ModuleTimedBans)
195