]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
5d18f9de895e906bfbe5433660f897f2de7a4e55
[user/henk/code/inspircd.git] / src / modules / m_censor.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 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 "users.h"
22 #include "channels.h"
23 #include "modules.h"
24 #include "helperfuncs.h"
25 #include "inspircd.h"
26
27 typedef std::map<irc::string,irc::string> censor_t;
28
29 /* $ModDesc: Provides user and channel +G mode */
30
31 class CensorException : public ModuleException
32 {
33  public:
34         virtual const char* GetReason()
35         {
36                 return "Could not find <censor file=\"\"> definition in your config file!";
37         }
38 };
39
40 class CensorUser : public ModeHandler
41 {
42  public:
43         CensorUser() : ModeHandler('G', 0, 0, false, MODETYPE_USER, false) { }
44
45         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
46         {
47                 /* Only opers can change other users modes */
48                 if ((source != dest) && (!*source->oper))
49                         return MODEACTION_DENY;
50
51                 if (adding)
52                 {
53                         if (!dest->IsModeSet('G'))
54                         {
55                                 dest->SetMode('G',true);
56                                 return MODEACTION_ALLOW;
57                         }
58                 }
59                 else
60                 {
61                         if (dest->IsModeSet('G'))
62                         {
63                                 dest->SetMode('G',false);
64                                 return MODEACTION_ALLOW;
65                         }
66                 }
67
68                 return MODEACTION_DENY;
69         }
70 };
71
72 class CensorChannel : public ModeHandler
73 {
74  public:
75         CensorChannel() : ModeHandler('G', 0, 0, false, MODETYPE_CHANNEL, false) { }
76
77         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
78         {
79                 if (adding)
80                 {
81                         if (!channel->IsModeSet('G'))
82                         {
83                                 channel->SetMode('G',false);
84                                 return MODEACTION_ALLOW;
85                         }
86                 }
87                 else
88                 {
89                         if (channel->IsModeSet('G'))
90                         {
91                                 channel->SetMode('G',false);
92                                 return MODEACTION_ALLOW;
93                         }
94                 }
95
96                 return MODEACTION_ALLOW;
97         }
98 };
99
100 class ModuleCensor : public Module
101 {
102
103         Server *Srv;
104         censor_t censors;
105         CensorUser *cu;
106         CensorChannel *cc;
107  
108  public:
109         ModuleCensor(Server* Me)
110                 : Module::Module(Me)
111         {
112                 /*
113                  * read the configuration file on startup.
114                  * it is perfectly valid to set <censor file> to the value of the
115                  * main config file, then append your <badword> tags to the bottom
116                  * of the main config... but rather messy. That's why the capability
117                  * of using a seperate config file is provided.
118                  *
119                  * XXX - Really, it'd be nice to scraip this kind of thing, and have something like
120                  * an include directive to include additional configuration files. Might make our lives easier. --w00t
121                  *
122                  * XXX - These module pre-date the include directive which exists since beta 5 -- Brain
123                  */
124                 Srv = Me;
125                 OnRehash("");
126                 cu = new CensorUser;
127                 cc = new CensorChannel;
128                 Srv->AddMode(cu, 'G');
129                 Srv->AddMode(cc, 'G');
130         }
131
132         void Implements(char* List)
133         {
134                 List[I_OnRehash] = List[I_On005Numeric] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
135         }
136
137
138         virtual void On005Numeric(std::string &output)
139         {
140                 InsertMode(output,"G",4);
141         }
142  
143         virtual ~ModuleCensor()
144         {
145                 DELETE(cu);
146                 DELETE(cc);
147         }
148         
149         virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
150         {
151                 if ((pattern != "") && (text != ""))
152                 {
153                         while (text.find(pattern) != irc::string::npos)
154                         {
155                                 int pos = text.find(pattern);
156                                 text.erase(pos,pattern.length());
157                                 text.insert(pos,replace);
158                         }
159                 }
160         }
161         
162         // format of a config entry is <badword text="shit" replace="poo">
163         
164         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
165         {
166                 bool active = false;
167                 irc::string text2 = text.c_str();
168                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
169                 { 
170                         if (text2.find(index->first) != irc::string::npos)
171                         {
172                                 if (target_type == TYPE_USER)
173                                 {
174                                         userrec* t = (userrec*)dest;
175                                         active = t->IsModeSet('G');
176                                 }
177                                 else if (target_type == TYPE_CHANNEL)
178                                 {
179                                         chanrec* t = (chanrec*)dest;
180                                         active = t->IsModeSet('G');
181                                 }
182                                 
183                                 if (active)
184                                 {
185                                         this->ReplaceLine(text2,index->first,index->second);
186                                         text = text2.c_str();
187                                 }
188                         }
189                 }
190                 return 0;
191         }
192         
193         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
194         {
195                 return OnUserPreMessage(user,dest,target_type,text,status);
196         }
197         
198         virtual void OnRehash(const std::string &parameter)
199         {
200                 /*
201                  * reload our config file on rehash - we must destroy and re-allocate the classes
202                  * to call the constructor again and re-read our data.
203                  */
204                 ConfigReader* Conf = new ConfigReader;
205                 std::string Censorfile = Conf->ReadValue("censor","file",0);
206                 // this automatically re-reads the configuration file into the class
207                 ConfigReader* MyConf = new ConfigReader(Censorfile);
208                 if ((Censorfile == "") || (!MyConf->Verify()))
209                 {
210                         CensorException e;
211                         throw(e);
212                 }
213                 censors.clear();
214                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
215                 {
216                         irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
217                         irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
218                         censors[pattern] = replace;
219                 }
220                 DELETE(Conf);
221                 DELETE(MyConf);
222         }
223         
224         virtual Version GetVersion()
225         {
226                 // This is version 2 because version 1.x is the unreleased unrealircd module
227                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
228         }
229         
230 };
231
232 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
233
234 class ModuleCensorFactory : public ModuleFactory
235 {
236  public:
237         ModuleCensorFactory()
238         {
239         }
240         
241         ~ModuleCensorFactory()
242         {
243         }
244         
245         virtual Module * CreateModule(Server* Me)
246         {
247                 return new ModuleCensor(Me);
248         }
249         
250 };
251
252
253 extern "C" void * init_module( void )
254 {
255         return new ModuleCensorFactory;
256 }