]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcaps.cpp
88c09d91cf52ecc37ac0aa3c77480554f8e67ad2
[user/henk/code/inspircd.git] / src / modules / m_blockcaps.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides support to block all-CAPS channel messages and notices */
17
18
19 /** Handles the +P channel mode
20  */
21 class BlockCaps : public SimpleChannelModeHandler
22 {
23  public:
24         BlockCaps(InspIRCd* Instance, Module* Creator) : SimpleChannelModeHandler(Instance, Creator, 'B') { }
25 };
26
27 class ModuleBlockCAPS : public Module
28 {
29         BlockCaps bc;
30         int percent;
31         unsigned int minlen;
32         char capsmap[256];
33 public:
34
35         ModuleBlockCAPS(InspIRCd* Me) : Module(Me), bc(Me, this)
36         {
37                 OnRehash(NULL);
38                 if (!ServerInstance->Modes->AddMode(&bc))
39                         throw ModuleException("Could not add new modes!");
40                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash, I_On005Numeric };
41                 ServerInstance->Modules->Attach(eventlist, this, 4);
42         }
43
44         virtual void On005Numeric(std::string &output)
45         {
46                 ServerInstance->AddExtBanChar('B');
47         }
48
49         virtual void OnRehash(User* user)
50         {
51                 ReadConf();
52         }
53
54         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
55         {
56                 if (target_type == TYPE_CHANNEL)
57                 {
58                         if ((!IS_LOCAL(user)) || (text.length() < minlen))
59                                 return MOD_RES_PASSTHRU;
60
61                         Channel* c = (Channel*)dest;
62
63                         if (CHANOPS_EXEMPT(ServerInstance, 'B') && c->GetPrefixValue(user) == OP_VALUE)
64                         {
65                                 return MOD_RES_PASSTHRU;
66                         }
67
68                         if (!c->GetExtBanStatus(user, 'B').check(!c->IsModeSet('B')))
69                         {
70                                 int caps = 0;
71                                 const char* actstr = "\1ACTION ";
72                                 int act = 0;
73
74                                 for (std::string::iterator i = text.begin(); i != text.end(); i++)
75                                 {
76                                         /* Smart fix for suggestion from Jobe, ignore CTCP ACTION (part of /ME) */
77                                         if (*actstr && *i == *actstr++ && act != -1)
78                                         {
79                                                 act++;
80                                                 continue;
81                                         }
82                                         else
83                                                 act = -1;
84
85                                         caps += capsmap[(unsigned char)*i];
86                                 }
87                                 if ( ((caps*100)/(int)text.length()) >= percent )
88                                 {
89                                         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);
90                                         return MOD_RES_DENY;
91                                 }
92                         }
93                 }
94                 return MOD_RES_PASSTHRU;
95         }
96
97         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
98         {
99                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
100         }
101
102         void ReadConf()
103         {
104                 ConfigReader Conf(ServerInstance);
105                 percent = Conf.ReadInteger("blockcaps", "percent", "100", 0, true);
106                 minlen = Conf.ReadInteger("blockcaps", "minlen", "1", 0, true);
107                 std::string hmap = Conf.ReadValue("blockcaps", "capsmap", 0);
108                 if (hmap.empty())
109                         hmap = "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                 if (percent < 1 || percent > 100)
114                 {
115                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "<blockcaps:percent> out of range, setting to default of 100.");
116                         percent = 100;
117                 }
118                 if (minlen < 1 || minlen > MAXBUF-1)
119                 {
120                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "<blockcaps:minlen> out of range, setting to default of 1.");
121                         minlen = 1;
122                 }
123         }
124
125         virtual ~ModuleBlockCAPS()
126         {
127                 ServerInstance->Modes->DelMode(&bc);
128         }
129
130         virtual Version GetVersion()
131         {
132                 return Version("Provides support to block all-CAPS channel messages and notices", VF_COMMON|VF_VENDOR,API_VERSION);
133         }
134 };
135
136 MODULE_INIT(ModuleBlockCAPS)