You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

543 lines
15 KiB

  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "lua_api/l_server.h"
  17. #include "lua_api/l_internal.h"
  18. #include "common/c_converter.h"
  19. #include "common/c_content.h"
  20. #include "cpp_api/s_base.h"
  21. #include "server.h"
  22. #include "environment.h"
  23. #include "remoteplayer.h"
  24. #include "log.h"
  25. #include <algorithm>
  26. // request_shutdown()
  27. int ModApiServer::l_request_shutdown(lua_State *L)
  28. {
  29. NO_MAP_LOCK_REQUIRED;
  30. const char *msg = lua_tolstring(L, 1, NULL);
  31. bool reconnect = readParam<bool>(L, 2);
  32. float seconds_before_shutdown = lua_tonumber(L, 3);
  33. getServer(L)->requestShutdown(msg ? msg : "", reconnect, seconds_before_shutdown);
  34. return 0;
  35. }
  36. // get_server_status()
  37. int ModApiServer::l_get_server_status(lua_State *L)
  38. {
  39. NO_MAP_LOCK_REQUIRED;
  40. lua_pushstring(L, wide_to_narrow(getServer(L)->getStatusString()).c_str());
  41. return 1;
  42. }
  43. // get_server_uptime()
  44. int ModApiServer::l_get_server_uptime(lua_State *L)
  45. {
  46. NO_MAP_LOCK_REQUIRED;
  47. lua_pushnumber(L, getServer(L)->getUptime());
  48. return 1;
  49. }
  50. // print(text)
  51. int ModApiServer::l_print(lua_State *L)
  52. {
  53. NO_MAP_LOCK_REQUIRED;
  54. std::string text;
  55. text = luaL_checkstring(L, 1);
  56. getServer(L)->printToConsoleOnly(text);
  57. return 0;
  58. }
  59. // chat_send_all(text)
  60. int ModApiServer::l_chat_send_all(lua_State *L)
  61. {
  62. NO_MAP_LOCK_REQUIRED;
  63. const char *text = luaL_checkstring(L, 1);
  64. // Get server from registry
  65. Server *server = getServer(L);
  66. // Send
  67. server->notifyPlayers(utf8_to_wide(text));
  68. return 0;
  69. }
  70. // chat_send_player(name, text)
  71. int ModApiServer::l_chat_send_player(lua_State *L)
  72. {
  73. NO_MAP_LOCK_REQUIRED;
  74. const char *name = luaL_checkstring(L, 1);
  75. const char *text = luaL_checkstring(L, 2);
  76. // Get server from registry
  77. Server *server = getServer(L);
  78. // Send
  79. server->notifyPlayer(name, utf8_to_wide(text));
  80. return 0;
  81. }
  82. // get_player_privs(name, text)
  83. int ModApiServer::l_get_player_privs(lua_State *L)
  84. {
  85. NO_MAP_LOCK_REQUIRED;
  86. const char *name = luaL_checkstring(L, 1);
  87. // Get server from registry
  88. Server *server = getServer(L);
  89. // Do it
  90. lua_newtable(L);
  91. int table = lua_gettop(L);
  92. std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
  93. for (const std::string &privs_ : privs_s) {
  94. lua_pushboolean(L, true);
  95. lua_setfield(L, table, privs_.c_str());
  96. }
  97. lua_pushvalue(L, table);
  98. return 1;
  99. }
  100. // get_player_ip()
  101. int ModApiServer::l_get_player_ip(lua_State *L)
  102. {
  103. NO_MAP_LOCK_REQUIRED;
  104. const char * name = luaL_checkstring(L, 1);
  105. RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
  106. if(player == NULL)
  107. {
  108. lua_pushnil(L); // no such player
  109. return 1;
  110. }
  111. try
  112. {
  113. Address addr = getServer(L)->getPeerAddress(player->getPeerId());
  114. std::string ip_str = addr.serializeString();
  115. lua_pushstring(L, ip_str.c_str());
  116. return 1;
  117. } catch (const con::PeerNotFoundException &) {
  118. dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
  119. lua_pushnil(L); // error
  120. return 1;
  121. }
  122. }
  123. // get_player_information(name)
  124. int ModApiServer::l_get_player_information(lua_State *L)
  125. {
  126. NO_MAP_LOCK_REQUIRED;
  127. const char * name = luaL_checkstring(L, 1);
  128. RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
  129. if (player == NULL) {
  130. lua_pushnil(L); // no such player
  131. return 1;
  132. }
  133. Address addr;
  134. try
  135. {
  136. addr = getServer(L)->getPeerAddress(player->getPeerId());
  137. } catch(const con::PeerNotFoundException &) {
  138. dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
  139. lua_pushnil(L); // error
  140. return 1;
  141. }
  142. float min_rtt,max_rtt,avg_rtt,min_jitter,max_jitter,avg_jitter;
  143. ClientState state;
  144. u32 uptime;
  145. u16 prot_vers;
  146. u8 ser_vers,major,minor,patch;
  147. std::string vers_string;
  148. #define ERET(code) \
  149. if (!(code)) { \
  150. dstream << FUNCTION_NAME << ": peer was not found" << std::endl; \
  151. lua_pushnil(L); /* error */ \
  152. return 1; \
  153. }
  154. ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MIN_RTT, &min_rtt))
  155. ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MAX_RTT, &max_rtt))
  156. ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::AVG_RTT, &avg_rtt))
  157. ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MIN_JITTER,
  158. &min_jitter))
  159. ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MAX_JITTER,
  160. &max_jitter))
  161. ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::AVG_JITTER,
  162. &avg_jitter))
  163. ERET(getServer(L)->getClientInfo(player->getPeerId(), &state, &uptime, &ser_vers,
  164. &prot_vers, &major, &minor, &patch, &vers_string))
  165. lua_newtable(L);
  166. int table = lua_gettop(L);
  167. lua_pushstring(L,"address");
  168. lua_pushstring(L, addr.serializeString().c_str());
  169. lua_settable(L, table);
  170. lua_pushstring(L,"ip_version");
  171. if (addr.getFamily() == AF_INET) {
  172. lua_pushnumber(L, 4);
  173. } else if (addr.getFamily() == AF_INET6) {
  174. lua_pushnumber(L, 6);
  175. } else {
  176. lua_pushnumber(L, 0);
  177. }
  178. lua_settable(L, table);
  179. lua_pushstring(L,"min_rtt");
  180. lua_pushnumber(L, min_rtt);
  181. lua_settable(L, table);
  182. lua_pushstring(L,"max_rtt");
  183. lua_pushnumber(L, max_rtt);
  184. lua_settable(L, table);
  185. lua_pushstring(L,"avg_rtt");
  186. lua_pushnumber(L, avg_rtt);
  187. lua_settable(L, table);
  188. lua_pushstring(L,"min_jitter");
  189. lua_pushnumber(L, min_jitter);
  190. lua_settable(L, table);
  191. lua_pushstring(L,"max_jitter");
  192. lua_pushnumber(L, max_jitter);
  193. lua_settable(L, table);
  194. lua_pushstring(L,"avg_jitter");
  195. lua_pushnumber(L, avg_jitter);
  196. lua_settable(L, table);
  197. lua_pushstring(L,"connection_uptime");
  198. lua_pushnumber(L, uptime);
  199. lua_settable(L, table);
  200. lua_pushstring(L,"protocol_version");
  201. lua_pushnumber(L, prot_vers);
  202. lua_settable(L, table);
  203. lua_pushstring(L, "formspec_version");
  204. lua_pushnumber(L, 1);
  205. lua_settable(L, table);
  206. #ifndef NDEBUG
  207. lua_pushstring(L,"serialization_version");
  208. lua_pushnumber(L, ser_vers);
  209. lua_settable(L, table);
  210. lua_pushstring(L,"major");
  211. lua_pushnumber(L, major);
  212. lua_settable(L, table);
  213. lua_pushstring(L,"minor");
  214. lua_pushnumber(L, minor);
  215. lua_settable(L, table);
  216. lua_pushstring(L,"patch");
  217. lua_pushnumber(L, patch);
  218. lua_settable(L, table);
  219. lua_pushstring(L,"version_string");
  220. lua_pushstring(L, vers_string.c_str());
  221. lua_settable(L, table);
  222. lua_pushstring(L,"state");
  223. lua_pushstring(L,ClientInterface::state2Name(state).c_str());
  224. lua_settable(L, table);
  225. #endif
  226. #undef ERET
  227. return 1;
  228. }
  229. // get_ban_list()
  230. int ModApiServer::l_get_ban_list(lua_State *L)
  231. {
  232. NO_MAP_LOCK_REQUIRED;
  233. lua_pushstring(L, getServer(L)->getBanDescription("").c_str());
  234. return 1;
  235. }
  236. // get_ban_description()
  237. int ModApiServer::l_get_ban_description(lua_State *L)
  238. {
  239. NO_MAP_LOCK_REQUIRED;
  240. const char * ip_or_name = luaL_checkstring(L, 1);
  241. lua_pushstring(L, getServer(L)->getBanDescription(std::string(ip_or_name)).c_str());
  242. return 1;
  243. }
  244. // ban_player()
  245. int ModApiServer::l_ban_player(lua_State *L)
  246. {
  247. NO_MAP_LOCK_REQUIRED;
  248. const char * name = luaL_checkstring(L, 1);
  249. RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
  250. if (player == NULL) {
  251. lua_pushboolean(L, false); // no such player
  252. return 1;
  253. }
  254. try
  255. {
  256. Address addr = getServer(L)->getPeerAddress(
  257. dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name)->getPeerId());
  258. std::string ip_str = addr.serializeString();
  259. getServer(L)->setIpBanned(ip_str, name);
  260. } catch(const con::PeerNotFoundException &) {
  261. dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
  262. lua_pushboolean(L, false); // error
  263. return 1;
  264. }
  265. lua_pushboolean(L, true);
  266. return 1;
  267. }
  268. // kick_player(name, [reason]) -> success
  269. int ModApiServer::l_kick_player(lua_State *L)
  270. {
  271. NO_MAP_LOCK_REQUIRED;
  272. const char *name = luaL_checkstring(L, 1);
  273. std::string message("Kicked");
  274. if (lua_isstring(L, 2))
  275. message.append(": ").append(readParam<std::string>(L, 2));
  276. else
  277. message.append(".");
  278. RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
  279. if (player == NULL) {
  280. lua_pushboolean(L, false); // No such player
  281. return 1;
  282. }
  283. getServer(L)->DenyAccess_Legacy(player->getPeerId(), utf8_to_wide(message));
  284. lua_pushboolean(L, true);
  285. return 1;
  286. }
  287. int ModApiServer::l_remove_player(lua_State *L)
  288. {
  289. NO_MAP_LOCK_REQUIRED;
  290. std::string name = luaL_checkstring(L, 1);
  291. ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
  292. assert(s_env);
  293. RemotePlayer *player = s_env->getPlayer(name.c_str());
  294. if (!player)
  295. lua_pushinteger(L, s_env->removePlayerFromDatabase(name) ? 0 : 1);
  296. else
  297. lua_pushinteger(L, 2);
  298. return 1;
  299. }
  300. // unban_player_or_ip()
  301. int ModApiServer::l_unban_player_or_ip(lua_State *L)
  302. {
  303. NO_MAP_LOCK_REQUIRED;
  304. const char * ip_or_name = luaL_checkstring(L, 1);
  305. getServer(L)->unsetIpBanned(ip_or_name);
  306. lua_pushboolean(L, true);
  307. return 1;
  308. }
  309. // show_formspec(playername,formname,formspec)
  310. int ModApiServer::l_show_formspec(lua_State *L)
  311. {
  312. NO_MAP_LOCK_REQUIRED;
  313. const char *playername = luaL_checkstring(L, 1);
  314. const char *formname = luaL_checkstring(L, 2);
  315. const char *formspec = luaL_checkstring(L, 3);
  316. if(getServer(L)->showFormspec(playername,formspec,formname))
  317. {
  318. lua_pushboolean(L, true);
  319. }else{
  320. lua_pushboolean(L, false);
  321. }
  322. return 1;
  323. }
  324. // get_current_modname()
  325. int ModApiServer::l_get_current_modname(lua_State *L)
  326. {
  327. NO_MAP_LOCK_REQUIRED;
  328. lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
  329. return 1;
  330. }
  331. // get_modpath(modname)
  332. int ModApiServer::l_get_modpath(lua_State *L)
  333. {
  334. NO_MAP_LOCK_REQUIRED;
  335. std::string modname = luaL_checkstring(L, 1);
  336. const ModSpec *mod = getServer(L)->getModSpec(modname);
  337. if (!mod) {
  338. lua_pushnil(L);
  339. return 1;
  340. }
  341. lua_pushstring(L, mod->path.c_str());
  342. return 1;
  343. }
  344. // get_modnames()
  345. // the returned list is sorted alphabetically for you
  346. int ModApiServer::l_get_modnames(lua_State *L)
  347. {
  348. NO_MAP_LOCK_REQUIRED;
  349. // Get a list of mods
  350. std::vector<std::string> modlist;
  351. getServer(L)->getModNames(modlist);
  352. // Take unsorted items from mods_unsorted and sort them into
  353. // mods_sorted; not great performance but the number of mods on a
  354. // server will likely be small.
  355. std::sort(modlist.begin(), modlist.end());
  356. // Package them up for Lua
  357. lua_createtable(L, modlist.size(), 0);
  358. std::vector<std::string>::iterator iter = modlist.begin();
  359. for (u16 i = 0; iter != modlist.end(); ++iter) {
  360. lua_pushstring(L, iter->c_str());
  361. lua_rawseti(L, -2, ++i);
  362. }
  363. return 1;
  364. }
  365. // get_worldpath()
  366. int ModApiServer::l_get_worldpath(lua_State *L)
  367. {
  368. NO_MAP_LOCK_REQUIRED;
  369. std::string worldpath = getServer(L)->getWorldPath();
  370. lua_pushstring(L, worldpath.c_str());
  371. return 1;
  372. }
  373. // sound_play(spec, parameters)
  374. int ModApiServer::l_sound_play(lua_State *L)
  375. {
  376. NO_MAP_LOCK_REQUIRED;
  377. SimpleSoundSpec spec;
  378. read_soundspec(L, 1, spec);
  379. ServerSoundParams params;
  380. read_server_sound_params(L, 2, params);
  381. s32 handle = getServer(L)->playSound(spec, params);
  382. lua_pushinteger(L, handle);
  383. return 1;
  384. }
  385. // sound_stop(handle)
  386. int ModApiServer::l_sound_stop(lua_State *L)
  387. {
  388. NO_MAP_LOCK_REQUIRED;
  389. int handle = luaL_checkinteger(L, 1);
  390. getServer(L)->stopSound(handle);
  391. return 0;
  392. }
  393. int ModApiServer::l_sound_fade(lua_State *L)
  394. {
  395. NO_MAP_LOCK_REQUIRED;
  396. s32 handle = luaL_checkinteger(L, 1);
  397. float step = readParam<float>(L, 2);
  398. float gain = readParam<float>(L, 3);
  399. getServer(L)->fadeSound(handle, step, gain);
  400. return 0;
  401. }
  402. // is_singleplayer()
  403. int ModApiServer::l_is_singleplayer(lua_State *L)
  404. {
  405. NO_MAP_LOCK_REQUIRED;
  406. lua_pushboolean(L, getServer(L)->isSingleplayer());
  407. return 1;
  408. }
  409. // notify_authentication_modified(name)
  410. int ModApiServer::l_notify_authentication_modified(lua_State *L)
  411. {
  412. NO_MAP_LOCK_REQUIRED;
  413. std::string name;
  414. if(lua_isstring(L, 1))
  415. name = readParam<std::string>(L, 1);
  416. getServer(L)->reportPrivsModified(name);
  417. return 0;
  418. }
  419. // get_last_run_mod()
  420. int ModApiServer::l_get_last_run_mod(lua_State *L)
  421. {
  422. NO_MAP_LOCK_REQUIRED;
  423. lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
  424. std::string current_mod = readParam<std::string>(L, -1, "");
  425. if (current_mod.empty()) {
  426. lua_pop(L, 1);
  427. lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
  428. }
  429. return 1;
  430. }
  431. // set_last_run_mod(modname)
  432. int ModApiServer::l_set_last_run_mod(lua_State *L)
  433. {
  434. NO_MAP_LOCK_REQUIRED;
  435. #ifdef SCRIPTAPI_DEBUG
  436. const char *mod = lua_tostring(L, 1);
  437. getScriptApiBase(L)->setOriginDirect(mod);
  438. //printf(">>>> last mod set from Lua: %s\n", mod);
  439. #endif
  440. return 0;
  441. }
  442. void ModApiServer::Initialize(lua_State *L, int top)
  443. {
  444. API_FCT(request_shutdown);
  445. API_FCT(get_server_status);
  446. API_FCT(get_server_uptime);
  447. API_FCT(get_worldpath);
  448. API_FCT(is_singleplayer);
  449. API_FCT(get_current_modname);
  450. API_FCT(get_modpath);
  451. API_FCT(get_modnames);
  452. API_FCT(print);
  453. API_FCT(chat_send_all);
  454. API_FCT(chat_send_player);
  455. API_FCT(show_formspec);
  456. API_FCT(sound_play);
  457. API_FCT(sound_stop);
  458. API_FCT(sound_fade);
  459. API_FCT(get_player_information);
  460. API_FCT(get_player_privs);
  461. API_FCT(get_player_ip);
  462. API_FCT(get_ban_list);
  463. API_FCT(get_ban_description);
  464. API_FCT(ban_player);
  465. API_FCT(kick_player);
  466. API_FCT(remove_player);
  467. API_FCT(unban_player_or_ip);
  468. API_FCT(notify_authentication_modified);
  469. API_FCT(get_last_run_mod);
  470. API_FCT(set_last_run_mod);
  471. }