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"
27 typedef std::map<irc::string,irc::string> censor_t;
29 /* $ModDesc: Provides user and channel +G mode */
31 class CensorException : public ModuleException
34 virtual const char* GetReason()
36 return "Could not find <censor file=\"\"> definition in your config file!";
40 class CensorUser : public ModeHandler
43 CensorUser() : ModeHandler('G', 0, 0, false, MODETYPE_USER, false) { }
45 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
47 /* Only opers can change other users modes */
48 if ((source != dest) && (!*source->oper))
49 return MODEACTION_DENY;
53 if (!dest->IsModeSet('G'))
55 dest->SetMode('G',true);
56 return MODEACTION_ALLOW;
61 if (dest->IsModeSet('G'))
63 dest->SetMode('G',false);
64 return MODEACTION_ALLOW;
68 return MODEACTION_DENY;
72 class CensorChannel : public ModeHandler
75 CensorChannel() : ModeHandler('G', 0, 0, false, MODETYPE_CHANNEL, false) { }
77 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
81 if (!channel->IsModeSet('G'))
83 channel->SetMode('G',false);
84 return MODEACTION_ALLOW;
89 if (channel->IsModeSet('G'))
91 channel->SetMode('G',false);
92 return MODEACTION_ALLOW;
96 return MODEACTION_ALLOW;
100 class ModuleCensor : public Module
109 ModuleCensor(Server* Me)
113 * read the configuration file on startup.
114 * it is perfectly valid to set <censor file> to the value of the
115 * main config file, then append your <badword> tags to the bottom
116 * of the main config... but rather messy. That's why the capability
117 * of using a seperate config file is provided.
119 * XXX - Really, it'd be nice to scraip this kind of thing, and have something like
120 * an include directive to include additional configuration files. Might make our lives easier. --w00t
122 * XXX - These module pre-date the include directive which exists since beta 5 -- Brain
127 cc = new CensorChannel;
128 Srv->AddMode(cu, 'G');
129 Srv->AddMode(cc, 'G');
132 void Implements(char* List)
134 List[I_OnRehash] = List[I_On005Numeric] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
138 virtual void On005Numeric(std::string &output)
140 InsertMode(output,"G",4);
143 virtual ~ModuleCensor()
149 virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
151 if ((pattern != "") && (text != ""))
153 while (text.find(pattern) != irc::string::npos)
155 int pos = text.find(pattern);
156 text.erase(pos,pattern.length());
157 text.insert(pos,replace);
162 // format of a config entry is <badword text="shit" replace="poo">
164 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
167 irc::string text2 = text.c_str();
168 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
170 if (text2.find(index->first) != irc::string::npos)
172 if (target_type == TYPE_USER)
174 userrec* t = (userrec*)dest;
175 active = t->IsModeSet('G');
177 else if (target_type == TYPE_CHANNEL)
179 chanrec* t = (chanrec*)dest;
180 active = t->IsModeSet('G');
185 this->ReplaceLine(text2,index->first,index->second);
186 text = text2.c_str();
193 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
195 return OnUserPreMessage(user,dest,target_type,text,status);
198 virtual void OnRehash(const std::string ¶meter)
201 * reload our config file on rehash - we must destroy and re-allocate the classes
202 * to call the constructor again and re-read our data.
204 ConfigReader* Conf = new ConfigReader;
205 std::string Censorfile = Conf->ReadValue("censor","file",0);
206 // this automatically re-reads the configuration file into the class
207 ConfigReader* MyConf = new ConfigReader(Censorfile);
208 if ((Censorfile == "") || (!MyConf->Verify()))
214 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
216 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
217 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
218 censors[pattern] = replace;
224 virtual Version GetVersion()
226 // This is version 2 because version 1.x is the unreleased unrealircd module
227 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
232 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
234 class ModuleCensorFactory : public ModuleFactory
237 ModuleCensorFactory()
241 ~ModuleCensorFactory()
245 virtual Module * CreateModule(Server* Me)
247 return new ModuleCensor(Me);
253 extern "C" void * init_module( void )
255 return new ModuleCensorFactory;