3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "lua_api/l_util.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
24 #include "cpp_api/s_async.h"
25 #include "serialization.h"
26 #include "json/json.h"
27 #include "cpp_api/s_security.h"
28 #include "areastore.h"
34 #include "util/auth.h"
38 // Writes a line to the logger.
39 // The one-argument version logs to infostream.
40 // The two-argument version accepts a log level.
41 // Either the special case "deprecated" for deprecation notices, or any specified in
42 // Logger::stringToLevel(name).
43 int ModApiUtil::l_log(lua_State *L)
47 LogLevel level = LL_NONE;
48 if (lua_isnone(L, 2)) {
49 text = luaL_checkstring(L, 1);
51 std::string name = luaL_checkstring(L, 1);
52 text = luaL_checkstring(L, 2);
53 if (name == "deprecated") {
54 log_deprecated(L, text);
57 level = Logger::stringToLevel(name);
58 if (level == LL_MAX) {
59 warningstream << "Tried to log at unknown level '" << name
60 << "'. Defaulting to \"none\"." << std::endl;
64 g_logger.log(level, text);
68 #define CHECK_SECURE_SETTING(L, name) \
69 if (name.compare(0, 7, "secure.") == 0) {\
70 lua_pushliteral(L, "Attempt to set secure setting.");\
74 // setting_set(name, value)
75 int ModApiUtil::l_setting_set(lua_State *L)
78 std::string name = luaL_checkstring(L, 1);
79 std::string value = luaL_checkstring(L, 2);
80 CHECK_SECURE_SETTING(L, name);
81 g_settings->set(name, value);
86 int ModApiUtil::l_setting_get(lua_State *L)
89 const char *name = luaL_checkstring(L, 1);
91 std::string value = g_settings->get(name);
92 lua_pushstring(L, value.c_str());
93 } catch(SettingNotFoundException &e){
99 // setting_setbool(name)
100 int ModApiUtil::l_setting_setbool(lua_State *L)
102 NO_MAP_LOCK_REQUIRED;
103 std::string name = luaL_checkstring(L, 1);
104 bool value = lua_toboolean(L, 2);
105 CHECK_SECURE_SETTING(L, name);
106 g_settings->setBool(name, value);
110 // setting_getbool(name)
111 int ModApiUtil::l_setting_getbool(lua_State *L)
113 NO_MAP_LOCK_REQUIRED;
114 const char *name = luaL_checkstring(L, 1);
116 bool value = g_settings->getBool(name);
117 lua_pushboolean(L, value);
118 } catch(SettingNotFoundException &e){
125 int ModApiUtil::l_setting_save(lua_State *L)
127 NO_MAP_LOCK_REQUIRED;
128 if(g_settings_path != "")
129 g_settings->updateConfigFile(g_settings_path.c_str());
133 // parse_json(str[, nullvalue])
134 int ModApiUtil::l_parse_json(lua_State *L)
136 NO_MAP_LOCK_REQUIRED;
138 const char *jsonstr = luaL_checkstring(L, 1);
140 // Use passed nullvalue or default to nil
142 if (lua_isnone(L, nullindex)) {
144 nullindex = lua_gettop(L);
151 std::istringstream stream(jsonstr);
153 if (!reader.parse(stream, root)) {
154 errorstream << "Failed to parse json data "
155 << reader.getFormattedErrorMessages();
156 errorstream << "data: \"" << jsonstr << "\""
163 if (!push_json_value(L, root, nullindex)) {
164 errorstream << "Failed to parse json data, "
165 << "depth exceeds lua stack limit" << std::endl;
166 errorstream << "data: \"" << jsonstr << "\"" << std::endl;
172 // write_json(data[, styled]) -> string or nil and error message
173 int ModApiUtil::l_write_json(lua_State *L)
175 NO_MAP_LOCK_REQUIRED;
178 if (!lua_isnone(L, 2)) {
179 styled = lua_toboolean(L, 2);
185 read_json_value(L, root, 1);
186 } catch (SerializationError &e) {
188 lua_pushstring(L, e.what());
194 Json::StyledWriter writer;
195 out = writer.write(root);
197 Json::FastWriter writer;
198 out = writer.write(root);
200 lua_pushlstring(L, out.c_str(), out.size());
204 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
205 int ModApiUtil::l_get_dig_params(lua_State *L)
207 NO_MAP_LOCK_REQUIRED;
208 std::map<std::string, int> groups;
209 read_groups(L, 1, groups);
210 ToolCapabilities tp = read_tool_capabilities(L, 2);
211 if(lua_isnoneornil(L, 3))
212 push_dig_params(L, getDigParams(groups, &tp));
214 push_dig_params(L, getDigParams(groups, &tp,
215 luaL_checknumber(L, 3)));
219 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
220 int ModApiUtil::l_get_hit_params(lua_State *L)
222 NO_MAP_LOCK_REQUIRED;
223 std::map<std::string, int> groups;
224 read_groups(L, 1, groups);
225 ToolCapabilities tp = read_tool_capabilities(L, 2);
226 if(lua_isnoneornil(L, 3))
227 push_hit_params(L, getHitParams(groups, &tp));
229 push_hit_params(L, getHitParams(groups, &tp,
230 luaL_checknumber(L, 3)));
234 // get_password_hash(name, raw_password)
235 int ModApiUtil::l_get_password_hash(lua_State *L)
237 NO_MAP_LOCK_REQUIRED;
238 std::string name = luaL_checkstring(L, 1);
239 std::string raw_password = luaL_checkstring(L, 2);
240 std::string hash = translatePassword(name, raw_password);
241 lua_pushstring(L, hash.c_str());
246 int ModApiUtil::l_is_yes(lua_State *L)
248 NO_MAP_LOCK_REQUIRED;
250 lua_getglobal(L, "tostring"); // function to be called
251 lua_pushvalue(L, 1); // 1st argument
252 lua_call(L, 1, 1); // execute function
253 std::string str(lua_tostring(L, -1)); // get result
256 bool yes = is_yes(str);
257 lua_pushboolean(L, yes);
261 int ModApiUtil::l_get_builtin_path(lua_State *L)
263 NO_MAP_LOCK_REQUIRED;
265 std::string path = porting::path_share + DIR_DELIM + "builtin";
266 lua_pushstring(L, path.c_str());
270 // compress(data, method, level)
271 int ModApiUtil::l_compress(lua_State *L)
273 NO_MAP_LOCK_REQUIRED;
276 const char *data = luaL_checklstring(L, 1, &size);
279 if (!lua_isnone(L, 3) && !lua_isnil(L, 3))
280 level = luaL_checknumber(L, 3);
282 std::ostringstream os;
283 compressZlib(std::string(data, size), os, level);
285 std::string out = os.str();
287 lua_pushlstring(L, out.data(), out.size());
291 // decompress(data, method)
292 int ModApiUtil::l_decompress(lua_State *L)
294 NO_MAP_LOCK_REQUIRED;
297 const char *data = luaL_checklstring(L, 1, &size);
299 std::istringstream is(std::string(data, size));
300 std::ostringstream os;
301 decompressZlib(is, os);
303 std::string out = os.str();
305 lua_pushlstring(L, out.data(), out.size());
310 int ModApiUtil::l_mkdir(lua_State *L)
312 NO_MAP_LOCK_REQUIRED;
313 const char *path = luaL_checkstring(L, 1);
314 CHECK_SECURE_PATH_OPTIONAL(L, path);
315 lua_pushboolean(L, fs::CreateAllDirs(path));
319 // get_dir_list(path, is_dir)
320 int ModApiUtil::l_get_dir_list(lua_State *L)
322 NO_MAP_LOCK_REQUIRED;
323 const char *path = luaL_checkstring(L, 1);
324 short is_dir = lua_isboolean(L, 2) ? lua_toboolean(L, 2) : -1;
326 CHECK_SECURE_PATH_OPTIONAL(L, path);
328 std::vector<fs::DirListNode> list = fs::GetDirListing(path);
333 for (size_t i = 0; i < list.size(); i++) {
334 if (is_dir == -1 || is_dir == list[i].dir) {
335 lua_pushstring(L, list[i].name.c_str());
336 lua_rawseti(L, -2, ++index);
343 int ModApiUtil::l_request_insecure_environment(lua_State *L)
345 NO_MAP_LOCK_REQUIRED;
346 if (!ScriptApiSecurity::isSecure(L)) {
347 lua_getglobal(L, "_G");
350 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
351 if (!lua_isstring(L, -1)) {
355 const char *mod_name = lua_tostring(L, -1);
356 std::string trusted_mods = g_settings->get("secure.trusted_mods");
357 std::vector<std::string> mod_list = str_split(trusted_mods, ',');
358 if (std::find(mod_list.begin(), mod_list.end(), mod_name) == mod_list.end()) {
362 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
367 void ModApiUtil::Initialize(lua_State *L, int top)
371 API_FCT(setting_set);
372 API_FCT(setting_get);
373 API_FCT(setting_setbool);
374 API_FCT(setting_getbool);
375 API_FCT(setting_save);
380 API_FCT(get_dig_params);
381 API_FCT(get_hit_params);
383 API_FCT(get_password_hash);
387 API_FCT(get_builtin_path);
393 API_FCT(get_dir_list);
395 API_FCT(request_insecure_environment);
398 void ModApiUtil::InitializeAsync(AsyncEngine& engine)
402 //ASYNC_API_FCT(setting_set);
403 ASYNC_API_FCT(setting_get);
404 //ASYNC_API_FCT(setting_setbool);
405 ASYNC_API_FCT(setting_getbool);
406 //ASYNC_API_FCT(setting_save);
408 ASYNC_API_FCT(parse_json);
409 ASYNC_API_FCT(write_json);
411 ASYNC_API_FCT(is_yes);
413 ASYNC_API_FCT(get_builtin_path);
415 ASYNC_API_FCT(compress);
416 ASYNC_API_FCT(decompress);
418 ASYNC_API_FCT(mkdir);
419 ASYNC_API_FCT(get_dir_list);