]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_server.cpp
Use "core" namespace internally
[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 "server.h"
25 #include "environment.h"
26 #include "player.h"
27
28 // request_shutdown()
29 int ModApiServer::l_request_shutdown(lua_State *L)
30 {
31         getServer(L)->requestShutdown();
32         return 0;
33 }
34
35 // get_server_status()
36 int ModApiServer::l_get_server_status(lua_State *L)
37 {
38         NO_MAP_LOCK_REQUIRED;
39         lua_pushstring(L, wide_to_narrow(getServer(L)->getStatusString()).c_str());
40         return 1;
41 }
42
43 // chat_send_all(text)
44 int ModApiServer::l_chat_send_all(lua_State *L)
45 {
46         NO_MAP_LOCK_REQUIRED;
47         const char *text = luaL_checkstring(L, 1);
48         // Get server from registry
49         Server *server = getServer(L);
50         // Send
51         server->notifyPlayers(narrow_to_wide(text));
52         return 0;
53 }
54
55 // chat_send_player(name, text)
56 int ModApiServer::l_chat_send_player(lua_State *L)
57 {
58         NO_MAP_LOCK_REQUIRED;
59         const char *name = luaL_checkstring(L, 1);
60         const char *text = luaL_checkstring(L, 2);
61
62         // Get server from registry
63         Server *server = getServer(L);
64         // Send
65         server->notifyPlayer(name, narrow_to_wide(text));
66         return 0;
67 }
68
69 // get_player_privs(name, text)
70 int ModApiServer::l_get_player_privs(lua_State *L)
71 {
72         NO_MAP_LOCK_REQUIRED;
73         const char *name = luaL_checkstring(L, 1);
74         // Get server from registry
75         Server *server = getServer(L);
76         // Do it
77         lua_newtable(L);
78         int table = lua_gettop(L);
79         std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
80         for(std::set<std::string>::const_iterator
81                         i = privs_s.begin(); i != privs_s.end(); i++){
82                 lua_pushboolean(L, true);
83                 lua_setfield(L, table, i->c_str());
84         }
85         lua_pushvalue(L, table);
86         return 1;
87 }
88
89 // get_player_ip()
90 int ModApiServer::l_get_player_ip(lua_State *L)
91 {
92         NO_MAP_LOCK_REQUIRED;
93         const char * name = luaL_checkstring(L, 1);
94         Player *player = getEnv(L)->getPlayer(name);
95         if(player == NULL)
96         {
97                 lua_pushnil(L); // no such player
98                 return 1;
99         }
100         try
101         {
102                 Address addr = getServer(L)->getPeerAddress(player->peer_id);
103                 std::string ip_str = addr.serializeString();
104                 lua_pushstring(L, ip_str.c_str());
105                 return 1;
106         }
107         catch(con::PeerNotFoundException) // unlikely
108         {
109                 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
110                 lua_pushnil(L); // error
111                 return 1;
112         }
113 }
114
115 // get_player_information()
116 int ModApiServer::l_get_player_information(lua_State *L)
117 {
118
119         NO_MAP_LOCK_REQUIRED;
120         const char * name = luaL_checkstring(L, 1);
121         Player *player = getEnv(L)->getPlayer(name);
122         if(player == NULL)
123         {
124                 lua_pushnil(L); // no such player
125                 return 1;
126         }
127
128         Address addr;
129         try
130         {
131                 addr = getServer(L)->getPeerAddress(player->peer_id);
132         }
133         catch(con::PeerNotFoundException) // unlikely
134         {
135                 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
136                 lua_pushnil(L); // error
137                 return 1;
138         }
139
140         float min_rtt,max_rtt,avg_rtt,min_jitter,max_jitter,avg_jitter;
141         ClientState state;
142         u32 uptime;
143         u16 prot_vers;
144         u8 ser_vers,major,minor,patch;
145         std::string vers_string;
146
147 #define ERET(code)                                                             \
148         if (!(code)) {                                                             \
149                 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;     \
150                 lua_pushnil(L); /* error */                                            \
151                 return 1;                                                              \
152         }
153
154         ERET(getServer(L)->getClientConInfo(player->peer_id,con::MIN_RTT,&min_rtt))
155         ERET(getServer(L)->getClientConInfo(player->peer_id,con::MAX_RTT,&max_rtt))
156         ERET(getServer(L)->getClientConInfo(player->peer_id,con::AVG_RTT,&avg_rtt))
157         ERET(getServer(L)->getClientConInfo(player->peer_id,con::MIN_JITTER,&min_jitter))
158         ERET(getServer(L)->getClientConInfo(player->peer_id,con::MAX_JITTER,&max_jitter))
159         ERET(getServer(L)->getClientConInfo(player->peer_id,con::AVG_JITTER,&avg_jitter))
160
161         ERET(getServer(L)->getClientInfo(player->peer_id,
162                                                                                 &state, &uptime, &ser_vers, &prot_vers,
163                                                                                 &major, &minor, &patch, &vers_string))
164
165         lua_newtable(L);
166         int table = lua_gettop(L);
167
168         lua_pushstring(L,"address");
169         lua_pushstring(L, addr.serializeString().c_str());
170         lua_settable(L, table);
171
172         lua_pushstring(L,"ip_version");
173         if (addr.getFamily() == AF_INET) {
174                 lua_pushnumber(L, 4);
175         } else if (addr.getFamily() == AF_INET6) {
176                 lua_pushnumber(L, 6);
177         } else {
178                 lua_pushnumber(L, 0);
179         }
180         lua_settable(L, table);
181
182         lua_pushstring(L,"min_rtt");
183         lua_pushnumber(L, min_rtt);
184         lua_settable(L, table);
185
186         lua_pushstring(L,"max_rtt");
187         lua_pushnumber(L, max_rtt);
188         lua_settable(L, table);
189
190         lua_pushstring(L,"avg_rtt");
191         lua_pushnumber(L, avg_rtt);
192         lua_settable(L, table);
193
194         lua_pushstring(L,"min_jitter");
195         lua_pushnumber(L, min_jitter);
196         lua_settable(L, table);
197
198         lua_pushstring(L,"max_jitter");
199         lua_pushnumber(L, max_jitter);
200         lua_settable(L, table);
201
202         lua_pushstring(L,"avg_jitter");
203         lua_pushnumber(L, avg_jitter);
204         lua_settable(L, table);
205
206         lua_pushstring(L,"connection_uptime");
207         lua_pushnumber(L, uptime);
208         lua_settable(L, table);
209
210 #ifndef NDEBUG
211         lua_pushstring(L,"serialization_version");
212         lua_pushnumber(L, ser_vers);
213         lua_settable(L, table);
214
215         lua_pushstring(L,"protocol_version");
216         lua_pushnumber(L, prot_vers);
217         lua_settable(L, table);
218
219         lua_pushstring(L,"major");
220         lua_pushnumber(L, major);
221         lua_settable(L, table);
222
223         lua_pushstring(L,"minor");
224         lua_pushnumber(L, minor);
225         lua_settable(L, table);
226
227         lua_pushstring(L,"patch");
228         lua_pushnumber(L, patch);
229         lua_settable(L, table);
230
231         lua_pushstring(L,"version_string");
232         lua_pushstring(L, vers_string.c_str());
233         lua_settable(L, table);
234
235         lua_pushstring(L,"state");
236         lua_pushstring(L,ClientInterface::state2Name(state).c_str());
237         lua_settable(L, table);
238 #endif
239
240 #undef ERET
241         return 1;
242 }
243
244 // get_ban_list()
245 int ModApiServer::l_get_ban_list(lua_State *L)
246 {
247         NO_MAP_LOCK_REQUIRED;
248         lua_pushstring(L, getServer(L)->getBanDescription("").c_str());
249         return 1;
250 }
251
252 // get_ban_description()
253 int ModApiServer::l_get_ban_description(lua_State *L)
254 {
255         NO_MAP_LOCK_REQUIRED;
256         const char * ip_or_name = luaL_checkstring(L, 1);
257         lua_pushstring(L, getServer(L)->getBanDescription(std::string(ip_or_name)).c_str());
258         return 1;
259 }
260
261 // ban_player()
262 int ModApiServer::l_ban_player(lua_State *L)
263 {
264         NO_MAP_LOCK_REQUIRED;
265         const char * name = luaL_checkstring(L, 1);
266         Player *player = getEnv(L)->getPlayer(name);
267         if(player == NULL)
268         {
269                 lua_pushboolean(L, false); // no such player
270                 return 1;
271         }
272         try
273         {
274                 Address addr = getServer(L)->getPeerAddress(getEnv(L)->getPlayer(name)->peer_id);
275                 std::string ip_str = addr.serializeString();
276                 getServer(L)->setIpBanned(ip_str, name);
277         }
278         catch(con::PeerNotFoundException) // unlikely
279         {
280                 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
281                 lua_pushboolean(L, false); // error
282                 return 1;
283         }
284         lua_pushboolean(L, true);
285         return 1;
286 }
287
288 // kick_player(name, [reason]) -> success
289 int ModApiServer::l_kick_player(lua_State *L)
290 {
291         NO_MAP_LOCK_REQUIRED;
292         const char *name = luaL_checkstring(L, 1);
293         std::string message;
294         if (lua_isstring(L, 2))
295         {
296                 message = std::string("Kicked: ") + lua_tostring(L, 2);
297         }
298         else
299         {
300                 message = "Kicked.";
301         }
302         Player *player = getEnv(L)->getPlayer(name);
303         if (player == NULL)
304         {
305                 lua_pushboolean(L, false); // No such player
306                 return 1;
307         }
308         getServer(L)->DenyAccess(player->peer_id, narrow_to_wide(message));
309         lua_pushboolean(L, true);
310         return 1;
311 }
312
313 // unban_player_or_ip()
314 int ModApiServer::l_unban_player_or_ip(lua_State *L)
315 {
316         NO_MAP_LOCK_REQUIRED;
317         const char * ip_or_name = luaL_checkstring(L, 1);
318         getServer(L)->unsetIpBanned(ip_or_name);
319         lua_pushboolean(L, true);
320         return 1;
321 }
322
323 // show_formspec(playername,formname,formspec)
324 int ModApiServer::l_show_formspec(lua_State *L)
325 {
326         NO_MAP_LOCK_REQUIRED;
327         const char *playername = luaL_checkstring(L, 1);
328         const char *formname = luaL_checkstring(L, 2);
329         const char *formspec = luaL_checkstring(L, 3);
330
331         if(getServer(L)->showFormspec(playername,formspec,formname))
332         {
333                 lua_pushboolean(L, true);
334         }else{
335                 lua_pushboolean(L, false);
336         }
337         return 1;
338 }
339
340 // get_current_modname()
341 int ModApiServer::l_get_current_modname(lua_State *L)
342 {
343         NO_MAP_LOCK_REQUIRED;
344         lua_getfield(L, LUA_REGISTRYINDEX, "current_modname");
345         return 1;
346 }
347
348 // get_modpath(modname)
349 int ModApiServer::l_get_modpath(lua_State *L)
350 {
351         NO_MAP_LOCK_REQUIRED;
352         std::string modname = luaL_checkstring(L, 1);
353         const ModSpec *mod = getServer(L)->getModSpec(modname);
354         if (!mod) {
355                 lua_pushnil(L);
356                 return 1;
357         }
358         lua_pushstring(L, mod->path.c_str());
359         return 1;
360 }
361
362 // get_modnames()
363 // the returned list is sorted alphabetically for you
364 int ModApiServer::l_get_modnames(lua_State *L)
365 {
366         NO_MAP_LOCK_REQUIRED;
367
368         // Get a list of mods
369         std::list<std::string> mods_unsorted, mods_sorted;
370         getServer(L)->getModNames(mods_unsorted);
371
372         // Take unsorted items from mods_unsorted and sort them into
373         // mods_sorted; not great performance but the number of mods on a
374         // server will likely be small.
375         for(std::list<std::string>::iterator i = mods_unsorted.begin();
376                         i != mods_unsorted.end(); ++i) {
377                 bool added = false;
378                 for(std::list<std::string>::iterator x = mods_sorted.begin();
379                                 x != mods_sorted.end(); ++x) {
380                         // I doubt anybody using Minetest will be using
381                         // anything not ASCII based :)
382                         if(i->compare(*x) <= 0) {
383                                 mods_sorted.insert(x, *i);
384                                 added = true;
385                                 break;
386                         }
387                 }
388                 if(!added)
389                         mods_sorted.push_back(*i);
390         }
391
392         // Package them up for Lua
393         lua_createtable(L, mods_sorted.size(), 0);
394         std::list<std::string>::iterator iter = mods_sorted.begin();
395         for (u16 i = 0; iter != mods_sorted.end(); iter++) {
396                 lua_pushstring(L, iter->c_str());
397                 lua_rawseti(L, -2, ++i);
398         }
399         return 1;
400 }
401
402 // get_worldpath()
403 int ModApiServer::l_get_worldpath(lua_State *L)
404 {
405         NO_MAP_LOCK_REQUIRED;
406         std::string worldpath = getServer(L)->getWorldPath();
407         lua_pushstring(L, worldpath.c_str());
408         return 1;
409 }
410
411 // sound_play(spec, parameters)
412 int ModApiServer::l_sound_play(lua_State *L)
413 {
414         NO_MAP_LOCK_REQUIRED;
415         SimpleSoundSpec spec;
416         read_soundspec(L, 1, spec);
417         ServerSoundParams params;
418         read_server_sound_params(L, 2, params);
419         s32 handle = getServer(L)->playSound(spec, params);
420         lua_pushinteger(L, handle);
421         return 1;
422 }
423
424 // sound_stop(handle)
425 int ModApiServer::l_sound_stop(lua_State *L)
426 {
427         NO_MAP_LOCK_REQUIRED;
428         int handle = luaL_checkinteger(L, 1);
429         getServer(L)->stopSound(handle);
430         return 0;
431 }
432
433 // is_singleplayer()
434 int ModApiServer::l_is_singleplayer(lua_State *L)
435 {
436         NO_MAP_LOCK_REQUIRED;
437         lua_pushboolean(L, getServer(L)->isSingleplayer());
438         return 1;
439 }
440
441 // notify_authentication_modified(name)
442 int ModApiServer::l_notify_authentication_modified(lua_State *L)
443 {
444         NO_MAP_LOCK_REQUIRED;
445         std::string name = "";
446         if(lua_isstring(L, 1))
447                 name = lua_tostring(L, 1);
448         getServer(L)->reportPrivsModified(name);
449         return 0;
450 }
451
452 void ModApiServer::Initialize(lua_State *L, int top)
453 {
454         API_FCT(request_shutdown);
455         API_FCT(get_server_status);
456         API_FCT(get_worldpath);
457         API_FCT(is_singleplayer);
458
459         API_FCT(get_current_modname);
460         API_FCT(get_modpath);
461         API_FCT(get_modnames);
462
463         API_FCT(chat_send_all);
464         API_FCT(chat_send_player);
465         API_FCT(show_formspec);
466         API_FCT(sound_play);
467         API_FCT(sound_stop);
468
469         API_FCT(get_player_information);
470         API_FCT(get_player_privs);
471         API_FCT(get_player_ip);
472         API_FCT(get_ban_list);
473         API_FCT(get_ban_description);
474         API_FCT(ban_player);
475         API_FCT(kick_player);
476         API_FCT(unban_player_or_ip);
477         API_FCT(notify_authentication_modified);
478 }