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