3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "lua_api/l_server.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
24 #include "common/c_packer.h"
25 #include "cpp_api/s_base.h"
26 #include "cpp_api/s_security.h"
27 #include "scripting_server.h"
29 #include "environment.h"
30 #include "remoteplayer.h"
35 int ModApiServer::l_request_shutdown(lua_State *L)
38 const char *msg = lua_tolstring(L, 1, NULL);
39 bool reconnect = readParam<bool>(L, 2);
40 float seconds_before_shutdown = lua_tonumber(L, 3);
41 getServer(L)->requestShutdown(msg ? msg : "", reconnect, seconds_before_shutdown);
45 // get_server_status()
46 int ModApiServer::l_get_server_status(lua_State *L)
49 lua_pushstring(L, getServer(L)->getStatusString().c_str());
53 // get_server_uptime()
54 int ModApiServer::l_get_server_uptime(lua_State *L)
57 lua_pushnumber(L, getServer(L)->getUptime());
61 // get_server_max_lag()
62 int ModApiServer::l_get_server_max_lag(lua_State *L)
66 lua_pushnumber(L, env->getMaxLagEstimate());
71 int ModApiServer::l_print(lua_State *L)
75 text = luaL_checkstring(L, 1);
76 getServer(L)->printToConsoleOnly(text);
80 // chat_send_all(text)
81 int ModApiServer::l_chat_send_all(lua_State *L)
84 const char *text = luaL_checkstring(L, 1);
85 // Get server from registry
86 Server *server = getServer(L);
88 server->notifyPlayers(utf8_to_wide(text));
92 // chat_send_player(name, text)
93 int ModApiServer::l_chat_send_player(lua_State *L)
96 const char *name = luaL_checkstring(L, 1);
97 const char *text = luaL_checkstring(L, 2);
99 // Get server from registry
100 Server *server = getServer(L);
102 server->notifyPlayer(name, utf8_to_wide(text));
106 // get_player_privs(name, text)
107 int ModApiServer::l_get_player_privs(lua_State *L)
109 NO_MAP_LOCK_REQUIRED;
110 const char *name = luaL_checkstring(L, 1);
111 // Get server from registry
112 Server *server = getServer(L);
115 int table = lua_gettop(L);
116 std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
117 for (const std::string &privs_ : privs_s) {
118 lua_pushboolean(L, true);
119 lua_setfield(L, table, privs_.c_str());
121 lua_pushvalue(L, table);
126 int ModApiServer::l_get_player_ip(lua_State *L)
128 NO_MAP_LOCK_REQUIRED;
130 Server *server = getServer(L);
132 const char *name = luaL_checkstring(L, 1);
133 RemotePlayer *player = server->getEnv().getPlayer(name);
135 lua_pushnil(L); // no such player
139 lua_pushstring(L, server->getPeerAddress(player->getPeerId()).serializeString().c_str());
143 // get_player_information(name)
144 int ModApiServer::l_get_player_information(lua_State *L)
146 NO_MAP_LOCK_REQUIRED;
148 Server *server = getServer(L);
150 const char *name = luaL_checkstring(L, 1);
151 RemotePlayer *player = server->getEnv().getPlayer(name);
153 lua_pushnil(L); // no such player
158 Be careful not to introduce a depdendency on the connection to
159 the peer here. This function is >>REQUIRED<< to still be able to return
160 values even when the peer unexpectedly disappears.
161 Hence all the ConInfo values here are optional.
164 auto getConInfo = [&] (con::rtt_stat_type type, float *value) -> bool {
165 return server->getClientConInfo(player->getPeerId(), type, value);
168 float min_rtt, max_rtt, avg_rtt, min_jitter, max_jitter, avg_jitter;
170 getConInfo(con::MIN_RTT, &min_rtt) &&
171 getConInfo(con::MAX_RTT, &max_rtt) &&
172 getConInfo(con::AVG_RTT, &avg_rtt) &&
173 getConInfo(con::MIN_JITTER, &min_jitter) &&
174 getConInfo(con::MAX_JITTER, &max_jitter) &&
175 getConInfo(con::AVG_JITTER, &avg_jitter);
178 if (!server->getClientInfo(player->getPeerId(), info)) {
179 warningstream << FUNCTION_NAME << ": no client info?!" << std::endl;
180 lua_pushnil(L); // error
185 int table = lua_gettop(L);
187 lua_pushstring(L,"address");
188 lua_pushstring(L, info.addr.serializeString().c_str());
189 lua_settable(L, table);
191 lua_pushstring(L,"ip_version");
192 if (info.addr.getFamily() == AF_INET) {
193 lua_pushnumber(L, 4);
194 } else if (info.addr.getFamily() == AF_INET6) {
195 lua_pushnumber(L, 6);
197 lua_pushnumber(L, 0);
199 lua_settable(L, table);
201 if (have_con_info) { // may be missing
202 lua_pushstring(L, "min_rtt");
203 lua_pushnumber(L, min_rtt);
204 lua_settable(L, table);
206 lua_pushstring(L, "max_rtt");
207 lua_pushnumber(L, max_rtt);
208 lua_settable(L, table);
210 lua_pushstring(L, "avg_rtt");
211 lua_pushnumber(L, avg_rtt);
212 lua_settable(L, table);
214 lua_pushstring(L, "min_jitter");
215 lua_pushnumber(L, min_jitter);
216 lua_settable(L, table);
218 lua_pushstring(L, "max_jitter");
219 lua_pushnumber(L, max_jitter);
220 lua_settable(L, table);
222 lua_pushstring(L, "avg_jitter");
223 lua_pushnumber(L, avg_jitter);
224 lua_settable(L, table);
227 lua_pushstring(L,"connection_uptime");
228 lua_pushnumber(L, info.uptime);
229 lua_settable(L, table);
231 lua_pushstring(L,"protocol_version");
232 lua_pushnumber(L, info.prot_vers);
233 lua_settable(L, table);
235 lua_pushstring(L, "formspec_version");
236 lua_pushnumber(L, player->formspec_version);
237 lua_settable(L, table);
239 lua_pushstring(L, "lang_code");
240 lua_pushstring(L, info.lang_code.c_str());
241 lua_settable(L, table);
244 lua_pushstring(L,"serialization_version");
245 lua_pushnumber(L, info.ser_vers);
246 lua_settable(L, table);
248 lua_pushstring(L,"major");
249 lua_pushnumber(L, info.major);
250 lua_settable(L, table);
252 lua_pushstring(L,"minor");
253 lua_pushnumber(L, info.minor);
254 lua_settable(L, table);
256 lua_pushstring(L,"patch");
257 lua_pushnumber(L, info.patch);
258 lua_settable(L, table);
260 lua_pushstring(L,"version_string");
261 lua_pushstring(L, info.vers_string.c_str());
262 lua_settable(L, table);
264 lua_pushstring(L,"state");
265 lua_pushstring(L, ClientInterface::state2Name(info.state).c_str());
266 lua_settable(L, table);
273 int ModApiServer::l_get_ban_list(lua_State *L)
275 NO_MAP_LOCK_REQUIRED;
276 lua_pushstring(L, getServer(L)->getBanDescription("").c_str());
280 // get_ban_description()
281 int ModApiServer::l_get_ban_description(lua_State *L)
283 NO_MAP_LOCK_REQUIRED;
284 const char * ip_or_name = luaL_checkstring(L, 1);
285 lua_pushstring(L, getServer(L)->getBanDescription(std::string(ip_or_name)).c_str());
290 int ModApiServer::l_ban_player(lua_State *L)
292 NO_MAP_LOCK_REQUIRED;
295 throw LuaError("Can't ban player before server has started up");
297 Server *server = getServer(L);
298 const char *name = luaL_checkstring(L, 1);
299 RemotePlayer *player = server->getEnv().getPlayer(name);
301 lua_pushboolean(L, false); // no such player
305 std::string ip_str = server->getPeerAddress(player->getPeerId()).serializeString();
306 server->setIpBanned(ip_str, name);
307 lua_pushboolean(L, true);
311 // disconnect_player(name, [reason]) -> success
312 int ModApiServer::l_disconnect_player(lua_State *L)
314 NO_MAP_LOCK_REQUIRED;
317 throw LuaError("Can't kick player before server has started up");
319 const char *name = luaL_checkstring(L, 1);
321 if (lua_isstring(L, 2))
322 message.append(readParam<std::string>(L, 2));
324 message.append("Disconnected.");
326 Server *server = getServer(L);
328 RemotePlayer *player = server->getEnv().getPlayer(name);
330 lua_pushboolean(L, false); // No such player
334 server->DenyAccess(player->getPeerId(), SERVER_ACCESSDENIED_CUSTOM_STRING, message);
335 lua_pushboolean(L, true);
339 // remove_player(name)
340 int ModApiServer::l_remove_player(lua_State *L)
342 NO_MAP_LOCK_REQUIRED;
343 std::string name = luaL_checkstring(L, 1);
344 ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
346 throw LuaError("Can't remove player before server has started up");
348 RemotePlayer *player = s_env->getPlayer(name.c_str());
350 lua_pushinteger(L, s_env->removePlayerFromDatabase(name) ? 0 : 1);
352 lua_pushinteger(L, 2);
357 // unban_player_or_ip()
358 int ModApiServer::l_unban_player_or_ip(lua_State *L)
360 NO_MAP_LOCK_REQUIRED;
361 const char * ip_or_name = luaL_checkstring(L, 1);
362 getServer(L)->unsetIpBanned(ip_or_name);
363 lua_pushboolean(L, true);
367 // show_formspec(playername,formname,formspec)
368 int ModApiServer::l_show_formspec(lua_State *L)
370 NO_MAP_LOCK_REQUIRED;
371 const char *playername = luaL_checkstring(L, 1);
372 const char *formname = luaL_checkstring(L, 2);
373 const char *formspec = luaL_checkstring(L, 3);
375 if(getServer(L)->showFormspec(playername,formspec,formname))
377 lua_pushboolean(L, true);
379 lua_pushboolean(L, false);
384 // get_current_modname()
385 int ModApiServer::l_get_current_modname(lua_State *L)
387 NO_MAP_LOCK_REQUIRED;
388 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
392 // get_modpath(modname)
393 int ModApiServer::l_get_modpath(lua_State *L)
395 NO_MAP_LOCK_REQUIRED;
396 std::string modname = luaL_checkstring(L, 1);
397 const ModSpec *mod = getGameDef(L)->getModSpec(modname);
401 lua_pushstring(L, mod->path.c_str());
406 // the returned list is sorted alphabetically for you
407 int ModApiServer::l_get_modnames(lua_State *L)
409 NO_MAP_LOCK_REQUIRED;
411 // Get a list of mods
412 std::vector<std::string> modlist;
413 for (auto &it : getGameDef(L)->getMods())
414 modlist.emplace_back(it.name);
416 std::sort(modlist.begin(), modlist.end());
418 // Package them up for Lua
419 lua_createtable(L, modlist.size(), 0);
420 auto iter = modlist.begin();
421 for (u16 i = 0; iter != modlist.end(); ++iter) {
422 lua_pushstring(L, iter->c_str());
423 lua_rawseti(L, -2, ++i);
429 int ModApiServer::l_get_worldpath(lua_State *L)
431 NO_MAP_LOCK_REQUIRED;
432 const Server *srv = getServer(L);
433 lua_pushstring(L, srv->getWorldPath().c_str());
437 // sound_play(spec, parameters, [ephemeral])
438 int ModApiServer::l_sound_play(lua_State *L)
440 NO_MAP_LOCK_REQUIRED;
441 SimpleSoundSpec spec;
442 read_soundspec(L, 1, spec);
443 ServerSoundParams params;
444 read_server_sound_params(L, 2, params);
445 bool ephemeral = lua_gettop(L) > 2 && readParam<bool>(L, 3);
447 getServer(L)->playSound(spec, params, true);
450 s32 handle = getServer(L)->playSound(spec, params);
451 lua_pushinteger(L, handle);
456 // sound_stop(handle)
457 int ModApiServer::l_sound_stop(lua_State *L)
459 NO_MAP_LOCK_REQUIRED;
460 s32 handle = luaL_checkinteger(L, 1);
461 getServer(L)->stopSound(handle);
465 int ModApiServer::l_sound_fade(lua_State *L)
467 NO_MAP_LOCK_REQUIRED;
468 s32 handle = luaL_checkinteger(L, 1);
469 float step = readParam<float>(L, 2);
470 float gain = readParam<float>(L, 3);
471 getServer(L)->fadeSound(handle, step, gain);
475 // dynamic_add_media(filepath)
476 int ModApiServer::l_dynamic_add_media(lua_State *L)
478 NO_MAP_LOCK_REQUIRED;
481 throw LuaError("Dynamic media cannot be added before server has started up");
482 Server *server = getServer(L);
484 std::string filepath;
485 std::string to_player;
486 bool ephemeral = false;
488 if (lua_istable(L, 1)) {
489 getstringfield(L, 1, "filepath", filepath);
490 getstringfield(L, 1, "to_player", to_player);
491 getboolfield(L, 1, "ephemeral", ephemeral);
493 filepath = readParam<std::string>(L, 1);
495 if (filepath.empty())
496 luaL_typerror(L, 1, "non-empty string");
497 luaL_checktype(L, 2, LUA_TFUNCTION);
499 CHECK_SECURE_PATH(L, filepath.c_str(), false);
501 u32 token = server->getScriptIface()->allocateDynamicMediaCallback(L, 2);
503 bool ok = server->dynamicAddMedia(filepath, token, to_player, ephemeral);
505 server->getScriptIface()->freeDynamicMediaCallback(token);
506 lua_pushboolean(L, ok);
512 int ModApiServer::l_is_singleplayer(lua_State *L)
514 NO_MAP_LOCK_REQUIRED;
515 const Server *srv = getServer(L);
516 lua_pushboolean(L, srv->isSingleplayer());
520 // notify_authentication_modified(name)
521 int ModApiServer::l_notify_authentication_modified(lua_State *L)
523 NO_MAP_LOCK_REQUIRED;
525 if(lua_isstring(L, 1))
526 name = readParam<std::string>(L, 1);
527 getServer(L)->reportPrivsModified(name);
531 // do_async_callback(func, params, mod_origin)
532 int ModApiServer::l_do_async_callback(lua_State *L)
534 NO_MAP_LOCK_REQUIRED;
535 ServerScripting *script = getScriptApi<ServerScripting>(L);
537 luaL_checktype(L, 1, LUA_TFUNCTION);
538 luaL_checktype(L, 2, LUA_TTABLE);
539 luaL_checktype(L, 3, LUA_TSTRING);
541 call_string_dump(L, 1);
543 const char *serialized_func_raw = lua_tolstring(L, -1, &func_length);
545 PackedValue *param = script_pack(L, 2);
547 std::string mod_origin = readParam<std::string>(L, 3);
549 u32 jobId = script->queueAsync(
550 std::string(serialized_func_raw, func_length),
554 lua_pushinteger(L, jobId);
558 // register_async_dofile(path)
559 int ModApiServer::l_register_async_dofile(lua_State *L)
561 NO_MAP_LOCK_REQUIRED;
563 std::string path = readParam<std::string>(L, 1);
564 CHECK_SECURE_PATH(L, path.c_str(), false);
566 // Find currently running mod name (only at init time)
567 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
568 if (!lua_isstring(L, -1))
570 std::string modname = readParam<std::string>(L, -1);
572 getServer(L)->m_async_init_files.emplace_back(modname, path);
573 lua_pushboolean(L, true);
577 // serialize_roundtrip(value)
578 // Meant for unit testing the packer from Lua
579 int ModApiServer::l_serialize_roundtrip(lua_State *L)
581 NO_MAP_LOCK_REQUIRED;
583 int top = lua_gettop(L);
584 auto *pv = script_pack(L, 1);
585 if (top != lua_gettop(L))
586 throw LuaError("stack values leaked");
589 script_dump_packed(pv);
593 script_unpack(L, pv);
595 if (top + 1 != lua_gettop(L))
596 throw LuaError("stack values leaked");
601 void ModApiServer::Initialize(lua_State *L, int top)
603 API_FCT(request_shutdown);
604 API_FCT(get_server_status);
605 API_FCT(get_server_uptime);
606 API_FCT(get_server_max_lag);
607 API_FCT(get_worldpath);
608 API_FCT(is_singleplayer);
610 API_FCT(get_current_modname);
611 API_FCT(get_modpath);
612 API_FCT(get_modnames);
616 API_FCT(chat_send_all);
617 API_FCT(chat_send_player);
618 API_FCT(show_formspec);
622 API_FCT(dynamic_add_media);
624 API_FCT(get_player_information);
625 API_FCT(get_player_privs);
626 API_FCT(get_player_ip);
627 API_FCT(get_ban_list);
628 API_FCT(get_ban_description);
630 API_FCT(disconnect_player);
631 API_FCT(remove_player);
632 API_FCT(unban_player_or_ip);
633 API_FCT(notify_authentication_modified);
635 API_FCT(do_async_callback);
636 API_FCT(register_async_dofile);
637 API_FCT(serialize_roundtrip);
640 void ModApiServer::InitializeAsync(lua_State *L, int top)
642 API_FCT(get_worldpath);
643 API_FCT(is_singleplayer);
645 API_FCT(get_current_modname);
646 API_FCT(get_modpath);
647 API_FCT(get_modnames);