Improve core.sound_play with ephemeral sounds and player exclusion
[oweals/minetest.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 "server.h"
26 #include "environment.h"
27 #include "remoteplayer.h"
28 #include "log.h"
29 #include <algorithm>
30
31 // request_shutdown()
32 int ModApiServer::l_request_shutdown(lua_State *L)
33 {
34         NO_MAP_LOCK_REQUIRED;
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);
39         return 0;
40 }
41
42 // get_server_status()
43 int ModApiServer::l_get_server_status(lua_State *L)
44 {
45         NO_MAP_LOCK_REQUIRED;
46         lua_pushstring(L, wide_to_narrow(getServer(L)->getStatusString()).c_str());
47         return 1;
48 }
49
50 // get_server_uptime()
51 int ModApiServer::l_get_server_uptime(lua_State *L)
52 {
53         NO_MAP_LOCK_REQUIRED;
54         lua_pushnumber(L, getServer(L)->getUptime());
55         return 1;
56 }
57
58
59 // print(text)
60 int ModApiServer::l_print(lua_State *L)
61 {
62         NO_MAP_LOCK_REQUIRED;
63         std::string text;
64         text = luaL_checkstring(L, 1);
65         getServer(L)->printToConsoleOnly(text);
66         return 0;
67 }
68
69 // chat_send_all(text)
70 int ModApiServer::l_chat_send_all(lua_State *L)
71 {
72         NO_MAP_LOCK_REQUIRED;
73         const char *text = luaL_checkstring(L, 1);
74         // Get server from registry
75         Server *server = getServer(L);
76         // Send
77         server->notifyPlayers(utf8_to_wide(text));
78         return 0;
79 }
80
81 // chat_send_player(name, text)
82 int ModApiServer::l_chat_send_player(lua_State *L)
83 {
84         NO_MAP_LOCK_REQUIRED;
85         const char *name = luaL_checkstring(L, 1);
86         const char *text = luaL_checkstring(L, 2);
87
88         // Get server from registry
89         Server *server = getServer(L);
90         // Send
91         server->notifyPlayer(name, utf8_to_wide(text));
92         return 0;
93 }
94
95 // get_player_privs(name, text)
96 int ModApiServer::l_get_player_privs(lua_State *L)
97 {
98         NO_MAP_LOCK_REQUIRED;
99         const char *name = luaL_checkstring(L, 1);
100         // Get server from registry
101         Server *server = getServer(L);
102         // Do it
103         lua_newtable(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());
109         }
110         lua_pushvalue(L, table);
111         return 1;
112 }
113
114 // get_player_ip()
115 int ModApiServer::l_get_player_ip(lua_State *L)
116 {
117         NO_MAP_LOCK_REQUIRED;
118         const char * name = luaL_checkstring(L, 1);
119         RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
120         if(player == NULL)
121         {
122                 lua_pushnil(L); // no such player
123                 return 1;
124         }
125         try
126         {
127                 Address addr = getServer(L)->getPeerAddress(player->getPeerId());
128                 std::string ip_str = addr.serializeString();
129                 lua_pushstring(L, ip_str.c_str());
130                 return 1;
131         } catch (const con::PeerNotFoundException &) {
132                 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
133                 lua_pushnil(L); // error
134                 return 1;
135         }
136 }
137
138 // get_player_information(name)
139 int ModApiServer::l_get_player_information(lua_State *L)
140 {
141
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
147                 return 1;
148         }
149
150         Address addr;
151         try
152         {
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
157                 return 1;
158         }
159
160         float min_rtt,max_rtt,avg_rtt,min_jitter,max_jitter,avg_jitter;
161         ClientState state;
162         u32 uptime;
163         u16 prot_vers;
164         u8 ser_vers,major,minor,patch;
165         std::string vers_string;
166
167 #define ERET(code)                                                             \
168         if (!(code)) {                                                             \
169                 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;     \
170                 lua_pushnil(L); /* error */                                            \
171                 return 1;                                                              \
172         }
173
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,
178                 &min_jitter))
179         ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::MAX_JITTER,
180                 &max_jitter))
181         ERET(getServer(L)->getClientConInfo(player->getPeerId(), con::AVG_JITTER,
182                 &avg_jitter))
183
184         ERET(getServer(L)->getClientInfo(player->getPeerId(), &state, &uptime, &ser_vers,
185                 &prot_vers, &major, &minor, &patch, &vers_string))
186
187         lua_newtable(L);
188         int table = lua_gettop(L);
189
190         lua_pushstring(L,"address");
191         lua_pushstring(L, addr.serializeString().c_str());
192         lua_settable(L, table);
193
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);
199         } else {
200                 lua_pushnumber(L, 0);
201         }
202         lua_settable(L, table);
203
204         lua_pushstring(L,"min_rtt");
205         lua_pushnumber(L, min_rtt);
206         lua_settable(L, table);
207
208         lua_pushstring(L,"max_rtt");
209         lua_pushnumber(L, max_rtt);
210         lua_settable(L, table);
211
212         lua_pushstring(L,"avg_rtt");
213         lua_pushnumber(L, avg_rtt);
214         lua_settable(L, table);
215
216         lua_pushstring(L,"min_jitter");
217         lua_pushnumber(L, min_jitter);
218         lua_settable(L, table);
219
220         lua_pushstring(L,"max_jitter");
221         lua_pushnumber(L, max_jitter);
222         lua_settable(L, table);
223
224         lua_pushstring(L,"avg_jitter");
225         lua_pushnumber(L, avg_jitter);
226         lua_settable(L, table);
227
228         lua_pushstring(L,"connection_uptime");
229         lua_pushnumber(L, uptime);
230         lua_settable(L, table);
231
232         lua_pushstring(L,"protocol_version");
233         lua_pushnumber(L, prot_vers);
234         lua_settable(L, table);
235
236         lua_pushstring(L, "formspec_version");
237         lua_pushnumber(L, player->formspec_version);
238         lua_settable(L, table);
239
240 #ifndef NDEBUG
241         lua_pushstring(L,"serialization_version");
242         lua_pushnumber(L, ser_vers);
243         lua_settable(L, table);
244
245         lua_pushstring(L,"major");
246         lua_pushnumber(L, major);
247         lua_settable(L, table);
248
249         lua_pushstring(L,"minor");
250         lua_pushnumber(L, minor);
251         lua_settable(L, table);
252
253         lua_pushstring(L,"patch");
254         lua_pushnumber(L, patch);
255         lua_settable(L, table);
256
257         lua_pushstring(L,"version_string");
258         lua_pushstring(L, vers_string.c_str());
259         lua_settable(L, table);
260
261         lua_pushstring(L,"state");
262         lua_pushstring(L,ClientInterface::state2Name(state).c_str());
263         lua_settable(L, table);
264 #endif
265
266 #undef ERET
267         return 1;
268 }
269
270 // get_ban_list()
271 int ModApiServer::l_get_ban_list(lua_State *L)
272 {
273         NO_MAP_LOCK_REQUIRED;
274         lua_pushstring(L, getServer(L)->getBanDescription("").c_str());
275         return 1;
276 }
277
278 // get_ban_description()
279 int ModApiServer::l_get_ban_description(lua_State *L)
280 {
281         NO_MAP_LOCK_REQUIRED;
282         const char * ip_or_name = luaL_checkstring(L, 1);
283         lua_pushstring(L, getServer(L)->getBanDescription(std::string(ip_or_name)).c_str());
284         return 1;
285 }
286
287 // ban_player()
288 int ModApiServer::l_ban_player(lua_State *L)
289 {
290         NO_MAP_LOCK_REQUIRED;
291         const char * name = luaL_checkstring(L, 1);
292         RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
293         if (player == NULL) {
294                 lua_pushboolean(L, false); // no such player
295                 return 1;
296         }
297         try
298         {
299                 Address addr = getServer(L)->getPeerAddress(
300                         dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name)->getPeerId());
301                 std::string ip_str = addr.serializeString();
302                 getServer(L)->setIpBanned(ip_str, name);
303         } catch(const con::PeerNotFoundException &) {
304                 dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
305                 lua_pushboolean(L, false); // error
306                 return 1;
307         }
308         lua_pushboolean(L, true);
309         return 1;
310 }
311
312 // kick_player(name, [reason]) -> success
313 int ModApiServer::l_kick_player(lua_State *L)
314 {
315         NO_MAP_LOCK_REQUIRED;
316         const char *name = luaL_checkstring(L, 1);
317         std::string message("Kicked");
318         if (lua_isstring(L, 2))
319                 message.append(": ").append(readParam<std::string>(L, 2));
320         else
321                 message.append(".");
322
323         RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
324         if (player == NULL) {
325                 lua_pushboolean(L, false); // No such player
326                 return 1;
327         }
328         getServer(L)->DenyAccess_Legacy(player->getPeerId(), utf8_to_wide(message));
329         lua_pushboolean(L, true);
330         return 1;
331 }
332
333 int ModApiServer::l_remove_player(lua_State *L)
334 {
335         NO_MAP_LOCK_REQUIRED;
336         std::string name = luaL_checkstring(L, 1);
337         ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
338         assert(s_env);
339
340         RemotePlayer *player = s_env->getPlayer(name.c_str());
341         if (!player)
342                 lua_pushinteger(L, s_env->removePlayerFromDatabase(name) ? 0 : 1);
343         else
344                 lua_pushinteger(L, 2);
345
346         return 1;
347 }
348
349 // unban_player_or_ip()
350 int ModApiServer::l_unban_player_or_ip(lua_State *L)
351 {
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);
356         return 1;
357 }
358
359 // show_formspec(playername,formname,formspec)
360 int ModApiServer::l_show_formspec(lua_State *L)
361 {
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);
366
367         if(getServer(L)->showFormspec(playername,formspec,formname))
368         {
369                 lua_pushboolean(L, true);
370         }else{
371                 lua_pushboolean(L, false);
372         }
373         return 1;
374 }
375
376 // get_current_modname()
377 int ModApiServer::l_get_current_modname(lua_State *L)
378 {
379         NO_MAP_LOCK_REQUIRED;
380         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
381         return 1;
382 }
383
384 // get_modpath(modname)
385 int ModApiServer::l_get_modpath(lua_State *L)
386 {
387         NO_MAP_LOCK_REQUIRED;
388         std::string modname = luaL_checkstring(L, 1);
389         const ModSpec *mod = getServer(L)->getModSpec(modname);
390         if (!mod) {
391                 lua_pushnil(L);
392                 return 1;
393         }
394         lua_pushstring(L, mod->path.c_str());
395         return 1;
396 }
397
398 // get_modnames()
399 // the returned list is sorted alphabetically for you
400 int ModApiServer::l_get_modnames(lua_State *L)
401 {
402         NO_MAP_LOCK_REQUIRED;
403
404         // Get a list of mods
405         std::vector<std::string> modlist;
406         getServer(L)->getModNames(modlist);
407
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());
412
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);
419         }
420         return 1;
421 }
422
423 // get_worldpath()
424 int ModApiServer::l_get_worldpath(lua_State *L)
425 {
426         NO_MAP_LOCK_REQUIRED;
427         std::string worldpath = getServer(L)->getWorldPath();
428         lua_pushstring(L, worldpath.c_str());
429         return 1;
430 }
431
432 // sound_play(spec, parameters, [ephemeral])
433 int ModApiServer::l_sound_play(lua_State *L)
434 {
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         bool ephemeral = lua_gettop(L) > 2 && readParam<bool>(L, 3);
441         if (ephemeral) {
442                 getServer(L)->playSound(spec, params, true);
443                 lua_pushnil(L);
444         } else {
445                 s32 handle = getServer(L)->playSound(spec, params);
446                 lua_pushinteger(L, handle);
447         }
448         return 1;
449 }
450
451 // sound_stop(handle)
452 int ModApiServer::l_sound_stop(lua_State *L)
453 {
454         NO_MAP_LOCK_REQUIRED;
455         s32 handle = luaL_checkinteger(L, 1);
456         getServer(L)->stopSound(handle);
457         return 0;
458 }
459
460 int ModApiServer::l_sound_fade(lua_State *L)
461 {
462         NO_MAP_LOCK_REQUIRED;
463         s32 handle = luaL_checkinteger(L, 1);
464         float step = readParam<float>(L, 2);
465         float gain = readParam<float>(L, 3);
466         getServer(L)->fadeSound(handle, step, gain);
467         return 0;
468 }
469
470 // is_singleplayer()
471 int ModApiServer::l_is_singleplayer(lua_State *L)
472 {
473         NO_MAP_LOCK_REQUIRED;
474         lua_pushboolean(L, getServer(L)->isSingleplayer());
475         return 1;
476 }
477
478 // notify_authentication_modified(name)
479 int ModApiServer::l_notify_authentication_modified(lua_State *L)
480 {
481         NO_MAP_LOCK_REQUIRED;
482         std::string name;
483         if(lua_isstring(L, 1))
484                 name = readParam<std::string>(L, 1);
485         getServer(L)->reportPrivsModified(name);
486         return 0;
487 }
488
489 // get_last_run_mod()
490 int ModApiServer::l_get_last_run_mod(lua_State *L)
491 {
492         NO_MAP_LOCK_REQUIRED;
493         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
494         std::string current_mod = readParam<std::string>(L, -1, "");
495         if (current_mod.empty()) {
496                 lua_pop(L, 1);
497                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
498         }
499         return 1;
500 }
501
502 // set_last_run_mod(modname)
503 int ModApiServer::l_set_last_run_mod(lua_State *L)
504 {
505         NO_MAP_LOCK_REQUIRED;
506 #ifdef SCRIPTAPI_DEBUG
507         const char *mod = lua_tostring(L, 1);
508         getScriptApiBase(L)->setOriginDirect(mod);
509         //printf(">>>> last mod set from Lua: %s\n", mod);
510 #endif
511         return 0;
512 }
513
514 void ModApiServer::Initialize(lua_State *L, int top)
515 {
516         API_FCT(request_shutdown);
517         API_FCT(get_server_status);
518         API_FCT(get_server_uptime);
519         API_FCT(get_worldpath);
520         API_FCT(is_singleplayer);
521
522         API_FCT(get_current_modname);
523         API_FCT(get_modpath);
524         API_FCT(get_modnames);
525
526         API_FCT(print);
527
528         API_FCT(chat_send_all);
529         API_FCT(chat_send_player);
530         API_FCT(show_formspec);
531         API_FCT(sound_play);
532         API_FCT(sound_stop);
533         API_FCT(sound_fade);
534
535         API_FCT(get_player_information);
536         API_FCT(get_player_privs);
537         API_FCT(get_player_ip);
538         API_FCT(get_ban_list);
539         API_FCT(get_ban_description);
540         API_FCT(ban_player);
541         API_FCT(kick_player);
542         API_FCT(remove_player);
543         API_FCT(unban_player_or_ip);
544         API_FCT(notify_authentication_modified);
545
546         API_FCT(get_last_run_mod);
547         API_FCT(set_last_run_mod);
548 }