CSM/SSM: Add on_mods_loaded callback (#7411)
[oweals/minetest.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         int error_handler = PUSH_ERROR_HANDLER(L);
31         getAuthHandler();
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);
39
40         // nil = login not allowed
41         if (lua_isnil(L, -1))
42                 return false;
43         luaL_checktype(L, -1, LUA_TTABLE);
44
45         std::string password;
46         bool found = getstringfield(L, -1, "password", password);
47         if (!found)
48                 throw LuaError("Authentication handler didn't return password");
49         if (dst_password)
50                 *dst_password = password;
51
52         lua_getfield(L, -1, "privileges");
53         if (!lua_istable(L, -1))
54                 throw LuaError("Authentication handler didn't return privilege table");
55         if (dst_privs)
56                 readPrivileges(-1, *dst_privs);
57         lua_pop(L, 1);
58
59         return true;
60 }
61
62 void ScriptApiServer::getAuthHandler()
63 {
64         lua_State *L = getStack();
65
66         lua_getglobal(L, "core");
67         lua_getfield(L, -1, "registered_auth_handler");
68         if (lua_isnil(L, -1)){
69                 lua_pop(L, 1);
70                 lua_getfield(L, -1, "builtin_auth_handler");
71         }
72
73         setOriginFromTable(-1);
74
75         lua_remove(L, -2); // Remove core
76         if (lua_type(L, -1) != LUA_TTABLE)
77                 throw LuaError("Authentication handler table not valid");
78 }
79
80 void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
81 {
82         lua_State *L = getStack();
83
84         result.clear();
85         lua_pushnil(L);
86         if (index < 0)
87                 index -= 1;
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);
92                 if (value)
93                         result.insert(key);
94                 // removes value, keeps key for next iteration
95                 lua_pop(L, 1);
96         }
97 }
98
99 void ScriptApiServer::createAuth(const std::string &playername,
100                 const std::string &password)
101 {
102         SCRIPTAPI_PRECHECKHEADER
103
104         int error_handler = PUSH_ERROR_HANDLER(L);
105         getAuthHandler();
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
114 }
115
116 bool ScriptApiServer::setPassword(const std::string &playername,
117                 const std::string &password)
118 {
119         SCRIPTAPI_PRECHECKHEADER
120
121         int error_handler = PUSH_ERROR_HANDLER(L);
122         getAuthHandler();
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);
132 }
133
134 bool ScriptApiServer::on_chat_message(const std::string &name,
135                 const std::string &message)
136 {
137         SCRIPTAPI_PRECHECKHEADER
138
139         // Get core.registered_on_chat_messages
140         lua_getglobal(L, "core");
141         lua_getfield(L, -1, "registered_on_chat_messages");
142         // Call callbacks
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);
147         return ate;
148 }
149
150 void ScriptApiServer::on_mods_loaded()
151 {
152         SCRIPTAPI_PRECHECKHEADER
153
154         // Get registered shutdown hooks
155         lua_getglobal(L, "core");
156         lua_getfield(L, -1, "registered_on_mods_loaded");
157         // Call callbacks
158         runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
159 }
160
161 void ScriptApiServer::on_shutdown()
162 {
163         SCRIPTAPI_PRECHECKHEADER
164
165         // Get registered shutdown hooks
166         lua_getglobal(L, "core");
167         lua_getfield(L, -1, "registered_on_shutdown");
168         // Call callbacks
169         runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
170 }
171