]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_blockcaps.cpp
cfad61f173d4c2a40b6d1d2603337cb033481ea1
[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 ModeHandler
22 {
23  public:
24         BlockCaps(InspIRCd* Instance) : ModeHandler(Instance, 'B', 0, 0, false, MODETYPE_CHANNEL, false) { }
25
26         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
27         {
28                 if (adding)
29                 {
30                         if (!channel->IsModeSet('B'))
31                         {
32                                 channel->SetMode('B',true);
33                                 return MODEACTION_ALLOW;
34                         }
35                 }
36                 else
37                 {
38                         if (channel->IsModeSet('B'))
39                         {
40                                 channel->SetMode('B',false);
41                                 return MODEACTION_ALLOW;
42                         }
43                 }
44
45                 return MODEACTION_DENY;
46         }
47 };
48
49 class ModuleBlockCAPS : public Module
50 {
51         BlockCaps* bc;
52         int percent;
53         unsigned int minlen;
54         char capsmap[256];
55 public:
56         
57         ModuleBlockCAPS(InspIRCd* Me) : Module(Me)
58         {
59                 OnRehash(NULL,"");
60                 bc = new BlockCaps(ServerInstance);
61                 if (!ServerInstance->Modes->AddMode(bc))
62                 {
63                         delete bc;
64                         throw ModuleException("Could not add new modes!");
65                 }
66                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash };
67                 ServerInstance->Modules->Attach(eventlist, this, 3);
68         }
69
70
71         virtual void OnRehash(User* user, const std::string &param)
72         {
73                 ReadConf();
74         }
75
76         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
77         {
78                 if (target_type == TYPE_CHANNEL)
79                 {
80                         if ((!IS_LOCAL(user)) || (text.length() < minlen))
81                                 return 0;
82
83                         Channel* c = (Channel*)dest;
84
85                         if (c->IsModeSet('B'))
86                         {
87                                 int caps = 0;
88                                 for (std::string::iterator i = text.begin(); i != text.end(); i++)
89                                         caps += capsmap[(unsigned char)*i];
90                                 if ( ((caps*100)/(int)text.length()) >= percent )
91                                 {
92                                         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);
93                                         return 1;
94                                 }
95                         }
96                 }
97                 return 0;
98         }
99
100         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
101         {
102                 return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
103         }
104
105         void ReadConf()
106         {
107                 ConfigReader Conf(ServerInstance);
108                 percent = Conf.ReadInteger("blockcaps", "percent", "100", 0, true);
109                 minlen = Conf.ReadInteger("blockcaps", "minlen", "1", 0, true);
110                 std::string hmap = Conf.ReadValue("blockcaps", "capsmap", 0);
111                 if (hmap.empty())
112                         hmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
113                 memset(&capsmap, 0, 255);
114                 for (std::string::iterator n = hmap.begin(); n != hmap.end(); n++)
115                         capsmap[(unsigned char)*n] = 1;
116                 if (percent < 1 || percent > 100)
117                 {
118                         ServerInstance->Log(DEFAULT, "<blockcaps:percent> out of range, setting to default of 100.");
119                         percent = 100;
120                 }
121                 if (minlen < 1 || minlen > MAXBUF-1)
122                 {
123                         ServerInstance->Log(DEFAULT, "<blockcaps:minlen> out of range, setting to default of 1.");
124                         minlen = 1;
125                 }
126         }
127
128         virtual ~ModuleBlockCAPS()
129         {
130                 ServerInstance->Modes->DelMode(bc);
131                 delete bc;
132         }
133
134         virtual Version GetVersion()
135         {
136                 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
137         }
138 };
139
140 MODULE_INIT(ModuleBlockCAPS)