]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcaps.cpp
Dynamically determine the size of the eventlist[] passed to Attach()
[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 /* $ModDesc: Provides support to block all-CAPS channel messages and notices */
26
27
28 /** Handles the +B channel mode
29  */
30 class BlockCaps : public SimpleChannelModeHandler
31 {
32  public:
33         BlockCaps(Module* Creator) : SimpleChannelModeHandler(Creator, "blockcaps", 'B') { }
34 };
35
36 class ModuleBlockCAPS : public Module
37 {
38         BlockCaps bc;
39         int percent;
40         unsigned int minlen;
41         char capsmap[256];
42 public:
43
44         ModuleBlockCAPS() : bc(this)
45         {
46         }
47
48         void init()
49         {
50                 OnRehash(NULL);
51                 if (!ServerInstance->Modes->AddMode(&bc))
52                         throw ModuleException("Could not add new modes!");
53                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash, I_On005Numeric };
54                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
55         }
56
57         virtual void On005Numeric(std::string &output)
58         {
59                 ServerInstance->AddExtBanChar('B');
60         }
61
62         virtual void OnRehash(User* user)
63         {
64                 ReadConf();
65         }
66
67         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
68         {
69                 if (target_type == TYPE_CHANNEL)
70                 {
71                         if ((!IS_LOCAL(user)) || (text.length() < minlen))
72                                 return MOD_RES_PASSTHRU;
73
74                         Channel* c = (Channel*)dest;
75                         ModResult res = ServerInstance->OnCheckExemption(user,c,"blockcaps");
76
77                         if (res == MOD_RES_ALLOW)
78                                 return MOD_RES_PASSTHRU;
79
80                         if (!c->GetExtBanStatus(user, 'B').check(!c->IsModeSet('B')))
81                         {
82                                 int caps = 0;
83                                 const char* actstr = "\1ACTION ";
84                                 int act = 0;
85
86                                 for (std::string::iterator i = text.begin(); i != text.end(); i++)
87                                 {
88                                         /* Smart fix for suggestion from Jobe, ignore CTCP ACTION (part of /ME) */
89                                         if (*actstr && *i == *actstr++ && act != -1)
90                                         {
91                                                 act++;
92                                                 continue;
93                                         }
94                                         else
95                                                 act = -1;
96
97                                         caps += capsmap[(unsigned char)*i];
98                                 }
99                                 if ( ((caps*100)/(int)text.length()) >= percent )
100                                 {
101                                         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);
102                                         return MOD_RES_DENY;
103                                 }
104                         }
105                 }
106                 return MOD_RES_PASSTHRU;
107         }
108
109         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
110         {
111                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
112         }
113
114         void ReadConf()
115         {
116                 ConfigTag* tag = ServerInstance->Config->ConfValue("blockcaps");
117                 percent = tag->getInt("percent", 100);
118                 minlen = tag->getInt("minlen", 1);
119                 std::string hmap = tag->getString("capsmap", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
120                 memset(capsmap, 0, sizeof(capsmap));
121                 for (std::string::iterator n = hmap.begin(); n != hmap.end(); n++)
122                         capsmap[(unsigned char)*n] = 1;
123                 if (percent < 1 || percent > 100)
124                 {
125                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "<blockcaps:percent> out of range, setting to default of 100.");
126                         percent = 100;
127                 }
128                 if (minlen < 1 || minlen > MAXBUF-1)
129                 {
130                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "<blockcaps:minlen> out of range, setting to default of 1.");
131                         minlen = 1;
132                 }
133         }
134
135         virtual ~ModuleBlockCAPS()
136         {
137         }
138
139         virtual Version GetVersion()
140         {
141                 return Version("Provides support to block all-CAPS channel messages and notices", VF_VENDOR);
142         }
143 };
144
145 MODULE_INIT(ModuleBlockCAPS)