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 * ---------------------------------------------------
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)
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)
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_ALLOW;
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->AddMode(cu, 'G') || !ServerInstance->AddMode(cc, 'G'))
101 throw ModuleException("Could not add new modes!");
104 void Implements(char* List)
106 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
109 virtual ~ModuleCensor()
111 ServerInstance->Modes->DelMode(cu);
112 ServerInstance->Modes->DelMode(cc);
117 virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
119 if ((!pattern.empty()) && (!text.empty()))
121 std::string::size_type pos;
122 while ((pos = text.find(pattern)) != irc::string::npos)
124 text.erase(pos,pattern.length());
125 text.insert(pos,replace);
130 // format of a config entry is <badword text="shit" replace="poo">
131 virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
138 if (target_type == TYPE_USER)
139 active = ((User*)dest)->IsModeSet('G');
140 else if (target_type == TYPE_CHANNEL)
141 active = ((Channel*)dest)->IsModeSet('G');
146 irc::string text2 = text.c_str();
147 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
149 if (text2.find(index->first) != irc::string::npos)
151 if (index->second.empty())
153 user->WriteServ("936 %s %s %s :Your message contained a censored word, and was blocked", user->nick, ((Channel*)dest)->name, index->first.c_str());
157 this->ReplaceLine(text2,index->first,index->second);
160 text = text2.c_str();
164 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
166 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
169 virtual void OnRehash(User* user, const std::string ¶meter)
172 * reload our config file on rehash - we must destroy and re-allocate the classes
173 * to call the constructor again and re-read our data.
175 ConfigReader* MyConf = new ConfigReader(ServerInstance);
178 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
180 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
181 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
182 censors[pattern] = replace;
188 virtual Version GetVersion()
190 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
195 MODULE_INIT(ModuleCensor)