aboutsummaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_server.cpp
blob: 21fe164aac0f15268f710b417a9856c7d53e3883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "cpp_api/s_server.h"
#include "cpp_api/s_internal.h"
#include "common/c_converter.h"

bool ScriptApiServer::getAuth(const std::string &playername,
		std::string *dst_password,
		std::set<std::string> *dst_privs)
{
	SCRIPTAPI_PRECHECKHEADER

	getAuthHandler();
	lua_getfield(L, -1, "get_auth");
	if (lua_type(L, -1) != LUA_TFUNCTION)
		throw LuaError("Authentication handler missing get_auth");
	lua_pushstring(L, playername.c_str());
	if (lua_pcall(L, 1, 1, m_errorhandler))
		scriptError();
	lua_remove(L, -2); // Remove auth handler

	// nil = login not allowed
	if (lua_isnil(L, -1))
		return false;
	luaL_checktype(L, -1, LUA_TTABLE);

	std::string password;
	bool found = getstringfield(L, -1, "password", password);
	if (!found)
		throw LuaError("Authentication handler didn't return password");
	if (dst_password)
		*dst_password = password;

	lua_getfield(L, -1, "privileges");
	if (!lua_istable(L, -1))
		throw LuaError("Authentication handler didn't return privilege table");
	if (dst_privs)
		readPrivileges(-1, *dst_privs);
	lua_pop(L, 1);

	return true;
}

void ScriptApiServer::getAuthHandler()
{
	lua_State *L = getStack();

	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_auth_handler");
	if (lua_isnil(L, -1)){
		lua_pop(L, 1);
		lua_getfield(L, -1, "builtin_auth_handler");
	}
	lua_remove(L, -2); // Remove core
	if (lua_type(L, -1) != LUA_TTABLE)
		throw LuaError("Authentication handler table not valid");
}

void ScriptApiServer::readPrivileges(int index, std::set<std::string> &result)
{
	lua_State *L = getStack();

	result.clear();
	lua_pushnil(L);
	if (index < 0)
		index -= 1;
	while (lua_next(L, index) != 0) {
		// key at index -2 and value at index -1
		std::string key = luaL_checkstring(L, -2);
		bool value = lua_toboolean(L, -1);
		if (value)
			result.insert(key);
		// removes value, keeps key for next iteration
		lua_pop(L, 1);
	}
}

void ScriptApiServer::createAuth(const std::string &playername,
		const std::string &password)
{
	SCRIPTAPI_PRECHECKHEADER

	getAuthHandler();
	lua_getfield(L, -1, "create_auth");
	lua_remove(L, -2); // Remove auth handler
	if (lua_type(L, -1) != LUA_TFUNCTION)
		throw LuaError("Authentication handler missing create_auth");
	lua_pushstring(L, playername.c_str());
	lua_pushstring(L, password.c_str());
	if (lua_pcall(L, 2, 0, m_errorhandler))
		scriptError();
}

bool ScriptApiServer::setPassword(const std::string &playername,
		const std::string &password)
{
	SCRIPTAPI_PRECHECKHEADER

	getAuthHandler();
	lua_getfield(L, -1, "set_password");
	lua_remove(L, -2); // Remove auth handler
	if (lua_type(L, -1) != LUA_TFUNCTION)
		throw LuaError("Authentication handler missing set_password");
	lua_pushstring(L, playername.c_str());
	lua_pushstring(L, password.c_str());
	if (lua_pcall(L, 2, 1, m_errorhandler))
		scriptError();
	return lua_toboolean(L, -1);
}

bool ScriptApiServer::on_chat_message(const std::string &name,
		const std::string &message)
{
	SCRIPTAPI_PRECHECKHEADER

	// Get core.registered_on_chat_messages
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_chat_messages");
	// Call callbacks
	lua_pushstring(L, name.c_str());
	lua_pushstring(L, message.c_str());
	script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
	bool ate = lua_toboolean(L, -1);
	return ate;
}

void ScriptApiServer::on_shutdown()
{
	SCRIPTAPI_PRECHECKHEADER

	// Get registered shutdown hooks
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "registered_on_shutdown");
	// Call callbacks
	script_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
}

