init.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. --[[This is a mod that contains a player register with the count of the reports.
  2. Mod created by Lejo!]]
  3. local settings = {
  4. time_played_to_report = 3600, -- in seconds Only needed when using playtime
  5. time_of_tempban = minetest.settings:get("report_tempbantime") or 259200, -- bantime inseconds
  6. }
  7. reportlist = {}
  8. local s = minetest.get_mod_storage()
  9. local new_table = {r = {}}
  10. --[[
  11. Player table schem:
  12. reporter = {["name"] = {reason = "..", time=os.time()}}
  13. {r = {["name"] = {r = "Reason", t = os.time()}},
  14. }
  15. ]]
  16. function reportlist.get_data(name)
  17. local data = s:get_string(name)
  18. if data ~= "" then
  19. return minetest.deserialize(data)
  20. else s:set_string(name, new_table)
  21. return new_table
  22. end
  23. end
  24. function reportlist.get_all_data()
  25. local out = {}
  26. for name, data in pairs(s:to_table().fields) do
  27. out[name] = minetest.deserialize(data)
  28. end
  29. return out
  30. end
  31. function reportlist.set_data(name, data)
  32. s:set_string(name, minetest.serialize(data))
  33. end
  34. function reportlist.remove_data(name)
  35. s:set_string(name, "")
  36. end
  37. function reportlist.exist(name)
  38. if s:get_string(name) ~= "" then
  39. return true
  40. else return false
  41. end
  42. end
  43. function reportlist.is_reporter(name, reportername)
  44. local data = reportlist.get_data(name)
  45. if data.r[reportername] then
  46. return true
  47. end
  48. end
  49. function reportlist.add_reporter(name, reportername, reason)
  50. local data = reportlist.get_data(name)
  51. data.r[reportername] = {r = reason, t = os.time()}
  52. reportlist.set_data(name, data)
  53. end
  54. -- Add the report Command
  55. minetest.register_chatcommand("report", {
  56. privs = {shout = true},
  57. params = "<name> <reason>",
  58. description = "Use it to report players, if they are hacking, cheating...",
  59. func = function(name, param)
  60. local reported, reason = param:match("(%S+)%s+(.+)")
  61. if type(reported) == "string" and type(reason) == "string" then
  62. if minetest.player_exists(reported) then
  63. if name ~= reported and minetest.get_player_ip(name) ~= minetest.get_player_ip(reported) then
  64. if minetest.get_player_by_name(reported) then
  65. if not playtime or playtime.get_total_playtime(name) >= settings.time_played_to_report then
  66. reportlist.add_reporter(reported, name, reason)
  67. minetest.log("action", "Player "..reported.." has been reported by "..name)
  68. return true, reported.." has been reported!"
  69. else return false, "You have to play longer to report a player!"
  70. end
  71. else return false, "The Player "..reported.." is not online!"
  72. end
  73. else return false, "You can't report yourself or somebody out of you ip-Group."
  74. end
  75. else return false, "The Player doesn't exist."
  76. end
  77. else return false, "Please specific a playername and a reason"
  78. end
  79. end,
  80. })
  81. function reportlist.show_form(name, fields)
  82. local reported = fields.name or ""
  83. local tabledata = ""
  84. local form = "size[14,5.5]" ..
  85. "label[3.5,0.1;Reportlist. Enter playername to do an action.]" ..
  86. "button[11.2,2.6;2.5,1;reset;reset]" ..
  87. "field[11.5,4;2.5,1;reason;Ban Reason;]" ..
  88. "button[11.2,4.4;2.5,1;tempban;Tempban(default 3 days)]" ..
  89. "field[11.5,1;1.7,1;name;Player Name;"..reported.."]" ..
  90. "field_close_on_enter[name;false]" ..
  91. "button[12.9,0.7;0.8,1;go;Go]"
  92. if fields and fields.name and fields.name ~= "" then
  93. if reportlist.exist(fields.name) then
  94. local data = reportlist.get_data(fields.name)
  95. for playername, d in pairs(data.r) do
  96. local pdata = reportlist.get_data(playername)
  97. tabledata = tabledata..playername..","..os.date("%c", d.t)..","..minetest.formspec_escape(d.r)..","
  98. end
  99. else tabledata = "Player hasn't been reported yet"
  100. end
  101. form = form .. "tablecolumns[text;text;text]"
  102. else
  103. for name, data in pairs(reportlist.get_all_data()) do
  104. local count = 0
  105. local pstuff = ""
  106. for name, d in pairs(data.r) do
  107. pstuff = pstuff.."1,"..name..","..os.date("%c", d.t)..","..minetest.formspec_escape(d.r)..","
  108. count = count + 1
  109. end
  110. tabledata = tabledata.."0,"..name..","..count..",,"..pstuff
  111. end
  112. form = form .. "tablecolumns[tree;text;text;text]"
  113. end
  114. form = form .. "table[0.2,0.6;10.8,4.6;reports;"..tabledata..";1]"
  115. minetest.show_formspec(name, "reportlist:reportlist", form)
  116. end
  117. minetest.register_on_player_receive_fields(function(player, formname, fields)
  118. if formname == "reportlist:reportlist" then
  119. local name = player:get_player_name()
  120. if minetest.get_player_privs(name).ban then
  121. if fields.name and fields.name ~= "" then
  122. if fields.reset then
  123. reportlist.remove_data(fields.name)
  124. minetest.chat_send_player(name, "Reseted the reports of the player "..fields.name)
  125. elseif fields.tempban and fields.reason and fields.reason ~= "" then
  126. -- Support for sban mod or others using this command
  127. if minetest.registered_chatcommands["tempban"] then
  128. local success, msg = minetest.registered_chatcommands["tempban"].func(name, fields.name.." "..tostring(settings.time_of_tempban).." "..fields.reason)
  129. minetest.chat_send_player(name, msg)
  130. -- Xban support
  131. elseif xban and xban.ban_player then
  132. local success, err = xban.ban_player(fields.name, name, settings.time_of_tempban, fields.reason)
  133. if success then
  134. minetest.chat_send_player(name, "Banned "..fields.name)
  135. else minetest.chat_send_player(name, "Failed to ban "..fields.name.." Err:"..err)
  136. end
  137. else minetest.chat_send_player(name, "No compatible ban mod found, use sban or xban!")
  138. end
  139. end
  140. end
  141. if fields.reset or fields.tempban or fields.go or fields.key_enter then
  142. reportlist.show_form(name, fields)
  143. end
  144. else
  145. minetest.log("error", "Player "..name.." sent fields to reportlist:reportlist without the ban privi")
  146. minetest.kick_player(name)
  147. end
  148. end
  149. end)
  150. -- Add a chat command to get the report counter
  151. minetest.register_chatcommand("reportlist", {
  152. privs = {ban = true},
  153. params = "[<name>]",
  154. description = "Use it to open the reportlist form.",
  155. func = function(name, param)
  156. reportlist.show_form(name, {name = param})
  157. end,
  158. })
  159. -- Add a chat command to set the report counter
  160. minetest.register_chatcommand("report_reset", {
  161. privs = {ban = true},
  162. params = "<name>",
  163. description = "Use it to reset the reports of a player.",
  164. func = function(name, param)
  165. if minetest.player_exists(param) then
  166. reportlist.remove_data(name)
  167. minetest.chat_send_player(name, "Reseted the reports of the player "..param)
  168. else minetest.chat_send_player(name, "The Player "..param.." doesn't exist.")
  169. end
  170. end,
  171. })