1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 * ---------------------------------------------------
14 #define _CRT_SECURE_NO_DEPRECATE
15 #define _SCL_SECURE_NO_DEPRECATE
19 typedef std::map<irc::string,irc::string> censor_t;
21 /* $ModDesc: Provides user and channel +G mode */
23 /** Handles usermode +G
25 class CensorUser : public ModeHandler
28 CensorUser(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_USER, false) { }
30 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
34 if (!dest->IsModeSet('G'))
36 dest->SetMode('G',true);
37 return MODEACTION_ALLOW;
42 if (dest->IsModeSet('G'))
44 dest->SetMode('G',false);
45 return MODEACTION_ALLOW;
49 return MODEACTION_DENY;
53 /** Handles channel mode +G
55 class CensorChannel : public ModeHandler
58 CensorChannel(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_CHANNEL, false) { }
60 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
64 if (!channel->IsModeSet('G'))
66 channel->SetMode('G',true);
67 return MODEACTION_ALLOW;
72 if (channel->IsModeSet('G'))
74 channel->SetMode('G',false);
75 return MODEACTION_ALLOW;
79 return MODEACTION_DENY;
83 class ModuleCensor : public Module
92 ModuleCensor(InspIRCd* Me)
95 /* Read the configuration file on startup.
98 cu = new CensorUser(ServerInstance);
99 cc = new CensorChannel(ServerInstance);
100 if (!ServerInstance->Modes->AddMode(cu) || !ServerInstance->Modes->AddMode(cc))
104 throw ModuleException("Could not add new modes!");
106 Implementation eventlist[] = { I_OnRehash, I_OnUserPreMessage, I_OnUserPreNotice };
107 ServerInstance->Modules->Attach(eventlist, this, 3);
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.empty()) && (!text.empty()))
123 std::string::size_type pos;
124 while ((pos = text.find(pattern)) != irc::string::npos)
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(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
140 if (target_type == TYPE_USER)
141 active = ((User*)dest)->IsModeSet('G');
142 else if (target_type == TYPE_CHANNEL)
144 active = ((Channel*)dest)->IsModeSet('G');
145 Channel* c = (Channel*)dest;
146 if (CHANOPS_EXEMPT(ServerInstance, 'G') && c->GetStatus(user) == STATUS_OP)
155 irc::string text2 = text.c_str();
156 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
158 if (text2.find(index->first) != irc::string::npos)
160 if (index->second.empty())
162 user->WriteNumeric(936, "%s %s %s :Your message contained a censored word, and was blocked", user->nick, ((Channel*)dest)->name, index->first.c_str());
166 this->ReplaceLine(text2,index->first,index->second);
169 text = text2.c_str();
173 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
175 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
178 virtual void OnRehash(User* user, const std::string ¶meter)
181 * reload our config file on rehash - we must destroy and re-allocate the classes
182 * to call the constructor again and re-read our data.
184 ConfigReader* MyConf = new ConfigReader(ServerInstance);
187 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
189 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
190 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
191 censors[pattern] = replace;
197 virtual Version GetVersion()
199 return Version(1,2,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
204 MODULE_INIT(ModuleCensor)