]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
Review and optimize
[user/henk/code/inspircd.git] / src / modules / m_censor.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 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         virtual void On005Numeric(std::string &output)
65         {
66                 std::stringstream line(output);
67                 std::string temp1, temp2;
68                 while (!line.eof())
69                 {
70                         line >> temp1;
71                         if (temp1.substr(0,10) == "CHANMODES=")
72                         {
73                                 // append the chanmode to the end
74                                 temp1 = temp1.substr(10,temp1.length());
75                                 temp1 = "CHANMODES=" + temp1 + "G";
76                         }
77                         temp2 = temp2 + temp1 + " ";
78                 }
79                 if (temp2.length())
80                         output = temp2.substr(0,temp2.length()-1);
81         }
82
83
84         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
85         {
86                 // check if this is our mode character...
87                 if (modechar == 'G')
88                 {
89                         return 1;
90                 }
91                 else
92                 {
93                         return 0;
94                 }
95         }
96         
97         virtual ~ModuleCensor()
98         {
99                 delete MyConf;
100                 delete Conf;
101         }
102         
103         virtual void ReplaceLine(std::string &text,std::string pattern, std::string replace)
104         {
105                 if ((pattern != "") && (text != ""))
106                 {
107                         while (text.find(pattern) != std::string::npos)
108                         {
109                                 int pos = text.find(pattern);
110                                 text.erase(pos,pattern.length());
111                                 text.insert(pos,replace);
112                         }
113                 }
114         }
115         
116         // format of a config entry is <badword text="shit" replace="poo">
117         
118         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
119         {
120                 bool active = false;
121                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
122                 {
123                         std::string pattern = MyConf->ReadValue("badword","text",index);
124                         if (text.find(pattern) != std::string::npos)
125                         {
126                                 std::string replace = MyConf->ReadValue("badword","replace",index);
127
128                                 if (target_type == TYPE_USER)
129                                 {
130                                         userrec* t = (userrec*)dest;
131                                         active = (strchr(t->modes,'G') > 0);
132                                 }
133                                 else if (target_type == TYPE_CHANNEL)
134                                 {
135                                         chanrec* t = (chanrec*)dest;
136                                         active = (t->IsCustomModeSet('G'));
137                                 }
138                                 
139                                 if (active)
140                                 {
141                                         this->ReplaceLine(text,pattern,replace);
142                                 }
143                         }
144                 }
145                 return 0;
146         }
147         
148         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
149         {
150                 bool active = false;
151                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
152                 {
153                         std::string pattern = MyConf->ReadValue("badword","text",index);
154                         if (text.find(pattern) != std::string::npos)
155                         {
156                                 std::string replace = MyConf->ReadValue("badword","replace",index);
157
158                                 if (target_type == TYPE_USER)
159                                 {
160                                         userrec* t = (userrec*)dest;
161                                         active = (strchr(t->modes,'G') > 0);
162                                 }
163                                 else if (target_type == TYPE_CHANNEL)
164                                 {
165                                         chanrec* t = (chanrec*)dest;
166                                         active = (t->IsCustomModeSet('G'));
167                                 }
168                                 
169                                 if (active)
170                                 {
171                                         this->ReplaceLine(text,pattern,replace);
172                                 }
173                         }
174                 }
175                 return 0;
176         }
177         
178         virtual void OnRehash(std::string parameter)
179         {
180                 /*
181                  * reload our config file on rehash - we must destroy and re-allocate the classes
182                  * to call the constructor again and re-read our data.
183                  */
184                 delete Conf;
185                 delete MyConf;
186                 Conf = new ConfigReader;
187                 std::string Censorfile = Conf->ReadValue("censor","file",0);
188                 // this automatically re-reads the configuration file into the class
189                 MyConf = new ConfigReader(Censorfile);
190                 if ((Censorfile == "") || (!MyConf->Verify()))
191                 {
192                         // bail if the user forgot to create a config file
193                         printf("Error, could not find <censor file=\"\"> definition in your config file!");
194                         log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
195                 }
196                 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
197         }
198         
199         virtual Version GetVersion()
200         {
201                 // This is version 2 because version 1.x is the unreleased unrealircd module
202                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
203         }
204         
205 };
206
207 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
208
209 class ModuleCensorFactory : public ModuleFactory
210 {
211  public:
212         ModuleCensorFactory()
213         {
214         }
215         
216         ~ModuleCensorFactory()
217         {
218         }
219         
220         virtual Module * CreateModule(Server* Me)
221         {
222                 return new ModuleCensor(Me);
223         }
224         
225 };
226
227
228 extern "C" void * init_module( void )
229 {
230         return new ModuleCensorFactory;
231 }
232