]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
40e4ee92bbb59cdefd9f93d7d8457172da61e1b6
[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 int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
53         {
54                 // check if this is our mode character...
55                 if (modechar == 'G')
56                 {
57                         return 1;
58                 }
59                 else
60                 {
61                         return 0;
62                 }
63         }
64         
65         virtual ~ModuleCensor()
66         {
67                 delete Srv;
68                 delete MyConf;
69                 delete Conf;
70         }
71         
72         virtual void ReplaceLine(std::string &text,std::string pattern, std::string replace)
73         {
74                 while (strstr(text.c_str(),pattern.c_str()))
75                 {
76                         int pos = text.find(pattern);
77                         text.erase(pos,pattern.length());
78                         text.insert(pos,replace);
79                 }
80         }
81         
82         // format of a config entry is <badword text="shit" replace="poo">
83         
84         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
85         {
86                 bool active = false;
87                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
88                 {
89                         std::string pattern = MyConf->ReadValue("badword","text",index);
90                         if (strstr(text.c_str(),pattern.c_str()))
91                         {
92                                 std::string replace = MyConf->ReadValue("badword","replace",index);
93
94                                 if (target_type == TYPE_USER)
95                                 {
96                                         userrec* t = (userrec*)dest;
97                                         active = (strchr(t->modes,'G') > 0);
98                                 }
99                                 else if (target_type == TYPE_CHANNEL)
100                                 {
101                                         chanrec* t = (chanrec*)dest;
102                                         active = (t->IsCustomModeSet('G'));
103                                 }
104                                 
105                                 if (active)
106                                 {
107                                         this->ReplaceLine(text,pattern,replace);
108                                 }
109                                 
110                                 return 0;
111                         }
112                 }
113                 return 0;
114         }
115         
116         virtual int OnUserPreNotice(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 (strstr(text.c_str(),pattern.c_str()))
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                                 return 0;
143                         }
144                 }
145                 return 0;
146         }
147         
148         virtual void OnRehash()
149         {
150                 // reload our config file on rehash - we must destroy and re-allocate the classes
151                 // to call the constructor again and re-read our data.
152                 delete Conf;
153                 delete MyConf;
154                 Conf = new ConfigReader;
155                 std::string Censorfile = Conf->ReadValue("censor","file",0);
156                 // this automatically re-reads the configuration file into the class
157                 MyConf = new ConfigReader(Censorfile);
158                 if ((Censorfile == "") || (!MyConf->Verify()))
159                 {
160                         // bail if the user forgot to create a config file
161                         printf("Error, could not find <censor file=\"\"> definition in your config file!");
162                         exit(0);
163                 }
164                 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
165         }
166         
167         virtual Version GetVersion()
168         {
169                 // This is version 2 because version 1.x is the unreleased unrealircd module
170                 return Version(1,0,0,0);
171         }
172         
173 };
174
175 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
176
177 class ModuleCensorFactory : public ModuleFactory
178 {
179  public:
180         ModuleCensorFactory()
181         {
182         }
183         
184         ~ModuleCensorFactory()
185         {
186         }
187         
188         virtual Module * CreateModule()
189         {
190                 return new ModuleCensor;
191         }
192         
193 };
194
195
196 extern "C" void * init_module( void )
197 {
198         return new ModuleCensorFactory;
199 }
200