2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Robby <robby@chatbelgie.be>
5 * Copyright (C) 2018-2020 Sadie Powell <sadie@witchery.services>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "modules/exemption.h"
33 class AntiCapsSettings
36 const AntiCapsMethod method;
37 const uint16_t minlen;
38 const uint8_t percent;
40 AntiCapsSettings(const AntiCapsMethod& Method, const uint16_t& MinLen, const uint8_t& Percent)
48 class AntiCapsMode : public ParamMode<AntiCapsMode, SimpleExtItem<AntiCapsSettings> >
51 bool ParseMethod(irc::sepstream& stream, AntiCapsMethod& method)
53 std::string methodstr;
54 if (!stream.GetToken(methodstr))
57 if (irc::equals(methodstr, "ban"))
59 else if (irc::equals(methodstr, "block"))
61 else if (irc::equals(methodstr, "mute"))
63 else if (irc::equals(methodstr, "kick"))
65 else if (irc::equals(methodstr, "kickban"))
66 method = ACM_KICK_BAN;
73 bool ParseMinimumLength(irc::sepstream& stream, uint16_t& minlen)
75 std::string minlenstr;
76 if (!stream.GetToken(minlenstr))
79 uint16_t result = ConvToNum<uint16_t>(minlenstr);
80 if (result < 1 || result > ServerInstance->Config->Limits.MaxLine)
87 bool ParsePercent(irc::sepstream& stream, uint8_t& percent)
89 std::string percentstr;
90 if (!stream.GetToken(percentstr))
93 uint8_t result = ConvToNum<uint8_t>(percentstr);
94 if (result < 1 || result > 100)
102 AntiCapsMode(Module* Creator)
103 : ParamMode<AntiCapsMode, SimpleExtItem<AntiCapsSettings> >(Creator, "anticaps", 'B')
105 syntax = "{ban|block|mute|kick|kickban}:<minlen>:<percent>";
108 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
110 irc::sepstream stream(parameter, ':');
111 AntiCapsMethod method;
115 // Attempt to parse the method.
116 if (!ParseMethod(stream, method) || !ParseMinimumLength(stream, minlen) || !ParsePercent(stream, percent))
118 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
119 return MODEACTION_DENY;
122 ext.set(channel, new AntiCapsSettings(method, minlen, percent));
123 return MODEACTION_ALLOW;
126 void SerializeParam(Channel* chan, const AntiCapsSettings* acs, std::string& out)
143 out.append("kickban");
146 out.append("unknown~");
147 out.append(ConvToStr(acs->method));
151 out.append(ConvToStr(acs->minlen));
153 out.append(ConvNumeric(acs->percent));
157 class ModuleAntiCaps : public Module
160 CheckExemption::EventProvider exemptionprov;
161 std::bitset<UCHAR_MAX + 1> uppercase;
162 std::bitset<UCHAR_MAX + 1> lowercase;
165 void CreateBan(Channel* channel, User* user, bool mute)
167 std::string banmask(mute ? "m:" : "");
168 banmask.append("*!*@");
169 banmask.append(user->GetDisplayedHost());
171 Modes::ChangeList changelist;
172 changelist.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), banmask);
173 ServerInstance->Modes->Process(ServerInstance->FakeClient, channel, NULL, changelist);
176 void InformUser(Channel* channel, User* user, const std::string& message)
178 user->WriteNumeric(Numerics::CannotSendTo(channel, message + " and was blocked."));
183 : exemptionprov(this)
188 void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
190 ConfigTag* tag = ServerInstance->Config->ConfValue("anticaps");
193 const std::string upper = tag->getString("uppercase", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1);
194 for (std::string::const_iterator iter = upper.begin(); iter != upper.end(); ++iter)
195 uppercase.set(static_cast<unsigned char>(*iter));
198 const std::string lower = tag->getString("lowercase", "abcdefghijklmnopqrstuvwxyz");
199 for (std::string::const_iterator iter = lower.begin(); iter != lower.end(); ++iter)
200 lowercase.set(static_cast<unsigned char>(*iter));
203 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
205 // We only want to operate on messages from local users.
207 return MOD_RES_PASSTHRU;
209 // The mode can only be applied to channels.
210 if (target.type != MessageTarget::TYPE_CHANNEL)
211 return MOD_RES_PASSTHRU;
213 // We only act if the channel has the mode set.
214 Channel* channel = target.Get<Channel>();
215 if (!channel->IsModeSet(&mode))
216 return MOD_RES_PASSTHRU;
218 // If the user is exempt from anticaps then we don't need
219 // to do anything else.
220 ModResult result = CheckExemption::Call(exemptionprov, user, channel, "anticaps");
221 if (result == MOD_RES_ALLOW)
222 return MOD_RES_PASSTHRU;
224 // If the message is a CTCP then we skip it unless it is
225 // an ACTION in which case we just check against the body.
226 std::string ctcpname;
227 std::string msgbody(details.text);
228 if (details.IsCTCP(ctcpname, msgbody))
230 // If the CTCP is not an action then skip it.
231 if (!irc::equals(ctcpname, "ACTION"))
232 return MOD_RES_PASSTHRU;
235 // Retrieve the anticaps config. This should never be
236 // null but its better to be safe than sorry.
237 AntiCapsSettings* config = mode.ext.get(channel);
239 return MOD_RES_PASSTHRU;
241 // If the message is shorter than the minimum length then
242 // we don't need to do anything else.
243 size_t length = msgbody.length();
244 if (length < config->minlen)
245 return MOD_RES_PASSTHRU;
247 // Count the characters to see how many upper case and
248 // ignored (non upper or lower) characters there are.
250 for (std::string::const_iterator iter = msgbody.begin(); iter != msgbody.end(); ++iter)
252 unsigned char chr = static_cast<unsigned char>(*iter);
253 if (uppercase.test(chr))
255 else if (!lowercase.test(chr))
259 // If the message was entirely symbols then the message
260 // can't contain any upper case letters.
262 return MOD_RES_PASSTHRU;
264 // Calculate the percentage.
265 double percent = round((upper * 100) / length);
266 if (percent < config->percent)
267 return MOD_RES_PASSTHRU;
269 const std::string message = InspIRCd::Format("Your message exceeded the %d%% upper case character threshold for %s",
270 config->percent, channel->name.c_str());
272 switch (config->method)
275 InformUser(channel, user, message);
276 CreateBan(channel, user, false);
280 InformUser(channel, user, message);
284 InformUser(channel, user, message);
285 CreateBan(channel, user, true);
289 channel->KickUser(ServerInstance->FakeClient, user, message);
293 CreateBan(channel, user, false);
294 channel->KickUser(ServerInstance->FakeClient, user, message);
300 Version GetVersion() CXX11_OVERRIDE
302 return Version("Adds channel mode B (anticaps) which allows channels to block messages which are excessively capitalised.", VF_COMMON|VF_VENDOR);
306 MODULE_INIT(ModuleAntiCaps)