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
31 lua_getfield(L, -1, "get_auth");
32 if(lua_type(L, -1) != LUA_TFUNCTION)
33 throw LuaError(L, "Authentication handler missing get_auth");
34 lua_pushstring(L, playername.c_str());
35 if(lua_pcall(L, 1, 1, 0))
36 scriptError("error: %s", lua_tostring(L, -1));
38 // nil = login not allowed
41 luaL_checktype(L, -1, LUA_TTABLE);
44 bool found = getstringfield(L, -1, "password", password);
46 throw LuaError(L, "Authentication handler didn't return password");
48 *dst_password = password;
50 lua_getfield(L, -1, "privileges");
51 if(!lua_istable(L, -1))
53 "Authentication handler didn't return privilege table");
55 readPrivileges(-1, *dst_privs);
61 void ScriptApiServer::getAuthHandler()
63 lua_State *L = getStack();
65 lua_getglobal(L, "minetest");
66 lua_getfield(L, -1, "registered_auth_handler");
69 lua_getfield(L, -1, "builtin_auth_handler");
71 if(lua_type(L, -1) != LUA_TTABLE)
72 throw LuaError(L, "Authentication handler table not valid");
75 void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
77 lua_State *L = getStack();
83 while(lua_next(L, index) != 0){
84 // key at index -2 and value at index -1
85 std::string key = luaL_checkstring(L, -2);
86 bool value = lua_toboolean(L, -1);
89 // removes value, keeps key for next iteration
94 void ScriptApiServer::createAuth(const std::string &playername,
95 const std::string &password)
97 SCRIPTAPI_PRECHECKHEADER
100 lua_getfield(L, -1, "create_auth");
101 if(lua_type(L, -1) != LUA_TFUNCTION)
102 throw LuaError(L, "Authentication handler missing create_auth");
103 lua_pushstring(L, playername.c_str());
104 lua_pushstring(L, password.c_str());
105 if(lua_pcall(L, 2, 0, 0))
106 scriptError("error: %s", lua_tostring(L, -1));
109 bool ScriptApiServer::setPassword(const std::string &playername,
110 const std::string &password)
112 SCRIPTAPI_PRECHECKHEADER
115 lua_getfield(L, -1, "set_password");
116 if(lua_type(L, -1) != LUA_TFUNCTION)
117 throw LuaError(L, "Authentication handler missing set_password");
118 lua_pushstring(L, playername.c_str());
119 lua_pushstring(L, password.c_str());
120 if(lua_pcall(L, 2, 1, 0))
121 scriptError("error: %s", lua_tostring(L, -1));
122 return lua_toboolean(L, -1);
125 bool ScriptApiServer::on_chat_message(const std::string &name,
126 const std::string &message)
128 SCRIPTAPI_PRECHECKHEADER
130 // Get minetest.registered_on_chat_messages
131 lua_getglobal(L, "minetest");
132 lua_getfield(L, -1, "registered_on_chat_messages");
134 lua_pushstring(L, name.c_str());
135 lua_pushstring(L, message.c_str());
136 script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
137 bool ate = lua_toboolean(L, -1);
141 void ScriptApiServer::on_shutdown()
143 SCRIPTAPI_PRECHECKHEADER
145 // Get registered shutdown hooks
146 lua_getglobal(L, "minetest");
147 lua_getfield(L, -1, "registered_on_shutdown");
149 script_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);