]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcaps.cpp
c36eeabffd1711fc789304550fe296aaa220c740
[user/henk/code/inspircd.git] / src / modules / m_blockcaps.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
8  *
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.
12  *
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
16  * details.
17  *
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/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "modules/exemption.h"
25
26 class ModuleBlockCAPS : public Module
27 {
28         CheckExemption::EventProvider exemptionprov;
29         SimpleChannelModeHandler bc;
30         unsigned int percent;
31         unsigned int minlen;
32         std::bitset<UCHAR_MAX> lowercase;
33         std::bitset<UCHAR_MAX> uppercase;
34
35 public:
36         ModuleBlockCAPS()
37                 : exemptionprov(this)
38                 , bc(this, "blockcaps", 'B')
39         {
40         }
41
42         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
43         {
44                 tokens["EXTBAN"].push_back('B');
45         }
46
47         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
48         {
49                 if (target_type == TYPE_CHANNEL)
50                 {
51                         if (!IS_LOCAL(user))
52                                 return MOD_RES_PASSTHRU;
53
54                         Channel* c = (Channel*)dest;
55                         ModResult res = CheckExemption::Call(exemptionprov, user, c, "blockcaps");
56
57                         if (res == MOD_RES_ALLOW)
58                                 return MOD_RES_PASSTHRU;
59
60                         if (!c->GetExtBanStatus(user, 'B').check(!c->IsModeSet(bc)))
61                         {
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 = text.begin();
65                                 std::string::const_iterator text_end = text.end();
66                                 if (text[0] == '\1')
67                                 {
68                                         // If the CTCP is not an action then skip it.
69                                         if (text.compare(0, 8, "\1ACTION ", 8))
70                                                 return MOD_RES_PASSTHRU;
71
72                                         // Skip the CTCP message characters.
73                                         text_begin += 8;
74                                         if (*text.rbegin() == '\1')
75                                                 text_end -= 1;
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 = std::distance(text_begin, text_end);
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 = text_begin; iter != text_end; ++iter)
88                                 {
89                                         if (uppercase.test(*iter))
90                                                 upper += 1;
91                                         else if (!lowercase.test(*iter))
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                                         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));
101                                         return MOD_RES_DENY;
102                                 }
103                         }
104                 }
105                 return MOD_RES_PASSTHRU;
106         }
107
108         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
109         {
110                 ConfigTag* tag = ServerInstance->Config->ConfValue("blockcaps");
111                 percent = tag->getInt("percent", 100, 1, 100);
112                 minlen = tag->getInt("minlen", 1, 1, ServerInstance->Config->Limits.MaxLine);
113
114                 lowercase.reset();
115                 const std::string lower = tag->getString("lowercase", "abcdefghijklmnopqrstuvwxyz");
116                 for (std::string::const_iterator iter = lower.begin(); iter != lower.end(); ++iter)
117                         lowercase.set(*iter);
118
119                 uppercase.reset();
120                 const std::string upper = tag->getString("uppercase", tag->getString("capsmap", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
121                 for (std::string::const_iterator iter = upper.begin(); iter != upper.end(); ++iter)
122                         uppercase.set(*iter);
123         }
124
125         Version GetVersion() CXX11_OVERRIDE
126         {
127                 return Version("Provides support to block all-CAPS channel messages and notices", VF_VENDOR);
128         }
129 };
130
131 MODULE_INIT(ModuleBlockCAPS)