]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_server.cpp
Various documentation fixes (#10692)
[dragonfireclient.git] / src / script / lua_api / l_server.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
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"
26 #include "server.h"
27 #include "environment.h"
28 #include "remoteplayer.h"
29 #include "log.h"
30 #include <algorithm>
31
32 // request_shutdown()
33 int ModApiServer::l_request_shutdown(lua_State *L)
34 {
35         NO_MAP_LOCK_REQUIRED;
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);
40         return 0;
41 }
42
43 // get_server_status()
44 int ModApiServer::l_get_server_status(lua_State *L)
45 {
46         NO_MAP_LOCK_REQUIRED;
47         lua_pushstring(L, wide_to_narrow(getServer(L)->getStatusString()).c_str());
48         return 1;
49 }
50
51 // get_server_uptime()
52 int ModApiServer::l_get_server_uptime(lua_State *L)
53 {
54         NO_MAP_LOCK_REQUIRED;
55         lua_pushnumber(L, getServer(L)->getUptime());
56         return 1;
57 }
58
59
60 // print(text)
61 int ModApiServer::l_print(lua_State *L)
62 {
63         NO_MAP_LOCK_REQUIRED;
64         std::string text;
65         text = luaL_checkstring(L, 1);
66         getServer(L)->printToConsoleOnly(text);
67         return 0;
68 }
69
70 // chat_send_all(text)
71 int ModApiServer::l_chat_send_all(lua_State *L)
72 {
73         NO_MAP_LOCK_REQUIRED;
74         const char *text = luaL_checkstring(L, 1);
75         // Get server from registry
76         Server *server = getServer(L);
77         // Send
78         server->notifyPlayers(utf8_to_wide(text));
79         return 0;
80 }
81
82 // chat_send_player(name, text)
83 int ModApiServer::l_chat_send_player(lua_State *L)
84 {
85         NO_MAP_LOCK_REQUIRED;
86         const char *name = luaL_checkstring(L, 1);
87         const char *text = luaL_checkstring(L, 2);
88
89         // Get server from registry
90         Server *server = getServer(L);
91         // Send
92         server->notifyPlayer(name, utf8_to_wide(text));
93         return 0;
94 }
95
96 // get_player_privs(name, text)
97 int ModApiServer::l_get_player_privs(lua_State *L)
98 {
99         NO_MAP_LOCK_REQUIRED;
100         const char *name = luaL_checkstring(L, 1);
101         // Get server from registry
102         Server *server = getServer(L);
103         // Do it
104         lua_newtable(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());
110         }
111         lua_pushvalue(L, table);
112         return 1;
113 }
114
115 // get_player_ip()
116 int ModApiServer::l_get_player_ip(lua_State *L)
117 {
118         NO_MAP_LOCK_REQUIRED;
119         const char * name = luaL_checkstring(L, 1);
120         RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
121         if(player == NULL)
122         {
123                 lua_pushnil(L); // no such player
124                 return 1;
125         }
126         try
127         {
128                 Address addr = getServer(L)->getPeerAddress(player->getPeerId());
129                 std::string ip_str = addr.serializeString();
130                 lua_pushstring(L, ip_str.c_str());
131                 return 1;
132         } catch (const con::PeerNotFoundException &) {
133                 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
134                 lua_pushnil(L); // error
135                 return 1;
136         }
137 }
138
139 // get_player_information(name)
140 int ModApiServer::l_get_player_information(lua_State *L)
141 {
142         NO_MAP_LOCK_REQUIRED;
143
144         Server *server = getServer(L);
145
146         const char *name = luaL_checkstring(L, 1);
147         RemotePlayer *player = server->getEnv().getPlayer(name);
148         if (!player) {
149                 lua_pushnil(L); // no such player
150                 return 1;
151         }
152
153         Address addr;
154         try {
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
159                 return 1;
160         }
161
162         float min_rtt, max_rtt, avg_rtt, min_jitter, max_jitter, avg_jitter;
163         ClientState state;
164         u32 uptime;
165         u16 prot_vers;
166         u8 ser_vers, major, minor, patch;
167         std::string vers_string, lang_code;
168
169         auto getConInfo = [&] (con::rtt_stat_type type, float *value) -> bool {
170                 return server->getClientConInfo(player->getPeerId(), type, value);
171         };
172
173         bool have_con_info =
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);
180
181         bool r = server->getClientInfo(player->getPeerId(), &state, &uptime,
182                 &ser_vers, &prot_vers, &major, &minor, &patch, &vers_string,
183                 &lang_code);
184         if (!r) {
185                 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
186                 lua_pushnil(L); // error
187                 return 1;
188         }
189
190         lua_newtable(L);
191         int table = lua_gettop(L);
192
193         lua_pushstring(L,"address");
194         lua_pushstring(L, addr.serializeString().c_str());
195         lua_settable(L, table);
196
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);
202         } else {
203                 lua_pushnumber(L, 0);
204         }
205         lua_settable(L, table);
206
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);
211
212                 lua_pushstring(L, "max_rtt");
213                 lua_pushnumber(L, max_rtt);
214                 lua_settable(L, table);
215
216                 lua_pushstring(L, "avg_rtt");
217                 lua_pushnumber(L, avg_rtt);
218                 lua_settable(L, table);
219
220                 lua_pushstring(L, "min_jitter");
221                 lua_pushnumber(L, min_jitter);
222                 lua_settable(L, table);
223
224                 lua_pushstring(L, "max_jitter");
225                 lua_pushnumber(L, max_jitter);
226                 lua_settable(L, table);
227
228                 lua_pushstring(L, "avg_jitter");
229                 lua_pushnumber(L, avg_jitter);
230                 lua_settable(L, table);
231         }
232
233         lua_pushstring(L,"connection_uptime");
234         lua_pushnumber(L, uptime);
235         lua_settable(L, table);
236
237         lua_pushstring(L,"protocol_version");
238         lua_pushnumber(L, prot_vers);
239         lua_settable(L, table);
240
241         lua_pushstring(L, "formspec_version");
242         lua_pushnumber(L, player->formspec_version);
243         lua_settable(L, table);
244
245         lua_pushstring(L, "lang_code");
246         lua_pushstring(L, lang_code.c_str());
247         lua_settable(L, table);
248
249 #ifndef NDEBUG
250         lua_pushstring(L,"serialization_version");
251         lua_pushnumber(L, ser_vers);
252         lua_settable(L, table);
253
254         lua_pushstring(L,"major");
255         lua_pushnumber(L, major);
256         lua_settable(L, table);
257
258         lua_pushstring(L,"minor");
259         lua_pushnumber(L, minor);
260         lua_settable(L, table);
261
262         lua_pushstring(L,"patch");
263         lua_pushnumber(L, patch);
264         lua_settable(L, table);
265
266         lua_pushstring(L,"version_string");
267         lua_pushstring(L, vers_string.c_str());
268         lua_settable(L, table);
269
270         lua_pushstring(L,"state");
271         lua_pushstring(L,ClientInterface::state2Name(state).c_str());
272         lua_settable(L, table);
273 #endif
274
275         return 1;
276 }
277
278 // get_ban_list()
279 int ModApiServer::l_get_ban_list(lua_State *L)
280 {
281         NO_MAP_LOCK_REQUIRED;
282         lua_pushstring(L, getServer(L)->getBanDescription("").c_str());
283         return 1;
284 }
285
286 // get_ban_description()
287 int ModApiServer::l_get_ban_description(lua_State *L)
288 {
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());
292         return 1;
293 }
294
295 // ban_player()
296 int ModApiServer::l_ban_player(lua_State *L)
297 {
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
303                 return 1;
304         }
305         try
306         {
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
314                 return 1;
315         }
316         lua_pushboolean(L, true);
317         return 1;
318 }
319
320 // kick_player(name, [reason]) -> success
321 int ModApiServer::l_kick_player(lua_State *L)
322 {
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));
328         else
329                 message.append(".");
330
331         RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
332         if (player == NULL) {
333                 lua_pushboolean(L, false); // No such player
334                 return 1;
335         }
336         getServer(L)->DenyAccess_Legacy(player->getPeerId(), utf8_to_wide(message));
337         lua_pushboolean(L, true);
338         return 1;
339 }
340
341 int ModApiServer::l_remove_player(lua_State *L)
342 {
343         NO_MAP_LOCK_REQUIRED;
344         std::string name = luaL_checkstring(L, 1);
345         ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
346         assert(s_env);
347
348         RemotePlayer *player = s_env->getPlayer(name.c_str());
349         if (!player)
350                 lua_pushinteger(L, s_env->removePlayerFromDatabase(name) ? 0 : 1);
351         else
352                 lua_pushinteger(L, 2);
353
354         return 1;
355 }
356
357 // unban_player_or_ip()
358 int ModApiServer::l_unban_player_or_ip(lua_State *L)
359 {
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);
364         return 1;
365 }
366
367 // show_formspec(playername,formname,formspec)
368 int ModApiServer::l_show_formspec(lua_State *L)
369 {
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);
374
375         if(getServer(L)->showFormspec(playername,formspec,formname))
376         {
377                 lua_pushboolean(L, true);
378         }else{
379                 lua_pushboolean(L, false);
380         }
381         return 1;
382 }
383
384 // get_current_modname()
385 int ModApiServer::l_get_current_modname(lua_State *L)
386 {
387         NO_MAP_LOCK_REQUIRED;
388         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
389         return 1;
390 }
391
392 // get_modpath(modname)
393 int ModApiServer::l_get_modpath(lua_State *L)
394 {
395         NO_MAP_LOCK_REQUIRED;
396         std::string modname = luaL_checkstring(L, 1);
397         const ModSpec *mod = getServer(L)->getModSpec(modname);
398         if (!mod) {
399                 lua_pushnil(L);
400                 return 1;
401         }
402         lua_pushstring(L, mod->path.c_str());
403         return 1;
404 }
405
406 // get_modnames()
407 // the returned list is sorted alphabetically for you
408 int ModApiServer::l_get_modnames(lua_State *L)
409 {
410         NO_MAP_LOCK_REQUIRED;
411
412         // Get a list of mods
413         std::vector<std::string> modlist;
414         getServer(L)->getModNames(modlist);
415
416         std::sort(modlist.begin(), modlist.end());
417
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);
424         }
425         return 1;
426 }
427
428 // get_worldpath()
429 int ModApiServer::l_get_worldpath(lua_State *L)
430 {
431         NO_MAP_LOCK_REQUIRED;
432         std::string worldpath = getServer(L)->getWorldPath();
433         lua_pushstring(L, worldpath.c_str());
434         return 1;
435 }
436
437 // sound_play(spec, parameters, [ephemeral])
438 int ModApiServer::l_sound_play(lua_State *L)
439 {
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);
446         if (ephemeral) {
447                 getServer(L)->playSound(spec, params, true);
448                 lua_pushnil(L);
449         } else {
450                 s32 handle = getServer(L)->playSound(spec, params);
451                 lua_pushinteger(L, handle);
452         }
453         return 1;
454 }
455
456 // sound_stop(handle)
457 int ModApiServer::l_sound_stop(lua_State *L)
458 {
459         NO_MAP_LOCK_REQUIRED;
460         s32 handle = luaL_checkinteger(L, 1);
461         getServer(L)->stopSound(handle);
462         return 0;
463 }
464
465 int ModApiServer::l_sound_fade(lua_State *L)
466 {
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);
472         return 0;
473 }
474
475 // dynamic_add_media(filepath)
476 int ModApiServer::l_dynamic_add_media(lua_State *L)
477 {
478         NO_MAP_LOCK_REQUIRED;
479
480         // Reject adding media before the server has started up
481         if (!getEnv(L))
482                 throw LuaError("Dynamic media cannot be added before server has started up");
483
484         std::string filepath = readParam<std::string>(L, 1);
485         CHECK_SECURE_PATH(L, filepath.c_str(), false);
486
487         bool ok = getServer(L)->dynamicAddMedia(filepath);
488         lua_pushboolean(L, ok);
489         return 1;
490 }
491
492 // is_singleplayer()
493 int ModApiServer::l_is_singleplayer(lua_State *L)
494 {
495         NO_MAP_LOCK_REQUIRED;
496         lua_pushboolean(L, getServer(L)->isSingleplayer());
497         return 1;
498 }
499
500 // notify_authentication_modified(name)
501 int ModApiServer::l_notify_authentication_modified(lua_State *L)
502 {
503         NO_MAP_LOCK_REQUIRED;
504         std::string name;
505         if(lua_isstring(L, 1))
506                 name = readParam<std::string>(L, 1);
507         getServer(L)->reportPrivsModified(name);
508         return 0;
509 }
510
511 // get_last_run_mod()
512 int ModApiServer::l_get_last_run_mod(lua_State *L)
513 {
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()) {
518                 lua_pop(L, 1);
519                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
520         }
521         return 1;
522 }
523
524 // set_last_run_mod(modname)
525 int ModApiServer::l_set_last_run_mod(lua_State *L)
526 {
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);
532 #endif
533         return 0;
534 }
535
536 void ModApiServer::Initialize(lua_State *L, int top)
537 {
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);
543
544         API_FCT(get_current_modname);
545         API_FCT(get_modpath);
546         API_FCT(get_modnames);
547
548         API_FCT(print);
549
550         API_FCT(chat_send_all);
551         API_FCT(chat_send_player);
552         API_FCT(show_formspec);
553         API_FCT(sound_play);
554         API_FCT(sound_stop);
555         API_FCT(sound_fade);
556         API_FCT(dynamic_add_media);
557
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);
563         API_FCT(ban_player);
564         API_FCT(kick_player);
565         API_FCT(remove_player);
566         API_FCT(unban_player_or_ip);
567         API_FCT(notify_authentication_modified);
568
569         API_FCT(get_last_run_mod);
570         API_FCT(set_last_run_mod);
571 }