]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcaps.cpp
3bf9c98d0940ab44354cffb481359b6b0c95f103
[user/henk/code/inspircd.git] / src / modules / m_blockcaps.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
13  *
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.
17  *
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
21  * details.
22  *
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/>.
25  */
26
27
28 #include "inspircd.h"
29 #include "modules/exemption.h"
30
31 class ModuleBlockCAPS : public Module
32 {
33         CheckExemption::EventProvider exemptionprov;
34         SimpleChannelModeHandler bc;
35         unsigned int percent;
36         unsigned int minlen;
37         std::bitset<UCHAR_MAX> lowercase;
38         std::bitset<UCHAR_MAX> uppercase;
39
40 public:
41         ModuleBlockCAPS()
42                 : exemptionprov(this)
43                 , bc(this, "blockcaps", 'B')
44         {
45         }
46
47         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
48         {
49                 tokens["EXTBAN"].push_back('B');
50         }
51
52         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
53         {
54                 if (target.type == MessageTarget::TYPE_CHANNEL)
55                 {
56                         if (!IS_LOCAL(user))
57                                 return MOD_RES_PASSTHRU;
58
59                         Channel* c = target.Get<Channel>();
60                         ModResult res = CheckExemption::Call(exemptionprov, user, c, "blockcaps");
61
62                         if (res == MOD_RES_ALLOW)
63                                 return MOD_RES_PASSTHRU;
64
65                         if (!c->GetExtBanStatus(user, 'B').check(!c->IsModeSet(bc)))
66                         {
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.
69                                 std::string ctcpname;
70                                 std::string message(details.text);
71                                 if (details.IsCTCP(ctcpname, message))
72                                 {
73                                         // If the CTCP is not an action then skip it.
74                                         if (!irc::equals(ctcpname, "ACTION"))
75                                                 return MOD_RES_PASSTHRU;
76                                 }
77
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();
81                                 if (length < minlen)
82                                         return MOD_RES_PASSTHRU;
83
84                                 // Count the characters to see how many upper case and
85                                 // ignored (non upper or lower) characters there are.
86                                 size_t upper = 0;
87                                 for (std::string::const_iterator iter = message.begin(); iter != message.end(); ++iter)
88                                 {
89                                         unsigned char chr = static_cast<unsigned char>(*iter);
90                                         if (uppercase.test(chr))
91                                                 upper += 1;
92                                         else if (!lowercase.test(chr))
93                                                 length -= 1;
94                                 }
95
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)
100                                 {
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));
103                                         return MOD_RES_DENY;
104                                 }
105                         }
106                 }
107                 return MOD_RES_PASSTHRU;
108         }
109
110         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
111         {
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);
115
116                 lowercase.reset();
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));
120
121                 uppercase.reset();
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));
125         }
126
127         Version GetVersion() CXX11_OVERRIDE
128         {
129                 return Version("Adds channel mode B (blockcaps) which allows channels to block messages which are excessively capitalised.", VF_VENDOR);
130         }
131 };
132
133 MODULE_INIT(ModuleBlockCAPS)