]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_server.cpp
Add 'on_prejoinplayer' callback
[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         lua_pushcfunction(L, script_error_handler);
31         int errorhandler = lua_gettop(L);
32
33         getAuthHandler();
34         lua_getfield(L, -1, "get_auth");
35         if(lua_type(L, -1) != LUA_TFUNCTION)
36                 throw LuaError(L, "Authentication handler missing get_auth");
37         lua_pushstring(L, playername.c_str());
38         if(lua_pcall(L, 1, 1, errorhandler))
39                 scriptError();
40         lua_remove(L, -2); // Remove auth handler
41         lua_remove(L, errorhandler); // Remove error handler
42
43         // nil = login not allowed
44         if(lua_isnil(L, -1))
45                 return false;
46         luaL_checktype(L, -1, LUA_TTABLE);
47
48         std::string password;
49         bool found = getstringfield(L, -1, "password", password);
50         if(!found)
51                 throw LuaError(L, "Authentication handler didn't return password");
52         if(dst_password)
53                 *dst_password = password;
54
55         lua_getfield(L, -1, "privileges");
56         if(!lua_istable(L, -1))
57                 throw LuaError(L, "Authentication handler didn't return privilege table");
58         if(dst_privs)
59                 readPrivileges(-1, *dst_privs);
60         lua_pop(L, 1);
61
62         return true;
63 }
64
65 void ScriptApiServer::getAuthHandler()
66 {
67         lua_State *L = getStack();
68
69         lua_getglobal(L, "minetest");
70         lua_getfield(L, -1, "registered_auth_handler");
71         if(lua_isnil(L, -1)){
72                 lua_pop(L, 1);
73                 lua_getfield(L, -1, "builtin_auth_handler");
74         }
75         lua_remove(L, -2); // Remove minetest
76         if(lua_type(L, -1) != LUA_TTABLE)
77                 throw LuaError(L, "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         lua_pushcfunction(L, script_error_handler);
105         int errorhandler = lua_gettop(L);
106
107         getAuthHandler();
108         lua_getfield(L, -1, "create_auth");
109         lua_remove(L, -2); // Remove auth handler
110         if(lua_type(L, -1) != LUA_TFUNCTION)
111                 throw LuaError(L, "Authentication handler missing create_auth");
112         lua_pushstring(L, playername.c_str());
113         lua_pushstring(L, password.c_str());
114         if(lua_pcall(L, 2, 0, errorhandler))
115                 scriptError();
116         lua_pop(L, 1); // Pop error handler
117 }
118
119 bool ScriptApiServer::setPassword(const std::string &playername,
120                 const std::string &password)
121 {
122         SCRIPTAPI_PRECHECKHEADER
123
124         lua_pushcfunction(L, script_error_handler);
125         int errorhandler = lua_gettop(L);
126
127         getAuthHandler();
128         lua_getfield(L, -1, "set_password");
129         lua_remove(L, -2); // Remove auth handler
130         if(lua_type(L, -1) != LUA_TFUNCTION)
131                 throw LuaError(L, "Authentication handler missing set_password");
132         lua_pushstring(L, playername.c_str());
133         lua_pushstring(L, password.c_str());
134         if(lua_pcall(L, 2, 1, errorhandler))
135                 scriptError();
136         lua_remove(L, -2); // Remove error handler
137         return lua_toboolean(L, -1);
138 }
139
140 bool ScriptApiServer::on_chat_message(const std::string &name,
141                 const std::string &message)
142 {
143         SCRIPTAPI_PRECHECKHEADER
144
145         // Get minetest.registered_on_chat_messages
146         lua_getglobal(L, "minetest");
147         lua_getfield(L, -1, "registered_on_chat_messages");
148         // Call callbacks
149         lua_pushstring(L, name.c_str());
150         lua_pushstring(L, message.c_str());
151         script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
152         bool ate = lua_toboolean(L, -1);
153         return ate;
154 }
155
156 void ScriptApiServer::on_shutdown()
157 {
158         SCRIPTAPI_PRECHECKHEADER
159
160         // Get registered shutdown hooks
161         lua_getglobal(L, "minetest");
162         lua_getfield(L, -1, "registered_on_shutdown");
163         // Call callbacks
164         script_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
165 }
166