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