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