1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
21 typedef std::map<irc::string,irc::string> censor_t;
23 /* $ModDesc: Provides user and channel +G mode */
25 /** Handles usermode +G
27 class CensorUser : public ModeHandler
30 CensorUser(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_USER, false) { }
32 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
34 /* Only opers can change other users modes */
35 if ((source != dest) && (!*source->oper))
36 return MODEACTION_DENY;
40 if (!dest->IsModeSet('G'))
42 dest->SetMode('G',true);
43 return MODEACTION_ALLOW;
48 if (dest->IsModeSet('G'))
50 dest->SetMode('G',false);
51 return MODEACTION_ALLOW;
55 return MODEACTION_DENY;
59 /** Handles channel mode +G
61 class CensorChannel : public ModeHandler
64 CensorChannel(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_CHANNEL, false) { }
66 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
70 if (!channel->IsModeSet('G'))
72 channel->SetMode('G',false);
73 return MODEACTION_ALLOW;
78 if (channel->IsModeSet('G'))
80 channel->SetMode('G',false);
81 return MODEACTION_ALLOW;
85 return MODEACTION_ALLOW;
89 class ModuleCensor : public Module
98 ModuleCensor(InspIRCd* Me)
101 /* Read the configuration file on startup.
104 cu = new CensorUser(ServerInstance);
105 cc = new CensorChannel(ServerInstance);
106 ServerInstance->AddMode(cu, 'G');
107 ServerInstance->AddMode(cc, 'G');
110 void Implements(char* List)
112 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
115 virtual ~ModuleCensor()
117 ServerInstance->Modes->DelMode(cu);
118 ServerInstance->Modes->DelMode(cc);
123 virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
125 if ((pattern != "") && (text != ""))
127 while (text.find(pattern) != irc::string::npos)
129 int pos = text.find(pattern);
130 text.erase(pos,pattern.length());
131 text.insert(pos,replace);
136 // format of a config entry is <badword text="shit" replace="poo">
137 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
140 irc::string text2 = text.c_str();
141 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
143 if (text2.find(index->first) != irc::string::npos)
145 if (target_type == TYPE_USER)
147 userrec* t = (userrec*)dest;
148 active = t->IsModeSet('G');
150 else if (target_type == TYPE_CHANNEL)
152 chanrec* t = (chanrec*)dest;
153 active = t->IsModeSet('G');
158 this->ReplaceLine(text2,index->first,index->second);
159 text = text2.c_str();
166 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
168 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
171 virtual void OnRehash(const std::string ¶meter)
174 * reload our config file on rehash - we must destroy and re-allocate the classes
175 * to call the constructor again and re-read our data.
177 ConfigReader* MyConf = new ConfigReader(ServerInstance);
179 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
181 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
182 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
183 censors[pattern] = replace;
188 virtual Version GetVersion()
190 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
195 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
197 class ModuleCensorFactory : public ModuleFactory
200 ModuleCensorFactory()
204 ~ModuleCensorFactory()
208 virtual Module * CreateModule(InspIRCd* Me)
210 return new ModuleCensor(Me);
216 extern "C" void * init_module( void )
218 return new ModuleCensorFactory;