functions.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. -- WARNING: ipnames.command_* are reserved for chat commands!
  2. function ipnames.command_list(name)
  3. local names = {} -- faster than string concat
  4. for k, v in pairs(ipnames.whitelist) do
  5. if v then
  6. names[#names + 1] = k
  7. end
  8. end
  9. return true, "All exceptions: " .. table.concat(names, ", ")
  10. end
  11. function ipnames.command_whois(name, param)
  12. if not ipnames.data[param] then
  13. return false, "The player '" .. param .. "' did not join yet."
  14. end
  15. local ip = ipnames.data[param][1]
  16. local names = ""
  17. for k, v in pairs(ipnames.data) do
  18. if v[1] == ip then
  19. names = names .. " " .. k
  20. end
  21. end
  22. return true, "Following players share an IP: " .. names
  23. end
  24. function ipnames.command_ignore(name, param)
  25. if not ipnames.data[param] then
  26. return false, "The player '" .. param .. "' did not join yet."
  27. end
  28. ipnames.whitelist[param] = true
  29. ipnames.save_whitelist()
  30. return true, "Added '" .. param .. "' to the name whitelist."
  31. end
  32. function ipnames.command_unignore(name, param)
  33. if not ipnames.whitelist[param] then
  34. return false, "The player '" .. param .. "' is not on the whitelist."
  35. end
  36. ipnames.whitelist[param] = nil
  37. ipnames.save_whitelist()
  38. return true, "Removed '" .. param .. "' from the name whitelist."
  39. end
  40. ipnames.is_registered = minetest.player_exists
  41. or function(name)
  42. -- 0.4.x support: If you get a nil error here -> update Minetest
  43. return minetest.auth_table[name]
  44. end
  45. -- TODO: Use mod storage
  46. function ipnames.load_data()
  47. local file = io.open(ipnames.file, "r")
  48. if not file then
  49. return
  50. end
  51. local t = os.time()
  52. for line in file:lines() do
  53. local data = line:split("|")
  54. if #data >= 2 then
  55. -- Ignore players which were removed (according to auth)
  56. local player_exists = ipnames.is_registered(data[1])
  57. if player_exists then
  58. data[3] = tonumber(data[3]) or 0
  59. -- Remove IP after 2 weeks: Expired
  60. if data[3] > 0 and t - data[3] > (3600 * 24 * 14) then
  61. player_exists = false
  62. end
  63. end
  64. if player_exists then
  65. ipnames.data[data[1]] = {data[2], data[3]}
  66. end
  67. end
  68. end
  69. io.close(file)
  70. end
  71. function ipnames.save_data()
  72. if not ipnames.changes then
  73. return
  74. end
  75. ipnames.changes = false
  76. local contents = {} -- faster than string concat
  77. for k, v in pairs(ipnames.data) do
  78. v[2] = v[2] or os.time()
  79. contents[#contents + 1] = k.."|"..v[1].."|"..v[2]
  80. end
  81. minetest.safe_file_write(ipnames.file, table.concat(contents, "\n"))
  82. end
  83. function ipnames.load_whitelist()
  84. local file = io.open(ipnames.whitelist_file, "r")
  85. if not file then
  86. return
  87. end
  88. for line in file:lines() do
  89. if line ~= "" then
  90. ipnames.whitelist[line] = true
  91. end
  92. end
  93. io.close(file)
  94. end
  95. function ipnames.save_whitelist()
  96. local names = {} -- faster than string concat
  97. for k, v in pairs(ipnames.whitelist) do
  98. if v then
  99. names[#names + 1] = k
  100. end
  101. end
  102. minetest.safe_file_write(ipnames.whitelist_file, table.concat(names, "\n"))
  103. end