Advertisement
krock186

Minetest Chat flood prevention

Aug 9th, 2016
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. -- Untested mod to prevent from chat flood
  2. -- Allows players to send 4 messages in 2s when a silence of >= 2s follows
  3. -- Created by Krock <mk939@ymail.com> - 2016
  4. -- License: BSD 3-Clause
  5. -- ALL YOUR BUG REPORT ARE BELONG TO ME
  6.  
  7.  
  8. local player_spam = {}
  9. local CHAR_REPEAT_MAX = 4
  10.  
  11. minetest.register_on_chat_message(function(name, msg)
  12.     if msg == "" or msg:sub(1, 1) == '/' then
  13.         return
  14.     end
  15.     if not minetest.check_player_privs(name, {shout = true}) then
  16.         minetest.chat_send_player(name, "You can not chat. Missing privilege: shout")
  17.         return true
  18.     end
  19.  
  20.     local count_as_messages = math.max(1, math.min(msg:len() / 100, 5))
  21.     player_spam[name] = (player_spam[name] or 0) + math.floor(count_as_messages + 0.5)
  22.  
  23.     if player_spam[name] > 5 then
  24.         minetest.kick_player(name, "You spammer you!")
  25.         return true
  26.     end
  27.     if player_spam[name] > 3 then
  28.         -- A message per second maximal
  29.         minetest.chat_send_player(name, "Your message was not sent due to flood detection. "..
  30.                 "Please try again in some seconds.")
  31.         return true
  32.     end
  33.  
  34.     local new_msg = ""
  35.     local last_char
  36.     local same_char_count = 0
  37.  
  38.     -- Prevent from repetive characters
  39.     for c in msg:gmatch(".") do
  40.         if c:byte() < 0x20 then
  41.             c = ' '
  42.         end
  43.         if last_char == c:lower() then
  44.             same_char_count = same_char_count + 1
  45.         else
  46.             last_char = c:lower()
  47.             same_char_count = 0
  48.         end
  49.  
  50.         if same_char_count < CHAR_REPEAT_MAX then
  51.             new_msg = new_msg .. c
  52.         end
  53.     end
  54.     if new_msg == msg then
  55.         return -- Nothing to replace (message ok)
  56.     end
  57.    
  58.     for i, player in pairs(minetest.get_connected_players()) do
  59.         local player_name = player:get_player_name()
  60.         if player_name ~= name then
  61.             minetest.chat_send_player(player_name, "<"..name.."> " .. new_msg)
  62.         end
  63.     end
  64.     --if new_msg:len() < msg:len() then
  65.     --    minetest.chat_send_player(name, "Your message was shortened a bit to prevent from spam.")
  66.     --end
  67.     return true
  68. end)
  69.  
  70. local timed = 0
  71. -- 1 message per second, decrease message count by X all X seconds
  72. local CHECK_COUNT = 2
  73. minetest.register_globalstep(function(dtime)
  74.     timed = timed + dtime
  75.     if timed < CHECK_COUNT then
  76.         return
  77.     end
  78.     timed = 0
  79.  
  80.     for i, player in pairs(minetest.get_connected_players()) do
  81.         local player_name = player:get_player_name()
  82.         local num = player_spam[player_name]
  83.         if num and num > 0 then
  84.             player_spam[player_name] = math.max(0, num - CHECK_COUNT)
  85.         end
  86.     end
  87. end)
  88.  
  89. minetest.register_on_leaveplayer(function(player)
  90.     player_spam[player:get_player_name()] = nil
  91. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement