]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanfilter.cpp
Fixed a minor bug in text formatting of errors
[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                 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
88                 if (spamlist)
89                 {
90                         for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
91                         {
92                                 if (strcasestr(text.c_str(),i->c_str()))
93                                 {
94                                         WriteServ(user->fd,"936 %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name);
95                                         return 1;
96                                 }
97                         }
98                 }
99                 return 0;
100         }
101
102         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
103         {
104                 if (target_type == TYPE_CHANNEL)
105                 {
106                         return ProcessMessages(user,(chanrec*)dest,text);
107                 }
108                 else return 0;
109         }
110
111         virtual int OnUserPreNotice(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 OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
121         {
122                 if ((modechar == 'g') && (type == MT_CHANNEL))
123                 {
124                         chanrec* chan = (chanrec*)target;
125                         std::string param = params[0];
126                 
127                         if (mode_on)
128                         {
129                                 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
130                                 if (!spamlist)
131                                 {
132                                         spamlist = new SpamList;
133                                         chan->Extend("spam_list",(char*)spamlist);
134                                 }
135                                 if (spamlist->size() < MaxEntries)
136                                 {
137                                         for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
138                                         {
139                                                 if (*i == params[0])
140                                                 {
141                                                         WriteServ(user->fd,"937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,params[0].c_str());
142                                                         return -1;
143                                                 }
144                                         }
145                                         spamlist->push_back(params[0]);
146                                         return 1;
147                                 }
148                                 WriteServ(user->fd,"939 %s %s :Channel spamfilter list is full",user->nick, chan->name);
149                                 return -1;
150                         }
151                         else
152                         {
153                                 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
154                                 if (spamlist)
155                                 {
156                                         for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
157                                         {
158                                                 if (*i == params[0])
159                                                 {
160                                                         spamlist->erase(i);
161                                                         return 1;
162                                                 }
163                                         }
164                                 }
165                                 WriteServ(user->fd,"938 %s %s :No such spamfilter word is set",user->nick, chan->name);
166                                 return -1;
167                         }
168                         return -1;
169                 }       
170                 return 0;
171         }
172
173         virtual void OnSendList(userrec* user, chanrec* channel, char mode)
174         {
175                 if (mode == 'g')
176                 {
177                         SpamList* spamlist = (SpamList*)channel->GetExt("spam_list");
178                         if (spamlist)
179                         {
180                                 for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
181                                 {
182                                         WriteServ(user->fd,"941 %s %s %s",user->nick, channel->name,i->c_str());
183                                 }
184                         }
185                         WriteServ(user->fd,"940 %s %s :End of channel spamfilter list",user->nick, channel->name);
186                 }
187         }
188         
189         virtual ~ModuleChanFilter()
190         {
191                 delete Conf;
192                 delete Srv;
193         }
194         
195         virtual Version GetVersion()
196         {
197                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
198         }
199         
200         virtual string_list OnChannelSync(chanrec* chan)
201         {
202                 SpamList* spamlist = (SpamList*)chan->GetExt("spam_list");
203                 string_list commands;
204                 if (spamlist)
205                 {
206                         for (SpamList::iterator i = spamlist->begin(); i != spamlist->end(); i++)
207                         {
208                                 commands.push_back("M "+std::string(chan->name)+" +g "+*i);
209                         }
210                 }
211                 return commands;
212         }
213
214 };
215
216
217 class ModuleChanFilterFactory : public ModuleFactory
218 {
219  public:
220         ModuleChanFilterFactory()
221         {
222         }
223         
224         ~ModuleChanFilterFactory()
225         {
226         }
227         
228         virtual Module * CreateModule()
229         {
230                 return new ModuleChanFilter;
231         }
232         
233 };
234
235
236 extern "C" void * init_module( void )
237 {
238         return new ModuleChanFilterFactory;
239 }
240