]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
Convert all to new Attach() system. The Implements() method needs removing from all...
[user/henk/code/inspircd.git] / src / modules / m_timedbans.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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** 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                                         T.channel = channelname;
89                                         T.mask = mask;
90                                         T.expire = expire;
91                                         TimedBanList.push_back(T);
92                                         channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s added a timed ban on %s lasting for %ld seconds.", channel->name, user->nick, mask.c_str(), duration);
93                                         return CMD_SUCCESS;
94                                 }
95                                 return CMD_FAILURE;
96                         }
97                         else user->WriteServ("482 %s %s :You must be at least a%soperator to change modes on this channel",user->nick, channel->name,
98                                         ServerInstance->Config->AllowHalfop ? " half-" : " channel ");
99                         return CMD_FAILURE;
100                 }
101                 user->WriteServ("401 %s %s :No such channel",user->nick, parameters[0]);
102                 return CMD_FAILURE;
103         }
104 };
105
106 class ModuleTimedBans : public Module
107 {
108         CommandTban* mycommand;
109  public:
110         ModuleTimedBans(InspIRCd* Me)
111                 : Module(Me)
112         {
113                 
114                 mycommand = new CommandTban(ServerInstance);
115                 ServerInstance->AddCommand(mycommand);
116                 TimedBanList.clear();
117                 Implementation eventlist[] = { I_OnDelBan, I_OnBackgroundTimer };
118                 ServerInstance->Modules->Attach(eventlist, this, 2);
119         }
120         
121         virtual ~ModuleTimedBans()
122         {
123                 TimedBanList.clear();
124         }
125
126         void Implements(char* List)
127         {
128                 List[I_OnDelBan] = List[I_OnBackgroundTimer] = 1;
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;
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                 bool again = true;
151                 while (again)
152                 {
153                         again = false;
154                         for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
155                         {
156                                 if (curtime > i->expire)
157                                 {
158                                         Channel* cr = ServerInstance->FindChan(i->channel);
159                                         again = true;
160                                         if (cr)
161                                         {
162                                                 cr->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :Timed ban on %s expired.", cr->name, i->mask.c_str());
163                                                 const char *setban[3];
164                                                 setban[0] = i->channel.c_str();
165                                                 setban[1] = "-b";
166                                                 setban[2] = i->mask.c_str();
167
168                                                 /* Send mode remotely*/
169                                                 std::deque<std::string> n;
170                                                 n.push_back(setban[0]);
171                                                 n.push_back("-b");
172                                                 n.push_back(setban[2]);
173                                                 ServerInstance->SendMode(setban,3, ServerInstance->FakeClient);
174                                                 Event rmode((char *)&n, NULL, "send_mode");
175                                                 rmode.Send(ServerInstance);
176                                         }
177                                         else
178                                         {
179                                                 /* Where the hell did our channel go?! */
180                                                 TimedBanList.erase(i);
181                                         }
182                                         // we used to delete the item here, but we dont need to as the servermode above does it for us,
183                                         break;
184                                 }
185                         }
186                 }
187         }
188         
189         virtual Version GetVersion()
190         {
191                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
192         }
193 };
194
195 MODULE_INIT(ModuleTimedBans)
196