2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
5 * Copyright (C) 2013, 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2012-2013 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
10 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
11 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12 * Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
14 * This file is part of InspIRCd. InspIRCd is free software: you can
15 * redistribute it and/or modify it under the terms of the GNU General Public
16 * License as published by the Free Software Foundation, version 2.
18 * This program is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "modules/exemption.h"
31 typedef insp::flat_map<std::string, std::string, irc::insensitive_swo> censor_t;
33 class ModuleCensor : public Module
35 CheckExemption::EventProvider exemptionprov;
37 SimpleUserModeHandler cu;
38 SimpleChannelModeHandler cc;
43 , cu(this, "u_censor", 'G')
44 , cc(this, "censor", 'G')
48 // format of a config entry is <badword text="shit" replace="poo">
49 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
52 return MOD_RES_PASSTHRU;
56 case MessageTarget::TYPE_USER:
58 User* targuser = target.Get<User>();
59 if (!targuser->IsModeSet(cu))
60 return MOD_RES_PASSTHRU;
64 case MessageTarget::TYPE_CHANNEL:
66 Channel* targchan = target.Get<Channel>();
67 if (!targchan->IsModeSet(cc))
68 return MOD_RES_PASSTHRU;
70 ModResult result = CheckExemption::Call(exemptionprov, user, targchan, "censor");
71 if (result == MOD_RES_ALLOW)
72 return MOD_RES_PASSTHRU;
77 return MOD_RES_PASSTHRU;
80 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
83 while ((censorpos = irc::find(details.text, index->first)) != std::string::npos)
85 if (index->second.empty())
87 const std::string msg = InspIRCd::Format("Your message to this channel contained a banned phrase (%s) and was blocked.", index->first.c_str());
88 if (target.type == MessageTarget::TYPE_CHANNEL)
89 user->WriteNumeric(Numerics::CannotSendTo(target.Get<Channel>(), msg));
91 user->WriteNumeric(Numerics::CannotSendTo(target.Get<User>(), msg));
95 details.text.replace(censorpos, index->first.size(), 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 const std::string text = tag->getString("text");
115 throw ModuleException("<badword:text> is empty! at " + tag->getTagLocation());
117 const std::string replace = tag->getString("replace");
118 newcensors[text] = replace;
120 censors.swap(newcensors);
123 Version GetVersion() CXX11_OVERRIDE
125 return Version("Allows the server administrator to define inappropriate phrases that are not allowed to be used in private or channel messages.", VF_VENDOR);
130 MODULE_INIT(ModuleCensor)