]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_anticaps.cpp
Update copyright headers.
[user/henk/code/inspircd.git] / src / modules / m_anticaps.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Robby <robby@chatbelgie.be>
5  *   Copyright (C) 2018-2021 Sadie Powell <sadie@witchery.services>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "modules/exemption.h"
23
24 enum AntiCapsMethod
25 {
26         ACM_BAN,
27         ACM_BLOCK,
28         ACM_MUTE,
29         ACM_KICK,
30         ACM_KICK_BAN
31 };
32
33 class AntiCapsSettings
34 {
35  public:
36         const AntiCapsMethod method;
37         const uint16_t minlen;
38         const uint8_t percent;
39
40         AntiCapsSettings(const AntiCapsMethod& Method, const uint16_t& MinLen, const uint8_t& Percent)
41                 : method(Method)
42                 , minlen(MinLen)
43                 , percent(Percent)
44         {
45         }
46 };
47
48 class AntiCapsMode : public ParamMode<AntiCapsMode, SimpleExtItem<AntiCapsSettings> >
49 {
50  private:
51         bool ParseMethod(irc::sepstream& stream, AntiCapsMethod& method)
52         {
53                 std::string methodstr;
54                 if (!stream.GetToken(methodstr))
55                         return false;
56
57                 if (irc::equals(methodstr, "ban"))
58                         method = ACM_BAN;
59                 else if (irc::equals(methodstr, "block"))
60                         method = ACM_BLOCK;
61                 else if (irc::equals(methodstr, "mute"))
62                         method = ACM_MUTE;
63                 else if (irc::equals(methodstr, "kick"))
64                         method = ACM_KICK;
65                 else if (irc::equals(methodstr, "kickban"))
66                         method = ACM_KICK_BAN;
67                 else
68                         return false;
69
70                 return true;
71         }
72
73         bool ParseMinimumLength(irc::sepstream& stream, uint16_t& minlen)
74         {
75                 std::string minlenstr;
76                 if (!stream.GetToken(minlenstr))
77                         return false;
78
79                 uint16_t result = ConvToNum<uint16_t>(minlenstr);
80                 if (result < 1 || result > ServerInstance->Config->Limits.MaxLine)
81                         return false;
82
83                 minlen = result;
84                 return true;
85         }
86
87         bool ParsePercent(irc::sepstream& stream, uint8_t& percent)
88         {
89                 std::string percentstr;
90                 if (!stream.GetToken(percentstr))
91                         return false;
92
93                 uint8_t result = ConvToNum<uint8_t>(percentstr);
94                 if (result < 1 || result > 100)
95                         return false;
96
97                 percent = result;
98                 return true;
99         }
100
101  public:
102         AntiCapsMode(Module* Creator)
103                 : ParamMode<AntiCapsMode, SimpleExtItem<AntiCapsSettings> >(Creator, "anticaps", 'B')
104         {
105                 syntax = "{ban|block|mute|kick|kickban}:<minlen>:<percent>";
106         }
107
108         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
109         {
110                 irc::sepstream stream(parameter, ':');
111                 AntiCapsMethod method;
112                 uint16_t minlen;
113                 uint8_t percent;
114
115                 // Attempt to parse the method.
116                 if (!ParseMethod(stream, method) || !ParseMinimumLength(stream, minlen) || !ParsePercent(stream, percent))
117                 {
118                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
119                         return MODEACTION_DENY;
120                 }
121
122                 ext.set(channel, new AntiCapsSettings(method, minlen, percent));
123                 return MODEACTION_ALLOW;
124         }
125
126         void SerializeParam(Channel* chan, const AntiCapsSettings* acs, std::string& out)
127         {
128                 switch (acs->method)
129                 {
130                         case ACM_BAN:
131                                 out.append("ban");
132                                 break;
133                         case ACM_BLOCK:
134                                 out.append("block");
135                                 break;
136                         case ACM_MUTE:
137                                 out.append("mute");
138                                 break;
139                         case ACM_KICK:
140                                 out.append("kick");
141                                 break;
142                         case ACM_KICK_BAN:
143                                 out.append("kickban");
144                                 break;
145                         default:
146                                 out.append("unknown~");
147                                 out.append(ConvToStr(acs->method));
148                                 break;
149                 }
150                 out.push_back(':');
151                 out.append(ConvToStr(acs->minlen));
152                 out.push_back(':');
153                 out.append(ConvNumeric(acs->percent));
154         }
155 };
156
157 class ModuleAntiCaps : public Module
158 {
159  private:
160         CheckExemption::EventProvider exemptionprov;
161         std::bitset<UCHAR_MAX + 1> uppercase;
162         std::bitset<UCHAR_MAX + 1> lowercase;
163         AntiCapsMode mode;
164
165         void CreateBan(Channel* channel, User* user, bool mute)
166         {
167                 std::string banmask(mute ? "m:" : "");
168                 banmask.append("*!*@");
169                 banmask.append(user->GetDisplayedHost());
170
171                 Modes::ChangeList changelist;
172                 changelist.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), banmask);
173                 ServerInstance->Modes->Process(ServerInstance->FakeClient, channel, NULL, changelist);
174         }
175
176         void InformUser(Channel* channel, User* user, const std::string& message)
177         {
178                 user->WriteNumeric(Numerics::CannotSendTo(channel, message + " and was blocked."));
179         }
180
181  public:
182         ModuleAntiCaps()
183                 : exemptionprov(this)
184                 , mode(this)
185         {
186         }
187
188         void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
189         {
190                 ConfigTag* tag = ServerInstance->Config->ConfValue("anticaps");
191
192                 uppercase.reset();
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));
196
197                 lowercase.reset();
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));
201         }
202
203         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
204         {
205                 // We only want to operate on messages from local users.
206                 if (!IS_LOCAL(user))
207                         return MOD_RES_PASSTHRU;
208
209                 // The mode can only be applied to channels.
210                 if (target.type != MessageTarget::TYPE_CHANNEL)
211                         return MOD_RES_PASSTHRU;
212
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;
217
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;
223
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))
229                 {
230                         // If the CTCP is not an action then skip it.
231                         if (!irc::equals(ctcpname, "ACTION"))
232                                 return MOD_RES_PASSTHRU;
233                 }
234
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);
238                 if (!config)
239                         return MOD_RES_PASSTHRU;
240
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;
246
247                 // Count the characters to see how many upper case and
248                 // ignored (non upper or lower) characters there are.
249                 size_t upper = 0;
250                 for (std::string::const_iterator iter = msgbody.begin(); iter != msgbody.end(); ++iter)
251                 {
252                         unsigned char chr = static_cast<unsigned char>(*iter);
253                         if (uppercase.test(chr))
254                                 upper += 1;
255                         else if (!lowercase.test(chr))
256                                 length -= 1;
257                 }
258
259                 // If the message was entirely symbols then the message
260                 // can't contain any upper case letters.
261                 if (length == 0)
262                         return MOD_RES_PASSTHRU;
263
264                 // Calculate the percentage.
265                 double percent = round((upper * 100) / length);
266                 if (percent < config->percent)
267                         return MOD_RES_PASSTHRU;
268
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());
271
272                 switch (config->method)
273                 {
274                         case ACM_BAN:
275                                 InformUser(channel, user, message);
276                                 CreateBan(channel, user, false);
277                                 break;
278
279                         case ACM_BLOCK:
280                                 InformUser(channel, user, message);
281                                 break;
282
283                         case ACM_MUTE:
284                                 InformUser(channel, user, message);
285                                 CreateBan(channel, user, true);
286                                 break;
287
288                         case ACM_KICK:
289                                 channel->KickUser(ServerInstance->FakeClient, user, message);
290                                 break;
291
292                         case ACM_KICK_BAN:
293                                 CreateBan(channel, user, false);
294                                 channel->KickUser(ServerInstance->FakeClient, user, message);
295                                 break;
296                 }
297                 return MOD_RES_DENY;
298         }
299
300         Version GetVersion() CXX11_OVERRIDE
301         {
302                 return Version("Adds channel mode B (anticaps) which allows channels to block messages which are excessively capitalised.", VF_COMMON|VF_VENDOR);
303         }
304 };
305
306 MODULE_INIT(ModuleAntiCaps)