init.lua 2.9 KB

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