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