aboutsummaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_server.cpp
blob: 1ce2f9d45d029fa8082364d966ba5544934971f6 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
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

	int error_handler = PUSH_ERROR_HANDLER(L);
	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());
	PCALL_RES(lua_pcall(L, 1, 1, error_handler));
	lua_remove(L, -2); // Remove auth handler
	lua_remove(L, error_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");
	}

	setOriginFromTable(-1);

	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 = readParam<bool>(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

	int error_handler = PUSH_ERROR_HANDLER(L);
	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());
	PCALL_RES(lua_pcall(L, 2, 0, error_handler));
	lua_pop(L, 1); // Pop error handler
}

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

	int error_handler = PUSH_ERROR_HANDLER(L);
	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());
	PCALL_RES(lua_pcall(L, 2, 1, error_handler));
	lua_remove(L, error_handler);
	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());
	runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
	return readParam<bool>(L, -1);
}

void ScriptApiServer::on_mods_loaded()
{
	SCRIPTAPI_PRECHECKHEADER

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

void ScriptApiServer::on_shutdown()
{
	SCRIPTAPI_PRECHECKHEADER

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

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

	// Push function onto stack
	lua_getglobal(L, "core");
	lua_getfield(L, -1, "format_chat_message");

	// Push arguments onto stack
	lua_pushstring(L, name.c_str());
	lua_pushstring(L, message.c_str());

	// Actually call the function
	lua_call(L, 2, 1);

	// Fetch return value
	std::string ret = lua_tostring(L, -1);
	lua_pop(L, 1);

	return ret;
}
s="hl opt">)) DEFINEKEY1(KEY_LWIN, N_("Left Windows")) DEFINEKEY1(KEY_RWIN, N_("Right Windows")) DEFINEKEY1(KEY_NUMPAD0, N_("Numpad 0")) // These are not assigned to a char DEFINEKEY1(KEY_NUMPAD1, N_("Numpad 1")) // to prevent interference with KEY_KEY_[0-9]. DEFINEKEY1(KEY_NUMPAD2, N_("Numpad 2")) DEFINEKEY1(KEY_NUMPAD3, N_("Numpad 3")) DEFINEKEY1(KEY_NUMPAD4, N_("Numpad 4")) DEFINEKEY1(KEY_NUMPAD5, N_("Numpad 5")) DEFINEKEY1(KEY_NUMPAD6, N_("Numpad 6")) DEFINEKEY1(KEY_NUMPAD7, N_("Numpad 7")) DEFINEKEY1(KEY_NUMPAD8, N_("Numpad 8")) DEFINEKEY1(KEY_NUMPAD9, N_("Numpad 9")) DEFINEKEY1(KEY_MULTIPLY, N_("Numpad *")) DEFINEKEY1(KEY_ADD, N_("Numpad +")) DEFINEKEY1(KEY_SEPARATOR, N_("Numpad .")) DEFINEKEY1(KEY_SUBTRACT, N_("Numpad -")) DEFINEKEY1(KEY_DECIMAL, NULL) DEFINEKEY1(KEY_DIVIDE, N_("Numpad /")) DEFINEKEY4(1) DEFINEKEY4(2) DEFINEKEY4(3) DEFINEKEY4(4) DEFINEKEY4(5) DEFINEKEY4(6) DEFINEKEY4(7) DEFINEKEY4(8) DEFINEKEY4(9) DEFINEKEY4(10) DEFINEKEY4(11) DEFINEKEY4(12) DEFINEKEY4(13) DEFINEKEY4(14) DEFINEKEY4(15) DEFINEKEY4(16) DEFINEKEY4(17) DEFINEKEY4(18) DEFINEKEY4(19) DEFINEKEY4(20) DEFINEKEY4(21) DEFINEKEY4(22) DEFINEKEY4(23) DEFINEKEY4(24) DEFINEKEY1(KEY_NUMLOCK, N_("Num Lock")) DEFINEKEY1(KEY_SCROLL, N_("Scroll Lock")) DEFINEKEY1(KEY_LSHIFT, N_("Left Shift")) DEFINEKEY1(KEY_RSHIFT, N_("Right Shift")) DEFINEKEY1(KEY_LCONTROL, N_("Left Control")) DEFINEKEY1(KEY_RCONTROL, N_("Right Control")) DEFINEKEY1(KEY_LMENU, N_("Left Menu")) DEFINEKEY1(KEY_RMENU, N_("Right Menu")) // Rare/weird keys DEFINEKEY1(KEY_KANA, "Kana") DEFINEKEY1(KEY_HANGUEL, "Hangul") DEFINEKEY1(KEY_HANGUL, "Hangul") DEFINEKEY1(KEY_JUNJA, "Junja") DEFINEKEY1(KEY_FINAL, "Final") DEFINEKEY1(KEY_KANJI, "Kanji") DEFINEKEY1(KEY_HANJA, "Hanja") DEFINEKEY1(KEY_ESCAPE, N_("IME Escape")) DEFINEKEY1(KEY_CONVERT, N_("IME Convert")) DEFINEKEY1(KEY_NONCONVERT, N_("IME Nonconvert")) DEFINEKEY1(KEY_ACCEPT, N_("IME Accept")) DEFINEKEY1(KEY_MODECHANGE, N_("IME Mode Change")) DEFINEKEY1(KEY_APPS, N_("Apps")) DEFINEKEY1(KEY_SLEEP, N_("Sleep")) #if !(IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 7 && IRRLICHT_VERSION_REVISION < 3) DEFINEKEY1(KEY_OEM_1, "OEM 1") // KEY_OEM_[0-9] and KEY_OEM_102 are assigned to multiple DEFINEKEY1(KEY_OEM_2, "OEM 2") // different chars (on different platforms too) and thus w/o char DEFINEKEY1(KEY_OEM_3, "OEM 3") DEFINEKEY1(KEY_OEM_4, "OEM 4") DEFINEKEY1(KEY_OEM_5, "OEM 5") DEFINEKEY1(KEY_OEM_6, "OEM 6") DEFINEKEY1(KEY_OEM_7, "OEM 7") DEFINEKEY1(KEY_OEM_8, "OEM 8") DEFINEKEY1(KEY_OEM_AX, "OEM AX") DEFINEKEY1(KEY_OEM_102, "OEM 102") #endif DEFINEKEY1(KEY_ATTN, "Attn") DEFINEKEY1(KEY_CRSEL, "CrSel") DEFINEKEY1(KEY_EXSEL, "ExSel") DEFINEKEY1(KEY_EREOF, N_("Erase EOF")) DEFINEKEY1(KEY_PLAY, N_("Play")) DEFINEKEY1(KEY_ZOOM, N_("Zoom")) DEFINEKEY1(KEY_PA1, "PA1") DEFINEKEY1(KEY_OEM_CLEAR, N_("OEM Clear")) // Keys without Irrlicht keycode DEFINEKEY5("!") DEFINEKEY5("\"") DEFINEKEY5("#") DEFINEKEY5("$") DEFINEKEY5("%") DEFINEKEY5("&") DEFINEKEY5("'") DEFINEKEY5("(") DEFINEKEY5(")") DEFINEKEY5("*") DEFINEKEY5("/") DEFINEKEY5(":") DEFINEKEY5(";") DEFINEKEY5("<") DEFINEKEY5("=") DEFINEKEY5(">") DEFINEKEY5("?") DEFINEKEY5("@") DEFINEKEY5("[") DEFINEKEY5("\\") DEFINEKEY5("]") DEFINEKEY5("^") DEFINEKEY5("_") }; #undef N_ struct table_key lookup_keyname(const char *name) { for (const auto &table_key : table) { if (strcmp(table_key.Name, name) == 0) return table_key; } throw UnknownKeycode(name); } struct table_key lookup_keykey(irr::EKEY_CODE key) { for (const auto &table_key : table) { if (table_key.Key == key) return table_key; } std::ostringstream os; os << "<Keycode " << (int) key << ">"; throw UnknownKeycode(os.str().c_str()); } struct table_key lookup_keychar(wchar_t Char) { for (const auto &table_key : table) { if (table_key.Char == Char) return table_key; } std::ostringstream os; os << "<Char " << hex_encode((char*) &Char, sizeof(wchar_t)) << ">"; throw UnknownKeycode(os.str().c_str()); } KeyPress::KeyPress(const char *name) { if (strlen(name) == 0) { Key = irr::KEY_KEY_CODES_COUNT; Char = L'\0'; m_name = ""; return; } if (strlen(name) <= 4) { // Lookup by resulting character int chars_read = mbtowc(&Char, name, 1); FATAL_ERROR_IF(chars_read != 1, "Unexpected multibyte character"); try { struct table_key k = lookup_keychar(Char); m_name = k.Name; Key = k.Key; return; } catch (UnknownKeycode &e) {}; } else { // Lookup by name m_name = name; try { struct table_key k = lookup_keyname(name); Key = k.Key; Char = k.Char; return; } catch (UnknownKeycode &e) {}; } // It's not a known key, complain and try to do something Key = irr::KEY_KEY_CODES_COUNT; int chars_read = mbtowc(&Char, name, 1); FATAL_ERROR_IF(chars_read != 1, "Unexpected multibyte character"); m_name = ""; warningstream << "KeyPress: Unknown key '" << name << "', falling back to first char." << std::endl; } KeyPress::KeyPress(const irr::SEvent::SKeyInput &in, bool prefer_character) { if (prefer_character) Key = irr::KEY_KEY_CODES_COUNT; else Key = in.Key; Char = in.Char; try { if (valid_kcode(Key)) m_name = lookup_keykey(Key).Name; else m_name = lookup_keychar(Char).Name; } catch (UnknownKeycode &e) { m_name = ""; }; } const char *KeyPress::sym() const { return m_name.c_str(); } const char *KeyPress::name() const { if (m_name.empty()) return ""; const char *ret; if (valid_kcode(Key)) ret = lookup_keykey(Key).LangName; else ret = lookup_keychar(Char).LangName; return ret ? ret : "<Unnamed key>"; } const KeyPress EscapeKey("KEY_ESCAPE"); const KeyPress CancelKey("KEY_CANCEL"); /* Key config */ // A simple cache for quicker lookup std::unordered_map<std::string, KeyPress> g_key_setting_cache; KeyPress getKeySetting(const char *settingname) { std::unordered_map<std::string, KeyPress>::iterator n; n = g_key_setting_cache.find(settingname); if (n != g_key_setting_cache.end()) return n->second; KeyPress k(g_settings->get(settingname).c_str()); g_key_setting_cache[settingname] = k; return k; } void clearKeyCache() { g_key_setting_cache.clear(); } irr::EKEY_CODE keyname_to_keycode(const char *name) { return lookup_keyname(name).Key; }