2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2017 Peter Powell <petpow@saberuk.com>
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.
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
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/>.
21 #include "modules/exemption.h"
32 class AntiCapsSettings
35 const AntiCapsMethod method;
36 const uint16_t minlen;
37 const uint8_t percent;
39 AntiCapsSettings(const AntiCapsMethod& Method, const uint16_t& MinLen, const uint8_t& Percent)
47 class AntiCapsMode : public ParamMode<AntiCapsMode, SimpleExtItem<AntiCapsSettings> >
50 bool ParseMethod(irc::sepstream& stream, AntiCapsMethod& method)
52 std::string methodstr;
53 if (!stream.GetToken(methodstr))
56 if (irc::equals(methodstr, "ban"))
58 else if (irc::equals(methodstr, "block"))
60 else if (irc::equals(methodstr, "mute"))
62 else if (irc::equals(methodstr, "kick"))
64 else if (irc::equals(methodstr, "kickban"))
65 method = ACM_KICK_BAN;
72 bool ParseMinimumLength(irc::sepstream& stream, uint16_t& minlen)
74 std::string minlenstr;
75 if (!stream.GetToken(minlenstr))
78 uint16_t result = ConvToNum<uint16_t>(minlenstr);
79 if (result < 1 || result > ServerInstance->Config->Limits.MaxLine)
86 bool ParsePercent(irc::sepstream& stream, uint8_t& percent)
88 std::string percentstr;
89 if (!stream.GetToken(percentstr))
92 uint8_t result = ConvToNum<uint8_t>(percentstr);
93 if (result < 1 || result > 100)
101 AntiCapsMode(Module* Creator)
102 : ParamMode<AntiCapsMode, SimpleExtItem<AntiCapsSettings> >(Creator, "anticaps", 'B')
106 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
108 irc::sepstream stream(parameter, ':');
109 AntiCapsMethod method;
113 // Attempt to parse the method.
114 if (!ParseMethod(stream, method) || !ParseMinimumLength(stream, minlen) || !ParsePercent(stream, percent))
116 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter, "Invalid anticaps mode parameter. Syntax: <ban|block|mute|kick|kickban>:{minlen}:{percent}."));
117 return MODEACTION_DENY;
120 ext.set(channel, new AntiCapsSettings(method, minlen, percent));
121 return MODEACTION_ALLOW;
124 void SerializeParam(Channel* chan, const AntiCapsSettings* acs, std::string& out)
141 out.append("kickban");
144 out.append("unknown~");
145 out.append(ConvToStr(acs->method));
149 out.append(ConvToStr(acs->minlen));
151 out.append(ConvNumeric(acs->percent));
155 class ModuleAntiCaps : public Module
158 CheckExemption::EventProvider exemptionprov;
159 std::bitset<UCHAR_MAX> uppercase;
160 std::bitset<UCHAR_MAX> lowercase;
163 void CreateBan(Channel* channel, User* user, bool mute)
165 std::string banmask(mute ? "m:" : "");
166 banmask.append("*!*@");
167 banmask.append(user->GetDisplayedHost());
169 Modes::ChangeList changelist;
170 changelist.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), banmask);
171 ServerInstance->Modes->Process(ServerInstance->FakeClient, channel, NULL, changelist);
174 void InformUser(Channel* channel, User* user, const std::string& message)
176 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, channel, message + " and was blocked.");
181 : exemptionprov(this)
186 void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
188 ConfigTag* tag = ServerInstance->Config->ConfValue("anticaps");
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));
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));
201 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
203 // We only want to operate on messages from local users.
205 return MOD_RES_PASSTHRU;
207 // The mode can only be applied to channels.
208 if (target.type != MessageTarget::TYPE_CHANNEL)
209 return MOD_RES_PASSTHRU;
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;
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;
222 // If the message is a CTCP then we skip it unless it is
223 // an ACTION in which case we just check against the body.
224 std::string ctcpname;
225 std::string msgbody(details.text);
226 if (details.IsCTCP(ctcpname, msgbody))
228 // If the CTCP is not an action then skip it.
229 if (!irc::equals(ctcpname, "ACTION"))
230 return MOD_RES_PASSTHRU;
233 // Retrieve the anticaps config. This should never be
234 // null but its better to be safe than sorry.
235 AntiCapsSettings* config = mode.ext.get(channel);
237 return MOD_RES_PASSTHRU;
239 // If the message is shorter than the minimum length then
240 // we don't need to do anything else.
241 size_t length = msgbody.length();
242 if (length < config->minlen)
243 return MOD_RES_PASSTHRU;
245 // Count the characters to see how many upper case and
246 // ignored (non upper or lower) characters there are.
248 for (std::string::const_iterator iter = msgbody.begin(); iter != msgbody.end(); ++iter)
250 unsigned char chr = static_cast<unsigned char>(*iter);
251 if (uppercase.test(chr))
253 else if (!lowercase.test(chr))
257 // If the message was entirely symbols then the message
258 // can't contain any upper case letters.
260 return MOD_RES_PASSTHRU;
262 // Calculate the percentage.
263 double percent = round((upper * 100) / length);
264 if (percent < config->percent)
265 return MOD_RES_PASSTHRU;
267 const std::string message = InspIRCd::Format("Your message exceeded the %d%% upper case character threshold for %s",
268 config->percent, channel->name.c_str());
270 switch (config->method)
273 InformUser(channel, user, message);
274 CreateBan(channel, user, false);
278 InformUser(channel, user, message);
282 InformUser(channel, user, message);
283 CreateBan(channel, user, true);
287 channel->KickUser(ServerInstance->FakeClient, user, message);
291 CreateBan(channel, user, false);
292 channel->KickUser(ServerInstance->FakeClient, user, message);
298 Version GetVersion() CXX11_OVERRIDE
300 return Version("Provides support for punishing users that send capitalised messages.", VF_COMMON|VF_VENDOR);
304 MODULE_INIT(ModuleAntiCaps)