]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
bbf14865758fcf2d4d1114e73d9b2ad1522c9c4b
[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
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         }
143  
144         virtual ~ModuleCensor()
145         {
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         
165         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
166         {
167                 bool active = false;
168                 irc::string text2 = text.c_str();
169                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
170                 { 
171                         if (text2.find(index->first) != irc::string::npos)
172                         {
173                                 if (target_type == TYPE_USER)
174                                 {
175                                         userrec* t = (userrec*)dest;
176                                         active = t->IsModeSet('G');
177                                 }
178                                 else if (target_type == TYPE_CHANNEL)
179                                 {
180                                         chanrec* t = (chanrec*)dest;
181                                         active = t->IsModeSet('G');
182                                 }
183                                 
184                                 if (active)
185                                 {
186                                         this->ReplaceLine(text2,index->first,index->second);
187                                         text = text2.c_str();
188                                 }
189                         }
190                 }
191                 return 0;
192         }
193         
194         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
195         {
196                 return OnUserPreMessage(user,dest,target_type,text,status);
197         }
198         
199         virtual void OnRehash(const std::string &parameter)
200         {
201                 /*
202                  * reload our config file on rehash - we must destroy and re-allocate the classes
203                  * to call the constructor again and re-read our data.
204                  */
205                 ConfigReader* Conf = new ConfigReader(ServerInstance);
206                 std::string Censorfile = Conf->ReadValue("censor","file",0);
207                 // this automatically re-reads the configuration file into the class
208                 ConfigReader* MyConf = new ConfigReader(ServerInstance, Censorfile);
209                 if ((Censorfile == "") || (!MyConf->Verify()))
210                 {
211                         CensorException e;
212                         throw(e);
213                 }
214                 censors.clear();
215                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
216                 {
217                         irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
218                         irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
219                         censors[pattern] = replace;
220                 }
221                 DELETE(Conf);
222                 DELETE(MyConf);
223         }
224         
225         virtual Version GetVersion()
226         {
227                 // This is version 2 because version 1.x is the unreleased unrealircd module
228                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
229         }
230         
231 };
232
233 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
234
235 class ModuleCensorFactory : public ModuleFactory
236 {
237  public:
238         ModuleCensorFactory()
239         {
240         }
241         
242         ~ModuleCensorFactory()
243         {
244         }
245         
246         virtual Module * CreateModule(InspIRCd* Me)
247         {
248                 return new ModuleCensor(Me);
249         }
250         
251 };
252
253
254 extern "C" void * init_module( void )
255 {
256         return new ModuleCensorFactory;
257 }