Modernize lua read (part 2 & 3): C++ templating assurance (#7410)
[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 #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("Kicked");
314         if (lua_isstring(L, 2))
315                 message.append(": ").append(readParam<std::string>(L, 2));
316         else
317                 message.append(".");
318
319         RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
320         if (player == NULL) {
321                 lua_pushboolean(L, false); // No such player
322                 return 1;
323         }
324         getServer(L)->DenyAccess_Legacy(player->getPeerId(), utf8_to_wide(message));
325         lua_pushboolean(L, true);
326         return 1;
327 }
328
329 int ModApiServer::l_remove_player(lua_State *L)
330 {
331         NO_MAP_LOCK_REQUIRED;
332         std::string name = luaL_checkstring(L, 1);
333         ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
334         assert(s_env);
335
336         RemotePlayer *player = s_env->getPlayer(name.c_str());
337         if (!player)
338                 lua_pushinteger(L, s_env->removePlayerFromDatabase(name) ? 0 : 1);
339         else
340                 lua_pushinteger(L, 2);
341
342         return 1;
343 }
344
345 // unban_player_or_ip()
346 int ModApiServer::l_unban_player_or_ip(lua_State *L)
347 {
348         NO_MAP_LOCK_REQUIRED;
349         const char * ip_or_name = luaL_checkstring(L, 1);
350         getServer(L)->unsetIpBanned(ip_or_name);
351         lua_pushboolean(L, true);
352         return 1;
353 }
354
355 // show_formspec(playername,formname,formspec)
356 int ModApiServer::l_show_formspec(lua_State *L)
357 {
358         NO_MAP_LOCK_REQUIRED;
359         const char *playername = luaL_checkstring(L, 1);
360         const char *formname = luaL_checkstring(L, 2);
361         const char *formspec = luaL_checkstring(L, 3);
362
363         if(getServer(L)->showFormspec(playername,formspec,formname))
364         {
365                 lua_pushboolean(L, true);
366         }else{
367                 lua_pushboolean(L, false);
368         }
369         return 1;
370 }
371
372 // get_current_modname()
373 int ModApiServer::l_get_current_modname(lua_State *L)
374 {
375         NO_MAP_LOCK_REQUIRED;
376         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
377         return 1;
378 }
379
380 // get_modpath(modname)
381 int ModApiServer::l_get_modpath(lua_State *L)
382 {
383         NO_MAP_LOCK_REQUIRED;
384         std::string modname = luaL_checkstring(L, 1);
385         const ModSpec *mod = getServer(L)->getModSpec(modname);
386         if (!mod) {
387                 lua_pushnil(L);
388                 return 1;
389         }
390         lua_pushstring(L, mod->path.c_str());
391         return 1;
392 }
393
394 // get_modnames()
395 // the returned list is sorted alphabetically for you
396 int ModApiServer::l_get_modnames(lua_State *L)
397 {
398         NO_MAP_LOCK_REQUIRED;
399
400         // Get a list of mods
401         std::vector<std::string> modlist;
402         getServer(L)->getModNames(modlist);
403
404         // Take unsorted items from mods_unsorted and sort them into
405         // mods_sorted; not great performance but the number of mods on a
406         // server will likely be small.
407         std::sort(modlist.begin(), modlist.end());
408
409         // Package them up for Lua
410         lua_createtable(L, modlist.size(), 0);
411         std::vector<std::string>::iterator iter = modlist.begin();
412         for (u16 i = 0; iter != modlist.end(); ++iter) {
413                 lua_pushstring(L, iter->c_str());
414                 lua_rawseti(L, -2, ++i);
415         }
416         return 1;
417 }
418
419 // get_worldpath()
420 int ModApiServer::l_get_worldpath(lua_State *L)
421 {
422         NO_MAP_LOCK_REQUIRED;
423         std::string worldpath = getServer(L)->getWorldPath();
424         lua_pushstring(L, worldpath.c_str());
425         return 1;
426 }
427
428 // sound_play(spec, parameters)
429 int ModApiServer::l_sound_play(lua_State *L)
430 {
431         NO_MAP_LOCK_REQUIRED;
432         SimpleSoundSpec spec;
433         read_soundspec(L, 1, spec);
434         ServerSoundParams params;
435         read_server_sound_params(L, 2, params);
436         s32 handle = getServer(L)->playSound(spec, params);
437         lua_pushinteger(L, handle);
438         return 1;
439 }
440
441 // sound_stop(handle)
442 int ModApiServer::l_sound_stop(lua_State *L)
443 {
444         NO_MAP_LOCK_REQUIRED;
445         int handle = luaL_checkinteger(L, 1);
446         getServer(L)->stopSound(handle);
447         return 0;
448 }
449
450 int ModApiServer::l_sound_fade(lua_State *L)
451 {
452         NO_MAP_LOCK_REQUIRED;
453         s32 handle = luaL_checkinteger(L, 1);
454         float step = readParam<float>(L, 2);
455         float gain = readParam<float>(L, 3);
456         getServer(L)->fadeSound(handle, step, gain);
457         return 0;
458 }
459
460 // is_singleplayer()
461 int ModApiServer::l_is_singleplayer(lua_State *L)
462 {
463         NO_MAP_LOCK_REQUIRED;
464         lua_pushboolean(L, getServer(L)->isSingleplayer());
465         return 1;
466 }
467
468 // notify_authentication_modified(name)
469 int ModApiServer::l_notify_authentication_modified(lua_State *L)
470 {
471         NO_MAP_LOCK_REQUIRED;
472         std::string name;
473         if(lua_isstring(L, 1))
474                 name = readParam<std::string>(L, 1);
475         getServer(L)->reportPrivsModified(name);
476         return 0;
477 }
478
479 // get_last_run_mod()
480 int ModApiServer::l_get_last_run_mod(lua_State *L)
481 {
482         NO_MAP_LOCK_REQUIRED;
483         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
484         std::string current_mod = readParam<std::string>(L, -1, "");
485         if (current_mod.empty()) {
486                 lua_pop(L, 1);
487                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
488         }
489         return 1;
490 }
491
492 // set_last_run_mod(modname)
493 int ModApiServer::l_set_last_run_mod(lua_State *L)
494 {
495         NO_MAP_LOCK_REQUIRED;
496 #ifdef SCRIPTAPI_DEBUG
497         const char *mod = lua_tostring(L, 1);
498         getScriptApiBase(L)->setOriginDirect(mod);
499         //printf(">>>> last mod set from Lua: %s\n", mod);
500 #endif
501         return 0;
502 }
503
504 void ModApiServer::Initialize(lua_State *L, int top)
505 {
506         API_FCT(request_shutdown);
507         API_FCT(get_server_status);
508         API_FCT(get_server_uptime);
509         API_FCT(get_worldpath);
510         API_FCT(is_singleplayer);
511
512         API_FCT(get_current_modname);
513         API_FCT(get_modpath);
514         API_FCT(get_modnames);
515
516         API_FCT(print);
517
518         API_FCT(chat_send_all);
519         API_FCT(chat_send_player);
520         API_FCT(show_formspec);
521         API_FCT(sound_play);
522         API_FCT(sound_stop);
523         API_FCT(sound_fade);
524
525         API_FCT(get_player_information);
526         API_FCT(get_player_privs);
527         API_FCT(get_player_ip);
528         API_FCT(get_ban_list);
529         API_FCT(get_ban_description);
530         API_FCT(ban_player);
531         API_FCT(kick_player);
532         API_FCT(remove_player);
533         API_FCT(unban_player_or_ip);
534         API_FCT(notify_authentication_modified);
535
536         API_FCT(get_last_run_mod);
537         API_FCT(set_last_run_mod);
538 }