-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathantispam.lua
More file actions
50 lines (42 loc) · 1.86 KB
/
antispam.lua
File metadata and controls
50 lines (42 loc) · 1.86 KB
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
local history = dofile(minetest.get_modpath("textshield") .. "/history.lua")
local last_message_time = {}
local SPAM_DELAY = tonumber(minetest.settings:get("textshield_spam_delay")) or 2
local MSG_SPAM_WARNING_LABEL = minetest.settings:get("textshield_spam_warning_label")
or "Please do not spam the chat or repeat characters."
local MSG_SPAM_WARNING_BUTTON = minetest.settings:get("textshield_spam_warning_button") or "Understood"
local MSG_LOG_SPAM_FAST = minetest.settings:get("textshield_log_spam_fast") or "Spam detected (message too fast): "
local MSG_LOG_SPAM_REPEAT = minetest.settings:get("textshield_log_spam_repeat") or "Spam detected (repeated characters): "
local function show_spam_warning(player_name)
local formspec = string.format([[
size[6,3]
label[0.5,1;%s]
button_exit[2,2;2,1;ok;%s]
]], MSG_SPAM_WARNING_LABEL, MSG_SPAM_WARNING_BUTTON)
minetest.show_formspec(player_name, "textshield:spam", formspec)
end
local function has_repeated_chars(message)
return message:find("(.)%1%1%1%1%1%1") ~= nil
end
minetest.register_on_chat_message(function(name, message)
local now = minetest.get_gametime()
local last = last_message_time[name] or 0
if now - last < SPAM_DELAY then
local player = minetest.get_player_by_name(name)
if player then
show_spam_warning(name)
player:set_hp(player:get_hp() - 2)
end
history.log(name, MSG_LOG_SPAM_FAST .. message)
return true
end
if has_repeated_chars(message) then
local player = minetest.get_player_by_name(name)
if player then
show_spam_warning(name)
player:set_hp(player:get_hp() - 2)
end
history.log(name, MSG_LOG_SPAM_REPEAT .. message)
return true
end
last_message_time[name] = now
end)