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