]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
a4502f81eb158d1119d0d5f4d42530bc1f05f5b8
[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                 /* Only opers can change other users modes */
35                 if ((source != dest) && (!*source->oper))
36                         return MODEACTION_DENY;
37
38                 if (adding)
39                 {
40                         if (!dest->IsModeSet('G'))
41                         {
42                                 dest->SetMode('G',true);
43                                 return MODEACTION_ALLOW;
44                         }
45                 }
46                 else
47                 {
48                         if (dest->IsModeSet('G'))
49                         {
50                                 dest->SetMode('G',false);
51                                 return MODEACTION_ALLOW;
52                         }
53                 }
54
55                 return MODEACTION_DENY;
56         }
57 };
58
59 /** Handles channel mode +G
60  */
61 class CensorChannel : public ModeHandler
62 {
63  public:
64         CensorChannel(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_CHANNEL, false) { }
65
66         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
67         {
68                 if (adding)
69                 {
70                         if (!channel->IsModeSet('G'))
71                         {
72                                 channel->SetMode('G',false);
73                                 return MODEACTION_ALLOW;
74                         }
75                 }
76                 else
77                 {
78                         if (channel->IsModeSet('G'))
79                         {
80                                 channel->SetMode('G',false);
81                                 return MODEACTION_ALLOW;
82                         }
83                 }
84
85                 return MODEACTION_ALLOW;
86         }
87 };
88
89 class ModuleCensor : public Module
90 {
91
92         
93         censor_t censors;
94         CensorUser *cu;
95         CensorChannel *cc;
96  
97  public:
98         ModuleCensor(InspIRCd* Me)
99                 : Module::Module(Me)
100         {
101                 /* Read the configuration file on startup.
102                  */
103                 OnRehash("");
104                 cu = new CensorUser(ServerInstance);
105                 cc = new CensorChannel(ServerInstance);
106                 ServerInstance->AddMode(cu, 'G');
107                 ServerInstance->AddMode(cc, 'G');
108         }
109
110         void Implements(char* List)
111         {
112                 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
113         }
114
115         virtual ~ModuleCensor()
116         {
117                 ServerInstance->Modes->DelMode(cu);
118                 ServerInstance->Modes->DelMode(cc);
119                 DELETE(cu);
120                 DELETE(cc);
121         }
122
123         virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
124         {
125                 if ((pattern != "") && (text != ""))
126                 {
127                         while (text.find(pattern) != irc::string::npos)
128                         {
129                                 int pos = text.find(pattern);
130                                 text.erase(pos,pattern.length());
131                                 text.insert(pos,replace);
132                         }
133                 }
134         }
135
136         // format of a config entry is <badword text="shit" replace="poo">
137         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
138         {
139                 bool active = false;
140                 irc::string text2 = text.c_str();
141                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
142                 { 
143                         if (text2.find(index->first) != irc::string::npos)
144                         {
145                                 if (target_type == TYPE_USER)
146                                 {
147                                         userrec* t = (userrec*)dest;
148                                         active = t->IsModeSet('G');
149                                 }
150                                 else if (target_type == TYPE_CHANNEL)
151                                 {
152                                         chanrec* t = (chanrec*)dest;
153                                         active = t->IsModeSet('G');
154                                 }
155                                 
156                                 if (active)
157                                 {
158                                         this->ReplaceLine(text2,index->first,index->second);
159                                         text = text2.c_str();
160                                 }
161                         }
162                 }
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(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 }