1 /*
2 script/cpp_api/s_server.cpp
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 */
5
6 /*
7 This file is part of Freeminer.
8
9 Freeminer is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Freeminer is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "cpp_api/s_server.h"
24 #include "cpp_api/s_internal.h"
25 #include "common/c_converter.h"
26
getAuth(const std::string & playername,std::string * dst_password,std::set<std::string> * dst_privs)27 bool ScriptApiServer::getAuth(const std::string &playername,
28 std::string *dst_password,
29 std::set<std::string> *dst_privs)
30 {
31 SCRIPTAPI_PRECHECKHEADER
32
33 getAuthHandler();
34 lua_getfield(L, -1, "get_auth");
35 if (lua_type(L, -1) != LUA_TFUNCTION)
36 throw LuaError("Authentication handler missing get_auth");
37 lua_pushstring(L, playername.c_str());
38 if (lua_pcall(L, 1, 1, m_errorhandler))
39 scriptError();
40 lua_remove(L, -2); // Remove auth handler
41
42 // nil = login not allowed
43 if (lua_isnil(L, -1))
44 return false;
45 luaL_checktype(L, -1, LUA_TTABLE);
46
47 std::string password;
48 bool found = getstringfield(L, -1, "password", password);
49 if (!found)
50 throw LuaError("Authentication handler didn't return password");
51 if (dst_password)
52 *dst_password = password;
53
54 lua_getfield(L, -1, "privileges");
55 if (!lua_istable(L, -1))
56 throw LuaError("Authentication handler didn't return privilege table");
57 if (dst_privs)
58 readPrivileges(-1, *dst_privs);
59 lua_pop(L, 1);
60
61 return true;
62 }
63
getAuthHandler()64 void ScriptApiServer::getAuthHandler()
65 {
66 lua_State *L = getStack();
67
68 lua_getglobal(L, "core");
69 lua_getfield(L, -1, "registered_auth_handler");
70 if (lua_isnil(L, -1)){
71 lua_pop(L, 1);
72 lua_getfield(L, -1, "builtin_auth_handler");
73 }
74 lua_remove(L, -2); // Remove core
75 if (lua_type(L, -1) != LUA_TTABLE)
76 throw LuaError("Authentication handler table not valid");
77 }
78
readPrivileges(int index,std::set<std::string> & result)79 void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
80 {
81 lua_State *L = getStack();
82
83 result.clear();
84 lua_pushnil(L);
85 if (index < 0)
86 index -= 1;
87 while (lua_next(L, index) != 0) {
88 // key at index -2 and value at index -1
89 std::string key = luaL_checkstring(L, -2);
90 bool value = lua_toboolean(L, -1);
91 if (value)
92 result.insert(key);
93 // removes value, keeps key for next iteration
94 lua_pop(L, 1);
95 }
96 }
97
createAuth(const std::string & playername,const std::string & password)98 void ScriptApiServer::createAuth(const std::string &playername,
99 const std::string &password)
100 {
101 SCRIPTAPI_PRECHECKHEADER
102
103 getAuthHandler();
104 lua_getfield(L, -1, "create_auth");
105 lua_remove(L, -2); // Remove auth handler
106 if (lua_type(L, -1) != LUA_TFUNCTION)
107 throw LuaError("Authentication handler missing create_auth");
108 lua_pushstring(L, playername.c_str());
109 lua_pushstring(L, password.c_str());
110 if (lua_pcall(L, 2, 0, m_errorhandler))
111 scriptError();
112 }
113
setPassword(const std::string & playername,const std::string & password)114 bool ScriptApiServer::setPassword(const std::string &playername,
115 const std::string &password)
116 {
117 SCRIPTAPI_PRECHECKHEADER
118
119 getAuthHandler();
120 lua_getfield(L, -1, "set_password");
121 lua_remove(L, -2); // Remove auth handler
122 if (lua_type(L, -1) != LUA_TFUNCTION)
123 throw LuaError("Authentication handler missing set_password");
124 lua_pushstring(L, playername.c_str());
125 lua_pushstring(L, password.c_str());
126 if (lua_pcall(L, 2, 1, m_errorhandler))
127 scriptError();
128 return lua_toboolean(L, -1);
129 }
130
on_chat_message(const std::string & name,const std::string & message)131 bool ScriptApiServer::on_chat_message(const std::string &name,
132 const std::string &message)
133 {
134 SCRIPTAPI_PRECHECKHEADER
135
136 // Get core.registered_on_chat_messages
137 lua_getglobal(L, "core");
138 lua_getfield(L, -1, "registered_on_chat_messages");
139 // Call callbacks
140 lua_pushstring(L, name.c_str());
141 lua_pushstring(L, message.c_str());
142 script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
143 bool ate = lua_toboolean(L, -1);
144 return ate;
145 }
146
on_shutdown()147 void ScriptApiServer::on_shutdown()
148 {
149 SCRIPTAPI_PRECHECKHEADER
150
151 // Get registered shutdown hooks
152 lua_getglobal(L, "core");
153 lua_getfield(L, -1, "registered_on_shutdown");
154 // Call callbacks
155 script_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
156 }
157
158