]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
Fixed 005 to be f not L
[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         bool ban;
30         int secs;
31         int lines;
32
33         floodsettings() : ban(0), secs(0), lines(0) {};
34         floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c) {};
35 }
36
37 class ModuleMsgFlood : public Module
38 {
39         Server *Srv;
40         
41  public:
42  
43         ModuleMsgFlood(Server* Me)
44                 : Module::Module(Me)
45         {
46                 Srv = Me;
47                 Srv->AddExtendedMode('f',MT_CHANNEL,false,1,0);
48         }
49         
50         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
51         {
52                 if ((modechar == 'f') && (type == MT_CHANNEL))
53                 {
54                         if (mode_on)
55                         {
56                                 std::string FloodParams = params[0];
57                                 chanrec* c = (chanrec*)target;
58                                 char data[MAXBUF];
59                                 strlcpy(data,FloodParams.c_str(),MAXBUF);
60                                 char* lines = data;
61                                 char* secs = NULL;
62                                 bool ban = false;
63                                 if (*data == '*')
64                                 {
65                                         ban = true;
66                                         data++;
67                                 }
68                                 else
69                                 {
70                                         ban = false;
71                                 }
72                                 while (*data)
73                                 {
74                                         if (*data == ':')
75                                         {
76                                                 *data = 0;
77                                                 data++;
78                                                 secs = data;
79                                                 break;
80                                         }
81                                         else data++;
82                                 }
83                                 if (secs)
84                                 {
85                                         /* Set up the flood parameters for this channel */
86                                         int nlines = atoi(lines);
87                                         int nsecs = atoi(secs);
88                                         if ((nlines<1) || (nsecs<1))
89                                         {
90                                                 WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
91                                                 return 0;
92                                         }
93                                         else
94                                         {
95                                                 if (!c->GetExt("flood"))
96                                                 {
97                                                         floodsettings *f = new floodsettings(ban,nlines,nsecs);
98                                                         c->Extend("flood",(char*)f);
99                                                 }
100                                         }
101                                         return 1;
102                                 }
103                                 else
104                                 {
105                                         WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
106                                         return 0;
107                                 }
108                                 
109                         }
110                         else
111                         {
112                                 if (c->GetExt("flood"))
113                                 {
114                                         floodsettings *f = (floodsettings*)c->GetExt("flood");
115                                         delete f;
116                                         c->Shrink("flood");
117                                 }
118                         }
119                         return 1;
120                 }
121                 return 0;
122         }
123
124         void OnChannelDelete(chanrec* chan)
125         {
126                 if (c->GetExt("flood"))
127                 {
128                         floodsettings *f = (floodsettings*)c->GetExt("flood");
129                         delete f;
130                         c->Shrink("flood");
131                 }
132         }
133
134         void Implements(char* List)
135         {
136                 List[I_On005Numeric] = List[I_OnExtendedMode] = List[I_OnChannelDelete] = 1;
137         }
138
139         virtual void On005Numeric(std::string &output)
140         {
141                 std::stringstream line(output);
142                 std::string temp1, temp2;
143                 while (!line.eof())
144                 {
145                         line >> temp1;
146                         if (temp1.substr(0,10) == "CHANMODES=")
147                         {
148                                 // By doing this we're *assuming* no other module has fucked up the CHANMODES=
149                                 // section of the 005 numeric. If they have, we're going DOWN in a blaze of glory,
150                                 // with a honking great EXCEPTION :)
151                                 temp1.insert(temp1.find(",")+1,"f");
152                         }
153                         temp2 = temp2 + temp1 + " ";
154                 }
155                 if (temp2.length())
156                         output = temp2.substr(0,temp2.length()-1);
157         }
158
159         virtual ~ModuleMsgFlood()
160         {
161         }
162         
163         virtual Version GetVersion()
164         {
165                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
166         }
167 };
168
169
170 class ModuleMsgFloodFactory : public ModuleFactory
171 {
172  public:
173         ModuleMsgFloodFactory()
174         {
175         }
176         
177         ~ModuleMsgFloodFactory()
178         {
179         }
180         
181         virtual Module * CreateModule(Server* Me)
182         {
183                 return new ModuleMsgFlood(Me);
184         }
185         
186 };
187
188
189 extern "C" void * init_module( void )
190 {
191         return new ModuleMsgFloodFactory;
192 }
193