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 int error_handler = PUSH_ERROR_HANDLER(L);
32 lua_getfield(L, -1, "get_auth");
33 if (lua_type(L, -1) != LUA_TFUNCTION)
34 throw LuaError("Authentication handler missing get_auth");
35 lua_pushstring(L, playername.c_str());
36 PCALL_RES(lua_pcall(L, 1, 1, error_handler));
37 lua_remove(L, -2); // Remove auth handler
38 lua_remove(L, error_handler);
40 // nil = login not allowed
43 luaL_checktype(L, -1, LUA_TTABLE);
46 bool found = getstringfield(L, -1, "password", password);
48 throw LuaError("Authentication handler didn't return password");
50 *dst_password = password;
52 lua_getfield(L, -1, "privileges");
53 if (!lua_istable(L, -1))
54 throw LuaError("Authentication handler didn't return privilege table");
56 readPrivileges(-1, *dst_privs);
62 void ScriptApiServer::getAuthHandler()
64 lua_State *L = getStack();
66 lua_getglobal(L, "core");
67 lua_getfield(L, -1, "registered_auth_handler");
68 if (lua_isnil(L, -1)){
70 lua_getfield(L, -1, "builtin_auth_handler");
73 setOriginFromTable(-1);
75 lua_remove(L, -2); // Remove core
76 if (lua_type(L, -1) != LUA_TTABLE)
77 throw LuaError("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 int error_handler = PUSH_ERROR_HANDLER(L);
106 lua_getfield(L, -1, "create_auth");
107 lua_remove(L, -2); // Remove auth handler
108 if (lua_type(L, -1) != LUA_TFUNCTION)
109 throw LuaError("Authentication handler missing create_auth");
110 lua_pushstring(L, playername.c_str());
111 lua_pushstring(L, password.c_str());
112 PCALL_RES(lua_pcall(L, 2, 0, error_handler));
113 lua_pop(L, 1); // Pop error handler
116 bool ScriptApiServer::setPassword(const std::string &playername,
117 const std::string &password)
119 SCRIPTAPI_PRECHECKHEADER
121 int error_handler = PUSH_ERROR_HANDLER(L);
123 lua_getfield(L, -1, "set_password");
124 lua_remove(L, -2); // Remove auth handler
125 if (lua_type(L, -1) != LUA_TFUNCTION)
126 throw LuaError("Authentication handler missing set_password");
127 lua_pushstring(L, playername.c_str());
128 lua_pushstring(L, password.c_str());
129 PCALL_RES(lua_pcall(L, 2, 1, error_handler));
130 lua_remove(L, error_handler);
131 return lua_toboolean(L, -1);
134 bool ScriptApiServer::on_chat_message(const std::string &name,
135 const std::string &message)
137 SCRIPTAPI_PRECHECKHEADER
139 // Get core.registered_on_chat_messages
140 lua_getglobal(L, "core");
141 lua_getfield(L, -1, "registered_on_chat_messages");
143 lua_pushstring(L, name.c_str());
144 lua_pushstring(L, message.c_str());
145 runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
146 bool ate = lua_toboolean(L, -1);
150 void ScriptApiServer::on_shutdown()
152 SCRIPTAPI_PRECHECKHEADER
154 // Get registered shutdown hooks
155 lua_getglobal(L, "core");
156 lua_getfield(L, -1, "registered_on_shutdown");
158 runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);