Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use minetest.mkdir when available
  • Loading branch information
ShadowNinja committed May 16, 2015
1 parent 5c115e2 commit 6b2fe39
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions worldedit_commands/init.lua
Expand Up @@ -11,17 +11,18 @@ if minetest.place_schematic then
end

dofile(minetest.get_modpath("worldedit_commands") .. "/mark.lua")
dofile(minetest.get_modpath("worldedit_commands") .. "/safe.lua"); safe_region = safe_region or function(callback) return callback end
dofile(minetest.get_modpath("worldedit_commands") .. "/safe.lua")
safe_region = rawget(_G, "safe_region") or function(callback) return callback end

This comment has been minimized.

Copy link
@sfan5

sfan5 May 17, 2015

Collaborator

umm... Wouldn't using minetest.global_exists be less obscure?

This comment has been minimized.

Copy link
@ShadowNinja

ShadowNinja May 18, 2015

Author Collaborator

Yes, but it's less compatible (since that function's fairly recent) and I fixed this in a later commit.


local get_position = function(name) --position 1 retrieval function for when not using `safe_region`
local function get_position(name) --position 1 retrieval function for when not using `safe_region`
local pos1 = worldedit.pos1[name]
if pos1 == nil then
worldedit.player_notify(name, "no position 1 selected")
end
return pos1
end

local get_node = function(name, nodename)
local function get_node(name, nodename)
local node = worldedit.normalize_nodename(nodename)
if not node then
worldedit.player_notify(name, "invalid node name: " .. nodename)
Expand All @@ -30,7 +31,7 @@ local get_node = function(name, nodename)
return node
end

worldedit.player_notify = function(name, message)
function worldedit.player_notify(name, message)

This comment has been minimized.

Copy link
@sfan5

sfan5 May 17, 2015

Collaborator

We don't have a style guide for worldedit, why do these cosmetic changes?

This comment has been minimized.

Copy link
@ShadowNinja
minetest.chat_send_player(name, "WorldEdit -!- " .. message, false)
end

Expand All @@ -56,8 +57,8 @@ worldedit.normalize_nodename = function(nodename)
return nil
end

--determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1)
worldedit.player_axis = function(name)
-- Determines the axis in which a player is facing, returning an axis ("x", "y", or "z") and the sign (1 or -1)
function worldedit.player_axis(name)
local dir = minetest.get_player_by_name(name):get_look_dir()
local x, y, z = math.abs(dir.x), math.abs(dir.y), math.abs(dir.z)
if x > y then
Expand All @@ -70,6 +71,15 @@ worldedit.player_axis = function(name)
return "z", dir.z > 0 and 1 or -1
end

function worldedit.mkdir(path)

This comment has been minimized.

Copy link
@sfan5

sfan5 May 17, 2015

Collaborator

This shouldn't belong to the public API

This comment has been minimized.

Copy link
@ShadowNinja

ShadowNinja May 18, 2015

Author Collaborator

I saw that other internal functions like player_notify were exposed, so I followed that style. I can localize it though.

This comment has been minimized.

Copy link
@sfan5

sfan5 May 27, 2015

Collaborator

Yes, that would be better.

if minetest.mkdir then
minetest.mkdir(path)
else
os.execute('mkdir "' .. path .. '"')
end
end


minetest.register_chatcommand("/about", {
params = "",
description = "Get information about the mod",
Expand Down Expand Up @@ -876,20 +886,22 @@ minetest.register_chatcommand("/save", {
worldedit.player_notify(name, "invalid usage: " .. param)
return
end
if not string.find(param, "^[%w \t.,+-_=!@#$%%^&*()%[%]{};'\"]+$") then
worldedit.player_notify(name, "invalid file name: " .. param)
if not param:find("^[a-zA-Z0-9_%-.]+$") then

This comment has been minimized.

Copy link
@sfan5

sfan5 May 17, 2015

Collaborator

No brackets allowed?

This comment has been minimized.

Copy link
@ShadowNinja

ShadowNinja May 18, 2015

Author Collaborator

No. I'm not sure if they're valid on Windows.

This comment has been minimized.

Copy link
@sfan5

sfan5 May 27, 2015

Collaborator

You can use any kind of brackets in filenames on Windows (source), please add them to the list of allowed characters.

worldedit.player_notify(name, "Disallowed file name: " .. param)
return
end

local result, count = worldedit.serialize(worldedit.pos1[name], worldedit.pos2[name])
local result, count = worldedit.serialize(worldedit.pos1[name],
worldedit.pos2[name])

local path = minetest.get_worldpath() .. "/schems"
-- Create directory if it does not already exist
worldedit.mkdir(path)

local filename = path .. "/" .. param .. ".we"
filename = filename:gsub("\"", "\\\""):gsub("\\", "\\\\") --escape any nasty characters

This comment has been minimized.

Copy link
@est31

est31 May 16, 2015

Contributor

so when security is disabled, you can put nasty characters into the filename?

This comment has been minimized.

Copy link
@ShadowNinja

ShadowNinja May 17, 2015

Author Collaborator

No. This was broken anyways because:

  • It escaped the entire filename, not just the user entered part.
  • It was blacklist-based and missed a lot of things (so ../../../foo.txt was valid)

Also, I replaced it with a whitelist-based method (if not param:find("^[a-zA-Z0-9_%-.]+$" then ...) which is more secure.

os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist
local file, err = io.open(filename, "wb")
if err ~= nil then
worldedit.player_notify(name, "could not save file to \"" .. filename .. "\"")
worldedit.player_notify(name, "Could not save file to \"" .. filename .. "\"")
return
end
file:write(result)
Expand Down Expand Up @@ -1037,24 +1049,31 @@ minetest.register_chatcommand("/luatransform", {

minetest.register_chatcommand("/mtschemcreate", {
params = "<file>",
description = "Save the current WorldEdit region using the Minetest Schematic format to \"(world folder)/schems/<filename>.mts\"",
description = "Save the current WorldEdit region using the Minetest "..
"Schematic format to \"(world folder)/schems/<filename>.mts\"",
privs = {worldedit=true},
func = safe_region(function(name, param)
if param == nil then
worldedit.player_notify(name, "No filename specified")
return
end
if not param:find("^[a-zA-Z0-9_%-.]+$") then
worldedit.player_notify(name, "Disallowed file name: " .. param)
return
end

local path = minetest.get_worldpath() .. "/schems"
local filename = path .. "/" .. param .. ".mts"
filename = filename:gsub("\"", "\\\""):gsub("\\", "\\\\") --escape any nasty characters
os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist
-- Create directory if it does not already exist
worldedit.mkdir(path)

local ret = minetest.create_schematic(worldedit.pos1[name], worldedit.pos2[name], worldedit.prob_list[name], filename)
local filename = path .. "/" .. param .. ".mts"
local ret = minetest.create_schematic(worldedit.pos1[name],
worldedit.pos2[name], worldedit.prob_list[name],
filename)
if ret == nil then
worldedit.player_notify(name, "failed to create Minetest schematic", false)
worldedit.player_notify(name, "Failed to create Minetest schematic", false)
else
worldedit.player_notify(name, "saved Minetest schematic to " .. param, false)
worldedit.player_notify(name, "Saved Minetest schematic to " .. param, false)
end
worldedit.prob_list[name] = {}
end),
Expand Down

0 comments on commit 6b2fe39

Please sign in to comment.