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
48 ModuleCensor(InspIRCd* Me)
51 /* Read the configuration file on startup.
54 cu = new CensorUser(ServerInstance);
55 cc = new CensorChannel(ServerInstance);
56 if (!ServerInstance->Modes->AddMode(cu) || !ServerInstance->Modes->AddMode(cc))
60 throw ModuleException("Could not add new modes!");
62 Implementation eventlist[] = { I_OnRehash, I_OnUserPreMessage, I_OnUserPreNotice };
63 ServerInstance->Modules->Attach(eventlist, this, 3);
67 virtual ~ModuleCensor()
69 ServerInstance->Modes->DelMode(cu);
70 ServerInstance->Modes->DelMode(cc);
75 virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
77 if ((!pattern.empty()) && (!text.empty()))
79 std::string::size_type pos;
80 while ((pos = text.find(pattern)) != irc::string::npos)
82 text.erase(pos,pattern.length());
83 text.insert(pos,replace);
88 // format of a config entry is <badword text="shit" replace="poo">
89 virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
96 if (target_type == TYPE_USER)
97 active = ((User*)dest)->IsModeSet('G');
98 else if (target_type == TYPE_CHANNEL)
100 active = ((Channel*)dest)->IsModeSet('G');
101 Channel* c = (Channel*)dest;
102 if (CHANOPS_EXEMPT(ServerInstance, 'G') && c->GetStatus(user) == STATUS_OP)
111 irc::string text2 = text.c_str();
112 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
114 if (text2.find(index->first) != irc::string::npos)
116 if (index->second.empty())
118 user->WriteNumeric(936, "%s %s %s :Your message contained a censored word, and was blocked", user->nick.c_str(), ((Channel*)dest)->name, index->first.c_str());
122 this->ReplaceLine(text2,index->first,index->second);
125 text = text2.c_str();
129 virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
131 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
134 virtual void OnRehash(User* user, const std::string ¶meter)
137 * reload our config file on rehash - we must destroy and re-allocate the classes
138 * to call the constructor again and re-read our data.
140 ConfigReader* MyConf = new ConfigReader(ServerInstance);
143 for (int index = 0; index < MyConf->Enumerate("badword"); index++)
145 irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
146 irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
147 censors[pattern] = replace;
153 virtual Version GetVersion()
155 return Version(1,2,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
160 MODULE_INIT(ModuleCensor)