chatcommands.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. --Modified from builtin/game/chatcommands.lua to hide privs starting with GAME
  2. minetest.register_chatcommand("privs", {
  3. params = "<name>",
  4. description = "print out privileges of player",
  5. func = function(name, param)
  6. param = (param ~= "" and param or name)
  7. local privs_table = minetest.get_player_privs(param)
  8. local privs = ""
  9. local i = 1
  10. for key,value in pairs(privs_table) do
  11. if key:match("GAME", 1) then
  12. key, value = nil
  13. elseif i == 1 then
  14. privs = privs .. key
  15. i = i + 1
  16. else
  17. privs = privs .. ", " .. key
  18. i = i + 1
  19. end
  20. end
  21. return true, "Privileges of " .. param .. ": " .. privs
  22. end,
  23. })
  24. minetest.register_chatcommand("grant", {
  25. params = "<name> <privilege>|all",
  26. description = "Give privilege to player",
  27. func = function(name, param)
  28. if not minetest.check_player_privs(name, {privs=true}) and
  29. not minetest.check_player_privs(name, {basic_privs=true}) then
  30. return false, "Your privileges are insufficient."
  31. end
  32. local grant_name, grantprivstr = string.match(param, "([^ ]+) (.+)")
  33. if not grant_name or not grantprivstr then
  34. return false, "Invalid parameters (see /help grant)"
  35. elseif not minetest.player_exists(grant_name) then
  36. return false, "Player " .. grant_name .. " does not exist."
  37. end
  38. local grantprivs = minetest.string_to_privs(grantprivstr)
  39. if grantprivstr == "all" then
  40. grantprivs = minetest.registered_privileges
  41. end
  42. local privs = minetest.get_player_privs(grant_name)
  43. local privs_unknown = ""
  44. for priv, _ in pairs(grantprivs) do
  45. if priv ~= "interact" and priv ~= "shout" and
  46. not minetest.check_player_privs(name, {privs=true}) then
  47. return false, "Your privileges are insufficient."
  48. end
  49. if not minetest.registered_privileges[priv] then
  50. privs_unknown = privs_unknown .. "Unknown privilege: " .. priv .. "\n"
  51. end
  52. if not priv:match("GAME", 1) then
  53. privs[priv] = true
  54. end
  55. end
  56. if privs_unknown ~= "" then
  57. return false, privs_unknown
  58. end
  59. minetest.set_player_privs(grant_name, privs)
  60. local privs_table = minetest.get_player_privs(grant_name)
  61. local privs_string = ""
  62. local i = 1
  63. for key,value in pairs(privs_table) do
  64. if key:match("GAME", 1) then
  65. key, value = nil
  66. elseif i == 1 then
  67. privs_string = privs_string .. key
  68. i = i + 1
  69. else
  70. privs_string = privs_string .. ", " .. key
  71. i = i + 1
  72. end
  73. end
  74. minetest.log("action", name..' granted ' .. privs_string .. ' privileges to '.. grant_name)
  75. if grant_name ~= name then
  76. minetest.chat_send_player(grant_name, name
  77. .. " granted you privileges: "
  78. .. privs_string)
  79. end
  80. return true, "Privileges of " .. grant_name .. ": " .. privs_string
  81. end,
  82. })
  83. minetest.register_chatcommand("grantme", {
  84. params = "<privilege>|all",
  85. description = "Give privilege to yourself",
  86. func = function(name, param)
  87. if not minetest.check_player_privs(name, {privs=true}) and
  88. not minetest.check_player_privs(name, {basic_privs=true}) then
  89. return false, "Your privileges are insufficient."
  90. end
  91. if not param then
  92. return false, "Invalid parameters (see /help grant)"
  93. end
  94. local grantprivs = minetest.string_to_privs(param)
  95. if param == "all" then
  96. grantprivs = minetest.registered_privileges
  97. end
  98. local privs = minetest.get_player_privs(name)
  99. local privs_unknown = ""
  100. for priv, _ in pairs(grantprivs) do
  101. if priv ~= "interact" and priv ~= "shout" and
  102. not minetest.check_player_privs(name, {privs=true}) then
  103. return false, "Your privileges are insufficient."
  104. end
  105. if not minetest.registered_privileges[priv] then
  106. privs_unknown = privs_unknown .. "Unknown privilege: " .. priv .. "\n"
  107. end
  108. if not priv:match("GAME", 1) then
  109. privs[priv] = true
  110. end
  111. end
  112. if privs_unknown ~= "" then
  113. return false, privs_unknown
  114. end
  115. minetest.set_player_privs(name, privs)
  116. local privs_table = minetest.get_player_privs(name)
  117. local privs_string = ""
  118. local i = 1
  119. for key,value in pairs(privs_table) do
  120. if key:match("GAME", 1) then
  121. key, value = nil
  122. elseif i == 1 then
  123. privs_string = privs_string .. key
  124. i = i + 1
  125. else
  126. privs_string = privs_string .. ", " .. key
  127. i = i + 1
  128. end
  129. end
  130. minetest.log("action", name ..' granted ' .. privs_string .. ' privileges to him/herself')
  131. return true, "Privileges of " .. name .. ": " .. privs_string
  132. end,
  133. })
  134. minetest.register_chatcommand("revoke", {
  135. params = "<name> <privilege>|all",
  136. description = "Remove privilege from player",
  137. privs = {},
  138. func = function(name, param)
  139. if not minetest.check_player_privs(name, {privs=true}) and
  140. not minetest.check_player_privs(name, {basic_privs=true}) then
  141. return false, "Your privileges are insufficient."
  142. end
  143. local revoke_name, revoke_priv_str = string.match(param, "([^ ]+) (.+)")
  144. if not revoke_name or not revoke_priv_str then
  145. return false, "Invalid parameters (see /help revoke)"
  146. elseif not minetest.player_exists(revoke_name) then
  147. return false, "Player " .. revoke_name .. " does not exist."
  148. end
  149. local revoke_privs = minetest.string_to_privs(revoke_priv_str)
  150. local privs = minetest.get_player_privs(revoke_name)
  151. for priv, _ in pairs(revoke_privs) do
  152. if priv ~= "interact" and priv ~= "shout" and
  153. not minetest.check_player_privs(name, {privs=true}) then
  154. return false, "Your privileges are insufficient."
  155. end
  156. end
  157. if revoke_priv_str == "all" then
  158. for priv, _ in pairs(privs) do
  159. if priv:find("GAME", 1) == nil then
  160. privs[priv] = nil
  161. end
  162. end
  163. else
  164. for priv, _ in pairs(revoke_privs) do
  165. if priv:find("GAME", 1) == nil then
  166. privs[priv] = nil
  167. end
  168. end
  169. end
  170. minetest.set_player_privs(revoke_name, privs)
  171. local privs_table = minetest.get_player_privs(revoke_name)
  172. local privs_string = ""
  173. local i = 1
  174. for key,value in pairs(privs_table) do
  175. if key:match("GAME", 1) then
  176. key, value = nil
  177. elseif i == 1 then
  178. privs_string = privs_string .. key
  179. i = i + 1
  180. else
  181. privs_string = privs_string .. ", " .. key
  182. i = i + 1
  183. end
  184. end
  185. minetest.log("action", name..' revoked ('
  186. .. privs_string
  187. ..') privileges from '..revoke_name)
  188. if revoke_name ~= name then
  189. minetest.chat_send_player(revoke_name, name
  190. .. " revoked privileges from you: "
  191. .. privs_string)
  192. end
  193. return true, "Privileges of " .. revoke_name .. ": " .. privs_string
  194. end,
  195. })