init.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. -- Advanced Ban [advancedban] by srifqi
  2. advancedban = {}
  3. -- parameter
  4. local FILE_NAME = "bannedplayerlist.txt"
  5. local BAN_MESSAGE = "Your username is banned."
  6. function advancedban.is_banned(name)
  7. if not file_exists(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME) then
  8. return false
  9. end
  10. local list = io.open(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME, "r")
  11. for username in list:lines() do
  12. if name == username then
  13. list:close()
  14. return true
  15. end
  16. end
  17. list:close()
  18. return false
  19. end
  20. function advancedban.ban(name)
  21. if advancedban.is_banned(name) then
  22. -- minetest.log("info", name .. " is already in advancedban list.") -- print debug
  23. return false
  24. else
  25. local list = io.open(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME, "a")
  26. list:write(name .. "\n")
  27. list:close()
  28. minetest.log("action", name .. " has been added to advancedban list.") -- print debug
  29. return true
  30. end
  31. end
  32. function advancedban.unban(name)
  33. local found = false
  34. if file_exists(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME) then
  35. local list = io.open(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME, "r")
  36. local text = ""
  37. for username in list:lines() do
  38. if name == username then
  39. found = true
  40. else
  41. text = text .. username .. "\n"
  42. end
  43. end
  44. list:close()
  45. local list = io.open(minetest.get_worldpath() .. DIR_DELIM .. FILE_NAME, "w")
  46. list:write(text)
  47. list:close()
  48. end
  49. if found then
  50. minetest.log("action", name .. " has been removed from advancedban list.") -- print debug
  51. end
  52. return found
  53. end
  54. minetest.register_chatcommand("abancheck", {
  55. params = "<player name>",
  56. description = "Check ban for a player with given name",
  57. privs = {},
  58. func = function(name, param)
  59. if advancedban.is_banned(param) then
  60. minetest.chat_send_player(name, param .. " is in advancedban list.")
  61. else
  62. minetest.chat_send_player(name, param .. " is not in advancedban list.")
  63. end
  64. end
  65. })
  66. minetest.register_chatcommand("aban", {
  67. params = "<player name>",
  68. description = "Ban a player with given name",
  69. privs = {ban = true},
  70. func = function(name, param)
  71. if advancedban.ban(param) then
  72. minetest.chat_send_player(name, param .. " has been added to advancedban list.")
  73. else
  74. minetest.chat_send_player(name, param .. " is already in advancedban list.")
  75. end
  76. end
  77. })
  78. minetest.register_chatcommand("abankick", {
  79. params = "<player name>",
  80. description = "Ban and kick a player with given name",
  81. privs = {ban = true, kick = true},
  82. func = function(name, param)
  83. local kick = ", but failed to kick the player."
  84. if minetest.kick_player(param) then
  85. kick = " and has been kicked."
  86. end
  87. if advancedban.ban(param) then
  88. minetest.chat_send_player(name, param .. " has been added to advancedban list" .. kick)
  89. else
  90. minetest.chat_send_player(name, param .. " is already in advancedban list" .. kick)
  91. end
  92. end
  93. })
  94. minetest.register_chatcommand("aban+", {
  95. params = "<player name>",
  96. description = "Ban a player and its IP with given name",
  97. privs = {ban = true},
  98. func = function(name, param)
  99. local IP = ", but failed to ban IP of the player."
  100. if minetest.ban_player(param) then
  101. IP = " and IP of the player is banned."
  102. end
  103. if advancedban.ban(param) then
  104. minetest.chat_send_player(name, param .. " has been added to advancedban list" .. IP)
  105. else
  106. minetest.chat_send_player(name, param .. " is already in advancedban list" .. IP)
  107. end
  108. end
  109. })
  110. minetest.register_chatcommand("unaban", {
  111. params = "<player name>",
  112. description = "Remove player ban with given name",
  113. privs = {ban = true},
  114. func = function(name, param)
  115. if advancedban.unban(param) then
  116. minetest.chat_send_player(name, param .. " has been removed from advancedban list.")
  117. else
  118. minetest.chat_send_player(name, param .. " is not found in advancedban list.")
  119. end
  120. end
  121. })
  122. minetest.register_chatcommand("unaban+", {
  123. params = "<player name>",
  124. description = "Remove player ban and its IP with given name",
  125. privs = {ban = true},
  126. func = function(name, param)
  127. local IP = ", but failed to unban IP of the player"
  128. if minetest.unban_player_or_ip(param) then
  129. IP = " and IP of the player is unbanned."
  130. end
  131. if advancedban.unban(param) then
  132. minetest.chat_send_player(name, param .. " has been removed from advancedban list" .. IP)
  133. else
  134. minetest.chat_send_player(name, param .. " is not found in advancedban list" .. IP)
  135. end
  136. end
  137. })
  138. -- prevent advancedbanned player to join
  139. minetest.register_on_prejoinplayer(function(name)
  140. if advancedban.is_banned(name) then
  141. return BAN_MESSAGE
  142. end
  143. end)
  144. -- Minetest library - misc_helpers.lua
  145. function file_exists(filename)local f=io.open(filename,"r");if f==nil then return false else f:close() return true end end