]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcaps.cpp
Update all wiki links to point to the new wiki. This was done automatically with...
[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) : SimpleChannelModeHandler(Instance, '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)
36         {
37                 OnRehash(NULL,"");
38                 bc = new BlockCaps(ServerInstance);
39                 if (!ServerInstance->Modes->AddMode(bc))
40                 {
41                         delete bc;
42                         throw ModuleException("Could not add new modes!");
43                 }
44                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash, I_On005Numeric };
45                 ServerInstance->Modules->Attach(eventlist, this, 4);
46         }
47
48         virtual void On005Numeric(std::string &output)
49         {
50                 ServerInstance->AddExtBanChar('B');
51         }
52
53         virtual void OnRehash(User* user, const std::string &param)
54         {
55                 ReadConf();
56         }
57
58         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
59         {
60                 if (target_type == TYPE_CHANNEL)
61                 {
62                         if ((!IS_LOCAL(user)) || (text.length() < minlen))
63                                 return 0;
64
65                         Channel* c = (Channel*)dest;
66
67                         if (CHANOPS_EXEMPT(ServerInstance, 'B') && c->GetStatus(user) == STATUS_OP)
68                         {
69                                 return 0;
70                         }
71
72                         if (c->IsModeSet('B') || c->GetExtBanStatus(user, 'B') < 0)
73                         {
74                                 int caps = 0;
75                                 const char* actstr = "\1ACTION ";
76                                 int act = 0;
77
78                                 for (std::string::iterator i = text.begin(); i != text.end(); i++)
79                                 {
80                                         /* Smart fix for suggestion from Jobe, ignore CTCP ACTION (part of /ME) */
81                                         if (*actstr && *i == *actstr++ && act != -1)
82                                         {
83                                                 act++;
84                                                 continue;
85                                         }
86                                         else
87                                                 act = -1;
88
89                                         caps += capsmap[(unsigned char)*i];
90                                 }
91                                 if ( ((caps*100)/(int)text.length()) >= percent )
92                                 {
93                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, "%s %s :Your line cannot be more than %d%% capital letters if it is %d or more letters long", user->nick.c_str(), c->name.c_str(), percent, minlen);
94                                         return 1;
95                                 }
96                         }
97                 }
98                 return 0;
99         }
100
101         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
102         {
103                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
104         }
105
106         void ReadConf()
107         {
108                 ConfigReader Conf(ServerInstance);
109                 percent = Conf.ReadInteger("blockcaps", "percent", "100", 0, true);
110                 minlen = Conf.ReadInteger("blockcaps", "minlen", "1", 0, true);
111                 std::string hmap = Conf.ReadValue("blockcaps", "capsmap", 0);
112                 if (hmap.empty())
113                         hmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
114                 memset(capsmap, 0, sizeof(capsmap));
115                 for (std::string::iterator n = hmap.begin(); n != hmap.end(); n++)
116                         capsmap[(unsigned char)*n] = 1;
117                 if (percent < 1 || percent > 100)
118                 {
119                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "<blockcaps:percent> out of range, setting to default of 100.");
120                         percent = 100;
121                 }
122                 if (minlen < 1 || minlen > MAXBUF-1)
123                 {
124                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "<blockcaps:minlen> out of range, setting to default of 1.");
125                         minlen = 1;
126                 }
127         }
128
129         virtual ~ModuleBlockCAPS()
130         {
131                 ServerInstance->Modes->DelMode(bc);
132                 delete bc;
133         }
134
135         virtual Version GetVersion()
136         {
137                 return Version("$Id$", VF_COMMON|VF_VENDOR,API_VERSION);
138         }
139 };
140
141 MODULE_INIT(ModuleBlockCAPS)