l str">" "; os<<serializeJsonString(inventory_stack); os<<"]"; return os.str(); } default: return "none"; } } void RollbackAction::fromStream(std::istream &is) throw(SerializationError) { int c = is.get(); if(c != '['){ is.putback(c); throw SerializationError("RollbackAction: starting [ not found"); } std::string id; std::getline(is, id, ' '); if(id == "set_node") { c = is.get(); if(c != '('){ is.putback(c); throw SerializationError("RollbackAction: starting ( not found"); } // Position std::string px_raw; std::string py_raw; std::string pz_raw; std::getline(is, px_raw, ','); std::getline(is, py_raw, ','); std::getline(is, pz_raw, ')'); c = is.get(); if(c != ' '){ is.putback(c); throw SerializationError("RollbackAction: after-p ' ' not found"); } v3s16 loaded_p(stoi(px_raw), stoi(py_raw), stoi(pz_raw)); // Old node std::string old_name; try{ old_name = deSerializeJsonString(is); }catch(SerializationError &e){ errorstream<<"Serialization error in RollbackAction::fromStream(): " <<"old_name: "<<e.what()<<std::endl; throw e; } c = is.get(); if(c != ' '){ is.putback(c); throw SerializationError("RollbackAction: after-old_name ' ' not found"); } std::string old_p1_raw; std::string old_p2_raw; std::getline(is, old_p1_raw, ' '); std::getline(is, old_p2_raw, ' '); int old_p1 = stoi(old_p1_raw); int old_p2 = stoi(old_p2_raw); std::string old_meta; try{ old_meta = deSerializeJsonString(is); }catch(SerializationError &e){ errorstream<<"Serialization error in RollbackAction::fromStream(): " <<"old_meta: "<<e.what()<<std::endl; throw e; } c = is.get(); if(c != ' '){ is.putback(c); throw SerializationError("RollbackAction: after-old_meta ' ' not found"); } // New node std::string new_name; try{ new_name = deSerializeJsonString(is); }catch(SerializationError &e){ errorstream<<"Serialization error in RollbackAction::fromStream(): " <<"new_name: "<<e.what()<<std::endl; throw e; } c = is.get(); if(c != ' '){ is.putback(c); throw SerializationError("RollbackAction: after-new_name ' ' not found"); } std::string new_p1_raw; std::string new_p2_raw; std::getline(is, new_p1_raw, ' '); std::getline(is, new_p2_raw, ' '); int new_p1 = stoi(new_p1_raw); int new_p2 = stoi(new_p2_raw); std::string new_meta; try{ new_meta = deSerializeJsonString(is); }catch(SerializationError &e){ errorstream<<"Serialization error in RollbackAction::fromStream(): " <<"new_meta: "<<e.what()<<std::endl; throw e; } c = is.get(); if(c != ']'){ is.putback(c); throw SerializationError("RollbackAction: after-new_meta ] not found"); } // Set values type = TYPE_SET_NODE; p = loaded_p; n_old.name = old_name; n_old.param1 = old_p1; n_old.param2 = old_p2; n_old.meta = old_meta; n_new.name = new_name; n_new.param1 = new_p1; n_new.param2 = new_p2; n_new.meta = new_meta; } else if(id == "modify_inventory_stack") { // Location std::string location; try{ location = deSerializeJsonString(is); }catch(SerializationError &e){ errorstream<<"Serialization error in RollbackAction::fromStream(): " <<"location: "<<e.what()<<std::endl; throw e; } c = is.get(); if(c != ' '){ is.putback(c); throw SerializationError("RollbackAction: after-loc ' ' not found"); } // List std::string listname; try{ listname = deSerializeJsonString(is); }catch(SerializationError &e){ errorstream<<"Serialization error in RollbackAction::fromStream(): " <<"listname: "<<e.what()<<std::endl; throw e; } c = is.get(); if(c != ' '){ is.putback(c); throw SerializationError("RollbackAction: after-list ' ' not found"); } // Index std::string index_raw; std::getline(is, index_raw, ' '); // add/remove std::string addremove; std::getline(is, addremove, ' '); if(addremove != "add" && addremove != "remove"){ throw SerializationError("RollbackAction: addremove is not add or remove"); }