]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_anticaps.cpp
Merge branch 'insp20' into master.
[user/henk/code/inspircd.git] / src / modules / m_anticaps.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017 Peter Powell <petpow@saberuk.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "modules/exemption.h"
22
23 enum AntiCapsMethod
24 {
25         ACM_BAN,
26         ACM_BLOCK,
27         ACM_MUTE,
28         ACM_KICK,
29         ACM_KICK_BAN
30 };
31
32 class AntiCapsSettings
33 {
34  public:
35         const AntiCapsMethod method;
36         const uint16_t minlen;
37         const uint8_t percent;
38
39         AntiCapsSettings(const AntiCapsMethod& Method, const uint16_t& MinLen, const uint8_t& Percent)
40                 : method(Method)
41                 , minlen(MinLen)
42                 , percent(Percent)
43         {
44         }
45 };
46
47 class AntiCapsMode : public ParamMode<AntiCapsMode, SimpleExtItem<AntiCapsSettings> >
48 {
49  private:
50         bool ParseMethod(irc::sepstream& stream, AntiCapsMethod& method)
51         {
52                 std::string methodstr;
53                 if (!stream.GetToken(methodstr))
54                         return false;
55
56                 if (irc::equals(methodstr, "ban"))
57                         method = ACM_BAN;
58                 else if (irc::equals(methodstr, "block"))
59                         method = ACM_BLOCK;
60                 else if (irc::equals(methodstr, "mute"))
61                         method = ACM_MUTE;
62                 else if (irc::equals(methodstr, "kick"))
63                         method = ACM_KICK;
64                 else if (irc::equals(methodstr, "kickban"))
65                         method = ACM_KICK_BAN;
66                 else
67                         return false;
68
69                 return true;
70         }
71
72         bool ParseMinimumLength(irc::sepstream& stream, uint16_t& minlen)
73         {
74                 std::string minlenstr;
75                 if (!stream.GetToken(minlenstr))
76                         return false;
77
78                 uint16_t result = ConvToNum<uint16_t>(minlenstr);
79                 if (result < 1 || result > ServerInstance->Config->Limits.MaxLine)
80                         return false;
81
82                 minlen = result;
83                 return true;
84         }
85
86         bool ParsePercent(irc::sepstream& stream, uint8_t& percent)
87         {
88                 std::string percentstr;
89                 if (!stream.GetToken(percentstr))
90                         return false;
91
92                 uint8_t result = ConvToNum<uint8_t>(percentstr);
93                 if (result < 1 || result > 100)
94                         return false;
95
96                 percent = result;
97                 return true;
98         }
99
100  public:
101         AntiCapsMode(Module* Creator)
102                 : ParamMode<AntiCapsMode, SimpleExtItem<AntiCapsSettings> >(Creator, "anticaps", 'B')
103         {
104         }
105
106         ModeAction OnSet(User* source, Channel* channel, std::string& parameter)
107         {
108                 irc::sepstream stream(parameter, ':');
109                 AntiCapsMethod method;
110                 uint16_t minlen;
111                 uint8_t percent;
112
113                 // Attempt to parse the method.
114                 if (!ParseMethod(stream, method) || !ParseMinimumLength(stream, minlen) || !ParsePercent(stream, percent))
115                 {
116                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter, "Invalid anticaps mode parameter. Syntax: <ban|block|mute|kick|kickban>:{minlen}:{percent}."));
117                         return MODEACTION_DENY;
118                 }
119
120                 ext.set(channel, new AntiCapsSettings(method, minlen, percent));
121                 return MODEACTION_ALLOW;
122         }
123
124         void SerializeParam(Channel* chan, const AntiCapsSettings* acs, std::string& out)
125         {
126                 switch (acs->method)
127                 {
128                         case ACM_BAN:
129                                 out.append("ban");
130                                 break;
131                         case ACM_BLOCK:
132                                 out.append("block");
133                                 break;
134                         case ACM_MUTE:
135                                 out.append("mute");
136                                 break;
137                         case ACM_KICK:
138                                 out.append("kick");
139                                 break;
140                         case ACM_KICK_BAN:
141                                 out.append("kickban");
142                                 break;
143                         default:
144                                 out.append("unknown~");
145                                 out.append(ConvToStr(acs->method));
146                                 break;
147                 }
148                 out.push_back(':');
149                 out.append(ConvToStr(acs->minlen));
150                 out.push_back(':');
151                 out.append(ConvToStr(acs->percent));
152         }
153 };
154
155 class ModuleAntiCaps : public Module
156 {
157  private:
158         CheckExemption::EventProvider exemptionprov;
159         std::bitset<UCHAR_MAX> uppercase;
160         std::bitset<UCHAR_MAX> lowercase;
161         AntiCapsMode mode;
162
163         void CreateBan(Channel* channel, User* user, bool mute)
164         {
165                 std::string banmask(mute ? "m:" : "");
166                 banmask.append("*!*@");
167                 banmask.append(user->GetDisplayedHost());
168
169                 Modes::ChangeList changelist;
170                 changelist.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), banmask);
171                 ServerInstance->Modes->Process(ServerInstance->FakeClient, channel, NULL, changelist);
172         }
173
174         void InformUser(Channel* channel, User* user, const std::string& message)
175         {
176                 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, channel, message + " and was blocked.");
177         }
178
179  public:
180         ModuleAntiCaps()
181                 : exemptionprov(this)
182                 , mode(this)
183         {
184         }
185
186         void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
187         {
188                 ConfigTag* tag = ServerInstance->Config->ConfValue("anticaps");
189
190                 uppercase.reset();
191                 const std::string upper = tag->getString("uppercase", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
192                 for (std::string::const_iterator iter = upper.begin(); iter != upper.end(); ++iter)
193                         uppercase.set(static_cast<unsigned char>(*iter));
194
195                 lowercase.reset();
196                 const std::string lower = tag->getString("lowercase", "abcdefghijklmnopqrstuvwxyz");
197                 for (std::string::const_iterator iter = lower.begin(); iter != lower.end(); ++iter)
198                         lowercase.set(static_cast<unsigned char>(*iter));
199         }
200
201         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
202         {
203                 // We only want to operate on messages from local users.
204                 if (!IS_LOCAL(user))
205                         return MOD_RES_PASSTHRU;
206
207                 // The mode can only be applied to channels.
208                 if (target.type != MessageTarget::TYPE_CHANNEL)
209                         return MOD_RES_PASSTHRU;
210
211                 // We only act if the channel has the mode set.
212                 Channel* channel = target.Get<Channel>();
213                 if (!channel->IsModeSet(&mode))
214                         return MOD_RES_PASSTHRU;
215
216                 // If the user is exempt from anticaps then we don't need
217                 // to do anything else.
218                 ModResult result = CheckExemption::Call(exemptionprov, user, channel, "anticaps");
219                         if (result == MOD_RES_ALLOW)
220                         return MOD_RES_PASSTHRU;
221
222                 // If the message is a CTCP then we skip it unless it is
223                 // an ACTION in which case we skip the prefix and suffix.
224                 std::string::const_iterator text_begin = details.text.begin();
225                 std::string::const_iterator text_end = details.text.end();
226                 if (details.text[0] == '\1')
227                 {
228                         // If the CTCP is not an action then skip it.
229                         if (details.text.compare(0, 8, "\1ACTION ", 8))
230                                 return MOD_RES_PASSTHRU;
231
232                         // Skip the CTCP message characters.
233                         text_begin += 8;
234                         if (*details.text.rbegin() == '\1')
235                                 text_end -= 1;
236                 }
237
238                 // Retrieve the anticaps config. This should never be
239                 // null but its better to be safe than sorry.
240                 AntiCapsSettings* config = mode.ext.get(channel);
241                 if (!config)
242                         return MOD_RES_PASSTHRU;
243
244                 // If the message is shorter than the minimum length then
245                 // we don't need to do anything else.
246                 size_t length = std::distance(text_begin, text_end);
247                 if (length < config->minlen)
248                         return MOD_RES_PASSTHRU;
249
250                 // Count the characters to see how many upper case and
251                 // ignored (non upper or lower) characters there are.
252                 size_t upper = 0;
253                 for (std::string::const_iterator iter = text_begin; iter != text_end; ++iter)
254                 {
255                         unsigned char chr = static_cast<unsigned char>(*iter);
256                         if (uppercase.test(chr))
257                                 upper += 1;
258                         else if (!lowercase.test(chr))
259                                 length -= 1;
260                 }
261
262                 // If the message was entirely symbols then the message
263                 // can't contain any upper case letters.
264                 if (length == 0)
265                         return MOD_RES_PASSTHRU;
266
267                 // Calculate the percentage.
268                 double percent = round((upper * 100) / length);
269                 if (percent < config->percent)
270                         return MOD_RES_PASSTHRU;
271
272                 const std::string message = InspIRCd::Format("Your message exceeded the %d%% upper case character threshold for %s",
273                         config->percent, channel->name.c_str());
274
275                 switch (config->method)
276                 {
277                         case ACM_BAN:
278                                 InformUser(channel, user, message);
279                                 CreateBan(channel, user, false);
280                                 break;
281
282                         case ACM_BLOCK:
283                                 InformUser(channel, user, message);
284                                 break;
285
286                         case ACM_MUTE:
287                                 InformUser(channel, user, message);
288                                 CreateBan(channel, user, true);
289                                 break;
290
291                         case ACM_KICK:
292                                 channel->KickUser(ServerInstance->FakeClient, user, message);
293                                 break;
294
295                         case ACM_KICK_BAN:
296                                 CreateBan(channel, user, false);
297                                 channel->KickUser(ServerInstance->FakeClient, user, message);
298                                 break;
299                 }
300                 return MOD_RES_DENY;
301         }
302
303         Version GetVersion() CXX11_OVERRIDE
304         {
305                 return Version("Provides support for punishing users that send capitalised messages.", VF_COMMON|VF_VENDOR);
306         }
307 };
308
309 MODULE_INIT(ModuleAntiCaps)