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