diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index 68481f7c8..d162bc0a2 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -723,3 +723,28 @@ function core.pointed_thing_to_face_pos(placer, pointed_thing) end return fine_pos end + +function core.string_to_privs(str, delim) + assert(type(str) == "string") + delim = delim or ',' + local privs = {} + for _, priv in pairs(string.split(str, delim)) do + privs[priv:trim()] = true + end + return privs +end + +function core.privs_to_string(privs, delim) + assert(type(privs) == "table") + delim = delim or ',' + local list = {} + for priv, bool in pairs(privs) do + if bool then + list[#list + 1] = priv + end + end + return table.concat(list, delim) +end + +assert(core.string_to_privs("a,b").b == true) +assert(core.privs_to_string({a=true,b=true}) == "a,b") diff --git a/builtin/game/auth.lua b/builtin/game/auth.lua index 8cb4ebf57..7a6be8788 100644 --- a/builtin/game/auth.lua +++ b/builtin/game/auth.lua @@ -4,31 +4,6 @@ -- Authentication handler -- -function core.string_to_privs(str, delim) - assert(type(str) == "string") - delim = delim or ',' - local privs = {} - for _, priv in pairs(string.split(str, delim)) do - privs[priv:trim()] = true - end - return privs -end - -function core.privs_to_string(privs, delim) - assert(type(privs) == "table") - delim = delim or ',' - local list = {} - for priv, bool in pairs(privs) do - if bool then - list[#list + 1] = priv - end - end - return table.concat(list, delim) -end - -assert(core.string_to_privs("a,b").b == true) -assert(core.privs_to_string({a=true,b=true}) == "a,b") - core.auth_file_path = core.get_worldpath().."/auth.txt" core.auth_table = {} diff --git a/clientmods/preview/init.lua b/clientmods/preview/init.lua index f3992612a..073ea11db 100644 --- a/clientmods/preview/init.lua +++ b/clientmods/preview/init.lua @@ -150,3 +150,9 @@ core.register_on_punchnode(function(pos, node) return false end) +core.register_chatcommand("privs", { + func = function(param) + return true, core.privs_to_string(minetest.get_privilege_list()) + end, +}) + diff --git a/doc/client_lua_api.md b/doc/client_lua_api.md index 1375e1355..42d2bfbbf 100644 --- a/doc/client_lua_api.md +++ b/doc/client_lua_api.md @@ -730,6 +730,13 @@ Call these functions only at load time! * `minetest.localplayer` * Reference to the LocalPlayer object. See [`LocalPlayer`](#localplayer) class reference for methods. +### Privileges +* `minetest.get_privilege_list()` + * Returns a list of privileges the currect player has in the format `{priv1=true,...}` +* `minetest.string_to_privs(str)`: returns `{priv1=true,...}` +* `minetest.privs_to_string(privs)`: returns `"priv1,priv2,..."` + * Convert between two privilege representations + ### Client Environment * `minetest.get_player_names()` * Returns list of player names on server diff --git a/src/client.h b/src/client.h index 149fdfe57..9aec0d061 100644 --- a/src/client.h +++ b/src/client.h @@ -416,6 +416,9 @@ public: bool checkPrivilege(const std::string &priv) const { return (m_privileges.count(priv) != 0); } + const std::unordered_set &getPrivilegeList() const + { return m_privileges; } + bool getChatMessage(std::wstring &message); void typeChatMessage(const std::wstring& message); diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 3c2955bcd..eab7bdfae 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -329,6 +329,16 @@ int ModApiClient::l_take_screenshot(lua_State *L) return 0; } +int ModApiClient::l_get_privilege_list(lua_State *L) +{ + const Client *client = getClient(L); + lua_newtable(L); + for (const std::string &priv : client->getPrivilegeList()) { + lua_pushboolean(L, true); + lua_setfield(L, -2, priv.c_str()); + } + return 1; +} void ModApiClient::Initialize(lua_State *L, int top) { API_FCT(get_current_modname); @@ -353,4 +363,5 @@ void ModApiClient::Initialize(lua_State *L, int top) API_FCT(get_item_def); API_FCT(get_node_def); API_FCT(take_screenshot); + API_FCT(get_privilege_list); } diff --git a/src/script/lua_api/l_client.h b/src/script/lua_api/l_client.h index fe5780fb1..2f0c2e3f8 100644 --- a/src/script/lua_api/l_client.h +++ b/src/script/lua_api/l_client.h @@ -89,8 +89,12 @@ private: // get_node_def(nodename) static int l_get_node_def(lua_State *L); + // take_screenshot() static int l_take_screenshot(lua_State *L); + // get_privilege_list() + static int l_get_privilege_list(lua_State *L); + public: static void Initialize(lua_State *L, int top); };