]> git.lizzy.rs Git - dragonfireclient.git/blob - src/scriptapi.cpp
split scriptapi.cpp
[dragonfireclient.git] / src / scriptapi.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 "scriptapi.h"
21
22 #include <iostream>
23 #include <list>
24 extern "C" {
25 #include <lua.h>
26 #include <lualib.h>
27 #include <lauxlib.h>
28 }
29
30 #include "settings.h" // For accessing g_settings
31 #include "main.h" // For g_settings
32 #include "biome.h"
33 #include "script.h"
34 #include "rollback.h"
35
36 #include "scriptapi_types.h"
37 #include "scriptapi_env.h"
38 #include "scriptapi_nodetimer.h"
39 #include "scriptapi_inventory.h"
40 #include "scriptapi_nodemeta.h"
41 #include "scriptapi_object.h"
42 #include "scriptapi_noise.h"
43 #include "scriptapi_common.h"
44 #include "scriptapi_item.h"
45 #include "scriptapi_content.h"
46 #include "scriptapi_craft.h"
47
48 /*****************************************************************************/
49 /* Mod related                                                               */
50 /*****************************************************************************/
51
52 class ModNameStorer
53 {
54 private:
55         lua_State *L;
56 public:
57         ModNameStorer(lua_State *L_, const std::string modname):
58                 L(L_)
59         {
60                 // Store current modname in registry
61                 lua_pushstring(L, modname.c_str());
62                 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
63         }
64         ~ModNameStorer()
65         {
66                 // Clear current modname in registry
67                 lua_pushnil(L);
68                 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
69         }
70 };
71
72 bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,
73                 const std::string &modname)
74 {
75         ModNameStorer modnamestorer(L, modname);
76
77         if(!string_allowed(modname, "abcdefghijklmnopqrstuvwxyz"
78                         "0123456789_")){
79                 errorstream<<"Error loading mod \""<<modname
80                                 <<"\": modname does not follow naming conventions: "
81                                 <<"Only chararacters [a-z0-9_] are allowed."<<std::endl;
82                 return false;
83         }
84
85         bool success = false;
86
87         try{
88                 success = script_load(L, scriptpath.c_str());
89         }
90         catch(LuaError &e){
91                 errorstream<<"Error loading mod \""<<modname
92                                 <<"\": "<<e.what()<<std::endl;
93         }
94
95         return success;
96 }
97
98
99 /*****************************************************************************/
100 /* Auth                                                                      */
101 /*****************************************************************************/
102
103 /*
104         Privileges
105 */
106 static void read_privileges(lua_State *L, int index,
107                 std::set<std::string> &result)
108 {
109         result.clear();
110         lua_pushnil(L);
111         if(index < 0)
112                 index -= 1;
113         while(lua_next(L, index) != 0){
114                 // key at index -2 and value at index -1
115                 std::string key = luaL_checkstring(L, -2);
116                 bool value = lua_toboolean(L, -1);
117                 if(value)
118                         result.insert(key);
119                 // removes value, keeps key for next iteration
120                 lua_pop(L, 1);
121         }
122 }
123
124 static void get_auth_handler(lua_State *L)
125 {
126         lua_getglobal(L, "minetest");
127         lua_getfield(L, -1, "registered_auth_handler");
128         if(lua_isnil(L, -1)){
129                 lua_pop(L, 1);
130                 lua_getfield(L, -1, "builtin_auth_handler");
131         }
132         if(lua_type(L, -1) != LUA_TTABLE)
133                 throw LuaError(L, "Authentication handler table not valid");
134 }
135
136 bool scriptapi_get_auth(lua_State *L, const std::string &playername,
137                 std::string *dst_password, std::set<std::string> *dst_privs)
138 {
139         realitycheck(L);
140         assert(lua_checkstack(L, 20));
141         StackUnroller stack_unroller(L);
142
143         get_auth_handler(L);
144         lua_getfield(L, -1, "get_auth");
145         if(lua_type(L, -1) != LUA_TFUNCTION)
146                 throw LuaError(L, "Authentication handler missing get_auth");
147         lua_pushstring(L, playername.c_str());
148         if(lua_pcall(L, 1, 1, 0))
149                 script_error(L, "error: %s", lua_tostring(L, -1));
150
151         // nil = login not allowed
152         if(lua_isnil(L, -1))
153                 return false;
154         luaL_checktype(L, -1, LUA_TTABLE);
155
156         std::string password;
157         bool found = getstringfield(L, -1, "password", password);
158         if(!found)
159                 throw LuaError(L, "Authentication handler didn't return password");
160         if(dst_password)
161                 *dst_password = password;
162
163         lua_getfield(L, -1, "privileges");
164         if(!lua_istable(L, -1))
165                 throw LuaError(L,
166                                 "Authentication handler didn't return privilege table");
167         if(dst_privs)
168                 read_privileges(L, -1, *dst_privs);
169         lua_pop(L, 1);
170
171         return true;
172 }
173
174 void scriptapi_create_auth(lua_State *L, const std::string &playername,
175                 const std::string &password)
176 {
177         realitycheck(L);
178         assert(lua_checkstack(L, 20));
179         StackUnroller stack_unroller(L);
180
181         get_auth_handler(L);
182         lua_getfield(L, -1, "create_auth");
183         if(lua_type(L, -1) != LUA_TFUNCTION)
184                 throw LuaError(L, "Authentication handler missing create_auth");
185         lua_pushstring(L, playername.c_str());
186         lua_pushstring(L, password.c_str());
187         if(lua_pcall(L, 2, 0, 0))
188                 script_error(L, "error: %s", lua_tostring(L, -1));
189 }
190
191 bool scriptapi_set_password(lua_State *L, const std::string &playername,
192                 const std::string &password)
193 {
194         realitycheck(L);
195         assert(lua_checkstack(L, 20));
196         StackUnroller stack_unroller(L);
197
198         get_auth_handler(L);
199         lua_getfield(L, -1, "set_password");
200         if(lua_type(L, -1) != LUA_TFUNCTION)
201                 throw LuaError(L, "Authentication handler missing set_password");
202         lua_pushstring(L, playername.c_str());
203         lua_pushstring(L, password.c_str());
204         if(lua_pcall(L, 2, 1, 0))
205                 script_error(L, "error: %s", lua_tostring(L, -1));
206         return lua_toboolean(L, -1);
207 }
208
209 /*****************************************************************************/
210 /* Misc                                                                      */
211 /*****************************************************************************/
212 /*
213         Groups
214 */
215 void read_groups(lua_State *L, int index,
216                 std::map<std::string, int> &result)
217 {
218         if (!lua_istable(L,index))
219                 return;
220         result.clear();
221         lua_pushnil(L);
222         if(index < 0)
223                 index -= 1;
224         while(lua_next(L, index) != 0){
225                 // key at index -2 and value at index -1
226                 std::string name = luaL_checkstring(L, -2);
227                 int rating = luaL_checkinteger(L, -1);
228                 result[name] = rating;
229                 // removes value, keeps key for next iteration
230                 lua_pop(L, 1);
231         }
232 }
233
234 struct EnumString es_BiomeTerrainType[] =
235 {
236         {BIOME_TERRAIN_NORMAL, "normal"},
237         {BIOME_TERRAIN_LIQUID, "liquid"},
238         {BIOME_TERRAIN_NETHER, "nether"},
239         {BIOME_TERRAIN_AETHER, "aether"},
240         {BIOME_TERRAIN_FLAT,   "flat"},
241         {0, NULL},
242 };
243
244 /*****************************************************************************/
245 /* Parameters                                                                */
246 /*****************************************************************************/
247 /*
248         DigParams
249 */
250
251 static void set_dig_params(lua_State *L, int table,
252                 const DigParams &params)
253 {
254         setboolfield(L, table, "diggable", params.diggable);
255         setfloatfield(L, table, "time", params.time);
256         setintfield(L, table, "wear", params.wear);
257 }
258
259 static void push_dig_params(lua_State *L,
260                 const DigParams &params)
261 {
262         lua_newtable(L);
263         set_dig_params(L, -1, params);
264 }
265
266 /*
267         HitParams
268 */
269
270 static void set_hit_params(lua_State *L, int table,
271                 const HitParams &params)
272 {
273         setintfield(L, table, "hp", params.hp);
274         setintfield(L, table, "wear", params.wear);
275 }
276
277 static void push_hit_params(lua_State *L,
278                 const HitParams &params)
279 {
280         lua_newtable(L);
281         set_hit_params(L, -1, params);
282 }
283
284 /*
285         ServerSoundParams
286 */
287
288 static void read_server_sound_params(lua_State *L, int index,
289                 ServerSoundParams &params)
290 {
291         if(index < 0)
292                 index = lua_gettop(L) + 1 + index;
293         // Clear
294         params = ServerSoundParams();
295         if(lua_istable(L, index)){
296                 getfloatfield(L, index, "gain", params.gain);
297                 getstringfield(L, index, "to_player", params.to_player);
298                 lua_getfield(L, index, "pos");
299                 if(!lua_isnil(L, -1)){
300                         v3f p = read_v3f(L, -1)*BS;
301                         params.pos = p;
302                         params.type = ServerSoundParams::SSP_POSITIONAL;
303                 }
304                 lua_pop(L, 1);
305                 lua_getfield(L, index, "object");
306                 if(!lua_isnil(L, -1)){
307                         ObjectRef *ref = ObjectRef::checkobject(L, -1);
308                         ServerActiveObject *sao = ObjectRef::getobject(ref);
309                         if(sao){
310                                 params.object = sao->getId();
311                                 params.type = ServerSoundParams::SSP_OBJECT;
312                         }
313                 }
314                 lua_pop(L, 1);
315                 params.max_hear_distance = BS*getfloatfield_default(L, index,
316                                 "max_hear_distance", params.max_hear_distance/BS);
317                 getboolfield(L, index, "loop", params.loop);
318         }
319 }
320
321 /*****************************************************************************/
322 /* callbacks                                                                 */
323 /*****************************************************************************/
324
325 // Push the list of callbacks (a lua table).
326 // Then push nargs arguments.
327 // Then call this function, which
328 // - runs the callbacks
329 // - removes the table and arguments from the lua stack
330 // - pushes the return value, computed depending on mode
331 void scriptapi_run_callbacks(lua_State *L, int nargs,
332                 RunCallbacksMode mode)
333 {
334         // Insert the return value into the lua stack, below the table
335         assert(lua_gettop(L) >= nargs + 1);
336         lua_pushnil(L);
337         lua_insert(L, -(nargs + 1) - 1);
338         // Stack now looks like this:
339         // ... <return value = nil> <table> <arg#1> <arg#2> ... <arg#n>
340
341         int rv = lua_gettop(L) - nargs - 1;
342         int table = rv + 1;
343         int arg = table + 1;
344
345         luaL_checktype(L, table, LUA_TTABLE);
346
347         // Foreach
348         lua_pushnil(L);
349         bool first_loop = true;
350         while(lua_next(L, table) != 0){
351                 // key at index -2 and value at index -1
352                 luaL_checktype(L, -1, LUA_TFUNCTION);
353                 // Call function
354                 for(int i = 0; i < nargs; i++)
355                         lua_pushvalue(L, arg+i);
356                 if(lua_pcall(L, nargs, 1, 0))
357                         script_error(L, "error: %s", lua_tostring(L, -1));
358
359                 // Move return value to designated space in stack
360                 // Or pop it
361                 if(first_loop){
362                         // Result of first callback is always moved
363                         lua_replace(L, rv);
364                         first_loop = false;
365                 } else {
366                         // Otherwise, what happens depends on the mode
367                         if(mode == RUN_CALLBACKS_MODE_FIRST)
368                                 lua_pop(L, 1);
369                         else if(mode == RUN_CALLBACKS_MODE_LAST)
370                                 lua_replace(L, rv);
371                         else if(mode == RUN_CALLBACKS_MODE_AND ||
372                                         mode == RUN_CALLBACKS_MODE_AND_SC){
373                                 if((bool)lua_toboolean(L, rv) == true &&
374                                                 (bool)lua_toboolean(L, -1) == false)
375                                         lua_replace(L, rv);
376                                 else
377                                         lua_pop(L, 1);
378                         }
379                         else if(mode == RUN_CALLBACKS_MODE_OR ||
380                                         mode == RUN_CALLBACKS_MODE_OR_SC){
381                                 if((bool)lua_toboolean(L, rv) == false &&
382                                                 (bool)lua_toboolean(L, -1) == true)
383                                         lua_replace(L, rv);
384                                 else
385                                         lua_pop(L, 1);
386                         }
387                         else
388                                 assert(0);
389                 }
390
391                 // Handle short circuit modes
392                 if(mode == RUN_CALLBACKS_MODE_AND_SC &&
393                                 (bool)lua_toboolean(L, rv) == false)
394                         break;
395                 else if(mode == RUN_CALLBACKS_MODE_OR_SC &&
396                                 (bool)lua_toboolean(L, rv) == true)
397                         break;
398
399                 // value removed, keep key for next iteration
400         }
401
402         // Remove stuff from stack, leaving only the return value
403         lua_settop(L, rv);
404
405         // Fix return value in case no callbacks were called
406         if(first_loop){
407                 if(mode == RUN_CALLBACKS_MODE_AND ||
408                                 mode == RUN_CALLBACKS_MODE_AND_SC){
409                         lua_pop(L, 1);
410                         lua_pushboolean(L, true);
411                 }
412                 else if(mode == RUN_CALLBACKS_MODE_OR ||
413                                 mode == RUN_CALLBACKS_MODE_OR_SC){
414                         lua_pop(L, 1);
415                         lua_pushboolean(L, false);
416                 }
417         }
418 }
419
420 bool scriptapi_on_chat_message(lua_State *L, const std::string &name,
421                 const std::string &message)
422 {
423         realitycheck(L);
424         assert(lua_checkstack(L, 20));
425         StackUnroller stack_unroller(L);
426
427         // Get minetest.registered_on_chat_messages
428         lua_getglobal(L, "minetest");
429         lua_getfield(L, -1, "registered_on_chat_messages");
430         // Call callbacks
431         lua_pushstring(L, name.c_str());
432         lua_pushstring(L, message.c_str());
433         scriptapi_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
434         bool ate = lua_toboolean(L, -1);
435         return ate;
436 }
437
438 void scriptapi_on_shutdown(lua_State *L)
439 {
440         realitycheck(L);
441         assert(lua_checkstack(L, 20));
442         StackUnroller stack_unroller(L);
443
444         // Get registered shutdown hooks
445         lua_getglobal(L, "minetest");
446         lua_getfield(L, -1, "registered_on_shutdown");
447         // Call callbacks
448         scriptapi_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
449 }
450
451 void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player)
452 {
453         realitycheck(L);
454         assert(lua_checkstack(L, 20));
455         StackUnroller stack_unroller(L);
456
457         // Get minetest.registered_on_newplayers
458         lua_getglobal(L, "minetest");
459         lua_getfield(L, -1, "registered_on_newplayers");
460         // Call callbacks
461         objectref_get_or_create(L, player);
462         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
463 }
464
465 void scriptapi_on_dieplayer(lua_State *L, ServerActiveObject *player)
466 {
467         realitycheck(L);
468         assert(lua_checkstack(L, 20));
469         StackUnroller stack_unroller(L);
470
471         // Get minetest.registered_on_dieplayers
472         lua_getglobal(L, "minetest");
473         lua_getfield(L, -1, "registered_on_dieplayers");
474         // Call callbacks
475         objectref_get_or_create(L, player);
476         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
477 }
478
479 bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player)
480 {
481         realitycheck(L);
482         assert(lua_checkstack(L, 20));
483         StackUnroller stack_unroller(L);
484
485         // Get minetest.registered_on_respawnplayers
486         lua_getglobal(L, "minetest");
487         lua_getfield(L, -1, "registered_on_respawnplayers");
488         // Call callbacks
489         objectref_get_or_create(L, player);
490         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
491         bool positioning_handled_by_some = lua_toboolean(L, -1);
492         return positioning_handled_by_some;
493 }
494
495 void scriptapi_on_joinplayer(lua_State *L, ServerActiveObject *player)
496 {
497         realitycheck(L);
498         assert(lua_checkstack(L, 20));
499         StackUnroller stack_unroller(L);
500
501         // Get minetest.registered_on_joinplayers
502         lua_getglobal(L, "minetest");
503         lua_getfield(L, -1, "registered_on_joinplayers");
504         // Call callbacks
505         objectref_get_or_create(L, player);
506         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
507 }
508
509 void scriptapi_on_leaveplayer(lua_State *L, ServerActiveObject *player)
510 {
511         realitycheck(L);
512         assert(lua_checkstack(L, 20));
513         StackUnroller stack_unroller(L);
514
515         // Get minetest.registered_on_leaveplayers
516         lua_getglobal(L, "minetest");
517         lua_getfield(L, -1, "registered_on_leaveplayers");
518         // Call callbacks
519         objectref_get_or_create(L, player);
520         scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
521 }
522
523 /*
524         player
525 */
526 void scriptapi_on_player_receive_fields(lua_State *L,
527                 ServerActiveObject *player,
528                 const std::string &formname,
529                 const std::map<std::string, std::string> &fields)
530 {
531         realitycheck(L);
532         assert(lua_checkstack(L, 20));
533         StackUnroller stack_unroller(L);
534
535         // Get minetest.registered_on_chat_messages
536         lua_getglobal(L, "minetest");
537         lua_getfield(L, -1, "registered_on_player_receive_fields");
538         // Call callbacks
539         // param 1
540         objectref_get_or_create(L, player);
541         // param 2
542         lua_pushstring(L, formname.c_str());
543         // param 3
544         lua_newtable(L);
545         for(std::map<std::string, std::string>::const_iterator
546                         i = fields.begin(); i != fields.end(); i++){
547                 const std::string &name = i->first;
548                 const std::string &value = i->second;
549                 lua_pushstring(L, name.c_str());
550                 lua_pushlstring(L, value.c_str(), value.size());
551                 lua_settable(L, -3);
552         }
553         scriptapi_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
554 }
555 /*****************************************************************************/
556 /* Api functions                                                             */
557 /*****************************************************************************/
558
559 // debug(text)
560 // Writes a line to dstream
561 static int l_debug(lua_State *L)
562 {
563         std::string text = lua_tostring(L, 1);
564         dstream << text << std::endl;
565         return 0;
566 }
567
568 // log([level,] text)
569 // Writes a line to the logger.
570 // The one-argument version logs to infostream.
571 // The two-argument version accept a log level: error, action, info, or verbose.
572 static int l_log(lua_State *L)
573 {
574         std::string text;
575         LogMessageLevel level = LMT_INFO;
576         if(lua_isnone(L, 2))
577         {
578                 text = lua_tostring(L, 1);
579         }
580         else
581         {
582                 std::string levelname = luaL_checkstring(L, 1);
583                 text = luaL_checkstring(L, 2);
584                 if(levelname == "error")
585                         level = LMT_ERROR;
586                 else if(levelname == "action")
587                         level = LMT_ACTION;
588                 else if(levelname == "verbose")
589                         level = LMT_VERBOSE;
590         }
591         log_printline(level, text);
592         return 0;
593 }
594
595 // request_shutdown()
596 static int l_request_shutdown(lua_State *L)
597 {
598         get_server(L)->requestShutdown();
599         return 0;
600 }
601
602 // get_server_status()
603 static int l_get_server_status(lua_State *L)
604 {
605         lua_pushstring(L, wide_to_narrow(get_server(L)->getStatusString()).c_str());
606         return 1;
607 }
608
609 // register_biome_groups({frequencies})
610 static int l_register_biome_groups(lua_State *L)
611 {
612         luaL_checktype(L, 1, LUA_TTABLE);
613         int index = 1;
614         if (!lua_istable(L, index))
615                 throw LuaError(L, "register_biome_groups: parameter is not a table");
616
617         BiomeDefManager *bmgr = get_server(L)->getBiomeDef();
618         if (!bmgr) {
619                 verbosestream << "register_biome_groups: BiomeDefManager not active" << std::endl;
620                 return 0;
621         }
622
623         lua_pushnil(L);
624         for (int i = 1; lua_next(L, index) != 0; i++) {
625                 bmgr->addBiomeGroup(lua_tonumber(L, -1));
626                 lua_pop(L, 1);
627         }
628         lua_pop(L, 1);
629
630         return 0;
631 }
632
633 // register_biome({lots of stuff})
634 static int l_register_biome(lua_State *L)
635 {
636         luaL_checktype(L, 1, LUA_TTABLE);
637         int index = 1, groupid;
638         std::string nodename;
639
640         IWritableNodeDefManager *ndef = get_server(L)->getWritableNodeDefManager();
641         BiomeDefManager *bmgr = get_server(L)->getBiomeDef();
642         if (!bmgr) {
643                 verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
644                 return 0;
645         }
646
647         groupid = getintfield_default(L, index, "group_id", 0);
648
649         enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
650                                         "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
651         Biome *b = bmgr->createBiome(terrain);
652
653         b->name = getstringfield_default(L, index, "name", "");
654
655         if (getstringfield(L, index, "node_top", nodename))
656                 b->n_top = MapNode(ndef->getId(nodename));
657         else
658                 b->n_top = MapNode(CONTENT_IGNORE);
659
660         if (getstringfield(L, index, "node_filler", nodename))
661                 b->n_filler = MapNode(ndef->getId(nodename));
662         else
663                 b->n_filler = b->n_top;
664
665         b->ntopnodes = getintfield_default(L, index, "num_top_nodes", 0);
666
667         b->height_min   = getintfield_default(L, index, "height_min", 0);
668         b->height_max   = getintfield_default(L, index, "height_max", 0);
669         b->heat_min     = getfloatfield_default(L, index, "heat_min", 0.);
670         b->heat_max     = getfloatfield_default(L, index, "heat_max", 0.);
671         b->humidity_min = getfloatfield_default(L, index, "humidity_min", 0.);
672         b->humidity_max = getfloatfield_default(L, index, "humidity_max", 0.);
673
674         b->np = new NoiseParams; // should read an entire NoiseParams later on...
675         getfloatfield(L, index, "scale", b->np->scale);
676         getfloatfield(L, index, "offset", b->np->offset);
677
678         b->groupid = (s8)groupid;
679         b->flags   = 0; //reserved
680
681         bmgr->addBiome(b);
682
683         verbosestream << "register_biome: " << b->name << std::endl;
684         return 0;
685 }
686
687
688
689 // setting_set(name, value)
690 static int l_setting_set(lua_State *L)
691 {
692         const char *name = luaL_checkstring(L, 1);
693         const char *value = luaL_checkstring(L, 2);
694         g_settings->set(name, value);
695         return 0;
696 }
697
698 // setting_get(name)
699 static int l_setting_get(lua_State *L)
700 {
701         const char *name = luaL_checkstring(L, 1);
702         try{
703                 std::string value = g_settings->get(name);
704                 lua_pushstring(L, value.c_str());
705         } catch(SettingNotFoundException &e){
706                 lua_pushnil(L);
707         }
708         return 1;
709 }
710
711 // setting_getbool(name)
712 static int l_setting_getbool(lua_State *L)
713 {
714         const char *name = luaL_checkstring(L, 1);
715         try{
716                 bool value = g_settings->getBool(name);
717                 lua_pushboolean(L, value);
718         } catch(SettingNotFoundException &e){
719                 lua_pushnil(L);
720         }
721         return 1;
722 }
723
724 // setting_save()
725 static int l_setting_save(lua_State *L)
726 {
727         get_server(L)->saveConfig();
728         return 0;
729 }
730
731 // chat_send_all(text)
732 static int l_chat_send_all(lua_State *L)
733 {
734         const char *text = luaL_checkstring(L, 1);
735         // Get server from registry
736         Server *server = get_server(L);
737         // Send
738         server->notifyPlayers(narrow_to_wide(text));
739         return 0;
740 }
741
742 // chat_send_player(name, text)
743 static int l_chat_send_player(lua_State *L)
744 {
745         const char *name = luaL_checkstring(L, 1);
746         const char *text = luaL_checkstring(L, 2);
747         // Get server from registry
748         Server *server = get_server(L);
749         // Send
750         server->notifyPlayer(name, narrow_to_wide(text));
751         return 0;
752 }
753
754 // get_player_privs(name, text)
755 static int l_get_player_privs(lua_State *L)
756 {
757         const char *name = luaL_checkstring(L, 1);
758         // Get server from registry
759         Server *server = get_server(L);
760         // Do it
761         lua_newtable(L);
762         int table = lua_gettop(L);
763         std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
764         for(std::set<std::string>::const_iterator
765                         i = privs_s.begin(); i != privs_s.end(); i++){
766                 lua_pushboolean(L, true);
767                 lua_setfield(L, table, i->c_str());
768         }
769         lua_pushvalue(L, table);
770         return 1;
771 }
772
773 // get_ban_list()
774 static int l_get_ban_list(lua_State *L)
775 {
776         lua_pushstring(L, get_server(L)->getBanDescription("").c_str());
777         return 1;
778 }
779
780 // get_ban_description()
781 static int l_get_ban_description(lua_State *L)
782 {
783         const char * ip_or_name = luaL_checkstring(L, 1);
784         lua_pushstring(L, get_server(L)->getBanDescription(std::string(ip_or_name)).c_str());
785         return 1;
786 }
787
788 // ban_player()
789 static int l_ban_player(lua_State *L)
790 {
791         const char * name = luaL_checkstring(L, 1);
792         Player *player = get_env(L)->getPlayer(name);
793         if(player == NULL)
794         {
795                 lua_pushboolean(L, false); // no such player
796                 return 1;
797         }
798         try
799         {
800                 Address addr = get_server(L)->getPeerAddress(get_env(L)->getPlayer(name)->peer_id);
801                 std::string ip_str = addr.serializeString();
802                 get_server(L)->setIpBanned(ip_str, name);
803         }
804         catch(con::PeerNotFoundException) // unlikely
805         {
806                 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
807                 lua_pushboolean(L, false); // error
808                 return 1;
809         }
810         lua_pushboolean(L, true);
811         return 1;
812 }
813
814 // unban_player_or_ip()
815 static int l_unban_player_of_ip(lua_State *L)
816 {
817         const char * ip_or_name = luaL_checkstring(L, 1);
818         get_server(L)->unsetIpBanned(ip_or_name);
819         lua_pushboolean(L, true);
820         return 1;
821 }
822
823 // show_formspec(playername,formname,formspec)
824 static int l_show_formspec(lua_State *L)
825 {
826         const char *playername = luaL_checkstring(L, 1);
827         const char *formname = luaL_checkstring(L, 2);
828         const char *formspec = luaL_checkstring(L, 3);
829
830         if(get_server(L)->showFormspec(playername,formspec,formname))
831         {
832                 lua_pushboolean(L, true);
833         }else{
834                 lua_pushboolean(L, false);
835         }
836         return 1;
837 }
838
839 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
840 static int l_get_dig_params(lua_State *L)
841 {
842         std::map<std::string, int> groups;
843         read_groups(L, 1, groups);
844         ToolCapabilities tp = read_tool_capabilities(L, 2);
845         if(lua_isnoneornil(L, 3))
846                 push_dig_params(L, getDigParams(groups, &tp));
847         else
848                 push_dig_params(L, getDigParams(groups, &tp,
849                                         luaL_checknumber(L, 3)));
850         return 1;
851 }
852
853 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
854 static int l_get_hit_params(lua_State *L)
855 {
856         std::map<std::string, int> groups;
857         read_groups(L, 1, groups);
858         ToolCapabilities tp = read_tool_capabilities(L, 2);
859         if(lua_isnoneornil(L, 3))
860                 push_hit_params(L, getHitParams(groups, &tp));
861         else
862                 push_hit_params(L, getHitParams(groups, &tp,
863                                         luaL_checknumber(L, 3)));
864         return 1;
865 }
866
867 // get_current_modname()
868 static int l_get_current_modname(lua_State *L)
869 {
870         lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
871         return 1;
872 }
873
874 // get_modpath(modname)
875 static int l_get_modpath(lua_State *L)
876 {
877         std::string modname = luaL_checkstring(L, 1);
878         // Do it
879         if(modname == "__builtin"){
880                 std::string path = get_server(L)->getBuiltinLuaPath();
881                 lua_pushstring(L, path.c_str());
882                 return 1;
883         }
884         const ModSpec *mod = get_server(L)->getModSpec(modname);
885         if(!mod){
886                 lua_pushnil(L);
887                 return 1;
888         }
889         lua_pushstring(L, mod->path.c_str());
890         return 1;
891 }
892
893 // get_modnames()
894 // the returned list is sorted alphabetically for you
895 static int l_get_modnames(lua_State *L)
896 {
897         // Get a list of mods
898         core::list<std::string> mods_unsorted, mods_sorted;
899         get_server(L)->getModNames(mods_unsorted);
900
901         // Take unsorted items from mods_unsorted and sort them into
902         // mods_sorted; not great performance but the number of mods on a
903         // server will likely be small.
904         for(core::list<std::string>::Iterator i = mods_unsorted.begin();
905             i != mods_unsorted.end(); i++)
906         {
907                 bool added = false;
908                 for(core::list<std::string>::Iterator x = mods_sorted.begin();
909                     x != mods_unsorted.end(); x++)
910                 {
911                         // I doubt anybody using Minetest will be using
912                         // anything not ASCII based :)
913                         if((*i).compare(*x) <= 0)
914                         {
915                                 mods_sorted.insert_before(x, *i);
916                                 added = true;
917                                 break;
918                         }
919                 }
920                 if(!added)
921                         mods_sorted.push_back(*i);
922         }
923
924         // Get the table insertion function from Lua.
925         lua_getglobal(L, "table");
926         lua_getfield(L, -1, "insert");
927         int insertion_func = lua_gettop(L);
928
929         // Package them up for Lua
930         lua_newtable(L);
931         int new_table = lua_gettop(L);
932         core::list<std::string>::Iterator i = mods_sorted.begin();
933         while(i != mods_sorted.end())
934         {
935                 lua_pushvalue(L, insertion_func);
936                 lua_pushvalue(L, new_table);
937                 lua_pushstring(L, (*i).c_str());
938                 if(lua_pcall(L, 2, 0, 0) != 0)
939                 {
940                         script_error(L, "error: %s", lua_tostring(L, -1));
941                 }
942                 i++;
943         }
944         return 1;
945 }
946
947 // get_worldpath()
948 static int l_get_worldpath(lua_State *L)
949 {
950         std::string worldpath = get_server(L)->getWorldPath();
951         lua_pushstring(L, worldpath.c_str());
952         return 1;
953 }
954
955 // sound_play(spec, parameters)
956 static int l_sound_play(lua_State *L)
957 {
958         SimpleSoundSpec spec;
959         read_soundspec(L, 1, spec);
960         ServerSoundParams params;
961         read_server_sound_params(L, 2, params);
962         s32 handle = get_server(L)->playSound(spec, params);
963         lua_pushinteger(L, handle);
964         return 1;
965 }
966
967 // sound_stop(handle)
968 static int l_sound_stop(lua_State *L)
969 {
970         int handle = luaL_checkinteger(L, 1);
971         get_server(L)->stopSound(handle);
972         return 0;
973 }
974
975 // is_singleplayer()
976 static int l_is_singleplayer(lua_State *L)
977 {
978         lua_pushboolean(L, get_server(L)->isSingleplayer());
979         return 1;
980 }
981
982 // get_password_hash(name, raw_password)
983 static int l_get_password_hash(lua_State *L)
984 {
985         std::string name = luaL_checkstring(L, 1);
986         std::string raw_password = luaL_checkstring(L, 2);
987         std::string hash = translatePassword(name,
988                         narrow_to_wide(raw_password));
989         lua_pushstring(L, hash.c_str());
990         return 1;
991 }
992
993 // notify_authentication_modified(name)
994 static int l_notify_authentication_modified(lua_State *L)
995 {
996         std::string name = "";
997         if(lua_isstring(L, 1))
998                 name = lua_tostring(L, 1);
999         get_server(L)->reportPrivsModified(name);
1000         return 0;
1001 }
1002
1003 // rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
1004 static int l_rollback_get_last_node_actor(lua_State *L)
1005 {
1006         v3s16 p = read_v3s16(L, 1);
1007         int range = luaL_checknumber(L, 2);
1008         int seconds = luaL_checknumber(L, 3);
1009         Server *server = get_server(L);
1010         IRollbackManager *rollback = server->getRollbackManager();
1011         v3s16 act_p;
1012         int act_seconds = 0;
1013         std::string actor = rollback->getLastNodeActor(p, range, seconds, &act_p, &act_seconds);
1014         lua_pushstring(L, actor.c_str());
1015         push_v3s16(L, act_p);
1016         lua_pushnumber(L, act_seconds);
1017         return 3;
1018 }
1019
1020 // rollback_revert_actions_by(actor, seconds) -> bool, log messages
1021 static int l_rollback_revert_actions_by(lua_State *L)
1022 {
1023         std::string actor = luaL_checkstring(L, 1);
1024         int seconds = luaL_checknumber(L, 2);
1025         Server *server = get_server(L);
1026         IRollbackManager *rollback = server->getRollbackManager();
1027         std::list<RollbackAction> actions = rollback->getRevertActions(actor, seconds);
1028         std::list<std::string> log;
1029         bool success = server->rollbackRevertActions(actions, &log);
1030         // Push boolean result
1031         lua_pushboolean(L, success);
1032         // Get the table insert function and push the log table
1033         lua_getglobal(L, "table");
1034         lua_getfield(L, -1, "insert");
1035         int table_insert = lua_gettop(L);
1036         lua_newtable(L);
1037         int table = lua_gettop(L);
1038         for(std::list<std::string>::const_iterator i = log.begin();
1039                         i != log.end(); i++)
1040         {
1041                 lua_pushvalue(L, table_insert);
1042                 lua_pushvalue(L, table);
1043                 lua_pushstring(L, i->c_str());
1044                 if(lua_pcall(L, 2, 0, 0))
1045                         script_error(L, "error: %s", lua_tostring(L, -1));
1046         }
1047         lua_remove(L, -2); // Remove table
1048         lua_remove(L, -2); // Remove insert
1049         return 2;
1050 }
1051
1052 static const struct luaL_Reg minetest_f [] = {
1053         {"debug", l_debug},
1054         {"log", l_log},
1055         {"request_shutdown", l_request_shutdown},
1056         {"get_server_status", l_get_server_status},
1057         {"register_item_raw", l_register_item_raw},
1058         {"register_alias_raw", l_register_alias_raw},
1059         {"register_craft", l_register_craft},
1060         {"register_biome", l_register_biome},
1061         {"register_biome_groups", l_register_biome_groups},
1062         {"setting_set", l_setting_set},
1063         {"setting_get", l_setting_get},
1064         {"setting_getbool", l_setting_getbool},
1065         {"setting_save",l_setting_save},
1066         {"chat_send_all", l_chat_send_all},
1067         {"chat_send_player", l_chat_send_player},
1068         {"get_player_privs", l_get_player_privs},
1069         {"get_ban_list", l_get_ban_list},
1070         {"get_ban_description", l_get_ban_description},
1071         {"ban_player", l_ban_player},
1072         {"unban_player_or_ip", l_unban_player_of_ip},
1073         {"get_inventory", l_get_inventory},
1074         {"create_detached_inventory_raw", l_create_detached_inventory_raw},
1075         {"show_formspec", l_show_formspec},
1076         {"get_dig_params", l_get_dig_params},
1077         {"get_hit_params", l_get_hit_params},
1078         {"get_current_modname", l_get_current_modname},
1079         {"get_modpath", l_get_modpath},
1080         {"get_modnames", l_get_modnames},
1081         {"get_worldpath", l_get_worldpath},
1082         {"sound_play", l_sound_play},
1083         {"sound_stop", l_sound_stop},
1084         {"is_singleplayer", l_is_singleplayer},
1085         {"get_password_hash", l_get_password_hash},
1086         {"notify_authentication_modified", l_notify_authentication_modified},
1087         {"get_craft_result", l_get_craft_result},
1088         {"get_craft_recipe", l_get_craft_recipe},
1089         {"rollback_get_last_node_actor", l_rollback_get_last_node_actor},
1090         {"rollback_revert_actions_by", l_rollback_revert_actions_by},
1091         {NULL, NULL}
1092 };
1093
1094
1095 /*
1096         Main export function
1097 */
1098
1099 void scriptapi_export(lua_State *L, Server *server)
1100 {
1101         realitycheck(L);
1102         assert(lua_checkstack(L, 20));
1103         verbosestream<<"scriptapi_export()"<<std::endl;
1104         StackUnroller stack_unroller(L);
1105
1106         // Store server as light userdata in registry
1107         lua_pushlightuserdata(L, server);
1108         lua_setfield(L, LUA_REGISTRYINDEX, "minetest_server");
1109
1110         // Register global functions in table minetest
1111         lua_newtable(L);
1112         luaL_register(L, NULL, minetest_f);
1113         lua_setglobal(L, "minetest");
1114
1115         // Get the main minetest table
1116         lua_getglobal(L, "minetest");
1117
1118         // Add tables to minetest
1119         lua_newtable(L);
1120         lua_setfield(L, -2, "object_refs");
1121         lua_newtable(L);
1122         lua_setfield(L, -2, "luaentities");
1123
1124         // Register wrappers
1125         LuaItemStack::Register(L);
1126         InvRef::Register(L);
1127         NodeMetaRef::Register(L);
1128         NodeTimerRef::Register(L);
1129         ObjectRef::Register(L);
1130         EnvRef::Register(L);
1131         LuaPseudoRandom::Register(L);
1132         LuaPerlinNoise::Register(L);
1133         LuaPerlinNoiseMap::Register(L);
1134 }