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 if (lua_pcall(L, 1, 1, m_errorhandler))
37 lua_remove(L, -2); // Remove auth handler
39 // nil = login not allowed
42 luaL_checktype(L, -1, LUA_TTABLE);
45 bool found = getstringfield(L, -1, "password", password);
47 throw LuaError("Authentication handler didn't return password");
49 *dst_password = password;
51 lua_getfield(L, -1, "privileges");
52 if (!lua_istable(L, -1))
53 throw LuaError("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, "core");
66 lua_getfield(L, -1, "registered_auth_handler");
67 if (lua_isnil(L, -1)){
69 lua_getfield(L, -1, "builtin_auth_handler");
71 lua_remove(L, -2); // Remove core
72 if (lua_type(L, -1) != LUA_TTABLE)
73 throw LuaError("Authentication handler table not valid");
76 void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
78 lua_State *L = getStack();
84 while (lua_next(L, index) != 0) {
85 // key at index -2 and value at index -1
86 std::string key = luaL_checkstring(L, -2);
87 bool value = lua_toboolean(L, -1);
90 // removes value, keeps key for next iteration
95 void ScriptApiServer::createAuth(const std::string &playername,
96 const std::string &password)
98 SCRIPTAPI_PRECHECKHEADER
101 lua_getfield(L, -1, "create_auth");
102 lua_remove(L, -2); // Remove auth handler
103 if (lua_type(L, -1) != LUA_TFUNCTION)
104 throw LuaError("Authentication handler missing create_auth");
105 lua_pushstring(L, playername.c_str());
106 lua_pushstring(L, password.c_str());
107 if (lua_pcall(L, 2, 0, m_errorhandler))
111 bool ScriptApiServer::setPassword(const std::string &playername,
112 const std::string &password)
114 SCRIPTAPI_PRECHECKHEADER
117 lua_getfield(L, -1, "set_password");
118 lua_remove(L, -2); // Remove auth handler
119 if (lua_type(L, -1) != LUA_TFUNCTION)
120 throw LuaError("Authentication handler missing set_password");
121 lua_pushstring(L, playername.c_str());
122 lua_pushstring(L, password.c_str());
123 if (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 script_run_callbacks(L, 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 script_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);