]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
d290ec18d260831f55a8100d02d7db03f2508685
[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                         user->WriteNumeric(401, "%s %s :No such channel",user->nick.c_str(), parameters[0].c_str());
48                         return CMD_FAILURE;
49                 }
50                 int cm = channel->GetPrefixValue(user);
51                 if (cm < HALFOP_VALUE)
52                 {
53                         user->WriteNumeric(482, "%s %s :You do not have permission to set bans on this channel",
54                                 user->nick.c_str(), channel->name.c_str());
55                         return CMD_FAILURE;
56                 }
57                 if (!ServerInstance->IsValidMask(parameters[2]))
58                 {
59                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban mask");
60                         return CMD_FAILURE;
61                 }
62                 TimedBan T;
63                 std::string channelname = parameters[0];
64                 long duration = ServerInstance->Duration(parameters[1]);
65                 unsigned long expire = duration + ServerInstance->Time();
66                 if (duration < 1)
67                 {
68                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban time");
69                         return CMD_FAILURE;
70                 }
71                 std::string mask = parameters[2];
72                 std::vector<std::string> setban;
73                 setban.push_back(parameters[0]);
74                 setban.push_back("+b");
75                 setban.push_back(parameters[2]);
76                 // use CallCommandHandler to make it so that the user sets the mode
77                 // themselves
78                 ServerInstance->CallCommandHandler("MODE",setban,user);
79                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
80                         if (!strcasecmp(i->data.c_str(), mask.c_str()))
81                                 goto found;
82                 return CMD_FAILURE;
83 found:
84                 CUList tmp;
85                 T.channel = channelname;
86                 T.mask = mask;
87                 T.expire = expire;
88                 TimedBanList.push_back(T);
89                 channel->WriteAllExcept(ServerInstance->FakeClient, 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);
90                 ServerInstance->PI->SendChannelNotice(channel, '@', user->nick + " added a timed ban on " + mask + " lasting for " + ConvToStr(duration) + " seconds.");
91                 return CMD_SUCCESS;
92         }
93
94         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
95         {
96                 return ROUTE_BROADCAST;
97         }
98 };
99
100 class ModuleTimedBans : public Module
101 {
102         CommandTban cmd;
103  public:
104         ModuleTimedBans()
105                 : cmd(this)
106         {
107                 ServerInstance->AddCommand(&cmd);
108                 TimedBanList.clear();
109                 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
110                 ServerInstance->Modules->Attach(eventlist, this, 2);
111         }
112
113         virtual ~ModuleTimedBans()
114         {
115                 TimedBanList.clear();
116         }
117
118         virtual ModResult OnDelBan(User* source, Channel* chan, const std::string &banmask)
119         {
120                 irc::string listitem = banmask.c_str();
121                 irc::string thischan = chan->name.c_str();
122                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end(); i++)
123                 {
124                         irc::string target = i->mask.c_str();
125                         irc::string tchan = i->channel.c_str();
126                         if ((listitem == target) && (tchan == thischan))
127                         {
128                                 TimedBanList.erase(i);
129                                 break;
130                         }
131                 }
132                 return MOD_RES_PASSTHRU;
133         }
134
135         virtual void OnBackgroundTimer(time_t curtime)
136         {
137                 for (timedbans::iterator i = TimedBanList.begin(); i != TimedBanList.end();)
138                 {
139                         if (curtime > i->expire)
140                         {
141                                 std::string chan = i->channel;
142                                 std::string mask = i->mask;
143                                 Channel* cr = ServerInstance->FindChan(chan);
144                                 i = TimedBanList.erase(i);
145                                 if (cr)
146                                 {
147                                         std::vector<std::string> setban;
148                                         setban.push_back(chan);
149                                         setban.push_back("-b");
150                                         setban.push_back(mask);
151
152                                         CUList empty;
153                                         std::string expiry = "*** Timed ban on " + chan + " expired.";
154                                         cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :%s", cr->name.c_str(), expiry.c_str());
155                                         ServerInstance->PI->SendChannelNotice(cr, '@', expiry);
156
157                                         ServerInstance->SendGlobalMode(setban, ServerInstance->FakeClient);
158                                 }
159                         }
160                         else
161                                 ++i;
162                 }
163         }
164
165         virtual Version GetVersion()
166         {
167                 return Version("Adds timed bans", VF_COMMON | VF_VENDOR);
168         }
169 };
170
171 MODULE_INIT(ModuleTimedBans)
172