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 * ---------------------------------------------------
27 typedef std::map<irc::string,irc::string> censor_t;
29 /* $ModDesc: Provides user and channel +G mode */
33 class CensorException : public ModuleException
36 virtual const char* GetReason()
38 return "Could not find <censor file=\"\"> definition in your config file!";
42 class CensorUser : public ModeHandler
45 CensorUser(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_USER, false) { }
47 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
49 /* Only opers can change other users modes */
50 if ((source != dest) && (!*source->oper))
51 return MODEACTION_DENY;
55 if (!dest->IsModeSet('G'))
57 dest->SetMode('G',true);
58 return MODEACTION_ALLOW;
63 if (dest->IsModeSet('G'))
65 dest->SetMode('G',false);
66 return MODEACTION_ALLOW;
70 return MODEACTION_DENY;
74 class CensorChannel : public ModeHandler
77 CensorChannel(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_CHANNEL, false) { }
79 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
83 if (!channel->IsModeSet('G'))
85 channel->SetMode('G',false);
86 return MODEACTION_ALLOW;
91 if (channel->IsModeSet('G'))
93 channel->SetMode('G',false);
94 return MODEACTION_ALLOW;
98 return MODEACTION_ALLOW;
102 class ModuleCensor : public Module
111 ModuleCensor(InspIRCd* Me)
115 * read the configuration file on startup.
116 * it is perfectly valid to set <censor file> to the value of the
117 * main config file, then append your <badword> tags to the bottom
118 * of the main config... but rather messy. That's why the capability
119 * of using a seperate config file is provided.
121 * XXX - Really, it'd be nice to scraip this kind of thing, and have something like
122 * an include directive to include additional configuration files. Might make our lives easier. --w00t
124 * XXX - These module pre-date the include directive which exists since beta 5 -- Brain
128 cu = new CensorUser(ServerInstance);
129 cc = new CensorChannel(ServerInstance);
130 ServerInstance->AddMode(cu, 'G');
131 ServerInstance->AddMode(cc, 'G');
134 void Implements(char* List)
136 List[I_OnRehash] = List[I_On005Numeric] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
140 virtual void On005Numeric(std::string &output)
142 ServerInstance->Modes->InsertMode(output,"G",4);
145 virtual ~ModuleCensor()
151 virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
153 if ((pattern != "") && (text != ""))
155 while (text.find(pattern) != irc::string::npos)
157 int pos = text.find(pattern);
158 text.erase(pos,pattern.length());
159 text.insert(pos,replace);
164 // format of a config entry is <badword text="shit" replace="poo">
166 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
169 irc::string text2 = text.c_str();
170 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
172 if (text2.find(index->first) != irc::string::npos)
174 if (target_type == TYPE_USER)
176 userrec* t = (userrec*)dest;
177 active = t->IsModeSet('G');
179 else if (target_type == TYPE_CHANNEL)
181 chanrec* t = (chanrec*)dest;
182 active = t->IsModeSet('G');
187 this->ReplaceLine(text2,index->first,index->second);
188 text = text2.c_str();
195 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
197 return OnUserPreMessage(user,dest,target_type,text,status);
200 virtual void OnRehash(const std::string ¶meter)
203 * reload our config file on rehash - we must destroy and re-allocate the classes
204 * to call the constructor again and re-read our data.
206 ConfigReader* Conf = new ConfigReader(ServerInstance);
207 std::string Censorfile = Conf->ReadValue("censor","file",0);
208 // this automatically re-reads the configuration file into the class
209 ConfigReader* MyConf = new ConfigReader(ServerInstance, Censorfile);
210 if ((Censorfile == "") || (!MyConf->Verify()))
216 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
218 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
219 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
220 censors[pattern] = replace;
226 virtual Version GetVersion()
228 // This is version 2 because version 1.x is the unreleased unrealircd module
229 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
234 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
236 class ModuleCensorFactory : public ModuleFactory
239 ModuleCensorFactory()
243 ~ModuleCensorFactory()
247 virtual Module * CreateModule(InspIRCd* Me)
249 return new ModuleCensor(Me);
255 extern "C" void * init_module( void )
257 return new ModuleCensorFactory;