]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcaps.cpp
b5347e9ad12ef97a03ba924d1805958a241fa3fd
[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> capsmap;
33
34 public:
35         ModuleBlockCAPS()
36                 : exemptionprov(this)
37                 , bc(this, "blockcaps", 'B')
38         {
39         }
40
41         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
42         {
43                 tokens["EXTBAN"].push_back('B');
44         }
45
46         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
47         {
48                 if (target_type == TYPE_CHANNEL)
49                 {
50                         if ((!IS_LOCAL(user)) || (text.length() < minlen) || (text == "\1ACTION\1") || (text == "\1ACTION"))
51                                 return MOD_RES_PASSTHRU;
52
53                         Channel* c = (Channel*)dest;
54                         ModResult res = CheckExemption::Call(exemptionprov, user, c, "blockcaps");
55
56                         if (res == MOD_RES_ALLOW)
57                                 return MOD_RES_PASSTHRU;
58
59                         if (!c->GetExtBanStatus(user, 'B').check(!c->IsModeSet(bc)))
60                         {
61                                 std::string::size_type caps = 0;
62                                 unsigned int offset = 0;
63                                 // Ignore the beginning of the text if it's a CTCP ACTION (/me)
64                                 if (!text.compare(0, 8, "\1ACTION ", 8))
65                                         offset = 8;
66
67                                 for (std::string::const_iterator i = text.begin() + offset; i != text.end(); ++i)
68                                         if (capsmap.test(*i))
69                                                 caps += 1;
70
71                                 if (((caps * 100) / text.length()) >= percent)
72                                 {
73                                         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));
74                                         return MOD_RES_DENY;
75                                 }
76                         }
77                 }
78                 return MOD_RES_PASSTHRU;
79         }
80
81         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
82         {
83                 ConfigTag* tag = ServerInstance->Config->ConfValue("blockcaps");
84                 percent = tag->getInt("percent", 100, 1, 100);
85                 minlen = tag->getInt("minlen", 1, 1, ServerInstance->Config->Limits.MaxLine);
86                 std::string hmap = tag->getString("capsmap", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
87                 capsmap.reset();
88                 for (std::string::iterator n = hmap.begin(); n != hmap.end(); n++)
89                         capsmap.set(*n);
90         }
91
92         Version GetVersion() CXX11_OVERRIDE
93         {
94                 return Version("Provides support to block all-CAPS channel messages and notices", VF_VENDOR);
95         }
96 };
97
98 MODULE_INIT(ModuleBlockCAPS)