]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_censor.cpp
Renamed to chanrec::modes
[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 ModuleCensor : public Module
40 {
41
42         Server *Srv;
43         censor_t censors;
44  
45  public:
46         ModuleCensor(Server* Me)
47                 : Module::Module(Me)
48         {
49                 /*
50                  * read the configuration file on startup.
51                  * it is perfectly valid to set <censor file> to the value of the
52                  * main config file, then append your <badword> tags to the bottom
53                  * of the main config... but rather messy. That's why the capability
54                  * of using a seperate config file is provided.
55                  *
56                  * XXX - Really, it'd be nice to scraip this kind of thing, and have something like
57                  * an include directive to include additional configuration files. Might make our lives easier. --w00t
58                  *
59                  * XXX - These module pre-date the include directive which exists since beta 5 -- Brain
60                  */
61                 Srv = Me;
62                 OnRehash("");
63                 Srv->AddExtendedMode('G',MT_CHANNEL,false,0,0);
64                 Srv->AddExtendedMode('G',MT_CLIENT,false,0,0);
65         }
66
67         void Implements(char* List)
68         {
69                 List[I_OnRehash] = List[I_On005Numeric] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnExtendedMode] = 1;
70         }
71
72
73         virtual void On005Numeric(std::string &output)
74         {
75                 InsertMode(output,"G",4);
76         }
77
78
79         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
80         {
81                 // check if this is our mode character...
82                 if (modechar == 'G')
83                 {
84                         return 1;
85                 }
86                 else
87                 {
88                         return 0;
89                 }
90         }
91         
92         virtual ~ModuleCensor()
93         {
94         }
95         
96         virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
97         {
98                 if ((pattern != "") && (text != ""))
99                 {
100                         while (text.find(pattern) != irc::string::npos)
101                         {
102                                 int pos = text.find(pattern);
103                                 text.erase(pos,pattern.length());
104                                 text.insert(pos,replace);
105                         }
106                 }
107         }
108         
109         // format of a config entry is <badword text="shit" replace="poo">
110         
111         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
112         {
113                 bool active = false;
114                 irc::string text2 = text.c_str();
115                 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
116                 { 
117                         if (text2.find(index->first) != irc::string::npos)
118                         {
119                                 if (target_type == TYPE_USER)
120                                 {
121                                         userrec* t = (userrec*)dest;
122                                         active = (strchr(t->modes,'G') > 0);
123                                 }
124                                 else if (target_type == TYPE_CHANNEL)
125                                 {
126                                         chanrec* t = (chanrec*)dest;
127                                         active = (t->IsModeSet('G'));
128                                 }
129                                 
130                                 if (active)
131                                 {
132                                         this->ReplaceLine(text2,index->first,index->second);
133                                         text = text2.c_str();
134                                 }
135                         }
136                 }
137                 return 0;
138         }
139         
140         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
141         {
142                 return OnUserPreMessage(user,dest,target_type,text,status);
143         }
144         
145         virtual void OnRehash(const std::string &parameter)
146         {
147                 /*
148                  * reload our config file on rehash - we must destroy and re-allocate the classes
149                  * to call the constructor again and re-read our data.
150                  */
151                 ConfigReader* Conf = new ConfigReader;
152                 std::string Censorfile = Conf->ReadValue("censor","file",0);
153                 // this automatically re-reads the configuration file into the class
154                 ConfigReader* MyConf = new ConfigReader(Censorfile);
155                 if ((Censorfile == "") || (!MyConf->Verify()))
156                 {
157                         CensorException e;
158                         throw(e);
159                 }
160                 censors.clear();
161                 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
162                 {
163                         irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
164                         irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
165                         censors[pattern] = replace;
166                 }
167                 delete Conf;
168                 delete MyConf;
169         }
170         
171         virtual Version GetVersion()
172         {
173                 // This is version 2 because version 1.x is the unreleased unrealircd module
174                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
175         }
176         
177 };
178
179 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
180
181 class ModuleCensorFactory : public ModuleFactory
182 {
183  public:
184         ModuleCensorFactory()
185         {
186         }
187         
188         ~ModuleCensorFactory()
189         {
190         }
191         
192         virtual Module * CreateModule(Server* Me)
193         {
194                 return new ModuleCensor(Me);
195         }
196         
197 };
198
199
200 extern "C" void * init_module( void )
201 {
202         return new ModuleCensorFactory;
203 }
204