1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 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 typedef std::map<irc::string,irc::string> censor_t;
28 /* $ModDesc: Provides user and channel +G mode */
30 class CensorException : public ModuleException
33 virtual const char* GetReason()
35 return "Could not find <censor file=\"\"> definition in your config file!";
39 class ModuleCensor : public Module
46 ModuleCensor(Server* Me)
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.
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
59 * XXX - These module pre-date the include directive which exists since beta 5 -- Brain
63 Srv->AddExtendedMode('G',MT_CHANNEL,false,0,0);
64 Srv->AddExtendedMode('G',MT_CLIENT,false,0,0);
67 void Implements(char* List)
69 List[I_OnRehash] = List[I_On005Numeric] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnExtendedMode] = 1;
73 virtual void On005Numeric(std::string &output)
75 InsertMode(output,"G",4);
79 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
81 // check if this is our mode character...
92 virtual ~ModuleCensor()
96 virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
98 if ((pattern != "") && (text != ""))
100 while (text.find(pattern) != irc::string::npos)
102 int pos = text.find(pattern);
103 text.erase(pos,pattern.length());
104 text.insert(pos,replace);
109 // format of a config entry is <badword text="shit" replace="poo">
111 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
114 irc::string text2 = text.c_str();
115 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
117 if (text2.find(index->first) != irc::string::npos)
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->IsModeSet('G'));
132 this->ReplaceLine(text2,index->first,index->second);
133 text = text2.c_str();
140 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
142 return OnUserPreMessage(user,dest,target_type,text,status);
145 virtual void OnRehash(const std::string ¶meter)
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.
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()))
161 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
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;
171 virtual Version GetVersion()
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);
179 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
181 class ModuleCensorFactory : public ModuleFactory
184 ModuleCensorFactory()
188 ~ModuleCensorFactory()
192 virtual Module * CreateModule(Server* Me)
194 return new ModuleCensor(Me);
200 extern "C" void * init_module( void )
202 return new ModuleCensorFactory;