Fix various bugs (Anticheat, Lua helpers) (#8013)
[oweals/minetest.git] / src / script / lua_api / l_client.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "l_client.h"
22 #include "chatmessage.h"
23 #include "client/client.h"
24 #include "client/clientevent.h"
25 #include "client/sound.h"
26 #include "client/clientenvironment.h"
27 #include "common/c_content.h"
28 #include "common/c_converter.h"
29 #include "cpp_api/s_base.h"
30 #include "gettext.h"
31 #include "l_internal.h"
32 #include "lua_api/l_item.h"
33 #include "lua_api/l_nodemeta.h"
34 #include "gui/mainmenumanager.h"
35 #include "map.h"
36 #include "util/string.h"
37 #include "nodedef.h"
38
39 int ModApiClient::l_get_current_modname(lua_State *L)
40 {
41         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
42         return 1;
43 }
44
45 // get_last_run_mod()
46 int ModApiClient::l_get_last_run_mod(lua_State *L)
47 {
48         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
49         std::string current_mod = readParam<std::string>(L, -1, "");
50         if (current_mod.empty()) {
51                 lua_pop(L, 1);
52                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
53         }
54         return 1;
55 }
56
57 // set_last_run_mod(modname)
58 int ModApiClient::l_set_last_run_mod(lua_State *L)
59 {
60         if (!lua_isstring(L, 1))
61                 return 0;
62
63         const char *mod = lua_tostring(L, 1);
64         getScriptApiBase(L)->setOriginDirect(mod);
65         lua_pushboolean(L, true);
66         return 1;
67 }
68
69 // print(text)
70 int ModApiClient::l_print(lua_State *L)
71 {
72         NO_MAP_LOCK_REQUIRED;
73         std::string text = luaL_checkstring(L, 1);
74         rawstream << text << std::endl;
75         return 0;
76 }
77
78 // display_chat_message(message)
79 int ModApiClient::l_display_chat_message(lua_State *L)
80 {
81         if (!lua_isstring(L, 1))
82                 return 0;
83
84         std::string message = luaL_checkstring(L, 1);
85         getClient(L)->pushToChatQueue(new ChatMessage(utf8_to_wide(message)));
86         lua_pushboolean(L, true);
87         return 1;
88 }
89
90 // send_chat_message(message)
91 int ModApiClient::l_send_chat_message(lua_State *L)
92 {
93         if (!lua_isstring(L, 1))
94                 return 0;
95
96         // If server disabled this API, discard
97
98         // clang-format off
99         if (getClient(L)->checkCSMRestrictionFlag(
100                         CSMRestrictionFlags::CSM_RF_CHAT_MESSAGES))
101                 return 0;
102         // clang-format on
103
104         std::string message = luaL_checkstring(L, 1);
105         getClient(L)->sendChatMessage(utf8_to_wide(message));
106         return 0;
107 }
108
109 // clear_out_chat_queue()
110 int ModApiClient::l_clear_out_chat_queue(lua_State *L)
111 {
112         getClient(L)->clearOutChatQueue();
113         return 0;
114 }
115
116 // get_player_names()
117 int ModApiClient::l_get_player_names(lua_State *L)
118 {
119         // clang-format off
120         if (getClient(L)->checkCSMRestrictionFlag(
121                         CSMRestrictionFlags::CSM_RF_READ_PLAYERINFO)) {
122                 return 0;
123         }
124         // clang-format on
125
126         const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
127         lua_createtable(L, plist.size(), 0);
128         int newTable = lua_gettop(L);
129         int index = 1;
130         std::list<std::string>::const_iterator iter;
131         for (iter = plist.begin(); iter != plist.end(); ++iter) {
132                 lua_pushstring(L, (*iter).c_str());
133                 lua_rawseti(L, newTable, index);
134                 index++;
135         }
136         return 1;
137 }
138
139 // show_formspec(formspec)
140 int ModApiClient::l_show_formspec(lua_State *L)
141 {
142         if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
143                 return 0;
144
145         ClientEvent *event = new ClientEvent();
146         event->type = CE_SHOW_LOCAL_FORMSPEC;
147         event->show_formspec.formname = new std::string(luaL_checkstring(L, 1));
148         event->show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
149         getClient(L)->pushToEventQueue(event);
150         lua_pushboolean(L, true);
151         return 1;
152 }
153
154 // send_respawn()
155 int ModApiClient::l_send_respawn(lua_State *L)
156 {
157         getClient(L)->sendRespawn();
158         return 0;
159 }
160
161 // disconnect()
162 int ModApiClient::l_disconnect(lua_State *L)
163 {
164         // Stops badly written Lua code form causing boot loops
165         if (getClient(L)->isShutdown()) {
166                 lua_pushboolean(L, false);
167                 return 1;
168         }
169
170         g_gamecallback->disconnect();
171         lua_pushboolean(L, true);
172         return 1;
173 }
174
175 // gettext(text)
176 int ModApiClient::l_gettext(lua_State *L)
177 {
178         std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
179         lua_pushstring(L, text.c_str());
180
181         return 1;
182 }
183
184 // get_node(pos)
185 // pos = {x=num, y=num, z=num}
186 int ModApiClient::l_get_node_or_nil(lua_State *L)
187 {
188         // pos
189         v3s16 pos = read_v3s16(L, 1);
190
191         // Do it
192         bool pos_ok;
193         MapNode n = getClient(L)->getNode(pos, &pos_ok);
194         if (pos_ok) {
195                 // Return node
196                 pushnode(L, n, getClient(L)->ndef());
197         } else {
198                 lua_pushnil(L);
199         }
200         return 1;
201 }
202
203 int ModApiClient::l_get_language(lua_State *L)
204 {
205         char *locale = setlocale(LC_ALL, "");
206         lua_pushstring(L, locale);
207         return 1;
208 }
209
210 int ModApiClient::l_get_wielded_item(lua_State *L)
211 {
212         Client *client = getClient(L);
213
214         Inventory local_inventory(client->idef());
215         client->getLocalInventory(local_inventory);
216
217         InventoryList *mlist = local_inventory.getList("main");
218
219         if (mlist && client->getPlayerItem() < mlist->getSize()) {
220                 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
221         } else {
222                 LuaItemStack::create(L, ItemStack());
223         }
224         return 1;
225 }
226
227 // get_meta(pos)
228 int ModApiClient::l_get_meta(lua_State *L)
229 {
230         v3s16 p = read_v3s16(L, 1);
231         NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
232         NodeMetaRef::createClient(L, meta);
233         return 1;
234 }
235
236 int ModApiClient::l_sound_play(lua_State *L)
237 {
238         ISoundManager *sound = getClient(L)->getSoundManager();
239
240         SimpleSoundSpec spec;
241         read_soundspec(L, 1, spec);
242         float gain = 1.0f;
243         float pitch = 1.0f;
244         bool looped = false;
245         s32 handle;
246
247         if (lua_istable(L, 2)) {
248                 getfloatfield(L, 2, "gain", gain);
249                 getfloatfield(L, 2, "pitch", pitch);
250                 getboolfield(L, 2, "loop", looped);
251
252                 lua_getfield(L, 2, "pos");
253                 if (!lua_isnil(L, -1)) {
254                         v3f pos = read_v3f(L, -1) * BS;
255                         lua_pop(L, 1);
256                         handle = sound->playSoundAt(
257                                         spec.name, looped, gain * spec.gain, pos, pitch);
258                         lua_pushinteger(L, handle);
259                         return 1;
260                 }
261         }
262
263         handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
264         lua_pushinteger(L, handle);
265
266         return 1;
267 }
268
269 int ModApiClient::l_sound_stop(lua_State *L)
270 {
271         u32 handle = luaL_checkinteger(L, 1);
272
273         getClient(L)->getSoundManager()->stopSound(handle);
274
275         return 0;
276 }
277
278 // get_server_info()
279 int ModApiClient::l_get_server_info(lua_State *L)
280 {
281         Client *client = getClient(L);
282         Address serverAddress = client->getServerAddress();
283         lua_newtable(L);
284         lua_pushstring(L, client->getAddressName().c_str());
285         lua_setfield(L, -2, "address");
286         lua_pushstring(L, serverAddress.serializeString().c_str());
287         lua_setfield(L, -2, "ip");
288         lua_pushinteger(L, serverAddress.getPort());
289         lua_setfield(L, -2, "port");
290         lua_pushinteger(L, client->getProtoVersion());
291         lua_setfield(L, -2, "protocol_version");
292         return 1;
293 }
294
295 // get_item_def(itemstring)
296 int ModApiClient::l_get_item_def(lua_State *L)
297 {
298         IGameDef *gdef = getGameDef(L);
299         assert(gdef);
300
301         IItemDefManager *idef = gdef->idef();
302         assert(idef);
303
304         // clang-format off
305         if (getClient(L)->checkCSMRestrictionFlag(
306                         CSMRestrictionFlags::CSM_RF_READ_ITEMDEFS))
307                 return 0;
308         // clang-format on
309
310         if (!lua_isstring(L, 1))
311                 return 0;
312
313         std::string name = readParam<std::string>(L, 1);
314         if (!idef->isKnown(name))
315                 return 0;
316         const ItemDefinition &def = idef->get(name);
317
318         push_item_definition_full(L, def);
319
320         return 1;
321 }
322
323 // get_node_def(nodename)
324 int ModApiClient::l_get_node_def(lua_State *L)
325 {
326         IGameDef *gdef = getGameDef(L);
327         assert(gdef);
328
329         const NodeDefManager *ndef = gdef->ndef();
330         assert(ndef);
331
332         if (!lua_isstring(L, 1))
333                 return 0;
334
335         // clang-format off
336         if (getClient(L)->checkCSMRestrictionFlag(
337                         CSMRestrictionFlags::CSM_RF_READ_NODEDEFS))
338                 return 0;
339         // clang-format on
340
341         std::string name = readParam<std::string>(L, 1);
342         const ContentFeatures &cf = ndef->get(ndef->getId(name));
343         if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
344                 return 0;
345
346         push_content_features(L, cf);
347
348         return 1;
349 }
350
351 int ModApiClient::l_get_privilege_list(lua_State *L)
352 {
353         const Client *client = getClient(L);
354         lua_newtable(L);
355         for (const std::string &priv : client->getPrivilegeList()) {
356                 lua_pushboolean(L, true);
357                 lua_setfield(L, -2, priv.c_str());
358         }
359         return 1;
360 }
361
362 // get_builtin_path()
363 int ModApiClient::l_get_builtin_path(lua_State *L)
364 {
365         lua_pushstring(L, BUILTIN_MOD_NAME ":");
366         return 1;
367 }
368
369 void ModApiClient::Initialize(lua_State *L, int top)
370 {
371         API_FCT(get_current_modname);
372         API_FCT(print);
373         API_FCT(display_chat_message);
374         API_FCT(send_chat_message);
375         API_FCT(clear_out_chat_queue);
376         API_FCT(get_player_names);
377         API_FCT(set_last_run_mod);
378         API_FCT(get_last_run_mod);
379         API_FCT(show_formspec);
380         API_FCT(send_respawn);
381         API_FCT(gettext);
382         API_FCT(get_node_or_nil);
383         API_FCT(get_wielded_item);
384         API_FCT(disconnect);
385         API_FCT(get_meta);
386         API_FCT(sound_play);
387         API_FCT(sound_stop);
388         API_FCT(get_server_info);
389         API_FCT(get_item_def);
390         API_FCT(get_node_def);
391         API_FCT(get_privilege_list);
392         API_FCT(get_builtin_path);
393         API_FCT(get_language);
394 }