]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_server.cpp
Clean up threading
[dragonfireclient.git] / src / script / cpp_api / s_server.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "cpp_api/s_server.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23
24 bool ScriptApiServer::getAuth(const std::string &playername,
25                 std::string *dst_password,
26                 std::set<std::string> *dst_privs)
27 {
28         SCRIPTAPI_PRECHECKHEADER
29
30         getAuthHandler();
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
37
38         // nil = login not allowed
39         if (lua_isnil(L, -1))
40                 return false;
41         luaL_checktype(L, -1, LUA_TTABLE);
42
43         std::string password;
44         bool found = getstringfield(L, -1, "password", password);
45         if (!found)
46                 throw LuaError("Authentication handler didn't return password");
47         if (dst_password)
48                 *dst_password = password;
49
50         lua_getfield(L, -1, "privileges");
51         if (!lua_istable(L, -1))
52                 throw LuaError("Authentication handler didn't return privilege table");
53         if (dst_privs)
54                 readPrivileges(-1, *dst_privs);
55         lua_pop(L, 1);
56
57         return true;
58 }
59
60 void ScriptApiServer::getAuthHandler()
61 {
62         lua_State *L = getStack();
63
64         lua_getglobal(L, "core");
65         lua_getfield(L, -1, "registered_auth_handler");
66         if (lua_isnil(L, -1)){
67                 lua_pop(L, 1);
68                 lua_getfield(L, -1, "builtin_auth_handler");
69         }
70
71         setOriginFromTable(-1);
72
73         lua_remove(L, -2); // Remove core
74         if (lua_type(L, -1) != LUA_TTABLE)
75                 throw LuaError("Authentication handler table not valid");
76 }
77
78 void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
79 {
80         lua_State *L = getStack();
81
82         result.clear();
83         lua_pushnil(L);
84         if (index < 0)
85                 index -= 1;
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);
90                 if (value)
91                         result.insert(key);
92                 // removes value, keeps key for next iteration
93                 lua_pop(L, 1);
94         }
95 }
96
97 void ScriptApiServer::createAuth(const std::string &playername,
98                 const std::string &password)
99 {
100         SCRIPTAPI_PRECHECKHEADER
101
102         getAuthHandler();
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));
110 }
111
112 bool ScriptApiServer::setPassword(const std::string &playername,
113                 const std::string &password)
114 {
115         SCRIPTAPI_PRECHECKHEADER
116
117         getAuthHandler();
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);
126 }
127
128 bool ScriptApiServer::on_chat_message(const std::string &name,
129                 const std::string &message)
130 {
131         SCRIPTAPI_PRECHECKHEADER
132
133         // Get core.registered_on_chat_messages
134         lua_getglobal(L, "core");
135         lua_getfield(L, -1, "registered_on_chat_messages");
136         // Call callbacks
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);
141         return ate;
142 }
143
144 void ScriptApiServer::on_shutdown()
145 {
146         SCRIPTAPI_PRECHECKHEADER
147
148         // Get registered shutdown hooks
149         lua_getglobal(L, "core");
150         lua_getfield(L, -1, "registered_on_shutdown");
151         // Call callbacks
152         runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
153 }
154