]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
Extbans can be VF_OPTCOMMON as they do not desync on module add/remove
[user/henk/code/inspircd.git] / src / modules / m_timedbans.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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                                         return CMD_SUCCESS;
95                                 }
96                                 return CMD_FAILURE;
97                         }
98                         else
99                         {
100                                 user->WriteNumeric(482, "%s %s :You do not have permission to change modes on this channel",
101                                         user->nick.c_str(), channel->name.c_str());
102                         }
103                         return CMD_FAILURE;
104                 }
105                 user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
106                 return CMD_FAILURE;
107         }
108
109         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
110         {
111                 return ROUTE_BROADCAST;
112         }
113 };
114
115 class ModuleTimedBans : public Module
116 {
117         CommandTban cmd;
118  public:
119         ModuleTimedBans()
120                 : cmd(this)
121         {
122                 ServerInstance->AddCommand(&cmd);
123                 TimedBanList.clear();
124                 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
125                 ServerInstance->Modules->Attach(eventlist, this, 2);
126         }
127
128         virtual ~ModuleTimedBans()
129         {
130                 TimedBanList.clear();
131         }
132
133         virtual ModResult OnDelBan(User* source, Channel* chan, const std::string &banmask)
134         {
135                 irc::string listitem = banmask.c_str();
136                 irc::string thischan = chan->name.c_str();
137                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); i++)
138                 {
139                         irc::string target = i->mask.c_str();
140                         irc::string tchan = i->channel.c_str();
141                         if ((listitem == target) && (tchan == thischan))
142                         {
143                                 TimedBanList.erase(i);
144                                 break;
145                         }
146                 }
147                 return MOD_RES_PASSTHRU;
148         }
149
150         virtual void OnBackgroundTimer(time_t curtime)
151         {
152                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
153                 {
154                         if (curtime > i->expire)
155                         {
156                                 std::string chan = i->channel;
157                                 std::string mask = i->mask;
158                                 Channel* cr = ServerInstance->FindChan(chan);
159                                 i = TimedBanList.erase(i);
160                                 if (cr)
161                                 {
162                                         std::vector<std::string> setban;
163                                         setban.push_back(chan);
164                                         setban.push_back("-b");
165                                         setban.push_back(mask);
166
167                                         CUList empty;
168                                         std::string expiry = "*** Timed ban on " + chan + " expired.";
169                                         cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
170                                         ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
171
172                                         ServerInstance->SendMode(setban, ServerInstance->FakeClient);
173                                         ServerInstance->PI->SendMode(chan, ServerInstance->Modes->GetLastParseParams(), ServerInstance->Modes->GetLastParseTranslate());
174                                 }
175                         }
176                         else
177                                 ++i;
178                 }
179         }
180
181         virtual Version GetVersion()
182         {
183                 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
184         }
185 };
186
187 MODULE_INIT(ModuleTimedBans)
188