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)
36 if (!dest->IsModeSet('G'))
38 dest->SetMode('G',true);
39 return MODEACTION_ALLOW;
44 if (dest->IsModeSet('G'))
46 dest->SetMode('G',false);
47 return MODEACTION_ALLOW;
51 return MODEACTION_DENY;
55 /** Handles channel mode +G
57 class CensorChannel : public ModeHandler
60 CensorChannel(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_CHANNEL, false) { }
62 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
66 if (!channel->IsModeSet('G'))
68 channel->SetMode('G',false);
69 return MODEACTION_ALLOW;
74 if (channel->IsModeSet('G'))
76 channel->SetMode('G',false);
77 return MODEACTION_ALLOW;
81 return MODEACTION_ALLOW;
85 class ModuleCensor : public Module
94 ModuleCensor(InspIRCd* Me)
97 /* Read the configuration file on startup.
100 cu = new CensorUser(ServerInstance);
101 cc = new CensorChannel(ServerInstance);
102 ServerInstance->AddMode(cu, 'G');
103 ServerInstance->AddMode(cc, 'G');
106 void Implements(char* List)
108 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
111 virtual ~ModuleCensor()
113 ServerInstance->Modes->DelMode(cu);
114 ServerInstance->Modes->DelMode(cc);
119 virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
121 if ((pattern != "") && (text != ""))
123 while (text.find(pattern) != irc::string::npos)
125 int pos = text.find(pattern);
126 text.erase(pos,pattern.length());
127 text.insert(pos,replace);
132 // format of a config entry is <badword text="shit" replace="poo">
133 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
136 irc::string text2 = text.c_str();
137 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
139 if (text2.find(index->first) != irc::string::npos)
141 if (target_type == TYPE_USER)
143 userrec* t = (userrec*)dest;
144 active = t->IsModeSet('G');
146 else if (target_type == TYPE_CHANNEL)
148 chanrec* t = (chanrec*)dest;
149 active = t->IsModeSet('G');
154 this->ReplaceLine(text2,index->first,index->second);
155 text = text2.c_str();
162 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
164 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
167 virtual void OnRehash(const std::string ¶meter)
170 * reload our config file on rehash - we must destroy and re-allocate the classes
171 * to call the constructor again and re-read our data.
173 ConfigReader* MyConf = new ConfigReader(ServerInstance);
175 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
177 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
178 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
179 censors[pattern] = replace;
184 virtual Version GetVersion()
186 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
191 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
193 class ModuleCensorFactory : public ModuleFactory
196 ModuleCensorFactory()
200 ~ModuleCensorFactory()
204 virtual Module * CreateModule(InspIRCd* Me)
206 return new ModuleCensor(Me);
212 extern "C" void * init_module( void )
214 return new ModuleCensorFactory;