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"
26 #include "environment.h"
27 #include "remoteplayer.h"
32 int ModApiServer::l_request_shutdown(lua_State *L)
35 const char *msg = lua_tolstring(L, 1, NULL);
36 bool reconnect = readParam<bool>(L, 2);
37 float seconds_before_shutdown = lua_tonumber(L, 3);
38 getServer(L)->requestShutdown(msg ? msg : "", reconnect, seconds_before_shutdown);
42 // get_server_status()
43 int ModApiServer::l_get_server_status(lua_State *L)
46 lua_pushstring(L, wide_to_narrow(getServer(L)->getStatusString()).c_str());
50 // get_server_uptime()
51 int ModApiServer::l_get_server_uptime(lua_State *L)
54 lua_pushnumber(L, getServer(L)->getUptime());
60 int ModApiServer::l_print(lua_State *L)
64 text = luaL_checkstring(L, 1);
65 getServer(L)->printToConsoleOnly(text);
69 // chat_send_all(text)
70 int ModApiServer::l_chat_send_all(lua_State *L)
73 const char *text = luaL_checkstring(L, 1);
74 // Get server from registry
75 Server *server = getServer(L);
77 server->notifyPlayers(utf8_to_wide(text));
81 // chat_send_player(name, text)
82 int ModApiServer::l_chat_send_player(lua_State *L)
85 const char *name = luaL_checkstring(L, 1);
86 const char *text = luaL_checkstring(L, 2);
88 // Get server from registry
89 Server *server = getServer(L);
91 server->notifyPlayer(name, utf8_to_wide(text));
95 // get_player_privs(name, text)
96 int ModApiServer::l_get_player_privs(lua_State *L)
99 const char *name = luaL_checkstring(L, 1);
100 // Get server from registry
101 Server *server = getServer(L);
104 int table = lua_gettop(L);
105 std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
106 for (const std::string &privs_ : privs_s) {
107 lua_pushboolean(L, true);
108 lua_setfield(L, table, privs_.c_str());
110 lua_pushvalue(L, table);
115 int ModApiServer::l_get_player_ip(lua_State *L)
117 NO_MAP_LOCK_REQUIRED;
118 const char * name = luaL_checkstring(L, 1);
119 RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
122 lua_pushnil(L); // no such player
127 Address addr = getServer(L)->getPeerAddress(player->getPeerId());
128 std::string ip_str = addr.serializeString();
129 lua_pushstring(L, ip_str.c_str());
131 } catch (const con::PeerNotFoundException &) {
132 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
133 lua_pushnil(L); // error
138 // get_player_information(name)
139 int ModApiServer::l_get_player_information(lua_State *L)
142 NO_MAP_LOCK_REQUIRED;
143 const char * name = luaL_checkstring(L, 1);
144 RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
145 if (player == NULL) {
146 lua_pushnil(L); // no such player
153 addr = getServer(L)->getPeerAddress(player->getPeerId());
154 } catch(const con::PeerNotFoundException &) {
155 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
156 lua_pushnil(L); // error
160 float min_rtt,max_rtt,avg_rtt,min_jitter,max_jitter,avg_jitter;
164 u8 ser_vers,major,minor,patch;
165 std::string vers_string;
169 dstream << FUNCTION_NAME << ": peer was not found" << std::endl; \
170 lua_pushnil(L); /* error */ \
174 ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MIN_RTT, &min_rtt))
175 ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MAX_RTT, &max_rtt))
176 ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::AVG_RTT, &avg_rtt))
177 ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MIN_JITTER,
179 ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MAX_JITTER,
181 ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::AVG_JITTER,
184 ERET(getServer(L)->getClientInfo(player->getPeerId(), &state, &uptime, &ser_vers,
185 &prot_vers, &major, &minor, &patch, &vers_string))
188 int table = lua_gettop(L);
190 lua_pushstring(L,"address");
191 lua_pushstring(L, addr.serializeString().c_str());
192 lua_settable(L, table);
194 lua_pushstring(L,"ip_version");
195 if (addr.getFamily() == AF_INET) {
196 lua_pushnumber(L, 4);
197 } else if (addr.getFamily() == AF_INET6) {
198 lua_pushnumber(L, 6);
200 lua_pushnumber(L, 0);
202 lua_settable(L, table);
204 lua_pushstring(L,"min_rtt");
205 lua_pushnumber(L, min_rtt);
206 lua_settable(L, table);
208 lua_pushstring(L,"max_rtt");
209 lua_pushnumber(L, max_rtt);
210 lua_settable(L, table);
212 lua_pushstring(L,"avg_rtt");
213 lua_pushnumber(L, avg_rtt);
214 lua_settable(L, table);
216 lua_pushstring(L,"min_jitter");
217 lua_pushnumber(L, min_jitter);
218 lua_settable(L, table);
220 lua_pushstring(L,"max_jitter");
221 lua_pushnumber(L, max_jitter);
222 lua_settable(L, table);
224 lua_pushstring(L,"avg_jitter");
225 lua_pushnumber(L, avg_jitter);
226 lua_settable(L, table);
228 lua_pushstring(L,"connection_uptime");
229 lua_pushnumber(L, uptime);
230 lua_settable(L, table);
232 lua_pushstring(L,"protocol_version");
233 lua_pushnumber(L, prot_vers);
234 lua_settable(L, table);
237 lua_pushstring(L,"serialization_version");
238 lua_pushnumber(L, ser_vers);
239 lua_settable(L, table);
241 lua_pushstring(L,"major");
242 lua_pushnumber(L, major);
243 lua_settable(L, table);
245 lua_pushstring(L,"minor");
246 lua_pushnumber(L, minor);
247 lua_settable(L, table);
249 lua_pushstring(L,"patch");
250 lua_pushnumber(L, patch);
251 lua_settable(L, table);
253 lua_pushstring(L,"version_string");
254 lua_pushstring(L, vers_string.c_str());
255 lua_settable(L, table);
257 lua_pushstring(L,"state");
258 lua_pushstring(L,ClientInterface::state2Name(state).c_str());
259 lua_settable(L, table);
267 int ModApiServer::l_get_ban_list(lua_State *L)
269 NO_MAP_LOCK_REQUIRED;
270 lua_pushstring(L, getServer(L)->getBanDescription("").c_str());
274 // get_ban_description()
275 int ModApiServer::l_get_ban_description(lua_State *L)
277 NO_MAP_LOCK_REQUIRED;
278 const char * ip_or_name = luaL_checkstring(L, 1);
279 lua_pushstring(L, getServer(L)->getBanDescription(std::string(ip_or_name)).c_str());
284 int ModApiServer::l_ban_player(lua_State *L)
286 NO_MAP_LOCK_REQUIRED;
287 const char * name = luaL_checkstring(L, 1);
288 RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
289 if (player == NULL) {
290 lua_pushboolean(L, false); // no such player
295 Address addr = getServer(L)->getPeerAddress(
296 dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name)->getPeerId());
297 std::string ip_str = addr.serializeString();
298 getServer(L)->setIpBanned(ip_str, name);
299 } catch(const con::PeerNotFoundException &) {
300 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
301 lua_pushboolean(L, false); // error
304 lua_pushboolean(L, true);
308 // kick_player(name, [reason]) -> success
309 int ModApiServer::l_kick_player(lua_State *L)
311 NO_MAP_LOCK_REQUIRED;
312 const char *name = luaL_checkstring(L, 1);
313 std::string message("Kicked");
314 if (lua_isstring(L, 2))
315 message.append(": ").append(readParam<std::string>(L, 2));
319 RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
320 if (player == NULL) {
321 lua_pushboolean(L, false); // No such player
324 getServer(L)->DenyAccess_Legacy(player->getPeerId(), utf8_to_wide(message));
325 lua_pushboolean(L, true);
329 int ModApiServer::l_remove_player(lua_State *L)
331 NO_MAP_LOCK_REQUIRED;
332 std::string name = luaL_checkstring(L, 1);
333 ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
336 RemotePlayer *player = s_env->getPlayer(name.c_str());
338 lua_pushinteger(L, s_env->removePlayerFromDatabase(name) ? 0 : 1);
340 lua_pushinteger(L, 2);
345 // unban_player_or_ip()
346 int ModApiServer::l_unban_player_or_ip(lua_State *L)
348 NO_MAP_LOCK_REQUIRED;
349 const char * ip_or_name = luaL_checkstring(L, 1);
350 getServer(L)->unsetIpBanned(ip_or_name);
351 lua_pushboolean(L, true);
355 // show_formspec(playername,formname,formspec)
356 int ModApiServer::l_show_formspec(lua_State *L)
358 NO_MAP_LOCK_REQUIRED;
359 const char *playername = luaL_checkstring(L, 1);
360 const char *formname = luaL_checkstring(L, 2);
361 const char *formspec = luaL_checkstring(L, 3);
363 if(getServer(L)->showFormspec(playername,formspec,formname))
365 lua_pushboolean(L, true);
367 lua_pushboolean(L, false);
372 // get_current_modname()
373 int ModApiServer::l_get_current_modname(lua_State *L)
375 NO_MAP_LOCK_REQUIRED;
376 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
380 // get_modpath(modname)
381 int ModApiServer::l_get_modpath(lua_State *L)
383 NO_MAP_LOCK_REQUIRED;
384 std::string modname = luaL_checkstring(L, 1);
385 const ModSpec *mod = getServer(L)->getModSpec(modname);
390 lua_pushstring(L, mod->path.c_str());
395 // the returned list is sorted alphabetically for you
396 int ModApiServer::l_get_modnames(lua_State *L)
398 NO_MAP_LOCK_REQUIRED;
400 // Get a list of mods
401 std::vector<std::string> modlist;
402 getServer(L)->getModNames(modlist);
404 // Take unsorted items from mods_unsorted and sort them into
405 // mods_sorted; not great performance but the number of mods on a
406 // server will likely be small.
407 std::sort(modlist.begin(), modlist.end());
409 // Package them up for Lua
410 lua_createtable(L, modlist.size(), 0);
411 std::vector<std::string>::iterator iter = modlist.begin();
412 for (u16 i = 0; iter != modlist.end(); ++iter) {
413 lua_pushstring(L, iter->c_str());
414 lua_rawseti(L, -2, ++i);
420 int ModApiServer::l_get_worldpath(lua_State *L)
422 NO_MAP_LOCK_REQUIRED;
423 std::string worldpath = getServer(L)->getWorldPath();
424 lua_pushstring(L, worldpath.c_str());
428 // sound_play(spec, parameters)
429 int ModApiServer::l_sound_play(lua_State *L)
431 NO_MAP_LOCK_REQUIRED;
432 SimpleSoundSpec spec;
433 read_soundspec(L, 1, spec);
434 ServerSoundParams params;
435 read_server_sound_params(L, 2, params);
436 s32 handle = getServer(L)->playSound(spec, params);
437 lua_pushinteger(L, handle);
441 // sound_stop(handle)
442 int ModApiServer::l_sound_stop(lua_State *L)
444 NO_MAP_LOCK_REQUIRED;
445 int handle = luaL_checkinteger(L, 1);
446 getServer(L)->stopSound(handle);
450 int ModApiServer::l_sound_fade(lua_State *L)
452 NO_MAP_LOCK_REQUIRED;
453 s32 handle = luaL_checkinteger(L, 1);
454 float step = readParam<float>(L, 2);
455 float gain = readParam<float>(L, 3);
456 getServer(L)->fadeSound(handle, step, gain);
461 int ModApiServer::l_is_singleplayer(lua_State *L)
463 NO_MAP_LOCK_REQUIRED;
464 lua_pushboolean(L, getServer(L)->isSingleplayer());
468 // notify_authentication_modified(name)
469 int ModApiServer::l_notify_authentication_modified(lua_State *L)
471 NO_MAP_LOCK_REQUIRED;
473 if(lua_isstring(L, 1))
474 name = readParam<std::string>(L, 1);
475 getServer(L)->reportPrivsModified(name);
479 // get_last_run_mod()
480 int ModApiServer::l_get_last_run_mod(lua_State *L)
482 NO_MAP_LOCK_REQUIRED;
483 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
484 std::string current_mod = readParam<std::string>(L, -1, "");
485 if (current_mod.empty()) {
487 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
492 // set_last_run_mod(modname)
493 int ModApiServer::l_set_last_run_mod(lua_State *L)
495 NO_MAP_LOCK_REQUIRED;
496 #ifdef SCRIPTAPI_DEBUG
497 const char *mod = lua_tostring(L, 1);
498 getScriptApiBase(L)->setOriginDirect(mod);
499 //printf(">>>> last mod set from Lua: %s\n", mod);
504 void ModApiServer::Initialize(lua_State *L, int top)
506 API_FCT(request_shutdown);
507 API_FCT(get_server_status);
508 API_FCT(get_server_uptime);
509 API_FCT(get_worldpath);
510 API_FCT(is_singleplayer);
512 API_FCT(get_current_modname);
513 API_FCT(get_modpath);
514 API_FCT(get_modnames);
518 API_FCT(chat_send_all);
519 API_FCT(chat_send_player);
520 API_FCT(show_formspec);
525 API_FCT(get_player_information);
526 API_FCT(get_player_privs);
527 API_FCT(get_player_ip);
528 API_FCT(get_ban_list);
529 API_FCT(get_ban_description);
531 API_FCT(kick_player);
532 API_FCT(remove_player);
533 API_FCT(unban_player_or_ip);
534 API_FCT(notify_authentication_modified);
536 API_FCT(get_last_run_mod);
537 API_FCT(set_last_run_mod);