]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
More typo fixes
[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 typedef std::map<irc::string,irc::string> censor_t;
27
28 /* $ModDesc: Provides user and channel +G mode */
29
30 class CensorException : public ModuleException
31 {
32  public:
33         virtual const char* GetReason()
34         {
35                 return "Could not find <censor file=\"\"> definition in your config file!";
36         }
37 };
38
39 class CensorUser : public ModeHandler
40 {
41  public:
42         CensorUser() : ModeHandler('G', 0, 0, false, MODETYPE_USER, false) { }
43
44         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
45         {
46                 if (adding)
47                 {
48                         if (!dest->IsModeSet('G'))
49                         {
50                                 dest->SetMode('G',true);
51                                 return MODEACTION_ALLOW;
52                         }
53                 }
54                 else
55                 {
56                         if (dest->IsModeSet('G'))
57                         {
58                                 dest->SetMode('G',false);
59                                 return MODEACTION_ALLOW;
60                         }
61                 }
62
63                 return MODEACTION_DENY;
64         }
65 };
66
67 class CensorChannel : public ModeHandler
68 {
69  public:
70         CensorChannel() : ModeHandler('G', 0, 0, false, MODETYPE_CHANNEL, false) { }
71
72         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
73         {
74                 if (adding)
75                 {
76                         if (!channel->IsModeSet('G'))
77                         {
78                                 channel->SetMode('G',false);
79                                 return MODEACTION_ALLOW;
80                         }
81                 }
82                 else
83                 {
84                         if (channel->IsModeSet('G'))
85                         {
86                                 channel->SetMode('G',false);
87                                 return MODEACTION_ALLOW;
88                         }
89                 }
90
91                 return MODEACTION_ALLOW;
92         }
93 };
94
95 class ModuleCensor : public Module
96 {
97
98         Server *Srv;
99         censor_t censors;
100         CensorUser *cu;
101         CensorChannel *cc;
102  
103  public:
104         ModuleCensor(Server* Me)
105                 : Module::Module(Me)
106         {
107                 /*
108                  * read the configuration file on startup.
109                  * it is perfectly valid to set <censor file> to the value of the
110                  * main config file, then append your <badword> tags to the bottom
111                  * of the main config... but rather messy. That's why the capability
112                  * of using a seperate config file is provided.
113                  *
114                  * XXX - Really, it'd be nice to scraip this kind of thing, and have something like
115                  * an include directive to include additional configuration files. Might make our lives easier. --w00t
116                  *
117                  * XXX - These module pre-date the include directive which exists since beta 5 -- Brain
118                  */
119                 Srv = Me;
120                 OnRehash("");
121                 cu = new CensorUser;
122                 cc = new CensorChannel;
123                 Srv->AddMode(cu, 'G');
124                 Srv->AddMode(cc, 'G');
125         }
126
127         void Implements(char* List)
128         {
129                 List[I_OnRehash] = List[I_On005Numeric] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
130         }
131
132
133         virtual void On005Numeric(std::string &output)
134         {
135                 InsertMode(output,"G",4);
136         }
137  
138         virtual ~ModuleCensor()
139         {
140                 DELETE(cu);
141                 DELETE(cc);
142         }
143         
144         virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
145         {
146                 if ((pattern != "") && (text != ""))
147                 {
148                         while (text.find(pattern) != irc::string::npos)
149                         {
150                                 int pos = text.find(pattern);
151                                 text.erase(pos,pattern.length());
152                                 text.insert(pos,replace);
153                         }
154                 }
155         }
156         
157         // format of a config entry is <badword text="shit" replace="poo">
158         
159         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
160         {
161                 bool active = false;
162                 irc::string text2 = text.c_str();
163                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
164                 { 
165                         if (text2.find(index->first) != irc::string::npos)
166                         {
167                                 if (target_type == TYPE_USER)
168                                 {
169                                         userrec* t = (userrec*)dest;
170                                         active = t->IsModeSet('G');
171                                 }
172                                 else if (target_type == TYPE_CHANNEL)
173                                 {
174                                         chanrec* t = (chanrec*)dest;
175                                         active = t->IsModeSet('G');
176                                 }
177                                 
178                                 if (active)
179                                 {
180                                         this->ReplaceLine(text2,index->first,index->second);
181                                         text = text2.c_str();
182                                 }
183                         }
184                 }
185                 return 0;
186         }
187         
188         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
189         {
190                 return OnUserPreMessage(user,dest,target_type,text,status);
191         }
192         
193         virtual void OnRehash(const std::string &parameter)
194         {
195                 /*
196                  * reload our config file on rehash - we must destroy and re-allocate the classes
197                  * to call the constructor again and re-read our data.
198                  */
199                 ConfigReader* Conf = new ConfigReader;
200                 std::string Censorfile = Conf->ReadValue("censor","file",0);
201                 // this automatically re-reads the configuration file into the class
202                 ConfigReader* MyConf = new ConfigReader(Censorfile);
203                 if ((Censorfile == "") || (!MyConf->Verify()))
204                 {
205                         CensorException e;
206                         throw(e);
207                 }
208                 censors.clear();
209                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
210                 {
211                         irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
212                         irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
213                         censors[pattern] = replace;
214                 }
215                 DELETE(Conf);
216                 DELETE(MyConf);
217         }
218         
219         virtual Version GetVersion()
220         {
221                 // This is version 2 because version 1.x is the unreleased unrealircd module
222                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
223         }
224         
225 };
226
227 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
228
229 class ModuleCensorFactory : public ModuleFactory
230 {
231  public:
232         ModuleCensorFactory()
233         {
234         }
235         
236         ~ModuleCensorFactory()
237         {
238         }
239         
240         virtual Module * CreateModule(Server* Me)
241         {
242                 return new ModuleCensor(Me);
243         }
244         
245 };
246
247
248 extern "C" void * init_module( void )
249 {
250         return new ModuleCensorFactory;
251 }