3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "cpp_api/s_server.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
24 bool ScriptApiServer::getAuth(const std::string &playername,
25 std::string *dst_password,
26 std::set<std::string> *dst_privs)
28 SCRIPTAPI_PRECHECKHEADER
30 lua_pushcfunction(L, script_error_handler);
31 int errorhandler = lua_gettop(L);
34 lua_getfield(L, -1, "get_auth");
35 if(lua_type(L, -1) != LUA_TFUNCTION)
36 throw LuaError(L, "Authentication handler missing get_auth");
37 lua_pushstring(L, playername.c_str());
38 if(lua_pcall(L, 1, 1, errorhandler))
40 lua_remove(L, -2); // Remove auth handler
41 lua_remove(L, errorhandler); // Remove error handler
43 // nil = login not allowed
46 luaL_checktype(L, -1, LUA_TTABLE);
49 bool found = getstringfield(L, -1, "password", password);
51 throw LuaError(L, "Authentication handler didn't return password");
53 *dst_password = password;
55 lua_getfield(L, -1, "privileges");
56 if(!lua_istable(L, -1))
57 throw LuaError(L, "Authentication handler didn't return privilege table");
59 readPrivileges(-1, *dst_privs);
65 void ScriptApiServer::getAuthHandler()
67 lua_State *L = getStack();
69 lua_getglobal(L, "minetest");
70 lua_getfield(L, -1, "registered_auth_handler");
73 lua_getfield(L, -1, "builtin_auth_handler");
75 lua_remove(L, -2); // Remove minetest
76 if(lua_type(L, -1) != LUA_TTABLE)
77 throw LuaError(L, "Authentication handler table not valid");
80 void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
82 lua_State *L = getStack();
88 while(lua_next(L, index) != 0){
89 // key at index -2 and value at index -1
90 std::string key = luaL_checkstring(L, -2);
91 bool value = lua_toboolean(L, -1);
94 // removes value, keeps key for next iteration
99 void ScriptApiServer::createAuth(const std::string &playername,
100 const std::string &password)
102 SCRIPTAPI_PRECHECKHEADER
104 lua_pushcfunction(L, script_error_handler);
105 int errorhandler = lua_gettop(L);
108 lua_getfield(L, -1, "create_auth");
109 lua_remove(L, -2); // Remove auth handler
110 if(lua_type(L, -1) != LUA_TFUNCTION)
111 throw LuaError(L, "Authentication handler missing create_auth");
112 lua_pushstring(L, playername.c_str());
113 lua_pushstring(L, password.c_str());
114 if(lua_pcall(L, 2, 0, errorhandler))
116 lua_pop(L, 1); // Pop error handler
119 bool ScriptApiServer::setPassword(const std::string &playername,
120 const std::string &password)
122 SCRIPTAPI_PRECHECKHEADER
124 lua_pushcfunction(L, script_error_handler);
125 int errorhandler = lua_gettop(L);
128 lua_getfield(L, -1, "set_password");
129 lua_remove(L, -2); // Remove auth handler
130 if(lua_type(L, -1) != LUA_TFUNCTION)
131 throw LuaError(L, "Authentication handler missing set_password");
132 lua_pushstring(L, playername.c_str());
133 lua_pushstring(L, password.c_str());
134 if(lua_pcall(L, 2, 1, errorhandler))
136 lua_remove(L, -2); // Remove error handler
137 return lua_toboolean(L, -1);
140 bool ScriptApiServer::on_chat_message(const std::string &name,
141 const std::string &message)
143 SCRIPTAPI_PRECHECKHEADER
145 // Get minetest.registered_on_chat_messages
146 lua_getglobal(L, "minetest");
147 lua_getfield(L, -1, "registered_on_chat_messages");
149 lua_pushstring(L, name.c_str());
150 lua_pushstring(L, message.c_str());
151 script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
152 bool ate = lua_toboolean(L, -1);
156 void ScriptApiServer::on_shutdown()
158 SCRIPTAPI_PRECHECKHEADER
160 // Get registered shutdown hooks
161 lua_getglobal(L, "minetest");
162 lua_getfield(L, -1, "registered_on_shutdown");
164 script_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);