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