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