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