init.lua 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. future_ban_list = {}
  2. local file = io.open(minetest.get_worldpath().."/future_banlist.lua", "r")
  3. if file then
  4. future_ban_list = minetest.deserialize(file:read("*all"))
  5. file:close()
  6. if not future_ban_list then
  7. future_ban_list = {}
  8. end
  9. end
  10. local function save_file()
  11. local file = io.open(minetest.get_worldpath().."/future_banlist.lua", "w")
  12. if file then
  13. file:write(minetest.serialize(future_ban_list))
  14. file:close()
  15. end
  16. end
  17. minetest.register_chatcommand("future_ban", {
  18. params = "<playername> | omit playername to see the future ban list",
  19. description = "The player will be banned when trying to join",
  20. privs = {ban=true},
  21. func = function(name, param)
  22. if param == "" then
  23. local list = {}
  24. local n=0
  25. for k,v in pairs(future_ban_list) do
  26. n = n+1
  27. list[n] = k
  28. end
  29. if n == 0 then
  30. minetest.chat_send_player(name, "Future ban list is empty.")
  31. else
  32. minetest.chat_send_player(name, "Future ban list:\n" .. table.concat(list, "\n"))
  33. end
  34. return
  35. end
  36. if minetest.get_ban_description(param) ~= "" then
  37. local desc = minetest.get_ban_description(param)
  38. minetest.chat_send_player(name, param .. " has been previously banned with the IP numbers " .. desc .. ".")
  39. end
  40. if future_ban_list[param] then
  41. future_ban_list[param] = nil
  42. minetest.chat_send_player(name, param .. " removed from the future ban list.")
  43. return
  44. end
  45. if not minetest.get_player_by_name(param) then
  46. future_ban_list[param] = true
  47. minetest.chat_send_player(name, param .. " added to the future ban list.")
  48. minetest.log("action", name .. " added " .. param .. " to the future ban list.")
  49. save_file()
  50. return
  51. end
  52. if not minetest.ban_player(param) then
  53. future_ban_list[param] = true
  54. minetest.chat_send_player(name, param .. " added to the future ban list.")
  55. minetest.log("action", name .. " added " .. param .. " to the future ban list.")
  56. save_file()
  57. else
  58. local desc = minetest.get_ban_description(param)
  59. minetest.chat_send_player(name, "Banned " .. desc .. ".")
  60. minetest.log("action", name .. " bans " .. desc .. ".")
  61. end
  62. end
  63. })
  64. minetest.register_on_joinplayer(function(player)
  65. local name = player:get_player_name()
  66. if future_ban_list[name] then
  67. if not minetest.ban_player(name) then
  68. minetest.log("warning", "Failed to ban player " .. name .. " who was on the future ban list when he joined.")
  69. else
  70. local desc = minetest.get_ban_description(name)
  71. minetest.log("action", desc .. " banned (from future ban list).")
  72. future_ban_list[name] = nil
  73. save_file()
  74. end
  75. end
  76. end)