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("Authentication handler missing get_auth");
34 lua_pushstring(L, playername.c_str());
35 PCALL_RES(lua_pcall(L, 1, 1, m_errorhandler));
36 lua_remove(L, -2); // Remove auth handler
38 // nil = login not allowed
41 luaL_checktype(L, -1, LUA_TTABLE);
44 bool found = getstringfield(L, -1, "password", password);
46 throw LuaError("Authentication handler didn't return password");
48 *dst_password = password;
50 lua_getfield(L, -1, "privileges");
51 if (!lua_istable(L, -1))
52 throw LuaError("Authentication handler didn't return privilege table");
54 readPrivileges(-1, *dst_privs);
60 void ScriptApiServer::getAuthHandler()
62 lua_State *L = getStack();
64 lua_getglobal(L, "core");
65 lua_getfield(L, -1, "registered_auth_handler");
66 if (lua_isnil(L, -1)){
68 lua_getfield(L, -1, "builtin_auth_handler");
71 setOriginFromTable(-1);
73 lua_remove(L, -2); // Remove core
74 if (lua_type(L, -1) != LUA_TTABLE)
75 throw LuaError("Authentication handler table not valid");
78 void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
80 lua_State *L = getStack();
86 while (lua_next(L, index) != 0) {
87 // key at index -2 and value at index -1
88 std::string key = luaL_checkstring(L, -2);
89 bool value = lua_toboolean(L, -1);
92 // removes value, keeps key for next iteration
97 void ScriptApiServer::createAuth(const std::string &playername,
98 const std::string &password)
100 SCRIPTAPI_PRECHECKHEADER
103 lua_getfield(L, -1, "create_auth");
104 lua_remove(L, -2); // Remove auth handler
105 if (lua_type(L, -1) != LUA_TFUNCTION)
106 throw LuaError("Authentication handler missing create_auth");
107 lua_pushstring(L, playername.c_str());
108 lua_pushstring(L, password.c_str());
109 PCALL_RES(lua_pcall(L, 2, 0, m_errorhandler));
112 bool ScriptApiServer::setPassword(const std::string &playername,
113 const std::string &password)
115 SCRIPTAPI_PRECHECKHEADER
118 lua_getfield(L, -1, "set_password");
119 lua_remove(L, -2); // Remove auth handler
120 if (lua_type(L, -1) != LUA_TFUNCTION)
121 throw LuaError("Authentication handler missing set_password");
122 lua_pushstring(L, playername.c_str());
123 lua_pushstring(L, password.c_str());
124 PCALL_RES(lua_pcall(L, 2, 1, m_errorhandler));
125 return lua_toboolean(L, -1);
128 bool ScriptApiServer::on_chat_message(const std::string &name,
129 const std::string &message)
131 SCRIPTAPI_PRECHECKHEADER
133 // Get core.registered_on_chat_messages
134 lua_getglobal(L, "core");
135 lua_getfield(L, -1, "registered_on_chat_messages");
137 lua_pushstring(L, name.c_str());
138 lua_pushstring(L, message.c_str());
139 runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
140 bool ate = lua_toboolean(L, -1);
144 void ScriptApiServer::on_shutdown()
146 SCRIPTAPI_PRECHECKHEADER
148 // Get registered shutdown hooks
149 lua_getglobal(L, "core");
150 lua_getfield(L, -1, "registered_on_shutdown");
152 runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);