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