]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcaps.cpp
Merge insp20
[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
25
26 /** Handles the +B channel mode
27  */
28 class BlockCaps : public SimpleChannelModeHandler
29 {
30  public:
31         BlockCaps(Module* Creator) : SimpleChannelModeHandler(Creator, "blockcaps", 'B') { }
32 };
33
34 class ModuleBlockCAPS : public Module
35 {
36         BlockCaps bc;
37         int percent;
38         unsigned int minlen;
39         char capsmap[256];
40
41 public:
42         ModuleBlockCAPS() : bc(this)
43         {
44         }
45
46         void init() CXX11_OVERRIDE
47         {
48                 OnRehash(NULL);
49                 ServerInstance->Modules->AddService(bc);
50         }
51
52         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
53         {
54                 tokens["EXTBAN"].push_back('B');
55         }
56
57         void OnRehash(User* user) CXX11_OVERRIDE
58         {
59                 ReadConf();
60         }
61
62         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
63         {
64                 if (target_type == TYPE_CHANNEL)
65                 {
66                         if ((!IS_LOCAL(user)) || (text.length() < minlen))
67                                 return MOD_RES_PASSTHRU;
68
69                         Channel* c = (Channel*)dest;
70                         ModResult res = ServerInstance->OnCheckExemption(user,c,"blockcaps");
71
72                         if (res == MOD_RES_ALLOW)
73                                 return MOD_RES_PASSTHRU;
74
75                         if (!c->GetExtBanStatus(user, 'B').check(!c->IsModeSet(bc)))
76                         {
77                                 int caps = 0;
78                                 const char* actstr = "\1ACTION ";
79                                 int act = 0;
80
81                                 for (std::string::iterator i = text.begin(); i != text.end(); i++)
82                                 {
83                                         /* Smart fix for suggestion from Jobe, ignore CTCP ACTION (part of /ME) */
84                                         if (*actstr && *i == *actstr++ && act != -1)
85                                         {
86                                                 act++;
87                                                 continue;
88                                         }
89                                         else
90                                                 act = -1;
91
92                                         caps += capsmap[(unsigned char)*i];
93                                 }
94                                 if ( ((caps*100)/(int)text.length()) >= percent )
95                                 {
96                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, "%s %s :Your message cannot contain more than %d%% capital letters if it's longer than %d characters", user->nick.c_str(), c->name.c_str(), percent, minlen);
97                                         return MOD_RES_DENY;
98                                 }
99                         }
100                 }
101                 return MOD_RES_PASSTHRU;
102         }
103
104         void ReadConf()
105         {
106                 ConfigTag* tag = ServerInstance->Config->ConfValue("blockcaps");
107                 percent = tag->getInt("percent", 100, 1, 100);
108                 minlen = tag->getInt("minlen", 1, 1, ServerInstance->Config->Limits.MaxLine);
109                 std::string hmap = tag->getString("capsmap", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
110                 memset(capsmap, 0, sizeof(capsmap));
111                 for (std::string::iterator n = hmap.begin(); n != hmap.end(); n++)
112                         capsmap[(unsigned char)*n] = 1;
113         }
114
115         Version GetVersion() CXX11_OVERRIDE
116         {
117                 return Version("Provides support to block all-CAPS channel messages and notices", VF_VENDOR);
118         }
119 };
120
121 MODULE_INIT(ModuleBlockCAPS)