]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcaps.cpp
Replace [cC]olour with [cC]olor
[user/henk/code/inspircd.git] / src / modules / m_blockcaps.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 +B channel mode
20  */
21 class BlockCaps : public SimpleChannelModeHandler
22 {
23  public:
24         BlockCaps(Module* Creator) : SimpleChannelModeHandler(Creator, "blockcaps", '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() : bc(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                         ModResult res = ServerInstance->OnCheckExemption(user,c,"blockcaps");
63
64                         if (res == MOD_RES_ALLOW)
65                                 return MOD_RES_PASSTHRU;
66
67                         if (!c->GetExtBanStatus(user, 'B').check(!c->IsModeSet('B')))
68                         {
69                                 int caps = 0;
70                                 const char* actstr = "\1ACTION ";
71                                 int act = 0;
72
73                                 for (std::string::iterator i = text.begin(); i != text.end(); i++)
74                                 {
75                                         /* Smart fix for suggestion from Jobe, ignore CTCP ACTION (part of /ME) */
76                                         if (*actstr && *i == *actstr++ && act != -1)
77                                         {
78                                                 act++;
79                                                 continue;
80                                         }
81                                         else
82                                                 act = -1;
83
84                                         caps += capsmap[(unsigned char)*i];
85                                 }
86                                 if ( ((caps*100)/(int)text.length()) >= percent )
87                                 {
88                                         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);
89                                         return MOD_RES_DENY;
90                                 }
91                         }
92                 }
93                 return MOD_RES_PASSTHRU;
94         }
95
96         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
97         {
98                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
99         }
100
101         void ReadConf()
102         {
103                 ConfigReader Conf;
104                 percent = Conf.ReadInteger("blockcaps", "percent", "100", 0, true);
105                 minlen = Conf.ReadInteger("blockcaps", "minlen", "1", 0, true);
106                 std::string hmap = Conf.ReadValue("blockcaps", "capsmap", 0);
107                 if (hmap.empty())
108                         hmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
109                 memset(capsmap, 0, sizeof(capsmap));
110                 for (std::string::iterator n = hmap.begin(); n != hmap.end(); n++)
111                         capsmap[(unsigned char)*n] = 1;
112                 if (percent < 1 || percent > 100)
113                 {
114                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "<blockcaps:percent> out of range, setting to default of 100.");
115                         percent = 100;
116                 }
117                 if (minlen < 1 || minlen > MAXBUF-1)
118                 {
119                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "<blockcaps:minlen> out of range, setting to default of 1.");
120                         minlen = 1;
121                 }
122         }
123
124         virtual ~ModuleBlockCAPS()
125         {
126         }
127
128         virtual Version GetVersion()
129         {
130                 return Version("Provides support to block all-CAPS channel messages and notices", VF_VENDOR);
131         }
132 };
133
134 MODULE_INIT(ModuleBlockCAPS)