]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
Fixed stupid typo again
[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                         exit(0);
46                 }
47                 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
48                 Srv->AddExtendedMode('G',MT_CHANNEL,false,0,0);
49                 Srv->AddExtendedMode('G',MT_CLIENT,false,0,0);
50         }
51
52         virtual void On005Numeric(std::string &output)
53         {
54                 std::stringstream line(output);
55                 std::string temp1, temp2;
56                 while (!line.eof())
57                 {
58                         line >> temp1;
59                         if (temp1.substr(0,10) == "CHANMODES=")
60                         {
61                                 // append the chanmode to the end
62                                 temp1 = temp1.substr(10,temp1.length());
63                                 temp1 = "CHANMODES=" + temp1 + "G";
64                         }
65                         temp2 = temp2 + temp1 + " ";
66                 }
67                 if (temp2.length())
68                         output = temp2.substr(0,temp2.length()-1);
69         }
70
71
72         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
73         {
74                 // check if this is our mode character...
75                 if (modechar == 'G')
76                 {
77                         return 1;
78                 }
79                 else
80                 {
81                         return 0;
82                 }
83         }
84         
85         virtual ~ModuleCensor()
86         {
87                 delete Srv;
88                 delete MyConf;
89                 delete Conf;
90         }
91         
92         virtual void ReplaceLine(std::string &text,std::string pattern, std::string replace)
93         {
94                 if ((pattern != "") && (text != ""))
95                 {
96                         while (strstr(text.c_str(),pattern.c_str()))
97                         {
98                                 int pos = text.find(pattern);
99                                 text.erase(pos,pattern.length());
100                                 text.insert(pos,replace);
101                         }
102                 }
103         }
104         
105         // format of a config entry is <badword text="shit" replace="poo">
106         
107         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
108         {
109                 bool active = false;
110                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
111                 {
112                         std::string pattern = MyConf->ReadValue("badword","text",index);
113                         if (strstr(text.c_str(),pattern.c_str()))
114                         {
115                                 std::string replace = MyConf->ReadValue("badword","replace",index);
116
117                                 if (target_type == TYPE_USER)
118                                 {
119                                         userrec* t = (userrec*)dest;
120                                         active = (strchr(t->modes,'G') > 0);
121                                 }
122                                 else if (target_type == TYPE_CHANNEL)
123                                 {
124                                         chanrec* t = (chanrec*)dest;
125                                         active = (t->IsCustomModeSet('G'));
126                                 }
127                                 
128                                 if (active)
129                                 {
130                                         this->ReplaceLine(text,pattern,replace);
131                                 }
132                                 
133                                 return 0;
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 (strstr(text.c_str(),pattern.c_str()))
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                                 return 0;
166                         }
167                 }
168                 return 0;
169         }
170         
171         virtual void OnRehash()
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                         exit(0);
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);
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