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
22 typedef std::map<irc::string,irc::string> censor_t;
24 /* $ModDesc: Provides user and channel +G mode */
26 /** Handles usermode +G
28 class CensorUser : public ModeHandler
31 CensorUser(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_USER, false) { }
33 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
37 if (!dest->IsModeSet('G'))
39 dest->SetMode('G',true);
40 return MODEACTION_ALLOW;
45 if (dest->IsModeSet('G'))
47 dest->SetMode('G',false);
48 return MODEACTION_ALLOW;
52 return MODEACTION_DENY;
56 /** Handles channel mode +G
58 class CensorChannel : public ModeHandler
61 CensorChannel(InspIRCd* Instance) : ModeHandler(Instance, 'G', 0, 0, false, MODETYPE_CHANNEL, false) { }
63 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
67 if (!channel->IsModeSet('G'))
69 channel->SetMode('G',true);
70 return MODEACTION_ALLOW;
75 if (channel->IsModeSet('G'))
77 channel->SetMode('G',false);
78 return MODEACTION_ALLOW;
82 return MODEACTION_ALLOW;
86 class ModuleCensor : public Module
95 ModuleCensor(InspIRCd* Me)
98 /* Read the configuration file on startup.
101 cu = new CensorUser(ServerInstance);
102 cc = new CensorChannel(ServerInstance);
103 if (!ServerInstance->AddMode(cu, 'G') || !ServerInstance->AddMode(cc, 'G'))
104 throw ModuleException("Could not add new modes!");
107 void Implements(char* List)
109 List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
112 virtual ~ModuleCensor()
114 ServerInstance->Modes->DelMode(cu);
115 ServerInstance->Modes->DelMode(cc);
120 virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
122 if ((!pattern.empty()) && (!text.empty()))
124 std::string::size_type pos;
125 while ((pos = text.find(pattern)) != irc::string::npos)
127 text.erase(pos,pattern.length());
128 text.insert(pos,replace);
133 // format of a config entry is <badword text="shit" replace="poo">
134 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
141 if (target_type == TYPE_USER)
142 active = ((userrec*)dest)->IsModeSet('G');
143 else if (target_type == TYPE_CHANNEL)
144 active = ((chanrec*)dest)->IsModeSet('G');
149 irc::string text2 = text.c_str();
150 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
152 if (text2.find(index->first) != irc::string::npos)
154 if (index->second.empty())
156 user->WriteServ("936 %s %s %s :Your message contained a censored word, and was blocked", user->nick, ((chanrec*)dest)->name, index->first.c_str());
160 this->ReplaceLine(text2,index->first,index->second);
163 text = text2.c_str();
167 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
169 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
172 virtual void OnRehash(userrec* user, const std::string ¶meter)
175 * reload our config file on rehash - we must destroy and re-allocate the classes
176 * to call the constructor again and re-read our data.
178 ConfigReader* MyConf = new ConfigReader(ServerInstance);
180 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
182 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
183 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
184 censors[pattern] = replace;
189 virtual Version GetVersion()
191 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
196 MODULE_INIT(ModuleCensor);