]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_timedbans.cpp
and a little tweak to remote MOTD too.
[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 #include "users.h"
18 #include "channels.h"
19 #include "modules.h"
20 #include "hashcomp.h"
21 #include "configreader.h"
22
23 /** Holds a timed ban
24  */
25 class TimedBan : public classbase
26 {
27  public:
28         std::string channel;
29         std::string mask;
30         time_t expire;
31 };
32
33 typedef std::vector<TimedBan> timedbans;
34 timedbans TimedBanList;
35
36 /** Handle /TBAN
37  */
38 class cmd_tban : public command_t
39 {
40  public:
41  cmd_tban (InspIRCd* Instance) : command_t(Instance,"TBAN", 0, 3)
42         {
43                 this->source = "m_timedbans.so";
44                 syntax = "<channel> <duration> <banmask>";
45         }
46
47         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
48         {
49                 chanrec* channel = ServerInstance->FindChan(parameters[0]);
50                 if (channel)
51                 {
52                         int cm = channel->GetStatus(user);
53                         if ((cm == STATUS_HOP) || (cm == STATUS_OP))
54                         {
55                                 if (!ServerInstance->IsValidMask(parameters[2]))
56                                 {
57                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban mask");
58                                         return CMD_FAILURE;
59                                 }
60                                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
61                                 {
62                                         if (!strcasecmp(i->data,parameters[2]))
63                                         {
64                                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :The ban "+std::string(parameters[2])+" is already on the banlist of "+std::string(parameters[0]));
65                                                 return CMD_FAILURE;
66                                         }
67                                 }
68                                 TimedBan T;
69                                 std::string channelname = parameters[0];
70                                 unsigned long expire = ServerInstance->Duration(parameters[1]) + time(NULL);
71                                 if (ServerInstance->Duration(parameters[1]) < 1)
72                                 {
73                                         user->WriteServ("NOTICE "+std::string(user->nick)+" :Invalid ban time");
74                                         return CMD_FAILURE;
75                                 }
76                                 char duration[MAXBUF];
77                                 snprintf(duration,MAXBUF,"%lu",ServerInstance->Duration(parameters[1]));
78                                 std::string mask = parameters[2];
79                                 const char *setban[32];
80                                 setban[0] = parameters[0];
81                                 setban[1] = "+b";
82                                 setban[2] = parameters[2];
83                                 // use CallCommandHandler to make it so that the user sets the mode
84                                 // themselves
85                                 ServerInstance->CallCommandHandler("MODE",setban,3,user);
86                                 /* Check if the ban was actually added (e.g. banlist was NOT full) */
87                                 bool was_added = false;
88                                 for (BanList::iterator i = channel->bans.begin(); i != channel->bans.end(); i++)
89                                         if (!strcasecmp(i->data,mask.c_str()))
90                                                 was_added = true;
91                                 if (was_added)
92                                 {
93                                         T.channel = channelname;
94                                         T.mask = mask;
95                                         T.expire = expire;
96                                         TimedBanList.push_back(T);
97                                         channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s added a timed ban on %s lasting for %s seconds.", channel->name, user->nick, mask.c_str(), duration);
98                                         return CMD_SUCCESS;
99                                 }
100                                 return CMD_FAILURE;
101                         }
102                         else user->WriteServ("482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, channel->name);
103                         return CMD_FAILURE;
104                 }
105                 user->WriteServ("401 %s %s :No such channel",user->nick, parameters[0]);
106                 return CMD_FAILURE;
107         }
108 };
109
110 class ModuleTimedBans : public Module
111 {
112         cmd_tban* mycommand;
113  public:
114         ModuleTimedBans(InspIRCd* Me)
115                 : Module(Me)
116         {
117                 
118                 mycommand = new cmd_tban(ServerInstance);
119                 ServerInstance->AddCommand(mycommand);
120                 TimedBanList.clear();
121         }
122         
123         virtual ~ModuleTimedBans()
124         {
125                 TimedBanList.clear();
126         }
127
128         void Implements(char* List)
129         {
130                 List[I_OnDelBan] = List[I_OnBackgroundTimer] = 1;
131         }
132
133         virtual int OnDelBan(userrec* source, chanrec* chan, const std::string &banmask)
134         {
135                 irc::string listitem = banmask.c_str();
136                 irc::string thischan = chan->name;
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 0;
148         }
149
150         virtual void OnBackgroundTimer(time_t curtime)
151         {
152                 bool again = true;
153                 while (again)
154                 {
155                         again = false;
156                         for (timedbans::iterator i = TimedBanList.begin(); i < TimedBanList.end(); i++)
157                         {
158                                 if (curtime > i->expire)
159                                 {
160                                         chanrec* cr = ServerInstance->FindChan(i->channel);
161                                         again = true;
162                                         if (cr)
163                                         {
164                                                 cr->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :Timed ban on %s expired.", cr->name, i->mask.c_str());
165                                                 const char *setban[3];
166                                                 setban[0] = i->channel.c_str();
167                                                 setban[1] = "-b";
168                                                 setban[2] = i->mask.c_str();
169                                                 // kludge alert!
170                                                 // ::SendMode expects a userrec* to send the numeric replies
171                                                 // back to, so we create it a fake user that isnt in the user
172                                                 // hash and set its descriptor to FD_MAGIC_NUMBER so the data
173                                                 // falls into the abyss :p
174                                                 userrec* temp = new userrec(ServerInstance);
175                                                 temp->SetFd(FD_MAGIC_NUMBER);
176                                                 /* FIX: Send mode remotely*/
177                                                 std::deque<std::string> n;
178                                                 n.push_back(setban[0]);
179                                                 n.push_back("-b");
180                                                 n.push_back(setban[2]);
181                                                 ServerInstance->SendMode(setban,3,temp);
182                                                 Event rmode((char *)&n, NULL, "send_mode");
183                                                 rmode.Send(ServerInstance);
184                                                 DELETE(temp);
185                                         }
186                                         else
187                                         {
188                                                 /* Where the hell did our channel go?! */
189                                                 TimedBanList.erase(i);
190                                         }
191                                         // we used to delete the item here, but we dont need to as the servermode above does it for us,
192                                         break;
193                                 }
194                         }
195                 }
196         }
197         
198         virtual Version GetVersion()
199         {
200                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
201         }
202 };
203
204
205 class ModuleTimedBansFactory : public ModuleFactory
206 {
207  public:
208         ModuleTimedBansFactory()
209         {
210         }
211         
212         ~ModuleTimedBansFactory()
213         {
214         }
215         
216         virtual Module * CreateModule(InspIRCd* Me)
217         {
218                 return new ModuleTimedBans(Me);
219         }
220         
221 };
222
223
224 extern "C" DllExport void * init_module( void )
225 {
226         return new ModuleTimedBansFactory;
227 }