1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
22 #include "helperfuncs.h"
24 /* $ModDesc: Provides user and channel +G mode */
26 class ModuleCensor : public Module
29 ConfigReader *Conf, *MyConf;
34 // read the configuration file on startup.
35 // it is perfectly valid to set <censor file> to the value of the
36 // main config file, then append your <badword> tags to the bottom
37 // of the main config... but rather messy. That's why the capability
38 // of using a seperate config file is provided.
40 Conf = new ConfigReader;
41 std::string Censorfile = Conf->ReadValue("censor","file",0);
42 MyConf = new ConfigReader(Censorfile);
43 if ((Censorfile == "") || (!MyConf->Verify()))
45 printf("Error, could not find <censor file=\"\"> definition in your config file!");
46 log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
49 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
50 Srv->AddExtendedMode('G',MT_CHANNEL,false,0,0);
51 Srv->AddExtendedMode('G',MT_CLIENT,false,0,0);
54 virtual void On005Numeric(std::string &output)
56 std::stringstream line(output);
57 std::string temp1, temp2;
61 if (temp1.substr(0,10) == "CHANMODES=")
63 // append the chanmode to the end
64 temp1 = temp1.substr(10,temp1.length());
65 temp1 = "CHANMODES=" + temp1 + "G";
67 temp2 = temp2 + temp1 + " ";
70 output = temp2.substr(0,temp2.length()-1);
74 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
76 // check if this is our mode character...
87 virtual ~ModuleCensor()
94 virtual void ReplaceLine(std::string &text,std::string pattern, std::string replace)
96 if ((pattern != "") && (text != ""))
98 while (text.find(pattern) != std::string::npos)
100 int pos = text.find(pattern);
101 text.erase(pos,pattern.length());
102 text.insert(pos,replace);
107 // format of a config entry is <badword text="shit" replace="poo">
109 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
112 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
114 std::string pattern = MyConf->ReadValue("badword","text",index);
115 if (text.find(pattern) != std::string::npos)
117 std::string replace = MyConf->ReadValue("badword","replace",index);
119 if (target_type == TYPE_USER)
121 userrec* t = (userrec*)dest;
122 active = (strchr(t->modes,'G') > 0);
124 else if (target_type == TYPE_CHANNEL)
126 chanrec* t = (chanrec*)dest;
127 active = (t->IsCustomModeSet('G'));
132 this->ReplaceLine(text,pattern,replace);
139 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
142 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
144 std::string pattern = MyConf->ReadValue("badword","text",index);
145 if (text.find(pattern) != std::string::npos)
147 std::string replace = MyConf->ReadValue("badword","replace",index);
149 if (target_type == TYPE_USER)
151 userrec* t = (userrec*)dest;
152 active = (strchr(t->modes,'G') > 0);
154 else if (target_type == TYPE_CHANNEL)
156 chanrec* t = (chanrec*)dest;
157 active = (t->IsCustomModeSet('G'));
162 this->ReplaceLine(text,pattern,replace);
169 virtual void OnRehash()
171 // reload our config file on rehash - we must destroy and re-allocate the classes
172 // to call the constructor again and re-read our data.
175 Conf = new ConfigReader;
176 std::string Censorfile = Conf->ReadValue("censor","file",0);
177 // this automatically re-reads the configuration file into the class
178 MyConf = new ConfigReader(Censorfile);
179 if ((Censorfile == "") || (!MyConf->Verify()))
181 // bail if the user forgot to create a config file
182 printf("Error, could not find <censor file=\"\"> definition in your config file!");
183 log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
185 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
188 virtual Version GetVersion()
190 // This is version 2 because version 1.x is the unreleased unrealircd module
191 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
196 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
198 class ModuleCensorFactory : public ModuleFactory
201 ModuleCensorFactory()
205 ~ModuleCensorFactory()
209 virtual Module * CreateModule()
211 return new ModuleCensor;
217 extern "C" void * init_module( void )
219 return new ModuleCensorFactory;