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<std::string, std::string, irc::insensitive_swo> censor_t;
28 class ModuleCensor : public Module
30 CheckExemption::EventProvider exemptionprov;
32 SimpleUserModeHandler cu;
33 SimpleChannelModeHandler cc;
38 , cu(this, "u_censor", 'G')
39 , cc(this, "censor", 'G')
43 // format of a config entry is <badword text="shit" replace="poo">
44 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
47 return MOD_RES_PASSTHRU;
51 if (target.type == MessageTarget::TYPE_USER)
52 active = target.Get<User>()->IsModeSet(cu);
53 else if (target.type == MessageTarget::TYPE_CHANNEL)
55 Channel* c = target.Get<Channel>();
56 active = c->IsModeSet(cc);
57 ModResult res = CheckExemption::Call(exemptionprov, user, c, "censor");
59 if (res == MOD_RES_ALLOW)
60 return MOD_RES_PASSTHRU;
64 return MOD_RES_PASSTHRU;
66 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
69 while ((censorpos = irc::find(details.text, index->first)) != std::string::npos)
71 if (index->second.empty())
73 const std::string targname = target.type == MessageTarget::TYPE_CHANNEL ? target.Get<Channel>()->name : target.Get<User>()->nick;
74 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, targname, "Your message contained a censored word (" + index->first + "), and was blocked");
78 details.text.replace(censorpos, index->first.size(), index->second);
81 return MOD_RES_PASSTHRU;
84 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
87 * reload our config file on rehash - we must destroy and re-allocate the classes
88 * to call the constructor again and re-read our data.
92 ConfigTagList badwords = ServerInstance->Config->ConfTags("badword");
93 for (ConfigIter i = badwords.first; i != badwords.second; ++i)
95 ConfigTag* tag = i->second;
96 const std::string text = tag->getString("text");
100 const std::string replace = tag->getString("replace");
101 censors[text] = replace;
105 Version GetVersion() CXX11_OVERRIDE
107 return Version("Provides user and channel +G mode",VF_VENDOR);
112 MODULE_INIT(ModuleCensor)