Allow damage for attached objects, add attach/detach callbacks (#6786)
[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 = lua_toboolean(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(narrow_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, narrow_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 #ifndef NDEBUG
237         lua_pushstring(L,"serialization_version");
238         lua_pushnumber(L, ser_vers);
239         lua_settable(L, table);
240
241         lua_pushstring(L,"major");
242         lua_pushnumber(L, major);
243         lua_settable(L, table);
244
245         lua_pushstring(L,"minor");
246         lua_pushnumber(L, minor);
247         lua_settable(L, table);
248
249         lua_pushstring(L,"patch");
250         lua_pushnumber(L, patch);
251         lua_settable(L, table);
252
253         lua_pushstring(L,"version_string");
254         lua_pushstring(L, vers_string.c_str());
255         lua_settable(L, table);
256
257         lua_pushstring(L,"state");
258         lua_pushstring(L,ClientInterface::state2Name(state).c_str());
259         lua_settable(L, table);
260 #endif
261
262 #undef ERET
263         return 1;
264 }
265
266 // get_ban_list()
267 int ModApiServer::l_get_ban_list(lua_State *L)
268 {
269         NO_MAP_LOCK_REQUIRED;
270         lua_pushstring(L, getServer(L)->getBanDescription("").c_str());
271         return 1;
272 }
273
274 // get_ban_description()
275 int ModApiServer::l_get_ban_description(lua_State *L)
276 {
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());
280         return 1;
281 }
282
283 // ban_player()
284 int ModApiServer::l_ban_player(lua_State *L)
285 {
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
291                 return 1;
292         }
293         try
294         {
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
302                 return 1;
303         }
304         lua_pushboolean(L, true);
305         return 1;
306 }
307
308 // kick_player(name, [reason]) -> success
309 int ModApiServer::l_kick_player(lua_State *L)
310 {
311         NO_MAP_LOCK_REQUIRED;
312         const char *name = luaL_checkstring(L, 1);
313         std::string message;
314         if (lua_isstring(L, 2))
315         {
316                 message = std::string("Kicked: ") + lua_tostring(L, 2);
317         }
318         else
319         {
320                 message = "Kicked.";
321         }
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)
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         s32 handle = getServer(L)->playSound(spec, params);
441         lua_pushinteger(L, handle);
442         return 1;
443 }
444
445 // sound_stop(handle)
446 int ModApiServer::l_sound_stop(lua_State *L)
447 {
448         NO_MAP_LOCK_REQUIRED;
449         int handle = luaL_checkinteger(L, 1);
450         getServer(L)->stopSound(handle);
451         return 0;
452 }
453
454 int ModApiServer::l_sound_fade(lua_State *L)
455 {
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);
461         return 0;
462 }
463
464 // is_singleplayer()
465 int ModApiServer::l_is_singleplayer(lua_State *L)
466 {
467         NO_MAP_LOCK_REQUIRED;
468         lua_pushboolean(L, getServer(L)->isSingleplayer());
469         return 1;
470 }
471
472 // notify_authentication_modified(name)
473 int ModApiServer::l_notify_authentication_modified(lua_State *L)
474 {
475         NO_MAP_LOCK_REQUIRED;
476         std::string name;
477         if(lua_isstring(L, 1))
478                 name = lua_tostring(L, 1);
479         getServer(L)->reportPrivsModified(name);
480         return 0;
481 }
482
483 // get_last_run_mod()
484 int ModApiServer::l_get_last_run_mod(lua_State *L)
485 {
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') {
490                 lua_pop(L, 1);
491                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
492         }
493         return 1;
494 }
495
496 // set_last_run_mod(modname)
497 int ModApiServer::l_set_last_run_mod(lua_State *L)
498 {
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);
504 #endif
505         return 0;
506 }
507
508 void ModApiServer::Initialize(lua_State *L, int top)
509 {
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);
515
516         API_FCT(get_current_modname);
517         API_FCT(get_modpath);
518         API_FCT(get_modnames);
519
520         API_FCT(print);
521
522         API_FCT(chat_send_all);
523         API_FCT(chat_send_player);
524         API_FCT(show_formspec);
525         API_FCT(sound_play);
526         API_FCT(sound_stop);
527         API_FCT(sound_fade);
528
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);
534         API_FCT(ban_player);
535         API_FCT(kick_player);
536         API_FCT(remove_player);
537         API_FCT(unban_player_or_ip);
538         API_FCT(notify_authentication_modified);
539
540         API_FCT(get_last_run_mod);
541         API_FCT(set_last_run_mod);
542 }