]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanfilter.cpp
48b81cc8fbbd1dcbce2a87deb73f6c9aa2668ee5
[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()
40         {
41                 Srv = new Server;
42                 Conf = new ConfigReader;
43                 Srv->AddExtendedListMode('g');
44                 MaxEntries = Conf->ReadInteger("chanfilter","maxsize",0,true);
45                 if (MaxEntries == 0)
46                         MaxEntries = 32;
47         }
48         
49         virtual void On005Numeric(std::string &output)
50         {
51                 std::stringstream line(output);
52                 std::string temp1, temp2;
53                 while (!line.eof())
54                 {
55                         line >> temp1;
56                         if (temp1.substr(0,10) == "CHANMODES=")
57                         {
58                                 // append the chanmode to the end
59                                 temp1 = temp1.substr(10,temp1.length());
60                                 temp1 = "CHANMODES=g" + temp1;
61                         }
62                         temp2 = temp2 + temp1 + " ";
63                 }
64                 if (temp2.length())
65                         output = temp2.substr(0,temp2.length()-1);
66         }
67
68         virtual void OnUserPart(userrec* user, chanrec* channel)
69         {
70                 // when the last user parts, delete the list
71                 if (Srv->CountUsers(channel) == 1)
72                 {
73                         SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
74                         if (spamlist)
75                         {
76                                 channel->Shrink("spam_list");
77                                 delete spamlist;
78                         }
79                 }
80         }
81
82         virtual void OnRehash(std::string parameter)
83         {
84                 delete Conf;
85                 Conf = new ConfigReader;
86                 // re-read our config options on a rehash
87                 MaxEntries = Conf->ReadInteger("chanfilter","maxsize",0,true);
88         }
89
90         virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
91         {
92                 char buffer[MAXBUF];
93                 strlcpy(buffer,text.c_str(),MAXBUF);
94                 for (unsigned int j = 0; j < strlen(buffer); j++)
95                         buffer[j] = tolower(buffer[j]);
96                 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
97                 if (spamlist)
98                 {
99                         for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
100                         {
101                                 if (strstr(text.c_str(),i->c_str()))
102                                 {
103                                         WriteServ(user->fd,"936 %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name);
104                                         return 1;
105                                 }
106                         }
107                 }
108                 return 0;
109         }
110
111         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
112         {
113                 if (target_type == TYPE_CHANNEL)
114                 {
115                         return ProcessMessages(user,(chanrec*)dest,text);
116                 }
117                 else return 0;
118         }
119
120         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
121         {
122                 if (target_type == TYPE_CHANNEL)
123                 {
124                         return ProcessMessages(user,(chanrec*)dest,text);
125                 }
126                 else return 0;
127         }
128         
129         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
130         {
131                 if ((modechar == 'g') && (type == MT_CHANNEL))
132                 {
133                         chanrec* chan = (chanrec*)target;
134
135                         for (unsigned int j = 0; j < params[0].length(); j++)
136                                 params[0][j] = tolower(params[0][j]);
137
138                         std::string param = params[0];
139                 
140                         if (mode_on)
141                         {
142                                 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
143                                 if (!spamlist)
144                                 {
145                                         spamlist = new SpamList;
146                                         chan->Extend("spam_list",(char*)spamlist);
147                                 }
148                                 if (spamlist->size() < (unsigned)MaxEntries)
149                                 {
150                                         for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
151                                         {
152                                                 if (*i == params[0])
153                                                 {
154                                                         WriteServ(user->fd,"937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,params[0].c_str());
155                                                         return -1;
156                                                 }
157                                         }
158                                         spamlist->push_back(params[0]);
159                                         return 1;
160                                 }
161                                 WriteServ(user->fd,"939 %s %s :Channel spamfilter list is full",user->nick, chan->name);
162                                 return -1;
163                         }
164                         else
165                         {
166                                 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
167                                 if (spamlist)
168                                 {
169                                         for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
170                                         {
171                                                 if (*i == params[0])
172                                                 {
173                                                         spamlist->erase(i);
174                                                         return 1;
175                                                 }
176                                         }
177                                 }
178                                 WriteServ(user->fd,"938 %s %s :No such spamfilter word is set",user->nick, chan->name);
179                                 return -1;
180                         }
181                         return -1;
182                 }       
183                 return 0;
184         }
185
186         virtual void OnSendList(userrec* user, chanrec* channel, char mode)
187         {
188                 if (mode == 'g')
189                 {
190                         SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
191                         if (spamlist)
192                         {
193                                 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
194                                 {
195                                         WriteServ(user->fd,"941 %s %s %s",user->nick, channel->name,i->c_str());
196                                 }
197                         }
198                         WriteServ(user->fd,"940 %s %s :End of channel spamfilter list",user->nick, channel->name);
199                 }
200         }
201         
202         virtual ~ModuleChanFilter()
203         {
204                 delete Conf;
205                 delete Srv;
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()
241         {
242                 return new ModuleChanFilter;
243         }
244         
245 };
246
247
248 extern "C" void * init_module( void )
249 {
250         return new ModuleChanFilterFactory;
251 }
252