2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2012-2014 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
9 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
10 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11 * Copyright (C) 2006, 2008-2009 Craig Edwards <brain@inspircd.org>
12 * Copyright (C) 2006 Oliver Lupton <om@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 class ModuleBlockCAPS : public Module
33 CheckExemption::EventProvider exemptionprov;
34 SimpleChannelModeHandler bc;
37 std::bitset<UCHAR_MAX> lowercase;
38 std::bitset<UCHAR_MAX> uppercase;
43 , bc(this, "blockcaps", 'B')
47 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
49 tokens["EXTBAN"].push_back('B');
52 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
54 if (target.type == MessageTarget::TYPE_CHANNEL)
57 return MOD_RES_PASSTHRU;
59 Channel* c = target.Get<Channel>();
60 ModResult res = CheckExemption::Call(exemptionprov, user, c, "blockcaps");
62 if (res == MOD_RES_ALLOW)
63 return MOD_RES_PASSTHRU;
65 if (!c->GetExtBanStatus(user, 'B').check(!c->IsModeSet(bc)))
67 // If the message is a CTCP then we skip it unless it is
68 // an ACTION in which case we just check against the body.
70 std::string message(details.text);
71 if (details.IsCTCP(ctcpname, message))
73 // If the CTCP is not an action then skip it.
74 if (!irc::equals(ctcpname, "ACTION"))
75 return MOD_RES_PASSTHRU;
78 // If the message is shorter than the minimum length
79 // then we don't need to do anything else.
80 size_t length = message.length();
82 return MOD_RES_PASSTHRU;
84 // Count the characters to see how many upper case and
85 // ignored (non upper or lower) characters there are.
87 for (std::string::const_iterator iter = message.begin(); iter != message.end(); ++iter)
89 unsigned char chr = static_cast<unsigned char>(*iter);
90 if (uppercase.test(chr))
92 else if (!lowercase.test(chr))
96 // Calculate the percentage which is upper case. If the
97 // message was entirely symbols then it can't contain
98 // any upper case letters.
99 if (length > 0 && round((upper * 100) / length) >= percent)
101 const std::string msg = InspIRCd::Format("Your message cannot contain %d%% or more capital letters if it's longer than %d characters", percent, minlen);
102 user->WriteNumeric(Numerics::CannotSendTo(c, msg));
107 return MOD_RES_PASSTHRU;
110 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
112 ConfigTag* tag = ServerInstance->Config->ConfValue("blockcaps");
113 percent = tag->getUInt("percent", 100, 1, 100);
114 minlen = tag->getUInt("minlen", 1, 1, ServerInstance->Config->Limits.MaxLine);
117 const std::string lower = tag->getString("lowercase", "abcdefghijklmnopqrstuvwxyz");
118 for (std::string::const_iterator iter = lower.begin(); iter != lower.end(); ++iter)
119 lowercase.set(static_cast<unsigned char>(*iter));
122 const std::string upper = tag->getString("uppercase", tag->getString("capsmap", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1));
123 for (std::string::const_iterator iter = upper.begin(); iter != upper.end(); ++iter)
124 uppercase.set(static_cast<unsigned char>(*iter));
127 Version GetVersion() CXX11_OVERRIDE
129 return Version("Adds channel mode B (blockcaps) which allows channels to block messages which are excessively capitalised.", VF_VENDOR);
133 MODULE_INIT(ModuleBlockCAPS)