]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanfilter.cpp
Fixed compile problems... Move along please, nothing to see here..
[user/henk/code/inspircd.git] / src / modules / m_chanfilter.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 <string>
21 #include <vector>
22 #include "users.h"
23 #include "channels.h"
24 #include "modules.h"
25 #include "helperfuncs.h"
26
27 /* $ModDesc: Provides channel-specific censor lists (like mode +G but varies from channel to channel) */
28
29 typedef std::vector<std::string> SpamList;
30
31 class ModuleChanFilter : public Module
32 {
33         Server *Srv;
34         ConfigReader *Conf;
35         long MaxEntries;
36         
37  public:
38  
39         ModuleChanFilter(Server* Me)
40                 : Module::Module(Me)
41         {
42                 Srv = Me;
43                 Conf = new ConfigReader;
44                 Srv->AddExtendedListMode('g');
45                 MaxEntries = Conf->ReadInteger("chanfilter","maxsize",0,true);
46                 if (MaxEntries == 0)
47                         MaxEntries = 32;
48         }
49         
50         virtual void On005Numeric(std::string &output)
51         {
52                 std::stringstream line(output);
53                 std::string temp1, temp2;
54                 while (!line.eof())
55                 {
56                         line >> temp1;
57                         if (temp1.substr(0,10) == "CHANMODES=")
58                         {
59                                 // append the chanmode to the end
60                                 temp1 = temp1.substr(10,temp1.length());
61                                 temp1 = "CHANMODES=g" + temp1;
62                         }
63                         temp2 = temp2 + temp1 + " ";
64                 }
65                 if (temp2.length())
66                         output = temp2.substr(0,temp2.length()-1);
67         }
68
69         virtual void OnUserPart(userrec* user, chanrec* channel)
70         {
71                 // when the last user parts, delete the list
72                 if (Srv->CountUsers(channel) == 1)
73                 {
74                         SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
75                         if (spamlist)
76                         {
77                                 channel->Shrink("spam_list");
78                                 delete spamlist;
79                         }
80                 }
81         }
82
83         virtual void OnRehash(std::string parameter)
84         {
85                 delete Conf;
86                 Conf = new ConfigReader;
87                 // re-read our config options on a rehash
88                 MaxEntries = Conf->ReadInteger("chanfilter","maxsize",0,true);
89         }
90
91         virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
92         {
93                 char buffer[MAXBUF];
94                 strlcpy(buffer,text.c_str(),MAXBUF);
95                 for (unsigned int j = 0; j < strlen(buffer); j++)
96                         buffer[j] = tolower(buffer[j]);
97                 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
98                 if (spamlist)
99                 {
100                         for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
101                         {
102                                 if (strstr(buffer,i->c_str()))
103                                 {
104                                         WriteServ(user->fd,"936 %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name);
105                                         return 1;
106                                 }
107                         }
108                 }
109                 return 0;
110         }
111
112         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
113         {
114                 if (target_type == TYPE_CHANNEL)
115                 {
116                         return ProcessMessages(user,(chanrec*)dest,text);
117                 }
118                 else return 0;
119         }
120
121         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
122         {
123                 if (target_type == TYPE_CHANNEL)
124                 {
125                         return ProcessMessages(user,(chanrec*)dest,text);
126                 }
127                 else return 0;
128         }
129         
130         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
131         {
132                 if ((modechar == 'g') && (type == MT_CHANNEL))
133                 {
134                         chanrec* chan = (chanrec*)target;
135
136                         for (unsigned int j = 0; j < params[0].length(); j++)
137                                 params[0][j] = tolower(params[0][j]);
138
139                         std::string param = params[0];
140                 
141                         if (mode_on)
142                         {
143                                 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
144                                 if (!spamlist)
145                                 {
146                                         spamlist = new SpamList;
147                                         chan->Extend("spam_list",(char*)spamlist);
148                                 }
149                                 if (spamlist->size() < (unsigned)MaxEntries)
150                                 {
151                                         for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
152                                         {
153                                                 if (*i == params[0])
154                                                 {
155                                                         WriteServ(user->fd,"937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,params[0].c_str());
156                                                         return -1;
157                                                 }
158                                         }
159                                         spamlist->push_back(params[0]);
160                                         return 1;
161                                 }
162                                 WriteServ(user->fd,"939 %s %s :Channel spamfilter list is full",user->nick, chan->name);
163                                 return -1;
164                         }
165                         else
166                         {
167                                 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
168                                 if (spamlist)
169                                 {
170                                         for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
171                                         {
172                                                 if (*i == params[0])
173                                                 {
174                                                         spamlist->erase(i);
175                                                         return 1;
176                                                 }
177                                         }
178                                 }
179                                 WriteServ(user->fd,"938 %s %s :No such spamfilter word is set",user->nick, chan->name);
180                                 return -1;
181                         }
182                         return -1;
183                 }       
184                 return 0;
185         }
186
187         virtual void OnSendList(userrec* user, chanrec* channel, char mode)
188         {
189                 if (mode == 'g')
190                 {
191                         SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
192                         if (spamlist)
193                         {
194                                 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
195                                 {
196                                         WriteServ(user->fd,"941 %s %s %s",user->nick, channel->name,i->c_str());
197                                 }
198                         }
199                         WriteServ(user->fd,"940 %s %s :End of channel spamfilter list",user->nick, channel->name);
200                 }
201         }
202         
203         virtual ~ModuleChanFilter()
204         {
205                 delete Conf;
206         }
207         
208         virtual Version GetVersion()
209         {
210                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
211         }
212         
213         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
214         {
215                 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
216                 string_list commands;
217                 if (spamlist)
218                 {
219                         for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
220                         {
221                                 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+g "+*i);
222                         }
223                 }
224         }
225
226 };
227
228
229 class ModuleChanFilterFactory : public ModuleFactory
230 {
231  public:
232         ModuleChanFilterFactory()
233         {
234         }
235         
236         ~ModuleChanFilterFactory()
237         {
238         }
239         
240         virtual Module * CreateModule(Server* Me)
241         {
242                 return new ModuleChanFilter(Me);
243         }
244         
245 };
246
247
248 extern "C" void * init_module( void )
249 {
250         return new ModuleChanFilterFactory;
251 }
252