]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_server.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[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(L, "Authentication handler missing get_auth");
34         lua_pushstring(L, playername.c_str());
35         if(lua_pcall(L, 1, 1, 0))
36                 scriptError("error: %s", lua_tostring(L, -1));
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(L, "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(L,
53                                 "Authentication handler didn't return privilege table");
54         if(dst_privs)
55                 readPrivileges(-1, *dst_privs);
56         lua_pop(L, 1);
57
58         return true;
59 }
60
61 void ScriptApiServer::getAuthHandler()
62 {
63         lua_State *L = getStack();
64
65         lua_getglobal(L, "minetest");
66         lua_getfield(L, -1, "registered_auth_handler");
67         if(lua_isnil(L, -1)){
68                 lua_pop(L, 1);
69                 lua_getfield(L, -1, "builtin_auth_handler");
70         }
71         if(lua_type(L, -1) != LUA_TTABLE)
72                 throw LuaError(L, "Authentication handler table not valid");
73 }
74
75 void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
76 {
77         lua_State *L = getStack();
78
79         result.clear();
80         lua_pushnil(L);
81         if(index < 0)
82                 index -= 1;
83         while(lua_next(L, index) != 0){
84                 // key at index -2 and value at index -1
85                 std::string key = luaL_checkstring(L, -2);
86                 bool value = lua_toboolean(L, -1);
87                 if(value)
88                         result.insert(key);
89                 // removes value, keeps key for next iteration
90                 lua_pop(L, 1);
91         }
92 }
93
94 void ScriptApiServer::createAuth(const std::string &playername,
95                 const std::string &password)
96 {
97         SCRIPTAPI_PRECHECKHEADER
98
99         getAuthHandler();
100         lua_getfield(L, -1, "create_auth");
101         if(lua_type(L, -1) != LUA_TFUNCTION)
102                 throw LuaError(L, "Authentication handler missing create_auth");
103         lua_pushstring(L, playername.c_str());
104         lua_pushstring(L, password.c_str());
105         if(lua_pcall(L, 2, 0, 0))
106                 scriptError("error: %s", lua_tostring(L, -1));
107 }
108
109 bool ScriptApiServer::setPassword(const std::string &playername,
110                 const std::string &password)
111 {
112         SCRIPTAPI_PRECHECKHEADER
113
114         getAuthHandler();
115         lua_getfield(L, -1, "set_password");
116         if(lua_type(L, -1) != LUA_TFUNCTION)
117                 throw LuaError(L, "Authentication handler missing set_password");
118         lua_pushstring(L, playername.c_str());
119         lua_pushstring(L, password.c_str());
120         if(lua_pcall(L, 2, 1, 0))
121                 scriptError("error: %s", lua_tostring(L, -1));
122         return lua_toboolean(L, -1);
123 }
124
125 bool ScriptApiServer::on_chat_message(const std::string &name,
126                 const std::string &message)
127 {
128         SCRIPTAPI_PRECHECKHEADER
129
130         // Get minetest.registered_on_chat_messages
131         lua_getglobal(L, "minetest");
132         lua_getfield(L, -1, "registered_on_chat_messages");
133         // Call callbacks
134         lua_pushstring(L, name.c_str());
135         lua_pushstring(L, message.c_str());
136         script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
137         bool ate = lua_toboolean(L, -1);
138         return ate;
139 }
140
141 void ScriptApiServer::on_shutdown()
142 {
143         SCRIPTAPI_PRECHECKHEADER
144
145         // Get registered shutdown hooks
146         lua_getglobal(L, "minetest");
147         lua_getfield(L, -1, "registered_on_shutdown");
148         // Call callbacks
149         script_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
150 }
151