-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_censorplus.cpp
363 lines (309 loc) · 11.9 KB
/
m_censorplus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2015-2016 reverse Chevronnet [email protected]
*
* This file is part of InspIRCd. InspIRCd is free software; you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* THIS MODULE IS A HACK OF THE ORIGINAL M_CENSOR MODULE USING HYPERSCAN.
*/
/// $CompilerFlags: find_compiler_flags("icu-uc")
/// $LinkerFlags: find_linker_flags("icu-uc")
/// $CompilerFlags: find_compiler_flags("icu-i18n")
/// $LinkerFlags: find_linker_flags("icu-i18n")
/// $CompilerFlags: -I/usr/local/include
/// $LinkerFlags: -L/usr/local/lib -lhs
#include "inspircd.h"
#include "modules/exemption.h"
#include "numerichelper.h"
#include "utility/string.h"
#include <hs/hs.h> // Hyperscan
#include <unicode/regex.h>
#include <unicode/unistr.h>
#include <codecvt>
#include <locale>
#include <fstream>
typedef insp::flat_map<std::string, std::string, irc::insensitive_swo> CensorMap;
class ModuleCensor : public Module
{
private:
CheckExemption::EventProvider exemptionprov;
CensorMap censors;
SimpleUserMode cu;
SimpleChannelMode cc;
std::unique_ptr<icu::RegexPattern> emoji_pattern;
std::unique_ptr<icu::RegexPattern> kiwiirc_pattern;
std::string whitelist_regex_str;
hs_database_t* whitelist_db = nullptr;
hs_scratch_t* scratch = nullptr;
static int onMatch(unsigned int id, unsigned long long from, unsigned long long to, unsigned int flags, void* ctx) {
bool* matched = (bool*)ctx;
*matched = true;
return 0;
}
bool CompileRegex(const std::string& pattern, hs_database_t** db) {
hs_compile_error_t* compile_err;
if (hs_compile(pattern.c_str(), HS_FLAG_UTF8 | HS_FLAG_UCP, HS_MODE_BLOCK, nullptr, db, &compile_err) != HS_SUCCESS) {
ServerInstance->Logs.Normal(MODNAME, INSP_FORMAT("Failed to compile regex pattern: {}", compile_err->message));
hs_free_compile_error(compile_err);
return false;
}
return true;
}
bool SerializeDatabase(hs_database_t* db, const std::string& filepath) {
char* serialized_db = nullptr;
size_t serialized_db_size = 0;
if (hs_serialize_database(db, &serialized_db, &serialized_db_size) != HS_SUCCESS) {
ServerInstance->Logs.Normal(MODNAME, "Failed to serialize Hyperscan database.");
return false;
}
std::ofstream ofs(filepath, std::ios::binary);
if (!ofs) {
free(serialized_db);
ServerInstance->Logs.Normal(MODNAME, "Failed to open file for writing serialized Hyperscan database.");
return false;
}
ofs.write(serialized_db, serialized_db_size);
free(serialized_db);
return ofs.good();
}
bool DeserializeDatabase(const std::string& filepath, hs_database_t** db) {
std::ifstream ifs(filepath, std::ios::binary | std::ios::ate);
if (!ifs) {
ServerInstance->Logs.Normal(MODNAME, "Failed to open file for reading serialized Hyperscan database.");
return false;
}
std::streamsize size = ifs.tellg();
ifs.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
if (!ifs.read(buffer.data(), size)) {
ServerInstance->Logs.Normal(MODNAME, "Failed to read serialized Hyperscan database.");
return false;
}
if (hs_deserialize_database(buffer.data(), size, db) != HS_SUCCESS) {
ServerInstance->Logs.Normal(MODNAME, "Failed to deserialize Hyperscan database.");
return false;
}
return true;
}
bool IsMatch(hs_database_t* db, const std::string& text) {
bool matched = false;
if (hs_scan(db, text.c_str(), text.length(), 0, scratch, onMatch, &matched) != HS_SUCCESS) {
ServerInstance->Logs.Normal(MODNAME, "Hyperscan scan error");
}
return matched;
}
bool IsMixedUTF8(const std::string& text)
{
if (text.empty())
return false;
enum ScriptType { SCRIPT_UNKNOWN, SCRIPT_LATIN, SCRIPT_NONLATIN };
ScriptType detected = SCRIPT_UNKNOWN;
for (const auto& c : text)
{
if (static_cast<unsigned char>(c) < 128)
continue; // ASCII characters are ignored
if (std::isalpha(static_cast<unsigned char>(c)))
{
ScriptType current = std::islower(static_cast<unsigned char>(c)) || std::isupper(static_cast<unsigned char>(c)) ? SCRIPT_LATIN : SCRIPT_NONLATIN;
if (detected == SCRIPT_UNKNOWN)
{
detected = current;
}
else if (detected != current)
{
return true; // Mixed scripts detected
}
}
}
return false;
}
bool IsEmojiOnly(const std::string& text)
{
UErrorCode status = U_ZERO_ERROR;
icu::UnicodeString ustr(text.c_str(), "UTF-8");
std::unique_ptr<icu::RegexMatcher> emoji_matcher(emoji_pattern->matcher(ustr, status));
if (U_FAILURE(status))
{
ServerInstance->Logs.Normal(MODNAME, INSP_FORMAT("Failed to create regex matcher for emojis: {}", u_errorName(status)));
return false;
}
// Check if the entire text is matched by the emoji pattern
return emoji_matcher->matches(status);
}
bool IsKiwiIRCOnly(const std::string& text)
{
UErrorCode status = U_ZERO_ERROR;
icu::UnicodeString ustr(text.c_str(), "UTF-8");
std::unique_ptr<icu::RegexMatcher> kiwiirc_matcher(kiwiirc_pattern->matcher(ustr, status));
if (U_FAILURE(status))
{
ServerInstance->Logs.Normal(MODNAME, INSP_FORMAT("Failed to create regex matcher for KiwiIRC: {}", u_errorName(status)));
return false;
}
// Check if the entire text is matched by the KiwiIRC pattern
return kiwiirc_matcher->matches(status);
}
bool IsAllowed(const std::string& text)
{
// Allow ASCII characters and common symbols by default
if (std::all_of(text.begin(), text.end(), [](unsigned char c) { return c >= 32 && c <= 126; }))
{
return true;
}
// First, try to match the whitelist using Hyperscan
if (IsMatch(whitelist_db, text))
return true;
// Then, try to match the text against emoji and KiwiIRC patterns using ICU
return IsEmojiOnly(text) || IsKiwiIRCOnly(text);
}
public:
ModuleCensor()
: Module(VF_NONE, "Allows the server administrator to define inappropriate phrases that are not allowed to be used in private or channel messages and blocks messages with mixed UTF-8 scripts, only allowing certain Unicode smileys.")
, exemptionprov(this)
, cu(this, "u_censor", 'G')
, cc(this, "censor", 'G')
{
}
~ModuleCensor() override {
if (whitelist_db)
hs_free_database(whitelist_db);
if (scratch)
hs_free_scratch(scratch);
}
void ReadConfig(ConfigStatus& status) override
{
CensorMap newcensors;
for (const auto& [_, badword_tag] : ServerInstance->Config->ConfTags("badword"))
{
const std::string text = badword_tag->getString("text");
if (text.empty())
throw ModuleException(this, INSP_FORMAT("<badword:text> is empty! at {}", badword_tag->source.str()));
const std::string replace = badword_tag->getString("replace");
newcensors[text] = replace;
}
censors.swap(newcensors);
const auto& tag = ServerInstance->Config->ConfValue("censorplus");
std::string emoji_regex_str = tag->getString("emojiregex");
whitelist_regex_str = tag->getString("whitelistregex");
std::string kiwiirc_regex_str = tag->getString("kiwiircregex");
UErrorCode icu_status = U_ZERO_ERROR;
emoji_pattern = std::unique_ptr<icu::RegexPattern>(icu::RegexPattern::compile(icu::UnicodeString::fromUTF8(emoji_regex_str), 0, icu_status));
if (U_FAILURE(icu_status))
{
throw ModuleException(this, INSP_FORMAT("Failed to compile emoji regex pattern: {}", u_errorName(icu_status)));
}
icu_status = U_ZERO_ERROR;
kiwiirc_pattern = std::unique_ptr<icu::RegexPattern>(icu::RegexPattern::compile(icu::UnicodeString::fromUTF8(kiwiirc_regex_str), 0, icu_status));
if (U_FAILURE(icu_status))
{
throw ModuleException(this, INSP_FORMAT("Failed to compile KiwiIRC regex pattern: {}", u_errorName(icu_status)));
}
const std::string db_path = "/home/debian/irc/ircd/inspircd/run/conf/hyperscan/whitelist.hsdb";
if (!DeserializeDatabase(db_path, &whitelist_db)) {
if (!CompileRegex(whitelist_regex_str, &whitelist_db) || !SerializeDatabase(whitelist_db, db_path)) {
throw ModuleException(this, "Failed to compile or serialize whitelist regex pattern for Hyperscan");
}
}
if (hs_alloc_scratch(whitelist_db, &scratch) != HS_SUCCESS) {
throw ModuleException(this, "Failed to allocate Hyperscan scratch space");
}
}
ModResult OnUserPreMessage(User* user, MessageTarget& target, MessageDetails& details) override
{
if (!IS_LOCAL(user))
return MOD_RES_PASSTHRU;
// Allow IRC operators to bypass the restrictions
if (user->IsOper())
return MOD_RES_PASSTHRU;
try {
switch (target.type)
{
case MessageTarget::TYPE_USER:
{
User* targuser = target.Get<User>();
if (!targuser->IsModeSet(cu))
return MOD_RES_PASSTHRU;
break;
}
case MessageTarget::TYPE_CHANNEL:
{
auto* targchan = target.Get<Channel>();
if (!targchan->IsModeSet(cc))
return MOD_RES_PASSTHRU;
ModResult result = exemptionprov.Check(user, targchan, "censor");
if (result == MOD_RES_ALLOW)
return MOD_RES_PASSTHRU;
break;
}
default:
return MOD_RES_PASSTHRU;
}
if (IsMixedUTF8(details.text) || !IsAllowed(details.text))
{
const std::string msg = "Your message contained disallowed characters and was blocked. IRC operators have been notified (Spamfilter purpose).";
// Announce to opers
std::string oper_announcement;
if (target.type == MessageTarget::TYPE_CHANNEL)
{
auto* targchan = target.Get<Channel>();
oper_announcement = INSP_FORMAT("MixedCharacterUTF8: User {} in channel {} sent a message containing disallowed characters: '{}', which was blocked.", user->nick, targchan->name, details.text);
ServerInstance->SNO.WriteGlobalSno('a', oper_announcement);
user->WriteNumeric(Numerics::CannotSendTo(targchan, msg));
}
else
{
auto* targuser = target.Get<User>();
oper_announcement = INSP_FORMAT("MixedCharacterUTF8: User {} sent a private message to {} containing disallowed characters: '{}', which was blocked.", user->nick, targuser->nick, details.text);
ServerInstance->SNO.WriteGlobalSno('a', oper_announcement);
user->WriteNumeric(Numerics::CannotSendTo(targuser, msg));
}
return MOD_RES_DENY;
}
for (const auto& [find, replace] : censors)
{
size_t censorpos;
while ((censorpos = irc::find(details.text, find)) != std::string::npos)
{
if (replace.empty())
{
const std::string msg = INSP_FORMAT("Your message to this channel contained a banned phrase ({}) and was blocked. IRC operators have been notified (Spamfilter purpose).", find);
// Announce to opers
std::string oper_announcement;
if (target.type == MessageTarget::TYPE_CHANNEL)
{
auto* targchan = target.Get<Channel>();
oper_announcement = INSP_FORMAT("CensorPlus: User {} in channel {} sent a message containing banned phrase ({}): '{}', which was blocked.", user->nick, targchan->name, find, details.text);
}
else
{
auto* targuser = target.Get<User>();
oper_announcement = INSP_FORMAT("CensorPlus: User {} sent a private message to {} containing banned phrase ({}): '{}', which was blocked.", user->nick, targuser->nick, find, details.text);
}
ServerInstance->SNO.WriteGlobalSno('a', oper_announcement);
if (target.type == MessageTarget::TYPE_CHANNEL)
user->WriteNumeric(Numerics::CannotSendTo(target.Get<Channel>(), msg));
else
user->WriteNumeric(Numerics::CannotSendTo(target.Get<User>(), msg));
return MOD_RES_DENY;
}
details.text.replace(censorpos, find.size(), replace);
}
}
} catch (const std::exception& e) {
ServerInstance->Logs.Normal(MODNAME, INSP_FORMAT("Exception in OnUserPreMessage: {}", e.what()));
}
return MOD_RES_PASSTHRU;
}
};
MODULE_INIT(ModuleCensor)