]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
45f5af551a80ccf9abc668f13156175ebd4ab1c6
[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
26 /* $ModDesc: Provides user and channel +G mode */
27
28 class ModuleCensor : public Module
29 {
30  Server *Srv;
31  ConfigReader *Conf, *MyConf;
32  
33  public:
34         ModuleCensor(Server* Me)
35                 : Module::Module(Me)
36         {
37                 /*
38                  * read the configuration file on startup.
39                  * it is perfectly valid to set <censor file> to the value of the
40                  * main config file, then append your <badword> tags to the bottom
41                  * of the main config... but rather messy. That's why the capability
42                  * of using a seperate config file is provided.
43                  *
44                  * XXX - Really, it'd be nice to scraip this kind of thing, and have something like
45                  * an include directive to include additional configuration files. Might make our lives easier. --w00t
46                  *
47                  * XXX - These module pre-date the include directive which exists since beta 5 -- Brain
48                  */
49                 Srv = Me;
50                 Conf = new ConfigReader;
51                 std::string Censorfile = Conf->ReadValue("censor","file",0);
52                 MyConf = new ConfigReader(Censorfile);
53                 if ((Censorfile == "") || (!MyConf->Verify()))
54                 {
55                         printf("Error, could not find <censor file=\"\"> definition in your config file!");
56                         log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
57                         return;
58                 }
59                 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
60                 Srv->AddExtendedMode('G',MT_CHANNEL,false,0,0);
61                 Srv->AddExtendedMode('G',MT_CLIENT,false,0,0);
62         }
63
64         void Implements(char* List)
65         {
66                 List[I_OnRehash] = List[I_On005Numeric] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnExtendedMode] = 1;
67         }
68
69
70         virtual void On005Numeric(std::string &output)
71         {
72                 InsertMode(output,"G",4);
73         }
74
75
76         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
77         {
78                 // check if this is our mode character...
79                 if (modechar == 'G')
80                 {
81                         return 1;
82                 }
83                 else
84                 {
85                         return 0;
86                 }
87         }
88         
89         virtual ~ModuleCensor()
90         {
91                 delete MyConf;
92                 delete Conf;
93         }
94         
95         virtual void ReplaceLine(std::string &text,std::string pattern, std::string replace)
96         {
97                 if ((pattern != "") && (text != ""))
98                 {
99                         while (text.find(pattern) != std::string::npos)
100                         {
101                                 int pos = text.find(pattern);
102                                 text.erase(pos,pattern.length());
103                                 text.insert(pos,replace);
104                         }
105                 }
106         }
107         
108         // format of a config entry is <badword text="shit" replace="poo">
109         
110         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
111         {
112                 bool active = false;
113                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
114                 {
115                         std::string pattern = MyConf->ReadValue("badword","text",index);
116                         if (text.find(pattern) != std::string::npos)
117                         {
118                                 std::string replace = MyConf->ReadValue("badword","replace",index);
119
120                                 if (target_type == TYPE_USER)
121                                 {
122                                         userrec* t = (userrec*)dest;
123                                         active = (strchr(t->modes,'G') > 0);
124                                 }
125                                 else if (target_type == TYPE_CHANNEL)
126                                 {
127                                         chanrec* t = (chanrec*)dest;
128                                         active = (t->IsCustomModeSet('G'));
129                                 }
130                                 
131                                 if (active)
132                                 {
133                                         this->ReplaceLine(text,pattern,replace);
134                                 }
135                         }
136                 }
137                 return 0;
138         }
139         
140         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
141         {
142                 bool active = false;
143                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
144                 {
145                         std::string pattern = MyConf->ReadValue("badword","text",index);
146                         if (text.find(pattern) != std::string::npos)
147                         {
148                                 std::string replace = MyConf->ReadValue("badword","replace",index);
149
150                                 if (target_type == TYPE_USER)
151                                 {
152                                         userrec* t = (userrec*)dest;
153                                         active = (strchr(t->modes,'G') > 0);
154                                 }
155                                 else if (target_type == TYPE_CHANNEL)
156                                 {
157                                         chanrec* t = (chanrec*)dest;
158                                         active = (t->IsCustomModeSet('G'));
159                                 }
160                                 
161                                 if (active)
162                                 {
163                                         this->ReplaceLine(text,pattern,replace);
164                                 }
165                         }
166                 }
167                 return 0;
168         }
169         
170         virtual void OnRehash(std::string parameter)
171         {
172                 /*
173                  * reload our config file on rehash - we must destroy and re-allocate the classes
174                  * to call the constructor again and re-read our data.
175                  */
176                 delete Conf;
177                 delete MyConf;
178                 Conf = new ConfigReader;
179                 std::string Censorfile = Conf->ReadValue("censor","file",0);
180                 // this automatically re-reads the configuration file into the class
181                 MyConf = new ConfigReader(Censorfile);
182                 if ((Censorfile == "") || (!MyConf->Verify()))
183                 {
184                         // bail if the user forgot to create a config file
185                         printf("Error, could not find <censor file=\"\"> definition in your config file!\n");
186                         log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
187                 }
188                 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
189         }
190         
191         virtual Version GetVersion()
192         {
193                 // This is version 2 because version 1.x is the unreleased unrealircd module
194                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
195         }
196         
197 };
198
199 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
200
201 class ModuleCensorFactory : public ModuleFactory
202 {
203  public:
204         ModuleCensorFactory()
205         {
206         }
207         
208         ~ModuleCensorFactory()
209         {
210         }
211         
212         virtual Module * CreateModule(Server* Me)
213         {
214                 return new ModuleCensor(Me);
215         }
216         
217 };
218
219
220 extern "C" void * init_module( void )
221 {
222         return new ModuleCensorFactory;
223 }
224