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 SimpleUserModeHandler
28 CensorUser(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'G') { }
31 /** Handles channel mode +G
33 class CensorChannel : public SimpleChannelModeHandler
36 CensorChannel(InspIRCd* Instance) : SimpleChannelModeHandler(Instance, 'G') { }
39 class ModuleCensor : public Module
46 ModuleCensor(InspIRCd* Me)
49 /* Read the configuration file on startup.
52 cu = new CensorUser(ServerInstance);
53 cc = new CensorChannel(ServerInstance);
54 if (!ServerInstance->Modes->AddMode(cu) || !ServerInstance->Modes->AddMode(cc))
58 throw ModuleException("Could not add new modes!");
60 Implementation eventlist[] = { I_OnRehash, I_OnUserPreMessage, I_OnUserPreNotice };
61 ServerInstance->Modules->Attach(eventlist, this, 3);
65 virtual ~ModuleCensor()
67 ServerInstance->Modes->DelMode(cu);
68 ServerInstance->Modes->DelMode(cc);
73 virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
75 if ((!pattern.empty()) && (!text.empty()))
77 std::string::size_type pos;
78 while ((pos = text.find(pattern)) != irc::string::npos)
80 text.erase(pos,pattern.length());
81 text.insert(pos,replace);
86 // format of a config entry is <badword text="shit" replace="poo">
87 virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
94 if (target_type == TYPE_USER)
95 active = ((User*)dest)->IsModeSet('G');
96 else if (target_type == TYPE_CHANNEL)
98 active = ((Channel*)dest)->IsModeSet('G');
99 Channel* c = (Channel*)dest;
100 if (CHANOPS_EXEMPT(ServerInstance, 'G') && c->GetStatus(user) == STATUS_OP)
109 irc::string text2 = text.c_str();
110 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
112 if (text2.find(index->first) != irc::string::npos)
114 if (index->second.empty())
116 user->WriteNumeric(ERR_WORDFILTERED, "%s %s %s :Your message contained a censored word, and was blocked", user->nick.c_str(), ((Channel*)dest)->name.c_str(), index->first.c_str());
120 this->ReplaceLine(text2,index->first,index->second);
123 text = text2.c_str();
127 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
129 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
132 virtual void OnRehash(User* user, const std::string ¶meter)
135 * reload our config file on rehash - we must destroy and re-allocate the classes
136 * to call the constructor again and re-read our data.
138 ConfigReader* MyConf = new ConfigReader(ServerInstance);
141 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
143 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
144 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
145 censors[pattern] = replace;
151 virtual Version GetVersion()
153 return Version("$Id$",VF_COMMON|VF_VENDOR,API_VERSION);
158 MODULE_INIT(ModuleCensor)