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