]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
c378080a542eec6d4eb03bc4de978fe58703e8ce
[user/henk/code/inspircd.git] / src / modules / m_censor.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <stdio.h>
15 #include <string>
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "inspircd.h"
20
21 typedef std::map<irc::string,irc::string> censor_t;
22
23 /* $ModDesc: Provides user and channel +G mode */
24
25 /** Handles usermode +G
26  */
27 class CensorUser : public ModeHandler
28 {
29  public:
30         CensorUser(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_USER, false) { }
31
32         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
33         {
34                 if (adding)
35                 {
36                         if (!dest->IsModeSet('G'))
37                         {
38                                 dest->SetMode('G',true);
39                                 return MODEACTION_ALLOW;
40                         }
41                 }
42                 else
43                 {
44                         if (dest->IsModeSet('G'))
45                         {
46                                 dest->SetMode('G',false);
47                                 return MODEACTION_ALLOW;
48                         }
49                 }
50
51                 return MODEACTION_DENY;
52         }
53 };
54
55 /** Handles channel mode +G
56  */
57 class CensorChannel : public ModeHandler
58 {
59  public:
60         CensorChannel(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_CHANNEL, false) { }
61
62         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
63         {
64                 if (adding)
65                 {
66                         if (!channel->IsModeSet('G'))
67                         {
68                                 channel->SetMode('G',true);
69                                 return MODEACTION_ALLOW;
70                         }
71                 }
72                 else
73                 {
74                         if (channel->IsModeSet('G'))
75                         {
76                                 channel->SetMode('G',false);
77                                 return MODEACTION_ALLOW;
78                         }
79                 }
80
81                 return MODEACTION_ALLOW;
82         }
83 };
84
85 class ModuleCensor : public Module
86 {
87
88         
89         censor_t censors;
90         CensorUser *cu;
91         CensorChannel *cc;
92  
93  public:
94         ModuleCensor(InspIRCd* Me)
95                 : Module::Module(Me)
96         {
97                 /* Read the configuration file on startup.
98                  */
99                 OnRehash(NULL,"");
100                 cu = new CensorUser(ServerInstance);
101                 cc = new CensorChannel(ServerInstance);
102                 if (!ServerInstance->AddMode(cu, 'G') || !ServerInstance->AddMode(cc, 'G'))
103                         throw ModuleException("Could not add new modes!");
104         }
105
106         void Implements(char* List)
107         {
108                 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
109         }
110
111         virtual ~ModuleCensor()
112         {
113                 ServerInstance->Modes->DelMode(cu);
114                 ServerInstance->Modes->DelMode(cc);
115                 DELETE(cu);
116                 DELETE(cc);
117         }
118
119         virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
120         {
121                 if ((!pattern.empty()) && (!text.empty()))
122                 {
123                         std::string::size_type pos;
124                         while ((pos = text.find(pattern)) != irc::string::npos)
125                         {
126                                 text.erase(pos,pattern.length());
127                                 text.insert(pos,replace);
128                         }
129                 }
130         }
131
132         // format of a config entry is <badword text="shit" replace="poo">
133         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
134         {
135                 if (!IS_LOCAL(user))
136                         return 0;
137
138                 bool active = false;
139
140                 if (target_type == TYPE_USER)
141                         active = ((userrec*)dest)->IsModeSet('G');
142                 else if (target_type == TYPE_CHANNEL)
143                         active = ((chanrec*)dest)->IsModeSet('G');
144
145                 if (!active)
146                         return 0;
147
148                 irc::string text2 = text.c_str();
149                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
150                 { 
151                         if (text2.find(index->first) != irc::string::npos)
152                         {
153                                 if (index->second.empty())
154                                 {
155                                         user->WriteServ("936 %s %s %s :Your message contained a censored word, and was blocked", user->nick, ((chanrec*)dest)->name, index->first.c_str());
156                                         return 1;
157                                 }
158                                 
159                                 this->ReplaceLine(text2,index->first,index->second);
160                         }
161                 }
162                 text = text2.c_str();
163                 return 0;
164         }
165         
166         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
167         {
168                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
169         }
170         
171         virtual void OnRehash(userrec* user, const std::string &parameter)
172         {
173                 /*
174                  * reload our config file on rehash - we must destroy and re-allocate the classes
175                  * to call the constructor again and re-read our data.
176                  */
177                 ConfigReader* MyConf = new ConfigReader(ServerInstance);
178                 censors.clear();
179                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
180                 {
181                         irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
182                         irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
183                         censors[pattern] = replace;
184                 }
185                 DELETE(MyConf);
186         }
187         
188         virtual Version GetVersion()
189         {
190                 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
191         }
192         
193 };
194
195 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
196
197 class ModuleCensorFactory : public ModuleFactory
198 {
199  public:
200         ModuleCensorFactory()
201         {
202         }
203         
204         ~ModuleCensorFactory()
205         {
206         }
207         
208         virtual Module * CreateModule(InspIRCd* Me)
209         {
210                 return new ModuleCensor(Me);
211         }
212         
213 };
214
215
216 extern "C" void * init_module( void )
217 {
218         return new ModuleCensorFactory;
219 }