2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2004, 2008-2009 Craig Edwards <craigedwards@brainbox.cc>
6 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7 * Copyright (C) 2005, 2007 Robin Burchell <robin+git@viroteck.net>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "modules/exemption.h"
26 typedef insp::flat_map<irc::string, irc::string> censor_t;
28 /** Handles usermode +G
30 class CensorUser : public SimpleUserModeHandler
33 CensorUser(Module* Creator) : SimpleUserModeHandler(Creator, "u_censor", 'G') { }
36 /** Handles channel mode +G
38 class CensorChannel : public SimpleChannelModeHandler
41 CensorChannel(Module* Creator) : SimpleChannelModeHandler(Creator, "censor", 'G') { }
44 class ModuleCensor : public Module
46 CheckExemption::EventProvider exemptionprov;
59 // format of a config entry is <badword text="shit" replace="poo">
60 ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
63 return MOD_RES_PASSTHRU;
67 if (target_type == TYPE_USER)
68 active = ((User*)dest)->IsModeSet(cu);
69 else if (target_type == TYPE_CHANNEL)
71 Channel* c = (Channel*)dest;
72 active = c->IsModeSet(cc);
74 FIRST_MOD_RESULT_CUSTOM(exemptionprov, CheckExemption::EventListener, OnCheckExemption, res, (user, c, "censor"));
76 if (res == MOD_RES_ALLOW)
77 return MOD_RES_PASSTHRU;
81 return MOD_RES_PASSTHRU;
83 irc::string text2 = text.c_str();
84 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
86 if (text2.find(index->first) != irc::string::npos)
88 if (index->second.empty())
90 user->WriteNumeric(ERR_WORDFILTERED, ((target_type == TYPE_CHANNEL) ? ((Channel*)dest)->name : ((User*)dest)->nick), index->first.c_str(), "Your message contained a censored word, and was blocked");
94 stdalgo::string::replace_all(text2, index->first, index->second);
98 return MOD_RES_PASSTHRU;
101 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
104 * reload our config file on rehash - we must destroy and re-allocate the classes
105 * to call the constructor again and re-read our data.
109 ConfigTagList badwords = ServerInstance->Config->ConfTags("badword");
110 for (ConfigIter i = badwords.first; i != badwords.second; ++i)
112 ConfigTag* tag = i->second;
113 std::string str = tag->getString("text");
114 irc::string pattern(str.c_str());
115 str = tag->getString("replace");
116 censors[pattern] = irc::string(str.c_str());
120 Version GetVersion() CXX11_OVERRIDE
122 return Version("Provides user and channel +G mode",VF_VENDOR);
127 MODULE_INIT(ModuleCensor)