2013-08-11 04:09:45 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpp_api/s_server.h"
|
|
|
|
#include "cpp_api/s_internal.h"
|
|
|
|
#include "common/c_converter.h"
|
2021-09-09 16:51:35 +02:00
|
|
|
#include "util/numeric.h" // myrand
|
2013-08-11 04:09:45 +02:00
|
|
|
|
|
|
|
bool ScriptApiServer::getAuth(const std::string &playername,
|
|
|
|
std::string *dst_password,
|
2020-05-23 13:24:06 +02:00
|
|
|
std::set<std::string> *dst_privs,
|
|
|
|
s64 *dst_last_login)
|
2013-08-11 04:09:45 +02:00
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
2013-08-11 04:09:45 +02:00
|
|
|
getAuthHandler();
|
|
|
|
lua_getfield(L, -1, "get_auth");
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_type(L, -1) != LUA_TFUNCTION)
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Authentication handler missing get_auth");
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_pushstring(L, playername.c_str());
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 1, 1, error_handler));
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_remove(L, -2); // Remove auth handler
|
2015-08-25 07:44:53 +02:00
|
|
|
lua_remove(L, error_handler);
|
2013-08-11 04:09:45 +02:00
|
|
|
|
|
|
|
// nil = login not allowed
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_isnil(L, -1))
|
2013-08-11 04:09:45 +02:00
|
|
|
return false;
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
|
|
|
|
std::string password;
|
2020-05-23 13:24:06 +02:00
|
|
|
if (!getstringfield(L, -1, "password", password))
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Authentication handler didn't return password");
|
2014-04-15 19:30:46 +02:00
|
|
|
if (dst_password)
|
2013-08-11 04:09:45 +02:00
|
|
|
*dst_password = password;
|
|
|
|
|
|
|
|
lua_getfield(L, -1, "privileges");
|
2014-04-15 19:30:46 +02:00
|
|
|
if (!lua_istable(L, -1))
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Authentication handler didn't return privilege table");
|
2014-04-15 19:30:46 +02:00
|
|
|
if (dst_privs)
|
2013-08-11 04:09:45 +02:00
|
|
|
readPrivileges(-1, *dst_privs);
|
2020-05-23 13:24:06 +02:00
|
|
|
lua_pop(L, 1); // Remove key from privs table
|
|
|
|
|
|
|
|
s64 last_login;
|
|
|
|
if(!getintfield(L, -1, "last_login", last_login))
|
|
|
|
throw LuaError("Authentication handler didn't return last_login");
|
|
|
|
if (dst_last_login)
|
|
|
|
*dst_last_login = (s64)last_login;
|
2013-08-11 04:09:45 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiServer::getAuthHandler()
|
|
|
|
{
|
|
|
|
lua_State *L = getStack();
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
lua_getglobal(L, "core");
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_getfield(L, -1, "registered_auth_handler");
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_isnil(L, -1)){
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_getfield(L, -1, "builtin_auth_handler");
|
|
|
|
}
|
2015-08-12 04:27:54 +02:00
|
|
|
|
|
|
|
setOriginFromTable(-1);
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
lua_remove(L, -2); // Remove core
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_type(L, -1) != LUA_TTABLE)
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Authentication handler table not valid");
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
|
|
|
|
{
|
|
|
|
lua_State *L = getStack();
|
|
|
|
|
|
|
|
result.clear();
|
|
|
|
lua_pushnil(L);
|
2014-04-15 19:30:46 +02:00
|
|
|
if (index < 0)
|
2013-08-11 04:09:45 +02:00
|
|
|
index -= 1;
|
2014-04-15 19:30:46 +02:00
|
|
|
while (lua_next(L, index) != 0) {
|
2013-08-11 04:09:45 +02:00
|
|
|
// key at index -2 and value at index -1
|
|
|
|
std::string key = luaL_checkstring(L, -2);
|
2018-06-30 17:11:38 +02:00
|
|
|
bool value = readParam<bool>(L, -1);
|
2014-04-15 19:30:46 +02:00
|
|
|
if (value)
|
2013-08-11 04:09:45 +02:00
|
|
|
result.insert(key);
|
|
|
|
// removes value, keeps key for next iteration
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiServer::createAuth(const std::string &playername,
|
|
|
|
const std::string &password)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
2013-08-11 04:09:45 +02:00
|
|
|
getAuthHandler();
|
|
|
|
lua_getfield(L, -1, "create_auth");
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_remove(L, -2); // Remove auth handler
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_type(L, -1) != LUA_TFUNCTION)
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Authentication handler missing create_auth");
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_pushstring(L, playername.c_str());
|
|
|
|
lua_pushstring(L, password.c_str());
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 2, 0, error_handler));
|
|
|
|
lua_pop(L, 1); // Pop error handler
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptApiServer::setPassword(const std::string &playername,
|
|
|
|
const std::string &password)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
2013-08-11 04:09:45 +02:00
|
|
|
getAuthHandler();
|
|
|
|
lua_getfield(L, -1, "set_password");
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_remove(L, -2); // Remove auth handler
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_type(L, -1) != LUA_TFUNCTION)
|
2014-03-15 21:28:59 +01:00
|
|
|
throw LuaError("Authentication handler missing set_password");
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_pushstring(L, playername.c_str());
|
|
|
|
lua_pushstring(L, password.c_str());
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 2, 1, error_handler));
|
|
|
|
lua_remove(L, error_handler);
|
2013-08-11 04:09:45 +02:00
|
|
|
return lua_toboolean(L, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptApiServer::on_chat_message(const std::string &name,
|
|
|
|
const std::string &message)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Get core.registered_on_chat_messages
|
|
|
|
lua_getglobal(L, "core");
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_getfield(L, -1, "registered_on_chat_messages");
|
|
|
|
// Call callbacks
|
|
|
|
lua_pushstring(L, name.c_str());
|
|
|
|
lua_pushstring(L, message.c_str());
|
2015-08-12 04:27:54 +02:00
|
|
|
runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
|
2018-06-30 17:11:38 +02:00
|
|
|
return readParam<bool>(L, -1);
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
|
|
|
|
2018-06-06 12:53:59 +02:00
|
|
|
void ScriptApiServer::on_mods_loaded()
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get registered shutdown hooks
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_mods_loaded");
|
|
|
|
// Call callbacks
|
|
|
|
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
|
|
|
|
}
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
void ScriptApiServer::on_shutdown()
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get registered shutdown hooks
|
2014-04-28 03:02:48 +02:00
|
|
|
lua_getglobal(L, "core");
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_getfield(L, -1, "registered_on_shutdown");
|
|
|
|
// Call callbacks
|
2015-08-12 04:27:54 +02:00
|
|
|
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
|
|
|
|
2019-08-08 18:04:46 +02:00
|
|
|
std::string ScriptApiServer::formatChatMessage(const std::string &name,
|
|
|
|
const std::string &message)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Push function onto stack
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "format_chat_message");
|
|
|
|
|
|
|
|
// Push arguments onto stack
|
|
|
|
lua_pushstring(L, name.c_str());
|
|
|
|
lua_pushstring(L, message.c_str());
|
|
|
|
|
|
|
|
// Actually call the function
|
|
|
|
lua_call(L, 2, 1);
|
|
|
|
|
|
|
|
// Fetch return value
|
|
|
|
std::string ret = lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2021-09-09 16:51:35 +02:00
|
|
|
|
|
|
|
u32 ScriptApiServer::allocateDynamicMediaCallback(int f_idx)
|
|
|
|
{
|
|
|
|
lua_State *L = getStack();
|
|
|
|
|
|
|
|
if (f_idx < 0)
|
|
|
|
f_idx = lua_gettop(L) + f_idx + 1;
|
|
|
|
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "dynamic_media_callbacks");
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
|
|
|
|
// Find a randomly generated token that doesn't exist yet
|
|
|
|
int tries = 100;
|
|
|
|
u32 token;
|
|
|
|
while (1) {
|
|
|
|
token = myrand();
|
|
|
|
lua_rawgeti(L, -2, token);
|
|
|
|
bool is_free = lua_isnil(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
if (is_free)
|
|
|
|
break;
|
|
|
|
if (--tries < 0)
|
|
|
|
FATAL_ERROR("Ran out of callbacks IDs?!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// core.dynamic_media_callbacks[token] = callback_func
|
|
|
|
lua_pushvalue(L, f_idx);
|
|
|
|
lua_rawseti(L, -2, token);
|
|
|
|
|
|
|
|
lua_pop(L, 2);
|
|
|
|
|
|
|
|
verbosestream << "allocateDynamicMediaCallback() = " << token << std::endl;
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiServer::freeDynamicMediaCallback(u32 token)
|
|
|
|
{
|
|
|
|
lua_State *L = getStack();
|
|
|
|
|
|
|
|
verbosestream << "freeDynamicMediaCallback(" << token << ")" << std::endl;
|
|
|
|
|
|
|
|
// core.dynamic_media_callbacks[token] = nil
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "dynamic_media_callbacks");
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_rawseti(L, -2, token);
|
|
|
|
lua_pop(L, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiServer::on_dynamic_media_added(u32 token, const char *playername)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "dynamic_media_callbacks");
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
lua_rawgeti(L, -1, token);
|
|
|
|
luaL_checktype(L, -1, LUA_TFUNCTION);
|
|
|
|
|
|
|
|
lua_pushstring(L, playername);
|
|
|
|
PCALL_RES(lua_pcall(L, 1, 0, error_handler));
|
|
|
|
}
|