]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
Replace std::deque with std::vector in spanningtree and related modules
[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) : Command(Instance,"TBAN", 0, 3)
37         {
38                 this->source = "m_timedbans.so";
39                 syntax = "<channel> <duration> <banmask>";
40                 TRANSLATE4(TR_TEXT, TR_TEXT, TR_TEXT, TR_END);
41         }
42
43         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
44         {
45                 Channel* channel = ServerInstance->FindChan(parameters[0]);
46                 if (channel)
47                 {
48                         int cm = channel->GetStatus(user);
49                         if ((cm == STATUS_HOP) || (cm == STATUS_OP))
50                         {
51                                 if (!ServerInstance->IsValidMask(parameters[2]))
52                                 {
53                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban mask");
54                                         return CMD_FAILURE;
55                                 }
56                                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
57                                 {
58                                         if (!strcasecmp(i->data.c_str(), parameters[2].c_str()))
59                                         {
60                                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :The ban "+parameters[2]+" is already on the banlist of "+parameters[0]);
61                                                 return CMD_FAILURE;
62                                         }
63                                 }
64                                 TimedBan T;
65                                 std::string channelname = parameters[0];
66                                 long duration = ServerInstance->Duration(parameters[1]);
67                                 unsigned long expire = duration + ServerInstance->Time();
68                                 if (duration < 1)
69                                 {
70                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban time");
71                                         return CMD_FAILURE;
72                                 }
73                                 std::string mask = parameters[2];
74                                 std::vector<std::string> setban;
75                                 setban.push_back(parameters[0]);
76                                 setban.push_back("+b");
77                                 setban.push_back(parameters[2]);
78                                 // use CallCommandHandler to make it so that the user sets the mode
79                                 // themselves
80                                 ServerInstance->CallCommandHandler("MODE",setban,user);
81                                 /* Check if the ban was actually added (e.g. banlist was NOT full) */
82                                 bool was_added = false;
83                                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
84                                         if (!strcasecmp(i->data.c_str(), mask.c_str()))
85                                                 was_added = true;
86                                 if (was_added)
87                                 {
88                                         CUList tmp;
89                                         T.channel = channelname;
90                                         T.mask = mask;
91                                         T.expire = expire;
92                                         TimedBanList.push_back(T);
93                                         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);
94                                         ServerInstance->PI->SendChannelNotice(channel, '@', user->nick + " added a timed ban on " + mask + " lasting for " + ConvToStr(duration) + " seconds.");
95                                         if (ServerInstance->Config->AllowHalfop)
96                                         {
97                                                 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);
98                                                 ServerInstance->PI->SendChannelNotice(channel, '%', user->nick + " added a timed ban on " + mask + " lasting for " + ConvToStr(duration) + " seconds.");
99                                         }
100                                         return CMD_SUCCESS;
101                                 }
102                                 return CMD_FAILURE;
103                         }
104                         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(),
105                                         ServerInstance->Config->AllowHalfop ? " half-" : " channel ");
106                         return CMD_FAILURE;
107                 }
108                 user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
109                 return CMD_FAILURE;
110         }
111 };
112
113 class ModuleTimedBans : public Module
114 {
115         CommandTban cmd;
116  public:
117         ModuleTimedBans(InspIRCd* Me)
118                 : Module(Me), cmd(Me)
119         {
120                 ServerInstance->AddCommand(&cmd);
121                 TimedBanList.clear();
122                 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
123                 ServerInstance->Modules->Attach(eventlist, this, 2);
124         }
125
126         virtual ~ModuleTimedBans()
127         {
128                 TimedBanList.clear();
129         }
130
131         virtual int OnDelBan(User* source, Channel* chan, const std::string &banmask)
132         {
133                 irc::string listitem = banmask.c_str();
134                 irc::string thischan = chan->name.c_str();
135                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); i++)
136                 {
137                         irc::string target = i->mask.c_str();
138                         irc::string tchan = i->channel.c_str();
139                         if ((listitem == target) && (tchan == thischan))
140                         {
141                                 TimedBanList.erase(i);
142                                 break;
143                         }
144                 }
145                 return 0;
146         }
147
148         virtual void OnBackgroundTimer(time_t curtime)
149         {
150                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
151                 {
152                         if (curtime > i->expire)
153                         {
154                                 std::string chan = i->channel;
155                                 std::string mask = i->mask;
156                                 Channel* cr = ServerInstance->FindChan(chan);
157                                 i = TimedBanList.erase(i);
158                                 if (cr)
159                                 {
160                                         std::vector<std::string> setban;
161                                         setban.push_back(chan);
162                                         setban.push_back("-b");
163                                         setban.push_back(mask);
164
165                                         CUList empty;
166                                         std::string expiry = "*** Timed ban on " + chan + " expired.";
167                                         cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
168                                         ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
169                                         if (ServerInstance->Config->AllowHalfop)
170                                         {
171                                                 cr->WriteAllExcept(ServerInstance->FakeClient, true, '%', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
172                                                 ServerInstance->PI->SendChannelNotice(cr, '%', expiry);
173                                         }
174
175                                         ServerInstance->SendMode(setban, ServerInstance->FakeClient);
176                                         ServerInstance->PI->SendMode(chan, ServerInstance->Modes->GetLastParseParams(), ServerInstance->Modes->GetLastParseTranslate());
177                                 }
178                         }
179                         else
180                                 ++i;
181                 }
182         }
183
184         virtual Version GetVersion()
185         {
186                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
187         }
188 };
189
190 MODULE_INIT(ModuleTimedBans)
191