]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
c3e52899aa359b132f38ba55d8671979b9de9c6d
[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 #include <stdio.h>
18 #include <string>
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22
23 /* $ModDesc: Provides user and channel +G mode */
24
25 class ModuleCensor : public Module
26 {
27  Server *Srv;
28  ConfigReader *Conf, *MyConf;
29  
30  public:
31         ModuleCensor()
32         {
33                 // read the configuration file on startup.
34                 // it is perfectly valid to set <censor file> to the value of the
35                 // main config file, then append your <badword> tags to the bottom
36                 // of the main config... but rather messy. That's why the capability
37                 // of using a seperate config file is provided.
38                 Srv = new Server;
39                 Conf = new ConfigReader;
40                 std::string Censorfile = Conf->ReadValue("censor","file",0);
41                 MyConf = new ConfigReader(Censorfile);
42                 if ((Censorfile == "") || (!MyConf->Verify()))
43                 {
44                         printf("Error, could not find <censor file=\"\"> definition in your config file!");
45                         log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
46                         return;
47                 }
48                 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
49                 Srv->AddExtendedMode('G',MT_CHANNEL,false,0,0);
50                 Srv->AddExtendedMode('G',MT_CLIENT,false,0,0);
51         }
52
53         virtual void On005Numeric(std::string &output)
54         {
55                 std::stringstream line(output);
56                 std::string temp1, temp2;
57                 while (!line.eof())
58                 {
59                         line >> temp1;
60                         if (temp1.substr(0,10) == "CHANMODES=")
61                         {
62                                 // append the chanmode to the end
63                                 temp1 = temp1.substr(10,temp1.length());
64                                 temp1 = "CHANMODES=" + temp1 + "G";
65                         }
66                         temp2 = temp2 + temp1 + " ";
67                 }
68                 if (temp2.length())
69                         output = temp2.substr(0,temp2.length()-1);
70         }
71
72
73         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
74         {
75                 // check if this is our mode character...
76                 if (modechar == 'G')
77                 {
78                         return 1;
79                 }
80                 else
81                 {
82                         return 0;
83                 }
84         }
85         
86         virtual ~ModuleCensor()
87         {
88                 delete Srv;
89                 delete MyConf;
90                 delete Conf;
91         }
92         
93         virtual void ReplaceLine(std::string &text,std::string pattern, std::string replace)
94         {
95                 if ((pattern != "") && (text != ""))
96                 {
97                         while (strstr(text.c_str(),pattern.c_str()))
98                         {
99                                 int pos = text.find(pattern);
100                                 text.erase(pos,pattern.length());
101                                 text.insert(pos,replace);
102                         }
103                 }
104         }
105         
106         // format of a config entry is <badword text="shit" replace="poo">
107         
108         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
109         {
110                 bool active = false;
111                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
112                 {
113                         std::string pattern = MyConf->ReadValue("badword","text",index);
114                         if (strstr(text.c_str(),pattern.c_str()))
115                         {
116                                 std::string replace = MyConf->ReadValue("badword","replace",index);
117
118                                 if (target_type == TYPE_USER)
119                                 {
120                                         userrec* t = (userrec*)dest;
121                                         active = (strchr(t->modes,'G') > 0);
122                                 }
123                                 else if (target_type == TYPE_CHANNEL)
124                                 {
125                                         chanrec* t = (chanrec*)dest;
126                                         active = (t->IsCustomModeSet('G'));
127                                 }
128                                 
129                                 if (active)
130                                 {
131                                         this->ReplaceLine(text,pattern,replace);
132                                 }
133                                 
134                                 return 0;
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 (strstr(text.c_str(),pattern.c_str()))
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                                 return 0;
167                         }
168                 }
169                 return 0;
170         }
171         
172         virtual void OnRehash()
173         {
174                 // reload our config file on rehash - we must destroy and re-allocate the classes
175                 // to call the constructor again and re-read our data.
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!");
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()
213         {
214                 return new ModuleCensor;
215         }
216         
217 };
218
219
220 extern "C" void * init_module( void )
221 {
222         return new ModuleCensorFactory;
223 }
224