]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
Add Module* creator to Command and ModeHandler
[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 : public classbase
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 (InspIRCd* Instance, Module* Creator) : Command(Instance, Creator,"TBAN", 0, 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->GetStatus(user);
48                         if ((cm == STATUS_HOP) || (cm == STATUS_OP))
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
112 class ModuleTimedBans : public Module
113 {
114         CommandTban cmd;
115  public:
116         ModuleTimedBans(InspIRCd* Me)
117                 : Module(Me), cmd(Me, this)
118         {
119                 ServerInstance->AddCommand(&cmd);
120                 TimedBanList.clear();
121                 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
122                 ServerInstance->Modules->Attach(eventlist, this, 2);
123         }
124
125         virtual ~ModuleTimedBans()
126         {
127                 TimedBanList.clear();
128         }
129
130         virtual int OnDelBan(User* source, Channel* chan, const std::string &banmask)
131         {
132                 irc::string listitem = banmask.c_str();
133                 irc::string thischan = chan->name.c_str();
134                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); i++)
135                 {
136                         irc::string target = i->mask.c_str();
137                         irc::string tchan = i->channel.c_str();
138                         if ((listitem == target) && (tchan == thischan))
139                         {
140                                 TimedBanList.erase(i);
141                                 break;
142                         }
143                 }
144                 return 0;
145         }
146
147         virtual void OnBackgroundTimer(time_t curtime)
148         {
149                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
150                 {
151                         if (curtime > i->expire)
152                         {
153                                 std::string chan = i->channel;
154                                 std::string mask = i->mask;
155                                 Channel* cr = ServerInstance->FindChan(chan);
156                                 i = TimedBanList.erase(i);
157                                 if (cr)
158                                 {
159                                         std::vector<std::string> setban;
160                                         setban.push_back(chan);
161                                         setban.push_back("-b");
162                                         setban.push_back(mask);
163
164                                         CUList empty;
165                                         std::string expiry = "*** Timed ban on " + chan + " expired.";
166                                         cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
167                                         ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
168                                         if (ServerInstance->Config->AllowHalfop)
169                                         {
170                                                 cr->WriteAllExcept(ServerInstance->FakeClient, true, '%', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
171                                                 ServerInstance->PI->SendChannelNotice(cr, '%', expiry);
172                                         }
173
174                                         ServerInstance->SendMode(setban, ServerInstance->FakeClient);
175                                         ServerInstance->PI->SendMode(chan, ServerInstance->Modes->GetLastParseParams(), ServerInstance->Modes->GetLastParseTranslate());
176                                 }
177                         }
178                         else
179                                 ++i;
180                 }
181         }
182
183         virtual Version GetVersion()
184         {
185                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
186         }
187 };
188
189 MODULE_INIT(ModuleTimedBans)
190