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