]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
acc93fa6c7658ad22f18ca2c4766912acdb2f51f
[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 "inspircd.h"
25
26 typedef std::map<irc::string,irc::string> censor_t;
27
28 /* $ModDesc: Provides user and channel +G mode */
29
30
31
32 class CensorException : public ModuleException
33 {
34  public:
35         virtual const char* GetReason()
36         {
37                 return "Could not find <censor file=\"\"> definition in your config file!";
38         }
39 };
40
41 class CensorUser : public ModeHandler
42 {
43  public:
44         CensorUser(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_USER, false) { }
45
46         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
47         {
48                 /* Only opers can change other users modes */
49                 if ((source != dest) && (!*source->oper))
50                         return MODEACTION_DENY;
51
52                 if (adding)
53                 {
54                         if (!dest->IsModeSet('G'))
55                         {
56                                 dest->SetMode('G',true);
57                                 return MODEACTION_ALLOW;
58                         }
59                 }
60                 else
61                 {
62                         if (dest->IsModeSet('G'))
63                         {
64                                 dest->SetMode('G',false);
65                                 return MODEACTION_ALLOW;
66                         }
67                 }
68
69                 return MODEACTION_DENY;
70         }
71 };
72
73 class CensorChannel : public ModeHandler
74 {
75  public:
76         CensorChannel(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_CHANNEL, false) { }
77
78         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
79         {
80                 if (adding)
81                 {
82                         if (!channel->IsModeSet('G'))
83                         {
84                                 channel->SetMode('G',false);
85                                 return MODEACTION_ALLOW;
86                         }
87                 }
88                 else
89                 {
90                         if (channel->IsModeSet('G'))
91                         {
92                                 channel->SetMode('G',false);
93                                 return MODEACTION_ALLOW;
94                         }
95                 }
96
97                 return MODEACTION_ALLOW;
98         }
99 };
100
101 class ModuleCensor : public Module
102 {
103
104         
105         censor_t censors;
106         CensorUser *cu;
107         CensorChannel *cc;
108  
109  public:
110         ModuleCensor(InspIRCd* Me)
111                 : Module::Module(Me)
112         {
113                 /*
114                  * read the configuration file on startup.
115                  * it is perfectly valid to set <censor file> to the value of the
116                  * main config file, then append your <badword> tags to the bottom
117                  * of the main config... but rather messy. That's why the capability
118                  * of using a seperate config file is provided.
119                  *
120                  * XXX - Really, it'd be nice to scraip this kind of thing, and have something like
121                  * an include directive to include additional configuration files. Might make our lives easier. --w00t
122                  *
123                  * XXX - These module pre-date the include directive which exists since beta 5 -- Brain
124                  */
125                 
126                 OnRehash("");
127                 cu = new CensorUser(ServerInstance);
128                 cc = new CensorChannel(ServerInstance);
129                 ServerInstance->AddMode(cu, 'G');
130                 ServerInstance->AddMode(cc, 'G');
131         }
132
133         void Implements(char* List)
134         {
135                 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
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(ServerInstance);
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(ServerInstance, 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(InspIRCd* 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 }