]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
94dbec85226da49f552e102669acc99d906ef210
[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()
35         {
36                 // read the configuration file on startup.
37                 // it is perfectly valid to set <censor file> to the value of the
38                 // main config file, then append your <badword> tags to the bottom
39                 // of the main config... but rather messy. That's why the capability
40                 // of using a seperate config file is provided.
41                 Srv = new Server;
42                 Conf = new ConfigReader;
43                 std::string Censorfile = Conf->ReadValue("censor","file",0);
44                 MyConf = new ConfigReader(Censorfile);
45                 if ((Censorfile == "") || (!MyConf->Verify()))
46                 {
47                         printf("Error, could not find <censor file=\"\"> definition in your config file!");
48                         log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
49                         return;
50                 }
51                 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
52                 Srv->AddExtendedMode('G',MT_CHANNEL,false,0,0);
53                 Srv->AddExtendedMode('G',MT_CLIENT,false,0,0);
54         }
55
56         virtual void On005Numeric(std::string &output)
57         {
58                 std::stringstream line(output);
59                 std::string temp1, temp2;
60                 while (!line.eof())
61                 {
62                         line >> temp1;
63                         if (temp1.substr(0,10) == "CHANMODES=")
64                         {
65                                 // append the chanmode to the end
66                                 temp1 = temp1.substr(10,temp1.length());
67                                 temp1 = "CHANMODES=" + temp1 + "G";
68                         }
69                         temp2 = temp2 + temp1 + " ";
70                 }
71                 if (temp2.length())
72                         output = temp2.substr(0,temp2.length()-1);
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 Srv;
92                 delete MyConf;
93                 delete Conf;
94         }
95         
96         virtual void ReplaceLine(std::string &text,std::string pattern, std::string replace)
97         {
98                 if ((pattern != "") && (text != ""))
99                 {
100                         while (text.find(pattern) != std::string::npos)
101                         {
102                                 int pos = text.find(pattern);
103                                 text.erase(pos,pattern.length());
104                                 text.insert(pos,replace);
105                         }
106                 }
107         }
108         
109         // format of a config entry is <badword text="shit" replace="poo">
110         
111         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
112         {
113                 bool active = false;
114                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
115                 {
116                         std::string pattern = MyConf->ReadValue("badword","text",index);
117                         if (text.find(pattern) != std::string::npos)
118                         {
119                                 std::string replace = MyConf->ReadValue("badword","replace",index);
120
121                                 if (target_type == TYPE_USER)
122                                 {
123                                         userrec* t = (userrec*)dest;
124                                         active = (strchr(t->modes,'G') > 0);
125                                 }
126                                 else if (target_type == TYPE_CHANNEL)
127                                 {
128                                         chanrec* t = (chanrec*)dest;
129                                         active = (t->IsCustomModeSet('G'));
130                                 }
131                                 
132                                 if (active)
133                                 {
134                                         this->ReplaceLine(text,pattern,replace);
135                                 }
136                         }
137                 }
138                 return 0;
139         }
140         
141         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
142         {
143                 bool active = false;
144                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
145                 {
146                         std::string pattern = MyConf->ReadValue("badword","text",index);
147                         if (text.find(pattern) != std::string::npos)
148                         {
149                                 std::string replace = MyConf->ReadValue("badword","replace",index);
150
151                                 if (target_type == TYPE_USER)
152                                 {
153                                         userrec* t = (userrec*)dest;
154                                         active = (strchr(t->modes,'G') > 0);
155                                 }
156                                 else if (target_type == TYPE_CHANNEL)
157                                 {
158                                         chanrec* t = (chanrec*)dest;
159                                         active = (t->IsCustomModeSet('G'));
160                                 }
161                                 
162                                 if (active)
163                                 {
164                                         this->ReplaceLine(text,pattern,replace);
165                                 }
166                         }
167                 }
168                 return 0;
169         }
170         
171         virtual void OnRehash(std::string parameter)
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                 delete Conf;
176                 delete MyConf;
177                 Conf = new ConfigReader;
178                 std::string Censorfile = Conf->ReadValue("censor","file",0);
179                 // this automatically re-reads the configuration file into the class
180                 MyConf = new ConfigReader(Censorfile);
181                 if ((Censorfile == "") || (!MyConf->Verify()))
182                 {
183                         // bail if the user forgot to create a config file
184                         printf("Error, could not find <censor file=\"\"> definition in your config file!");
185                         log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
186                 }
187                 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
188         }
189         
190         virtual Version GetVersion()
191         {
192                 // This is version 2 because version 1.x is the unreleased unrealircd module
193                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
194         }
195         
196 };
197
198 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
199
200 class ModuleCensorFactory : public ModuleFactory
201 {
202  public:
203         ModuleCensorFactory()
204         {
205         }
206         
207         ~ModuleCensorFactory()
208         {
209         }
210         
211         virtual Module * CreateModule()
212         {
213                 return new ModuleCensor;
214         }
215         
216 };
217
218
219 extern "C" void * init_module( void )
220 {
221         return new ModuleCensorFactory;
222 }
223