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 */
30 /** Handles usermode +G
32 class CensorUser : public ModeHandler
35 CensorUser(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_USER, false) { }
37 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
39 /* Only opers can change other users modes */
40 if ((source != dest) && (!*source->oper))
41 return MODEACTION_DENY;
45 if (!dest->IsModeSet('G'))
47 dest->SetMode('G',true);
48 return MODEACTION_ALLOW;
53 if (dest->IsModeSet('G'))
55 dest->SetMode('G',false);
56 return MODEACTION_ALLOW;
60 return MODEACTION_DENY;
64 /** Handles channel mode +G
66 class CensorChannel : public ModeHandler
69 CensorChannel(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_CHANNEL, false) { }
71 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
75 if (!channel->IsModeSet('G'))
77 channel->SetMode('G',false);
78 return MODEACTION_ALLOW;
83 if (channel->IsModeSet('G'))
85 channel->SetMode('G',false);
86 return MODEACTION_ALLOW;
90 return MODEACTION_ALLOW;
94 class ModuleCensor : public Module
103 ModuleCensor(InspIRCd* Me)
106 /* Read the configuration file on startup.
109 cu = new CensorUser(ServerInstance);
110 cc = new CensorChannel(ServerInstance);
111 ServerInstance->AddMode(cu, 'G');
112 ServerInstance->AddMode(cc, 'G');
115 void Implements(char* List)
117 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
120 virtual ~ModuleCensor()
122 ServerInstance->Modes->DelMode(cu);
123 ServerInstance->Modes->DelMode(cc);
128 virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
130 if ((pattern != "") && (text != ""))
132 while (text.find(pattern) != irc::string::npos)
134 int pos = text.find(pattern);
135 text.erase(pos,pattern.length());
136 text.insert(pos,replace);
141 // format of a config entry is <badword text="shit" replace="poo">
142 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
145 irc::string text2 = text.c_str();
146 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
148 if (text2.find(index->first) != irc::string::npos)
150 if (target_type == TYPE_USER)
152 userrec* t = (userrec*)dest;
153 active = t->IsModeSet('G');
155 else if (target_type == TYPE_CHANNEL)
157 chanrec* t = (chanrec*)dest;
158 active = t->IsModeSet('G');
163 this->ReplaceLine(text2,index->first,index->second);
164 text = text2.c_str();
171 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
173 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
176 virtual void OnRehash(const std::string ¶meter)
179 * reload our config file on rehash - we must destroy and re-allocate the classes
180 * to call the constructor again and re-read our data.
182 ConfigReader* MyConf = new ConfigReader(ServerInstance);
184 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
186 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
187 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
188 censors[pattern] = replace;
193 virtual Version GetVersion()
195 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
200 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
202 class ModuleCensorFactory : public ModuleFactory
205 ModuleCensorFactory()
209 ~ModuleCensorFactory()
213 virtual Module * CreateModule(InspIRCd* Me)
215 return new ModuleCensor(Me);
221 extern "C" void * init_module( void )
223 return new ModuleCensorFactory;