]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
Made '*' ban-specifier work
[user/henk/code/inspircd.git] / src / modules / m_messageflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23 #include "helperfuncs.h"
24
25 /* $ModDesc: Provides channel mode +f (message flood protection) */
26
27 class floodsettings
28 {
29  public:
30         bool ban;
31         int secs;
32         int lines;
33
34         floodsettings() : ban(0), secs(0), lines(0) {};
35         floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c) {};
36 };
37
38 class ModuleMsgFlood : public Module
39 {
40         Server *Srv;
41         
42  public:
43  
44         ModuleMsgFlood(Server* Me)
45                 : Module::Module(Me)
46         {
47                 Srv = Me;
48                 Srv->AddExtendedMode('f',MT_CHANNEL,false,1,0);
49         }
50         
51         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
52         {
53                 if ((modechar == 'f') && (type == MT_CHANNEL))
54                 {
55                         if (mode_on)
56                         {
57                                 std::string FloodParams = params[0];
58                                 chanrec* c = (chanrec*)target;
59                                 char ndata[MAXBUF];
60                                 char* data = ndata;
61                                 strlcpy(ndata,FloodParams.c_str(),MAXBUF);
62                                 char* lines = data;
63                                 char* secs = NULL;
64                                 bool ban = false;
65                                 if (*data == '*')
66                                 {
67                                         ban = true;
68                                         lines++;
69                                 }
70                                 else
71                                 {
72                                         ban = false;
73                                 }
74                                 while (*data)
75                                 {
76                                         if (*data == ':')
77                                         {
78                                                 *data = 0;
79                                                 data++;
80                                                 secs = data;
81                                                 break;
82                                         }
83                                         else data++;
84                                 }
85                                 if (secs)
86                                 {
87                                         /* Set up the flood parameters for this channel */
88                                         int nlines = atoi(lines);
89                                         int nsecs = atoi(secs);
90                                         if ((nlines<1) || (nsecs<1))
91                                         {
92                                                 WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
93                                                 return 0;
94                                         }
95                                         else
96                                         {
97                                                 if (!c->GetExt("flood"))
98                                                 {
99                                                         floodsettings *f = new floodsettings(ban,nlines,nsecs);
100                                                         c->Extend("flood",(char*)f);
101                                                 }
102                                         }
103                                         return 1;
104                                 }
105                                 else
106                                 {
107                                         WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
108                                         return 0;
109                                 }
110                                 
111                         }
112                         else
113                         {
114                                 chanrec* c = (chanrec*)target;
115                                 if (c->GetExt("flood"))
116                                 {
117                                         floodsettings *f = (floodsettings*)c->GetExt("flood");
118                                         delete f;
119                                         c->Shrink("flood");
120                                 }
121                         }
122                         return 1;
123                 }
124                 return 0;
125         }
126
127         void OnChannelDelete(chanrec* chan)
128         {
129                 if (chan->GetExt("flood"))
130                 {
131                         floodsettings *f = (floodsettings*)chan->GetExt("flood");
132                         delete f;
133                         chan->Shrink("flood");
134                 }
135         }
136
137         void Implements(char* List)
138         {
139                 List[I_On005Numeric] = List[I_OnExtendedMode] = List[I_OnChannelDelete] = 1;
140         }
141
142         virtual void On005Numeric(std::string &output)
143         {
144                 std::stringstream line(output);
145                 std::string temp1, temp2;
146                 while (!line.eof())
147                 {
148                         line >> temp1;
149                         if (temp1.substr(0,10) == "CHANMODES=")
150                         {
151                                 // By doing this we're *assuming* no other module has fucked up the CHANMODES=
152                                 // section of the 005 numeric. If they have, we're going DOWN in a blaze of glory,
153                                 // with a honking great EXCEPTION :)
154                                 temp1.insert(temp1.find(",")+1,"f");
155                         }
156                         temp2 = temp2 + temp1 + " ";
157                 }
158                 if (temp2.length())
159                         output = temp2.substr(0,temp2.length()-1);
160         }
161
162         virtual ~ModuleMsgFlood()
163         {
164         }
165         
166         virtual Version GetVersion()
167         {
168                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
169         }
170 };
171
172
173 class ModuleMsgFloodFactory : public ModuleFactory
174 {
175  public:
176         ModuleMsgFloodFactory()
177         {
178         }
179         
180         ~ModuleMsgFloodFactory()
181         {
182         }
183         
184         virtual Module * CreateModule(Server* Me)
185         {
186                 return new ModuleMsgFlood(Me);
187         }
188         
189 };
190
191
192 extern "C" void * init_module( void )
193 {
194         return new ModuleMsgFloodFactory;
195 }
196