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 = lua_toboolean(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(narrow_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, narrow_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);
314 if (lua_isstring(L, 2))
316 message = std::string("Kicked: ") + lua_tostring(L, 2);
323 RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
324 if (player == NULL) {
325 lua_pushboolean(L, false); // No such player
328 getServer(L)->DenyAccess_Legacy(player->getPeerId(), utf8_to_wide(message));
329 lua_pushboolean(L, true);
333 int ModApiServer::l_remove_player(lua_State *L)
335 NO_MAP_LOCK_REQUIRED;
336 std::string name = luaL_checkstring(L, 1);
337 ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
340 RemotePlayer *player = s_env->getPlayer(name.c_str());
342 lua_pushinteger(L, s_env->removePlayerFromDatabase(name) ? 0 : 1);
344 lua_pushinteger(L, 2);
349 // unban_player_or_ip()
350 int ModApiServer::l_unban_player_or_ip(lua_State *L)
352 NO_MAP_LOCK_REQUIRED;
353 const char * ip_or_name = luaL_checkstring(L, 1);
354 getServer(L)->unsetIpBanned(ip_or_name);
355 lua_pushboolean(L, true);
359 // show_formspec(playername,formname,formspec)
360 int ModApiServer::l_show_formspec(lua_State *L)
362 NO_MAP_LOCK_REQUIRED;
363 const char *playername = luaL_checkstring(L, 1);
364 const char *formname = luaL_checkstring(L, 2);
365 const char *formspec = luaL_checkstring(L, 3);
367 if(getServer(L)->showFormspec(playername,formspec,formname))
369 lua_pushboolean(L, true);
371 lua_pushboolean(L, false);
376 // get_current_modname()
377 int ModApiServer::l_get_current_modname(lua_State *L)
379 NO_MAP_LOCK_REQUIRED;
380 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
384 // get_modpath(modname)
385 int ModApiServer::l_get_modpath(lua_State *L)
387 NO_MAP_LOCK_REQUIRED;
388 std::string modname = luaL_checkstring(L, 1);
389 const ModSpec *mod = getServer(L)->getModSpec(modname);
394 lua_pushstring(L, mod->path.c_str());
399 // the returned list is sorted alphabetically for you
400 int ModApiServer::l_get_modnames(lua_State *L)
402 NO_MAP_LOCK_REQUIRED;
404 // Get a list of mods
405 std::vector<std::string> modlist;
406 getServer(L)->getModNames(modlist);
408 // Take unsorted items from mods_unsorted and sort them into
409 // mods_sorted; not great performance but the number of mods on a
410 // server will likely be small.
411 std::sort(modlist.begin(), modlist.end());
413 // Package them up for Lua
414 lua_createtable(L, modlist.size(), 0);
415 std::vector<std::string>::iterator iter = modlist.begin();
416 for (u16 i = 0; iter != modlist.end(); ++iter) {
417 lua_pushstring(L, iter->c_str());
418 lua_rawseti(L, -2, ++i);
424 int ModApiServer::l_get_worldpath(lua_State *L)
426 NO_MAP_LOCK_REQUIRED;
427 std::string worldpath = getServer(L)->getWorldPath();
428 lua_pushstring(L, worldpath.c_str());
432 // sound_play(spec, parameters)
433 int ModApiServer::l_sound_play(lua_State *L)
435 NO_MAP_LOCK_REQUIRED;
436 SimpleSoundSpec spec;
437 read_soundspec(L, 1, spec);
438 ServerSoundParams params;
439 read_server_sound_params(L, 2, params);
440 s32 handle = getServer(L)->playSound(spec, params);
441 lua_pushinteger(L, handle);
445 // sound_stop(handle)
446 int ModApiServer::l_sound_stop(lua_State *L)
448 NO_MAP_LOCK_REQUIRED;
449 int handle = luaL_checkinteger(L, 1);
450 getServer(L)->stopSound(handle);
454 int ModApiServer::l_sound_fade(lua_State *L)
456 NO_MAP_LOCK_REQUIRED;
457 s32 handle = luaL_checkinteger(L, 1);
458 float step = luaL_checknumber(L, 2);
459 float gain = luaL_checknumber(L, 3);
460 getServer(L)->fadeSound(handle, step, gain);
465 int ModApiServer::l_is_singleplayer(lua_State *L)
467 NO_MAP_LOCK_REQUIRED;
468 lua_pushboolean(L, getServer(L)->isSingleplayer());
472 // notify_authentication_modified(name)
473 int ModApiServer::l_notify_authentication_modified(lua_State *L)
475 NO_MAP_LOCK_REQUIRED;
477 if(lua_isstring(L, 1))
478 name = lua_tostring(L, 1);
479 getServer(L)->reportPrivsModified(name);
483 // get_last_run_mod()
484 int ModApiServer::l_get_last_run_mod(lua_State *L)
486 NO_MAP_LOCK_REQUIRED;
487 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
488 const char *current_mod = lua_tostring(L, -1);
489 if (current_mod == NULL || current_mod[0] == '\0') {
491 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
496 // set_last_run_mod(modname)
497 int ModApiServer::l_set_last_run_mod(lua_State *L)
499 NO_MAP_LOCK_REQUIRED;
500 #ifdef SCRIPTAPI_DEBUG
501 const char *mod = lua_tostring(L, 1);
502 getScriptApiBase(L)->setOriginDirect(mod);
503 //printf(">>>> last mod set from Lua: %s\n", mod);
508 void ModApiServer::Initialize(lua_State *L, int top)
510 API_FCT(request_shutdown);
511 API_FCT(get_server_status);
512 API_FCT(get_server_uptime);
513 API_FCT(get_worldpath);
514 API_FCT(is_singleplayer);
516 API_FCT(get_current_modname);
517 API_FCT(get_modpath);
518 API_FCT(get_modnames);
522 API_FCT(chat_send_all);
523 API_FCT(chat_send_player);
524 API_FCT(show_formspec);
529 API_FCT(get_player_information);
530 API_FCT(get_player_privs);
531 API_FCT(get_player_ip);
532 API_FCT(get_ban_list);
533 API_FCT(get_ban_description);
535 API_FCT(kick_player);
536 API_FCT(remove_player);
537 API_FCT(unban_player_or_ip);
538 API_FCT(notify_authentication_modified);
540 API_FCT(get_last_run_mod);
541 API_FCT(set_last_run_mod);