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
47 * XXX - These module pre-date the include directive which exists since beta 5 -- Brain
50 Conf = new ConfigReader;
51 std::string Censorfile = Conf->ReadValue("censor","file",0);
52 MyConf = new ConfigReader(Censorfile);
53 if ((Censorfile == "") || (!MyConf->Verify()))
55 printf("Error, could not find <censor file=\"\"> definition in your config file!");
56 log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
59 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
60 Srv->AddExtendedMode('G',MT_CHANNEL,false,0,0);
61 Srv->AddExtendedMode('G',MT_CLIENT,false,0,0);
64 void Implements(char* List)
66 List[I_OnRehash] = List[I_On005Numeric] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnExtendedMode] = 1;
70 virtual void On005Numeric(std::string &output)
72 std::stringstream line(output);
73 std::string temp1, temp2;
77 if (temp1.substr(0,10) == "CHANMODES=")
79 // append the chanmode to the end
80 temp1 = temp1.substr(10,temp1.length());
81 temp1 = "CHANMODES=" + temp1 + "G";
83 temp2 = temp2 + temp1 + " ";
86 output = temp2.substr(0,temp2.length()-1);
90 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
92 // check if this is our mode character...
103 virtual ~ModuleCensor()
109 virtual void ReplaceLine(std::string &text,std::string pattern, std::string replace)
111 if ((pattern != "") && (text != ""))
113 while (text.find(pattern) != std::string::npos)
115 int pos = text.find(pattern);
116 text.erase(pos,pattern.length());
117 text.insert(pos,replace);
122 // format of a config entry is <badword text="shit" replace="poo">
124 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
127 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
129 std::string pattern = MyConf->ReadValue("badword","text",index);
130 if (text.find(pattern) != std::string::npos)
132 std::string replace = MyConf->ReadValue("badword","replace",index);
134 if (target_type == TYPE_USER)
136 userrec* t = (userrec*)dest;
137 active = (strchr(t->modes,'G') > 0);
139 else if (target_type == TYPE_CHANNEL)
141 chanrec* t = (chanrec*)dest;
142 active = (t->IsCustomModeSet('G'));
147 this->ReplaceLine(text,pattern,replace);
154 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
157 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
159 std::string pattern = MyConf->ReadValue("badword","text",index);
160 if (text.find(pattern) != std::string::npos)
162 std::string replace = MyConf->ReadValue("badword","replace",index);
164 if (target_type == TYPE_USER)
166 userrec* t = (userrec*)dest;
167 active = (strchr(t->modes,'G') > 0);
169 else if (target_type == TYPE_CHANNEL)
171 chanrec* t = (chanrec*)dest;
172 active = (t->IsCustomModeSet('G'));
177 this->ReplaceLine(text,pattern,replace);
184 virtual void OnRehash(std::string parameter)
187 * reload our config file on rehash - we must destroy and re-allocate the classes
188 * to call the constructor again and re-read our data.
192 Conf = new ConfigReader;
193 std::string Censorfile = Conf->ReadValue("censor","file",0);
194 // this automatically re-reads the configuration file into the class
195 MyConf = new ConfigReader(Censorfile);
196 if ((Censorfile == "") || (!MyConf->Verify()))
198 // bail if the user forgot to create a config file
199 printf("Error, could not find <censor file=\"\"> definition in your config file!");
200 log(DEFAULT,"Error, could not find <censor file=\"\"> definition in your config file!");
202 Srv->Log(DEFAULT,std::string("m_censor: read configuration from ")+Censorfile);
205 virtual Version GetVersion()
207 // This is version 2 because version 1.x is the unreleased unrealircd module
208 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
213 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
215 class ModuleCensorFactory : public ModuleFactory
218 ModuleCensorFactory()
222 ~ModuleCensorFactory()
226 virtual Module * CreateModule(Server* Me)
228 return new ModuleCensor(Me);
234 extern "C" void * init_module( void )
236 return new ModuleCensorFactory;