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