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 * ---------------------------------------------------
26 typedef std::map<irc::string,irc::string> censor_t;
28 /* $ModDesc: Provides user and channel +G mode */
32 class CensorException : public ModuleException
35 virtual const char* GetReason()
37 return "Could not find <censor file=\"\"> definition in your config file!";
41 class CensorUser : public ModeHandler
44 CensorUser(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_USER, false) { }
46 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
48 /* Only opers can change other users modes */
49 if ((source != dest) && (!*source->oper))
50 return MODEACTION_DENY;
54 if (!dest->IsModeSet('G'))
56 dest->SetMode('G',true);
57 return MODEACTION_ALLOW;
62 if (dest->IsModeSet('G'))
64 dest->SetMode('G',false);
65 return MODEACTION_ALLOW;
69 return MODEACTION_DENY;
73 class CensorChannel : public ModeHandler
76 CensorChannel(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_CHANNEL, false) { }
78 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
82 if (!channel->IsModeSet('G'))
84 channel->SetMode('G',false);
85 return MODEACTION_ALLOW;
90 if (channel->IsModeSet('G'))
92 channel->SetMode('G',false);
93 return MODEACTION_ALLOW;
97 return MODEACTION_ALLOW;
101 class ModuleCensor : public Module
110 ModuleCensor(InspIRCd* Me)
114 * read the configuration file on startup.
115 * it is perfectly valid to set <censor file> to the value of the
116 * main config file, then append your <badword> tags to the bottom
117 * of the main config... but rather messy. That's why the capability
118 * of using a seperate config file is provided.
120 * XXX - Really, it'd be nice to scraip this kind of thing, and have something like
121 * an include directive to include additional configuration files. Might make our lives easier. --w00t
123 * XXX - These module pre-date the include directive which exists since beta 5 -- Brain
127 cu = new CensorUser(ServerInstance);
128 cc = new CensorChannel(ServerInstance);
129 ServerInstance->AddMode(cu, 'G');
130 ServerInstance->AddMode(cc, 'G');
133 void Implements(char* List)
135 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
138 virtual ~ModuleCensor()
140 ServerInstance->Modes->DelMode(cu);
141 ServerInstance->Modes->DelMode(cc);
146 virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
148 if ((pattern != "") && (text != ""))
150 while (text.find(pattern) != irc::string::npos)
152 int pos = text.find(pattern);
153 text.erase(pos,pattern.length());
154 text.insert(pos,replace);
159 // format of a config entry is <badword text="shit" replace="poo">
160 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status)
163 irc::string text2 = text.c_str();
164 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
166 if (text2.find(index->first) != irc::string::npos)
168 if (target_type == TYPE_USER)
170 userrec* t = (userrec*)dest;
171 active = t->IsModeSet('G');
173 else if (target_type == TYPE_CHANNEL)
175 chanrec* t = (chanrec*)dest;
176 active = t->IsModeSet('G');
181 this->ReplaceLine(text2,index->first,index->second);
182 text = text2.c_str();
189 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status)
191 return OnUserPreMessage(user,dest,target_type,text,status);
194 virtual void OnRehash(const std::string ¶meter)
197 * reload our config file on rehash - we must destroy and re-allocate the classes
198 * to call the constructor again and re-read our data.
200 ConfigReader* Conf = new ConfigReader(ServerInstance);
201 std::string Censorfile = Conf->ReadValue("censor","file",0);
202 // this automatically re-reads the configuration file into the class
203 ConfigReader* MyConf = new ConfigReader(ServerInstance, Censorfile);
204 if ((Censorfile == "") || (!MyConf->Verify()))
210 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
212 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
213 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
214 censors[pattern] = replace;
220 virtual Version GetVersion()
222 // This is version 2 because version 1.x is the unreleased unrealircd module
223 return Version(1,0,0,0,VF_COMMON|VF_VENDOR);
228 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
230 class ModuleCensorFactory : public ModuleFactory
233 ModuleCensorFactory()
237 ~ModuleCensorFactory()
241 virtual Module * CreateModule(InspIRCd* Me)
243 return new ModuleCensor(Me);
249 extern "C" void * init_module( void )
251 return new ModuleCensorFactory;