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>
13 * This file is part of InspIRCd. InspIRCd is free software: you can
14 * redistribute it and/or modify it under the terms of the GNU General Public
15 * License as published by the Free Software Foundation, version 2.
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "modules/exemption.h"
30 class ModuleBlockCAPS : public Module
32 CheckExemption::EventProvider exemptionprov;
33 SimpleChannelModeHandler bc;
36 std::bitset<UCHAR_MAX> lowercase;
37 std::bitset<UCHAR_MAX> uppercase;
42 , bc(this, "blockcaps", 'B')
46 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
48 tokens["EXTBAN"].push_back('B');
51 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
53 if (target.type == MessageTarget::TYPE_CHANNEL)
56 return MOD_RES_PASSTHRU;
58 Channel* c = target.Get<Channel>();
59 ModResult res = CheckExemption::Call(exemptionprov, user, c, "blockcaps");
61 if (res == MOD_RES_ALLOW)
62 return MOD_RES_PASSTHRU;
64 if (!c->GetExtBanStatus(user, 'B').check(!c->IsModeSet(bc)))
66 // If the message is a CTCP then we skip it unless it is
67 // an ACTION in which case we just check against the body.
69 std::string message(details.text);
70 if (details.IsCTCP(ctcpname, message))
72 // If the CTCP is not an action then skip it.
73 if (!irc::equals(ctcpname, "ACTION"))
74 return MOD_RES_PASSTHRU;
77 // If the message is shorter than the minimum length
78 // then we don't need to do anything else.
79 size_t length = message.length();
81 return MOD_RES_PASSTHRU;
83 // Count the characters to see how many upper case and
84 // ignored (non upper or lower) characters there are.
86 for (std::string::const_iterator iter = message.begin(); iter != message.end(); ++iter)
88 unsigned char chr = static_cast<unsigned char>(*iter);
89 if (uppercase.test(chr))
91 else if (!lowercase.test(chr))
95 // Calculate the percentage which is upper case. If the
96 // message was entirely symbols then it can't contain
97 // any upper case letters.
98 if (length > 0 && round((upper * 100) / length) >= percent)
100 const std::string msg = InspIRCd::Format("Your message cannot contain %d%% or more capital letters if it's longer than %d characters", percent, minlen);
101 user->WriteNumeric(Numerics::CannotSendTo(c, msg));
106 return MOD_RES_PASSTHRU;
109 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
111 ConfigTag* tag = ServerInstance->Config->ConfValue("blockcaps");
112 percent = tag->getUInt("percent", 100, 1, 100);
113 minlen = tag->getUInt("minlen", 1, 1, ServerInstance->Config->Limits.MaxLine);
116 const std::string lower = tag->getString("lowercase", "abcdefghijklmnopqrstuvwxyz");
117 for (std::string::const_iterator iter = lower.begin(); iter != lower.end(); ++iter)
118 lowercase.set(static_cast<unsigned char>(*iter));
121 const std::string upper = tag->getString("uppercase", tag->getString("capsmap", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1));
122 for (std::string::const_iterator iter = upper.begin(); iter != upper.end(); ++iter)
123 uppercase.set(static_cast<unsigned char>(*iter));
126 Version GetVersion() CXX11_OVERRIDE
128 return Version("Adds channel mode B (blockcaps) which allows channels to block messages which are excessively capitalised.", VF_VENDOR);
132 MODULE_INIT(ModuleBlockCAPS)