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"
31 int ModApiServer::l_request_shutdown(lua_State *L)
33 getServer(L)->requestShutdown();
37 // get_server_status()
38 int ModApiServer::l_get_server_status(lua_State *L)
41 lua_pushstring(L, wide_to_narrow(getServer(L)->getStatusString()).c_str());
45 // chat_send_all(text)
46 int ModApiServer::l_chat_send_all(lua_State *L)
49 const char *text = luaL_checkstring(L, 1);
50 // Get server from registry
51 Server *server = getServer(L);
53 server->notifyPlayers(narrow_to_wide(text));
57 // chat_send_player(name, text)
58 int ModApiServer::l_chat_send_player(lua_State *L)
61 const char *name = luaL_checkstring(L, 1);
62 const char *text = luaL_checkstring(L, 2);
64 // Get server from registry
65 Server *server = getServer(L);
67 server->notifyPlayer(name, narrow_to_wide(text));
71 // get_player_privs(name, text)
72 int ModApiServer::l_get_player_privs(lua_State *L)
75 const char *name = luaL_checkstring(L, 1);
76 // Get server from registry
77 Server *server = getServer(L);
80 int table = lua_gettop(L);
81 std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
82 for(std::set<std::string>::const_iterator
83 i = privs_s.begin(); i != privs_s.end(); i++){
84 lua_pushboolean(L, true);
85 lua_setfield(L, table, i->c_str());
87 lua_pushvalue(L, table);
92 int ModApiServer::l_get_player_ip(lua_State *L)
95 const char * name = luaL_checkstring(L, 1);
96 Player *player = getEnv(L)->getPlayer(name);
99 lua_pushnil(L); // no such player
104 Address addr = getServer(L)->getPeerAddress(player->peer_id);
105 std::string ip_str = addr.serializeString();
106 lua_pushstring(L, ip_str.c_str());
109 catch(con::PeerNotFoundException) // unlikely
111 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
112 lua_pushnil(L); // error
117 // get_player_information()
118 int ModApiServer::l_get_player_information(lua_State *L)
121 NO_MAP_LOCK_REQUIRED;
122 const char * name = luaL_checkstring(L, 1);
123 Player *player = getEnv(L)->getPlayer(name);
126 lua_pushnil(L); // no such player
133 addr = getServer(L)->getPeerAddress(player->peer_id);
135 catch(con::PeerNotFoundException) // unlikely
137 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
138 lua_pushnil(L); // error
142 float min_rtt,max_rtt,avg_rtt,min_jitter,max_jitter,avg_jitter;
146 u8 ser_vers,major,minor,patch;
147 std::string vers_string;
151 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl; \
152 lua_pushnil(L); /* error */ \
156 ERET(getServer(L)->getClientConInfo(player->peer_id,con::MIN_RTT,&min_rtt))
157 ERET(getServer(L)->getClientConInfo(player->peer_id,con::MAX_RTT,&max_rtt))
158 ERET(getServer(L)->getClientConInfo(player->peer_id,con::AVG_RTT,&avg_rtt))
159 ERET(getServer(L)->getClientConInfo(player->peer_id,con::MIN_JITTER,&min_jitter))
160 ERET(getServer(L)->getClientConInfo(player->peer_id,con::MAX_JITTER,&max_jitter))
161 ERET(getServer(L)->getClientConInfo(player->peer_id,con::AVG_JITTER,&avg_jitter))
163 ERET(getServer(L)->getClientInfo(player->peer_id,
164 &state, &uptime, &ser_vers, &prot_vers,
165 &major, &minor, &patch, &vers_string))
168 int table = lua_gettop(L);
170 lua_pushstring(L,"address");
171 lua_pushstring(L, addr.serializeString().c_str());
172 lua_settable(L, table);
174 lua_pushstring(L,"ip_version");
175 if (addr.getFamily() == AF_INET) {
176 lua_pushnumber(L, 4);
177 } else if (addr.getFamily() == AF_INET6) {
178 lua_pushnumber(L, 6);
180 lua_pushnumber(L, 0);
182 lua_settable(L, table);
184 lua_pushstring(L,"min_rtt");
185 lua_pushnumber(L, min_rtt);
186 lua_settable(L, table);
188 lua_pushstring(L,"max_rtt");
189 lua_pushnumber(L, max_rtt);
190 lua_settable(L, table);
192 lua_pushstring(L,"avg_rtt");
193 lua_pushnumber(L, avg_rtt);
194 lua_settable(L, table);
196 lua_pushstring(L,"min_jitter");
197 lua_pushnumber(L, min_jitter);
198 lua_settable(L, table);
200 lua_pushstring(L,"max_jitter");
201 lua_pushnumber(L, max_jitter);
202 lua_settable(L, table);
204 lua_pushstring(L,"avg_jitter");
205 lua_pushnumber(L, avg_jitter);
206 lua_settable(L, table);
208 lua_pushstring(L,"connection_uptime");
209 lua_pushnumber(L, uptime);
210 lua_settable(L, table);
213 lua_pushstring(L,"serialization_version");
214 lua_pushnumber(L, ser_vers);
215 lua_settable(L, table);
217 lua_pushstring(L,"protocol_version");
218 lua_pushnumber(L, prot_vers);
219 lua_settable(L, table);
221 lua_pushstring(L,"major");
222 lua_pushnumber(L, major);
223 lua_settable(L, table);
225 lua_pushstring(L,"minor");
226 lua_pushnumber(L, minor);
227 lua_settable(L, table);
229 lua_pushstring(L,"patch");
230 lua_pushnumber(L, patch);
231 lua_settable(L, table);
233 lua_pushstring(L,"version_string");
234 lua_pushstring(L, vers_string.c_str());
235 lua_settable(L, table);
237 lua_pushstring(L,"state");
238 lua_pushstring(L,ClientInterface::state2Name(state).c_str());
239 lua_settable(L, table);
247 int ModApiServer::l_get_ban_list(lua_State *L)
249 NO_MAP_LOCK_REQUIRED;
250 lua_pushstring(L, getServer(L)->getBanDescription("").c_str());
254 // get_ban_description()
255 int ModApiServer::l_get_ban_description(lua_State *L)
257 NO_MAP_LOCK_REQUIRED;
258 const char * ip_or_name = luaL_checkstring(L, 1);
259 lua_pushstring(L, getServer(L)->getBanDescription(std::string(ip_or_name)).c_str());
264 int ModApiServer::l_ban_player(lua_State *L)
266 NO_MAP_LOCK_REQUIRED;
267 const char * name = luaL_checkstring(L, 1);
268 Player *player = getEnv(L)->getPlayer(name);
271 lua_pushboolean(L, false); // no such player
276 Address addr = getServer(L)->getPeerAddress(getEnv(L)->getPlayer(name)->peer_id);
277 std::string ip_str = addr.serializeString();
278 getServer(L)->setIpBanned(ip_str, name);
280 catch(con::PeerNotFoundException) // unlikely
282 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
283 lua_pushboolean(L, false); // error
286 lua_pushboolean(L, true);
290 // kick_player(name, [reason]) -> success
291 int ModApiServer::l_kick_player(lua_State *L)
293 NO_MAP_LOCK_REQUIRED;
294 const char *name = luaL_checkstring(L, 1);
296 if (lua_isstring(L, 2))
298 message = std::string("Kicked: ") + lua_tostring(L, 2);
304 Player *player = getEnv(L)->getPlayer(name);
307 lua_pushboolean(L, false); // No such player
310 getServer(L)->DenyAccess_Legacy(player->peer_id, narrow_to_wide(message));
311 lua_pushboolean(L, true);
315 // unban_player_or_ip()
316 int ModApiServer::l_unban_player_or_ip(lua_State *L)
318 NO_MAP_LOCK_REQUIRED;
319 const char * ip_or_name = luaL_checkstring(L, 1);
320 getServer(L)->unsetIpBanned(ip_or_name);
321 lua_pushboolean(L, true);
325 // show_formspec(playername,formname,formspec)
326 int ModApiServer::l_show_formspec(lua_State *L)
328 NO_MAP_LOCK_REQUIRED;
329 const char *playername = luaL_checkstring(L, 1);
330 const char *formname = luaL_checkstring(L, 2);
331 const char *formspec = luaL_checkstring(L, 3);
333 if(getServer(L)->showFormspec(playername,formspec,formname))
335 lua_pushboolean(L, true);
337 lua_pushboolean(L, false);
342 // get_current_modname()
343 int ModApiServer::l_get_current_modname(lua_State *L)
345 NO_MAP_LOCK_REQUIRED;
346 lua_getfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
350 // get_modpath(modname)
351 int ModApiServer::l_get_modpath(lua_State *L)
353 NO_MAP_LOCK_REQUIRED;
354 std::string modname = luaL_checkstring(L, 1);
355 const ModSpec *mod = getServer(L)->getModSpec(modname);
360 lua_pushstring(L, mod->path.c_str());
365 // the returned list is sorted alphabetically for you
366 int ModApiServer::l_get_modnames(lua_State *L)
368 NO_MAP_LOCK_REQUIRED;
370 // Get a list of mods
371 std::vector<std::string> modlist;
372 getServer(L)->getModNames(modlist);
374 // Take unsorted items from mods_unsorted and sort them into
375 // mods_sorted; not great performance but the number of mods on a
376 // server will likely be small.
377 std::sort(modlist.begin(), modlist.end());
379 // Package them up for Lua
380 lua_createtable(L, modlist.size(), 0);
381 std::vector<std::string>::iterator iter = modlist.begin();
382 for (u16 i = 0; iter != modlist.end(); iter++) {
383 lua_pushstring(L, iter->c_str());
384 lua_rawseti(L, -2, ++i);
390 int ModApiServer::l_get_worldpath(lua_State *L)
392 NO_MAP_LOCK_REQUIRED;
393 std::string worldpath = getServer(L)->getWorldPath();
394 lua_pushstring(L, worldpath.c_str());
398 // sound_play(spec, parameters)
399 int ModApiServer::l_sound_play(lua_State *L)
401 NO_MAP_LOCK_REQUIRED;
402 SimpleSoundSpec spec;
403 read_soundspec(L, 1, spec);
404 ServerSoundParams params;
405 read_server_sound_params(L, 2, params);
406 s32 handle = getServer(L)->playSound(spec, params);
407 lua_pushinteger(L, handle);
411 // sound_stop(handle)
412 int ModApiServer::l_sound_stop(lua_State *L)
414 NO_MAP_LOCK_REQUIRED;
415 int handle = luaL_checkinteger(L, 1);
416 getServer(L)->stopSound(handle);
421 int ModApiServer::l_is_singleplayer(lua_State *L)
423 NO_MAP_LOCK_REQUIRED;
424 lua_pushboolean(L, getServer(L)->isSingleplayer());
428 // notify_authentication_modified(name)
429 int ModApiServer::l_notify_authentication_modified(lua_State *L)
431 NO_MAP_LOCK_REQUIRED;
432 std::string name = "";
433 if(lua_isstring(L, 1))
434 name = lua_tostring(L, 1);
435 getServer(L)->reportPrivsModified(name);
440 // cause_error(type_of_error)
441 int ModApiServer::l_cause_error(lua_State *L)
443 NO_MAP_LOCK_REQUIRED;
444 std::string type_of_error = "none";
445 if(lua_isstring(L, 1))
446 type_of_error = lua_tostring(L, 1);
448 errorstream << "Error handler test called, errortype=" << type_of_error << std::endl;
450 if(type_of_error == "segv") {
451 volatile int* some_pointer = 0;
452 errorstream << "Cause a sigsegv now: " << (*some_pointer) << std::endl;
454 } else if (type_of_error == "zerodivision") {
456 unsigned int some_number = porting::getTimeS();
457 unsigned int zerovalue = 0;
458 unsigned int result = some_number / zerovalue;
459 errorstream << "Well this shouldn't ever be shown: " << result << std::endl;
461 } else if (type_of_error == "exception") {
462 throw BaseException("Errorhandler test fct called");
469 void ModApiServer::Initialize(lua_State *L, int top)
471 API_FCT(request_shutdown);
472 API_FCT(get_server_status);
473 API_FCT(get_worldpath);
474 API_FCT(is_singleplayer);
476 API_FCT(get_current_modname);
477 API_FCT(get_modpath);
478 API_FCT(get_modnames);
480 API_FCT(chat_send_all);
481 API_FCT(chat_send_player);
482 API_FCT(show_formspec);
486 API_FCT(get_player_information);
487 API_FCT(get_player_privs);
488 API_FCT(get_player_ip);
489 API_FCT(get_ban_list);
490 API_FCT(get_ban_description);
492 API_FCT(kick_player);
493 API_FCT(unban_player_or_ip);
494 API_FCT(notify_authentication_modified);
497 API_FCT(cause_error);