]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcaps.cpp
If nobody else is going to submit a diff, may as well do it myself. Using your noggin...
[user/henk/code/inspircd.git] / src / modules / m_blockcaps.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 };
45                 ServerInstance->Modules->Attach(eventlist, this, 3);
46         }
47
48
49         virtual void OnRehash(User* user, const std::string &param)
50         {
51                 ReadConf();
52         }
53
54         virtual int 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 0;
60
61                         Channel* c = (Channel*)dest;
62
63                         if (CHANOPS_EXEMPT(ServerInstance, 'B') && c->GetStatus(user) == STATUS_OP)
64                         {
65                                 return 0;
66                         }
67
68                         if (c->IsModeSet('B'))
69                         {
70                                 int caps = 0;
71                                 const char* actstr = "\1ACTION ";
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++)
77                                                 continue;
78
79                                         caps += capsmap[(unsigned char)*i];
80                                 }
81                                 if ( ((caps*100)/(int)text.length()) >= percent )
82                                 {
83                                         user->WriteServ( "404 %s %s :Your line cannot be more than %d%% capital letters if it is %d or more letters long", user->nick, c->name, percent, minlen);
84                                         return 1;
85                                 }
86                         }
87                 }
88                 return 0;
89         }
90
91         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
92         {
93                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
94         }
95
96         void ReadConf()
97         {
98                 ConfigReader Conf(ServerInstance);
99                 percent = Conf.ReadInteger("blockcaps", "percent", "100", 0, true);
100                 minlen = Conf.ReadInteger("blockcaps", "minlen", "1", 0, true);
101                 std::string hmap = Conf.ReadValue("blockcaps", "capsmap", 0);
102                 if (hmap.empty())
103                         hmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
104                 memset(&capsmap, 0, 255);
105                 for (std::string::iterator n = hmap.begin(); n != hmap.end(); n++)
106                         capsmap[(unsigned char)*n] = 1;
107                 if (percent < 1 || percent > 100)
108                 {
109                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "<blockcaps:percent> out of range, setting to default of 100.");
110                         percent = 100;
111                 }
112                 if (minlen < 1 || minlen > MAXBUF-1)
113                 {
114                         ServerInstance->Logs->Log("CONFIG",DEFAULT, "<blockcaps:minlen> out of range, setting to default of 1.");
115                         minlen = 1;
116                 }
117         }
118
119         virtual ~ModuleBlockCAPS()
120         {
121                 ServerInstance->Modes->DelMode(bc);
122                 delete bc;
123         }
124
125         virtual Version GetVersion()
126         {
127                 return Version(1,2,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
128         }
129 };
130
131 MODULE_INIT(ModuleBlockCAPS)