]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
Break! ...no actually, take the break out :)
[user/henk/code/inspircd.git] / src / modules / m_timedbans.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 char* const* parameters, int pcnt, 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,parameters[2]))
59                                         {
60                                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :The ban "+std::string(parameters[2])+" is already on the banlist of "+std::string(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 + time(NULL);
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                                 const char *setban[32];
75                                 setban[0] = parameters[0];
76                                 setban[1] = "+b";
77                                 setban[2] = parameters[2];
78                                 // use CallCommandHandler to make it so that the user sets the mode
79                                 // themselves
80                                 ServerInstance->CallCommandHandler("MODE",setban,3,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,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, user->nick, mask.c_str(), duration);
94                                         if (ServerInstance->Config->AllowHalfop)
95                                                 channel->WriteAllExcept(user, true, '%', tmp, "NOTICE %s :%s added a timed ban on %s lasting for %ld seconds.", channel->name, user->nick, mask.c_str(), duration);
96                                         return CMD_SUCCESS;
97                                 }
98                                 return CMD_FAILURE;
99                         }
100                         else user->WriteNumeric(482, "%s %s :You must be at least a%soperator to change modes on this channel",user->nick, channel->name,
101                                         ServerInstance->Config->AllowHalfop ? " half-" : " channel ");
102                         return CMD_FAILURE;
103                 }
104                 user->WriteNumeric(401, "%s %s :No such channel",user->nick, parameters[0]);
105                 return CMD_FAILURE;
106         }
107 };
108
109 class ModuleTimedBans : public Module
110 {
111         CommandTban* mycommand;
112  public:
113         ModuleTimedBans(InspIRCd* Me)
114                 : Module(Me)
115         {
116                 
117                 mycommand = new CommandTban(ServerInstance);
118                 ServerInstance->AddCommand(mycommand);
119                 TimedBanList.clear();
120                 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
121                 ServerInstance->Modules->Attach(eventlist, this, 2);
122         }
123         
124         virtual ~ModuleTimedBans()
125         {
126                 TimedBanList.clear();
127         }
128
129         virtual int OnDelBan(User* source, Channel* chan, const std::string &banmask)
130         {
131                 irc::string listitem = banmask.c_str();
132                 irc::string thischan = chan->name;
133                 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
134                 {
135                         irc::string target = i->mask.c_str();
136                         irc::string tchan = i->channel.c_str();
137                         if ((listitem == target) && (tchan == thischan))
138                         {
139                                 TimedBanList.erase(i);
140                                 break;
141                         }
142                 }
143                 return 0;
144         }
145
146         virtual void OnBackgroundTimer(time_t curtime)
147         {
148                 timedbans::iterator safei;
149                 for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end();)
150                 {
151                         /* Safe copy of iterator, so we can erase as we iterate */
152                         safei = i;
153                         ++i;
154
155                         if (curtime > safei->expire)
156                         {
157                                 Channel* cr = ServerInstance->FindChan(safei->channel);
158                                 if (cr)
159                                 {
160                                         const char *setban[3];
161                                         std::string mask = safei->mask;
162
163                                         setban[0] = safei->channel.c_str();
164                                         setban[1] = "-b";
165                                         setban[2] = mask.c_str();
166
167                                         CUList empty;
168                                         cr->WriteAllExcept(ServerInstance->FakeClient, true, '@', empty, "NOTICE %s :*** Timed ban on %s expired.", cr->name, safei->mask.c_str());
169                                         if (ServerInstance->Config->AllowHalfop)
170                                                 cr->WriteAllExcept(ServerInstance->FakeClient, true, '%', empty, "NOTICE %s :*** Timed ban on %s expired.", cr->name, safei->mask.c_str());
171
172                                         /* Removes the ban item for us, no ::erase() needed */
173                                         ServerInstance->PI->SendModeStr(safei->channel, std::string("-b ") + setban[2]);
174                                         ServerInstance->SendMode(setban, 3, ServerInstance->FakeClient);
175
176                                         bool was_removed = true;
177                                         for (BanList::iterator j = cr->bans.begin(); j != cr->bans.end(); j++)
178                                                 if (!strcasecmp(j->data, mask.c_str()))
179                                                         was_removed = false;
180
181                                         /* Fix for crash if user cycles before the ban expires */ 
182                                         if (!was_removed)
183                                                 TimedBanList.erase(safei);
184                                 }
185                                 else
186                                 {
187                                         /* Where the hell did our channel go?! */
188                                         TimedBanList.erase(safei);
189                                 }
190                         }
191                 }
192         }
193         
194         virtual Version GetVersion()
195         {
196                 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
197         }
198 };
199
200 MODULE_INIT(ModuleTimedBans)
201