A mod for Minetest to stop annoyed kids
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.3KB

  1. -- Created by Krock to stop mass-account-creators
  2. -- License: CC0
  3. if not minetest.safe_file_write then
  4. error("[simple_protection] Your Minetest version is no longer supported."
  5. .. " (version < 0.4.17)")
  6. end
  7. ipnames = {}
  8. ipnames.data = {}
  9. ipnames.whitelist = {}
  10. ipnames.changes = false
  11. ipnames.file = minetest.get_worldpath().."/ipnames.data"
  12. ipnames.whitelist_file = minetest.get_worldpath().."/ipnames_whitelist.data"
  13. -- Limit 2 = maximal 2 accounts, the 3rd under the same IP gets blocked
  14. ipnames.name_per_ip_limit = tonumber(minetest.setting_get("max_names_per_ip")) or 2
  15. -- 2 + 3 = 5 accounts as limit for "ignored" players
  16. ipnames.extended_limit = 3
  17. -- Interval where the IP list gets saved/updated
  18. ipnames.save_interval = 240
  19. dofile(minetest.get_modpath("names_per_ip").."/functions.lua")
  20. minetest.register_chatcommand("ipnames", {
  21. description = "Get the features of names_per_ip",
  22. privs = {ban=true},
  23. func = function(name, param)
  24. if param == "" then
  25. return true,
  26. "Available commands:\n" ..
  27. "Get all accounts of <name>: /ipnames whois <name>\n" ..
  28. "Show all whitelisted names: /ipnames list\n" ..
  29. "Add/remove whitelist entry: /ipnames (un)ignore <name>"
  30. end
  31. if param == "list" then
  32. return ipnames.command_list(name)
  33. end
  34. -- Commands with two arguments
  35. local args = param:split(" ")
  36. if #args < 2 then
  37. return false, "Error: Too few command arguments."
  38. end
  39. local func = ipnames["command_" .. args[1]]
  40. if func then
  41. return func(name, args[2])
  42. end
  43. return false, "Error: No known action for argument #1 ('"..args[1].."')"
  44. end
  45. })
  46. -- Get IP if player tries to join, ban if there are too much names per IP
  47. minetest.register_on_prejoinplayer(function(name, ip)
  48. -- Only stop new accounts
  49. if ipnames.data[name] or ipnames.is_registered(name) then
  50. return
  51. end
  52. local names = {} -- faster than string concat
  53. local count_bonus = nil
  54. for k, v in pairs(ipnames.data) do
  55. if v[1] == ip then
  56. if not count_bonus and ipnames.whitelist[k] then
  57. count_bonus = ipnames.extended_limit
  58. end
  59. names[#names + 1] = k
  60. end
  61. end
  62. -- Return error message if too many accounts have been created
  63. if #names > ipnames.name_per_ip_limit + (count_bonus or 0) then
  64. return "\nYou exceeded the limit of accounts.\n" ..
  65. "You already own the following accounts:\n" .. table.concat(names, ", ")
  66. end
  67. end)
  68. -- Save IP if player joined
  69. local function update_player_address(name, recursive)
  70. local info = minetest.get_player_information(name)
  71. local address = info and info.address
  72. if not address then
  73. minetest.log("warning", "[names_per_ip] minetest.get_player_information(\"" ..
  74. name .. "\"): " .. dump(info) .. ". This is probably an engine bug.")
  75. if not recursive then
  76. -- Delay, hope it works next time
  77. minetest.after(0.5, update_player_address, name, true)
  78. return
  79. end
  80. end
  81. ipnames.data[name] = {
  82. address or "??",
  83. os.time()
  84. }
  85. ipnames.changes = true
  86. end
  87. minetest.register_on_joinplayer(function(player)
  88. update_player_address(player:get_player_name())
  89. end)
  90. -- Save changes at a fixed interval
  91. local function save_data_job()
  92. ipnames.save_data()
  93. minetest.after(ipnames.save_interval, save_data_job)
  94. end
  95. minetest.after(ipnames.save_interval, save_data_job)
  96. minetest.register_on_shutdown(ipnames.save_data)
  97. ipnames.load_data()
  98. ipnames.load_whitelist()
  99. minetest.log("action", "[names_per_ip] loaded.")