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