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;
57 case MessageTarget::TYPE_USER:
59 User* targuser = target.Get<User>();
60 if (!targuser->IsModeSet(cu))
61 return MOD_RES_PASSTHRU;
63 numeric = ERR_CANTSENDTOUSER;
67 case MessageTarget::TYPE_CHANNEL:
69 Channel* targchan = target.Get<Channel>();
70 if (!targchan->IsModeSet(cc))
71 return MOD_RES_PASSTHRU;
73 ModResult result = CheckExemption::Call(exemptionprov, user, targchan, "censor");
74 if (result == MOD_RES_ALLOW)
75 return MOD_RES_PASSTHRU;
77 numeric = ERR_CANNOTSENDTOCHAN;
82 return MOD_RES_PASSTHRU;
85 for (censor_t::iterator index = censors.begin(); index != censors.end(); index++)
88 while ((censorpos = irc::find(details.text, index->first)) != std::string::npos)
90 if (index->second.empty())
92 user->WriteNumeric(numeric, target.GetName(), "Your message contained a censored word (" + index->first + "), and was blocked");
96 details.text.replace(censorpos, index->first.size(), index->second);
99 return MOD_RES_PASSTHRU;
102 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
105 * reload our config file on rehash - we must destroy and re-allocate the classes
106 * to call the constructor again and re-read our data.
110 ConfigTagList badwords = ServerInstance->Config->ConfTags("badword");
111 for (ConfigIter i = badwords.first; i != badwords.second; ++i)
113 ConfigTag* tag = i->second;
114 const std::string text = tag->getString("text");
116 throw ModuleException("<badword:text> is empty! at " + tag->getTagLocation());
118 const std::string replace = tag->getString("replace");
119 newcensors[text] = replace;
121 censors.swap(newcensors);
124 Version GetVersion() CXX11_OVERRIDE
126 return Version("Provides user and channel mode +G", VF_VENDOR);
131 MODULE_INIT(ModuleCensor)