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 "cpp_api/s_base.h"
25 #include "cpp_api/s_security.h"
27 #include "environment.h"
28 #include "remoteplayer.h"
33 int ModApiServer::l_request_shutdown(lua_State *L)
36 const char *msg = lua_tolstring(L, 1, NULL);
37 bool reconnect = readParam<bool>(L, 2);
38 float seconds_before_shutdown = lua_tonumber(L, 3);
39 getServer(L)->requestShutdown(msg ? msg : "", reconnect, seconds_before_shutdown);
43 // get_server_status()
44 int ModApiServer::l_get_server_status(lua_State *L)
47 lua_pushstring(L, wide_to_narrow(getServer(L)->getStatusString()).c_str());
51 // get_server_uptime()
52 int ModApiServer::l_get_server_uptime(lua_State *L)
55 lua_pushnumber(L, getServer(L)->getUptime());
61 int ModApiServer::l_print(lua_State *L)
65 text = luaL_checkstring(L, 1);
66 getServer(L)->printToConsoleOnly(text);
70 // chat_send_all(text)
71 int ModApiServer::l_chat_send_all(lua_State *L)
74 const char *text = luaL_checkstring(L, 1);
75 // Get server from registry
76 Server *server = getServer(L);
78 server->notifyPlayers(utf8_to_wide(text));
82 // chat_send_player(name, text)
83 int ModApiServer::l_chat_send_player(lua_State *L)
86 const char *name = luaL_checkstring(L, 1);
87 const char *text = luaL_checkstring(L, 2);
89 // Get server from registry
90 Server *server = getServer(L);
92 server->notifyPlayer(name, utf8_to_wide(text));
96 // get_player_privs(name, text)
97 int ModApiServer::l_get_player_privs(lua_State *L)
100 const char *name = luaL_checkstring(L, 1);
101 // Get server from registry
102 Server *server = getServer(L);
105 int table = lua_gettop(L);
106 std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
107 for (const std::string &privs_ : privs_s) {
108 lua_pushboolean(L, true);
109 lua_setfield(L, table, privs_.c_str());
111 lua_pushvalue(L, table);
116 int ModApiServer::l_get_player_ip(lua_State *L)
118 NO_MAP_LOCK_REQUIRED;
119 const char * name = luaL_checkstring(L, 1);
120 RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
123 lua_pushnil(L); // no such player
128 Address addr = getServer(L)->getPeerAddress(player->getPeerId());
129 std::string ip_str = addr.serializeString();
130 lua_pushstring(L, ip_str.c_str());
132 } catch (const con::PeerNotFoundException &) {
133 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
134 lua_pushnil(L); // error
139 // get_player_information(name)
140 int ModApiServer::l_get_player_information(lua_State *L)
142 NO_MAP_LOCK_REQUIRED;
144 Server *server = getServer(L);
146 const char *name = luaL_checkstring(L, 1);
147 RemotePlayer *player = server->getEnv().getPlayer(name);
149 lua_pushnil(L); // no such player
155 addr = server->getPeerAddress(player->getPeerId());
156 } catch (const con::PeerNotFoundException &) {
157 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
158 lua_pushnil(L); // error
162 float min_rtt, max_rtt, avg_rtt, min_jitter, max_jitter, avg_jitter;
166 u8 ser_vers, major, minor, patch;
167 std::string vers_string, lang_code;
169 auto getConInfo = [&] (con::rtt_stat_type type, float *value) -> bool {
170 return server->getClientConInfo(player->getPeerId(), type, value);
174 getConInfo(con::MIN_RTT, &min_rtt) &&
175 getConInfo(con::MAX_RTT, &max_rtt) &&
176 getConInfo(con::AVG_RTT, &avg_rtt) &&
177 getConInfo(con::MIN_JITTER, &min_jitter) &&
178 getConInfo(con::MAX_JITTER, &max_jitter) &&
179 getConInfo(con::AVG_JITTER, &avg_jitter);
181 bool r = server->getClientInfo(player->getPeerId(), &state, &uptime,
182 &ser_vers, &prot_vers, &major, &minor, &patch, &vers_string,
185 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
186 lua_pushnil(L); // error
191 int table = lua_gettop(L);
193 lua_pushstring(L,"address");
194 lua_pushstring(L, addr.serializeString().c_str());
195 lua_settable(L, table);
197 lua_pushstring(L,"ip_version");
198 if (addr.getFamily() == AF_INET) {
199 lua_pushnumber(L, 4);
200 } else if (addr.getFamily() == AF_INET6) {
201 lua_pushnumber(L, 6);
203 lua_pushnumber(L, 0);
205 lua_settable(L, table);
207 if (have_con_info) { // may be missing
208 lua_pushstring(L, "min_rtt");
209 lua_pushnumber(L, min_rtt);
210 lua_settable(L, table);
212 lua_pushstring(L, "max_rtt");
213 lua_pushnumber(L, max_rtt);
214 lua_settable(L, table);
216 lua_pushstring(L, "avg_rtt");
217 lua_pushnumber(L, avg_rtt);
218 lua_settable(L, table);
220 lua_pushstring(L, "min_jitter");
221 lua_pushnumber(L, min_jitter);
222 lua_settable(L, table);
224 lua_pushstring(L, "max_jitter");
225 lua_pushnumber(L, max_jitter);
226 lua_settable(L, table);
228 lua_pushstring(L, "avg_jitter");
229 lua_pushnumber(L, avg_jitter);
230 lua_settable(L, table);
233 lua_pushstring(L,"connection_uptime");
234 lua_pushnumber(L, uptime);
235 lua_settable(L, table);
237 lua_pushstring(L,"protocol_version");
238 lua_pushnumber(L, prot_vers);
239 lua_settable(L, table);
241 lua_pushstring(L, "formspec_version");
242 lua_pushnumber(L, player->formspec_version);
243 lua_settable(L, table);
245 lua_pushstring(L, "lang_code");
246 lua_pushstring(L, lang_code.c_str());
247 lua_settable(L, table);
250 lua_pushstring(L,"serialization_version");
251 lua_pushnumber(L, ser_vers);
252 lua_settable(L, table);
254 lua_pushstring(L,"major");
255 lua_pushnumber(L, major);
256 lua_settable(L, table);
258 lua_pushstring(L,"minor");
259 lua_pushnumber(L, minor);
260 lua_settable(L, table);
262 lua_pushstring(L,"patch");
263 lua_pushnumber(L, patch);
264 lua_settable(L, table);
266 lua_pushstring(L,"version_string");
267 lua_pushstring(L, vers_string.c_str());
268 lua_settable(L, table);
270 lua_pushstring(L,"state");
271 lua_pushstring(L,ClientInterface::state2Name(state).c_str());
272 lua_settable(L, table);
279 int ModApiServer::l_get_ban_list(lua_State *L)
281 NO_MAP_LOCK_REQUIRED;
282 lua_pushstring(L, getServer(L)->getBanDescription("").c_str());
286 // get_ban_description()
287 int ModApiServer::l_get_ban_description(lua_State *L)
289 NO_MAP_LOCK_REQUIRED;
290 const char * ip_or_name = luaL_checkstring(L, 1);
291 lua_pushstring(L, getServer(L)->getBanDescription(std::string(ip_or_name)).c_str());
296 int ModApiServer::l_ban_player(lua_State *L)
298 NO_MAP_LOCK_REQUIRED;
299 const char * name = luaL_checkstring(L, 1);
300 RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
301 if (player == NULL) {
302 lua_pushboolean(L, false); // no such player
307 Address addr = getServer(L)->getPeerAddress(
308 dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name)->getPeerId());
309 std::string ip_str = addr.serializeString();
310 getServer(L)->setIpBanned(ip_str, name);
311 } catch(const con::PeerNotFoundException &) {
312 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
313 lua_pushboolean(L, false); // error
316 lua_pushboolean(L, true);
320 // kick_player(name, [reason]) -> success
321 int ModApiServer::l_kick_player(lua_State *L)
323 NO_MAP_LOCK_REQUIRED;
324 const char *name = luaL_checkstring(L, 1);
325 std::string message("Kicked");
326 if (lua_isstring(L, 2))
327 message.append(": ").append(readParam<std::string>(L, 2));
331 RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
332 if (player == NULL) {
333 lua_pushboolean(L, false); // No such player
336 getServer(L)->DenyAccess_Legacy(player->getPeerId(), utf8_to_wide(message));
337 lua_pushboolean(L, true);
341 int ModApiServer::l_remove_player(lua_State *L)
343 NO_MAP_LOCK_REQUIRED;
344 std::string name = luaL_checkstring(L, 1);
345 ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
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 = getServer(L)->getModSpec(modname);
402 lua_pushstring(L, mod->path.c_str());
407 // the returned list is sorted alphabetically for you
408 int ModApiServer::l_get_modnames(lua_State *L)
410 NO_MAP_LOCK_REQUIRED;
412 // Get a list of mods
413 std::vector<std::string> modlist;
414 getServer(L)->getModNames(modlist);
416 std::sort(modlist.begin(), modlist.end());
418 // Package them up for Lua
419 lua_createtable(L, modlist.size(), 0);
420 std::vector<std::string>::iterator 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 std::string worldpath = getServer(L)->getWorldPath();
433 lua_pushstring(L, worldpath.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;
480 // Reject adding media before the server has started up
482 throw LuaError("Dynamic media cannot be added before server has started up");
484 std::string filepath = readParam<std::string>(L, 1);
485 CHECK_SECURE_PATH(L, filepath.c_str(), false);
487 bool ok = getServer(L)->dynamicAddMedia(filepath);
488 lua_pushboolean(L, ok);
493 int ModApiServer::l_is_singleplayer(lua_State *L)
495 NO_MAP_LOCK_REQUIRED;
496 lua_pushboolean(L, getServer(L)->isSingleplayer());
500 // notify_authentication_modified(name)
501 int ModApiServer::l_notify_authentication_modified(lua_State *L)
503 NO_MAP_LOCK_REQUIRED;
505 if(lua_isstring(L, 1))
506 name = readParam<std::string>(L, 1);
507 getServer(L)->reportPrivsModified(name);
511 // get_last_run_mod()
512 int ModApiServer::l_get_last_run_mod(lua_State *L)
514 NO_MAP_LOCK_REQUIRED;
515 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
516 std::string current_mod = readParam<std::string>(L, -1, "");
517 if (current_mod.empty()) {
519 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
524 // set_last_run_mod(modname)
525 int ModApiServer::l_set_last_run_mod(lua_State *L)
527 NO_MAP_LOCK_REQUIRED;
528 #ifdef SCRIPTAPI_DEBUG
529 const char *mod = lua_tostring(L, 1);
530 getScriptApiBase(L)->setOriginDirect(mod);
531 //printf(">>>> last mod set from Lua: %s\n", mod);
536 void ModApiServer::Initialize(lua_State *L, int top)
538 API_FCT(request_shutdown);
539 API_FCT(get_server_status);
540 API_FCT(get_server_uptime);
541 API_FCT(get_worldpath);
542 API_FCT(is_singleplayer);
544 API_FCT(get_current_modname);
545 API_FCT(get_modpath);
546 API_FCT(get_modnames);
550 API_FCT(chat_send_all);
551 API_FCT(chat_send_player);
552 API_FCT(show_formspec);
556 API_FCT(dynamic_add_media);
558 API_FCT(get_player_information);
559 API_FCT(get_player_privs);
560 API_FCT(get_player_ip);
561 API_FCT(get_ban_list);
562 API_FCT(get_ban_description);
564 API_FCT(kick_player);
565 API_FCT(remove_player);
566 API_FCT(unban_player_or_ip);
567 API_FCT(notify_authentication_modified);
569 API_FCT(get_last_run_mod);
570 API_FCT(set_last_run_mod);