2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2006, 2008 Craig Edwards <craigedwards@brainbox.cc>
5 * Copyright (C) 2006-2007 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
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 class ModuleBlockCAPS : public Module
28 CheckExemption::EventProvider exemptionprov;
29 SimpleChannelModeHandler bc;
32 std::bitset<UCHAR_MAX> lowercase;
33 std::bitset<UCHAR_MAX> uppercase;
38 , bc(this, "blockcaps", 'B')
42 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
44 tokens["EXTBAN"].push_back('B');
47 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
49 if (target.type == MessageTarget::TYPE_CHANNEL)
52 return MOD_RES_PASSTHRU;
54 Channel* c = target.Get<Channel>();
55 ModResult res = CheckExemption::Call(exemptionprov, user, c, "blockcaps");
57 if (res == MOD_RES_ALLOW)
58 return MOD_RES_PASSTHRU;
60 if (!c->GetExtBanStatus(user, 'B').check(!c->IsModeSet(bc)))
62 // If the message is a CTCP then we skip it unless it is
63 // an ACTION in which case we strip the prefix and suffix.
64 std::string::const_iterator text_begin = details.text.begin();
65 std::string::const_iterator text_end = details.text.end();
66 if (details.text[0] == '\1')
68 // If the CTCP is not an action then skip it.
69 if (details.text.compare(0, 8, "\1ACTION ", 8))
70 return MOD_RES_PASSTHRU;
72 // Skip the CTCP message characters.
74 if (*details.text.rbegin() == '\1')
78 // If the message is shorter than the minimum length
79 // then we don't need to do anything else.
80 size_t length = std::distance(text_begin, text_end);
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 = text_begin; iter != text_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 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, c->name, InspIRCd::Format("Your message cannot contain %d%% or more capital letters if it's longer than %d characters", percent, minlen));
106 return MOD_RES_PASSTHRU;
109 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
111 ConfigTag* tag = ServerInstance->Config->ConfValue("blockcaps");
112 percent = tag->getInt("percent", 100, 1, 100);
113 minlen = tag->getInt("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"));
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("Provides support to block all-CAPS channel messages and notices", VF_VENDOR);
132 MODULE_INIT(ModuleBlockCAPS)