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 * ---------------------------------------------------
24 #include "helperfuncs.h"
26 /* $ModDesc: Provides user and channel +G mode */
28 class ModuleCensor : public Module
31 ConfigReader *Conf, *MyConf;
34 ModuleCensor(Server* Me)
38 * read the configuration file on startup.
39 * it is perfectly valid to set <censor file> to the value of the
40 * main config file, then append your <badword> tags to the bottom
41 * of the main config... but rather messy. That's why the capability
42 * of using a seperate config file is provided.
44 * XXX - Really, it'd be nice to scraip this kind of thing, and have something like
45 * an include directive to include additional configuration files. Might make our lives easier. --w00t
48 Conf = new ConfigReader;
49 std::string Censorfile = Conf->ReadValue("censor","file",0);
50 MyConf = new ConfigReader(Censorfile);
51 if ((Censorfile == "") || (!MyConf->Verify()))
53 printf("Error, could not find <censor file=\"\"> definition in your config file!");
54 log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
57 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
58 Srv->AddExtendedMode('G',MT_CHANNEL,false,0,0);
59 Srv->AddExtendedMode('G',MT_CLIENT,false,0,0);
62 virtual void On005Numeric(std::string &output)
64 std::stringstream line(output);
65 std::string temp1, temp2;
69 if (temp1.substr(0,10) == "CHANMODES=")
71 // append the chanmode to the end
72 temp1 = temp1.substr(10,temp1.length());
73 temp1 = "CHANMODES=" + temp1 + "G";
75 temp2 = temp2 + temp1 + " ";
78 output = temp2.substr(0,temp2.length()-1);
82 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
84 // check if this is our mode character...
95 virtual ~ModuleCensor()
101 virtual void ReplaceLine(std::string &text,std::string pattern, std::string replace)
103 if ((pattern != "") && (text != ""))
105 while (text.find(pattern) != std::string::npos)
107 int pos = text.find(pattern);
108 text.erase(pos,pattern.length());
109 text.insert(pos,replace);
114 // format of a config entry is <badword text="shit" replace="poo">
116 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
119 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
121 std::string pattern = MyConf->ReadValue("badword","text",index);
122 if (text.find(pattern) != std::string::npos)
124 std::string replace = MyConf->ReadValue("badword","replace",index);
126 if (target_type == TYPE_USER)
128 userrec* t = (userrec*)dest;
129 active = (strchr(t->modes,'G') > 0);
131 else if (target_type == TYPE_CHANNEL)
133 chanrec* t = (chanrec*)dest;
134 active = (t->IsCustomModeSet('G'));
139 this->ReplaceLine(text,pattern,replace);
146 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
149 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
151 std::string pattern = MyConf->ReadValue("badword","text",index);
152 if (text.find(pattern) != std::string::npos)
154 std::string replace = MyConf->ReadValue("badword","replace",index);
156 if (target_type == TYPE_USER)
158 userrec* t = (userrec*)dest;
159 active = (strchr(t->modes,'G') > 0);
161 else if (target_type == TYPE_CHANNEL)
163 chanrec* t = (chanrec*)dest;
164 active = (t->IsCustomModeSet('G'));
169 this->ReplaceLine(text,pattern,replace);
176 virtual void OnRehash(std::string parameter)
179 * reload our config file on rehash - we must destroy and re-allocate the classes
180 * to call the constructor again and re-read our data.
184 Conf = new ConfigReader;
185 std::string Censorfile = Conf->ReadValue("censor","file",0);
186 // this automatically re-reads the configuration file into the class
187 MyConf = new ConfigReader(Censorfile);
188 if ((Censorfile == "") || (!MyConf->Verify()))
190 // bail if the user forgot to create a config file
191 printf("Error, could not find <censor file=\"\"> definition in your config file!");
192 log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
194 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
197 virtual Version GetVersion()
199 // This is version 2 because version 1.x is the unreleased unrealircd module
200 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
205 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
207 class ModuleCensorFactory : public ModuleFactory
210 ModuleCensorFactory()
214 ~ModuleCensorFactory()
218 virtual Module * CreateModule(Server* Me)
220 return new ModuleCensor(Me);
226 extern "C" void * init_module( void )
228 return new ModuleCensorFactory;