ignore.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. -- chat3/ignore.lua
  2. chat3.ignore = {}
  3. local storage = chat3.storage
  4. ---
  5. --- Functions (API)
  6. ---
  7. -- [function] Get ignore list
  8. function chat3.ignore.get(name)
  9. local list = storage:get_string("ignore_list_"..name)
  10. if list then
  11. list = minetest.deserialize(list)
  12. end
  13. if not list or not list.ignoring or not list.ignored_by then
  14. list = {ignoring = {}, ignored_by = {}}
  15. end
  16. return list
  17. end
  18. -- [function] Set ignore list
  19. function chat3.ignore.set(name, list)
  20. storage:set_string("ignore_list_"..name, minetest.serialize(list))
  21. end
  22. -- [function] Add name to list
  23. function chat3.ignore.add_name(name, ignore)
  24. -- Update ignoring
  25. local list = chat3.ignore.get(name)
  26. list.ignoring[ignore] = true
  27. chat3.ignore.set(name, list)
  28. -- Update ignored_by
  29. list = chat3.ignore.get(ignore)
  30. list.ignored_by[name] = true
  31. chat3.ignore.set(ignore, list)
  32. return true
  33. end
  34. -- [function] Remove name from list
  35. function chat3.ignore.remove_name(name, unignore)
  36. -- Update ignoring
  37. local list = chat3.ignore.get(name)
  38. list.ignoring[unignore] = nil
  39. chat3.ignore.set(name, list)
  40. -- Update ignored_by
  41. list = chat3.ignore.get(unignore)
  42. list.ignored_by[name] = nil
  43. chat3.ignore.set(unignore, list)
  44. return true
  45. end
  46. -- [function] Clear ignore list
  47. function chat3.ignore.clear(name)
  48. local list = chat3.ignore.get(name)
  49. -- Clear resulting ignored_by entries and notify
  50. for t, i in pairs(list.ignoring) do
  51. local tlist = chat3.ignore.get(t)
  52. tlist.ignored_by[name] = nil
  53. chat3.ignore.set(t, tlist)
  54. minetest.chat_send_player(t, chat3.colorize(t, "#00ff00", name..
  55. " is no longer ignoring you."))
  56. list.ignoring[t] = nil
  57. end
  58. chat3.ignore.set(name, list)
  59. return true
  60. end
  61. -- [function] Can ignore
  62. function chat3.ignore.can(name, check)
  63. local priv = minetest.check_player_privs(check, "ignore_override")
  64. if not priv and minetest.settings:get("name") ~= check and name ~= check then
  65. return true
  66. else
  67. chat3.ignore.remove_name(name, check)
  68. end
  69. end
  70. -- [function] Is Ignoring
  71. function chat3.ignore.is(name, check)
  72. if name == check then
  73. return false
  74. else
  75. local list = chat3.ignore.get(check).ignored_by
  76. return list[name] and chat3.ignore.can(name, check)
  77. end
  78. end
  79. -- [function] Ignore
  80. function chat3.ignore.add(name, ignore)
  81. if chat3.ignore.can(name, ignore) then
  82. if chat3.ignore.get(name).ignoring[ignore] then
  83. return false, "You are already ignoring "..ignore.."."
  84. else
  85. chat3.ignore.add_name(name, ignore)
  86. -- Notify ignored player
  87. --[[if minetest.get_player_by_name(ignore) then
  88. minetest.chat_send_player(ignore, chat3.colorize(ignore, "red", name..
  89. " is now ignoring you."))
  90. end
  91. --]]
  92. return true, "Added "..ignore.." to your ignore list."
  93. end
  94. else
  95. return false, "You cannot ignore "..ignore.."."
  96. end
  97. end
  98. -- [function] Unignore
  99. function chat3.ignore.remove(name, unignore)
  100. if chat3.ignore.get(name).ignoring[unignore] then
  101. chat3.ignore.remove_name(name, unignore)
  102. -- Notify unignored player
  103. if minetest.get_player_by_name(unignore) then
  104. minetest.chat_send_player(unignore, chat3.colorize(unignore, "#00ff00",
  105. name.." is no longer ignoring you."))
  106. end
  107. return true, "Removed "..unignore.." from your ignore list."
  108. else
  109. return false, unignore.." is already not on your ignore list."
  110. end
  111. end
  112. -- [function] Check Ignore (verbose [chat] version if Is Ignoring)
  113. function chat3.ignore.check(name, check)
  114. minetest.log("chat3.ignore.check: "..name..", "..check)
  115. if chat3.ignore.is(name, check) then
  116. return true, "You are being ignored by "..name.."."
  117. else
  118. return false, name.." is not ignoring you."
  119. end
  120. end
  121. -- [function] List Ignored Players (returns a table suitable for use with table.concat)
  122. function chat3.ignore.list(name, subtable)
  123. local list, result = chat3.ignore.get(name)[subtable], {}
  124. if list then
  125. for ignored, i in pairs(list) do
  126. table.insert(result, ignored)
  127. end
  128. if #result > 0 then
  129. return result
  130. end
  131. end
  132. end
  133. ---
  134. --- Registrations
  135. ---
  136. -- [event] Show list of people ignoring you on join
  137. --[[
  138. minetest.register_on_joinplayer(function(player)
  139. local name = player:get_player_name()
  140. local list = chat3.ignore.list(name, "ignored_by")
  141. if list then
  142. local p = "players"
  143. if #list == 1 then
  144. p = "player"
  145. end
  146. minetest.chat_send_player(name, string.format(
  147. "You are being ignored by %i %s: %s.", #list, p,
  148. table.concat(list, ", ")))
  149. end
  150. end)
  151. --]]
  152. -- [privilege] Ignore Override
  153. minetest.register_privilege("ignore_override", {
  154. description = "Prevent players from ignoring anyone with this privilege.",
  155. give_to_singleplayer = false,
  156. })
  157. -- [chatcommand] Ignore
  158. minetest.register_chatcommand("ignore", {
  159. description = "Ignore players",
  160. params = "[list | by | add | del | rst | check] [<player username>]",
  161. func = function(name, params)
  162. params = params:split(" ")
  163. local operation, target = params[1], params[2]
  164. local invalid = "Invalid parameters (see /help ignore)"
  165. if operation and operation ~= "" then
  166. if operation == "list" then
  167. local list = chat3.ignore.list(name, "ignoring")
  168. if list then
  169. return true, "Ignored Players: "..table.concat(list, ", ").."."
  170. else
  171. return false, "You are not ignoring any players."
  172. end
  173. elseif operation == "by" then
  174. local list = chat3.ignore.list(name, "ignored_by")
  175. if list then
  176. return true, "You are being ignored by: "..table.concat(list, ", ").."."
  177. else
  178. return false, "You are not being ignored by any players."
  179. end
  180. elseif operation == "add" and target and target ~= "" then
  181. return chat3.ignore.add(name, target)
  182. elseif operation == "del" and target and target ~= "" then
  183. return chat3.ignore.remove(name, target)
  184. elseif operation == "check" and target and target ~= "" then
  185. return chat3.ignore.check(target, name)
  186. elseif operation == "rst" then
  187. chat3.ignore.clear(name)
  188. return true, "Cleared your ignore list."
  189. else
  190. return false, invalid
  191. end
  192. else
  193. return false, invalid
  194. end
  195. end,
  196. })