From d167aacf4d2cdcc98914cd05153b81e709e0eaa7 Mon Sep 17 00:00:00 2001
From: Tim <t4im@users.noreply.github.com>
Date: Thu, 5 Mar 2015 11:14:30 +0100
Subject: add ownership/protection handling for device configuration

take any available ownership into account before deciding on area protection
---
 autocrafter.lua     |  7 +++++++
 filter-injector.lua | 13 +++++++++++++
 init.lua            | 11 +++++++++++
 sorting_tubes.lua   |  4 ++++
 teleport_tube.lua   |  5 +++--
 vacuum_tubes.lua    |  1 +
 wielder.lua         | 21 ++++++---------------
 7 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/autocrafter.lua b/autocrafter.lua
index 89559ba..3609068 100644
--- a/autocrafter.lua
+++ b/autocrafter.lua
@@ -266,6 +266,7 @@ minetest.register_node("pipeworks:autocrafter", {
 		update_meta(meta, false)
 	end,
 	on_receive_fields = function(pos, formname, fields, sender)
+		if not pipeworks.may_configure(pos, sender) then return end
 		local meta = minetest.get_meta(pos)
 		if fields.on then
 			update_meta(meta, false)
@@ -290,6 +291,7 @@ minetest.register_node("pipeworks:autocrafter", {
 		autocrafterCache[minetest.hash_node_position(pos)] = nil
 	end,
 	allow_metadata_inventory_put = function(pos, listname, index, stack, player)
+		if not pipeworks.may_configure(pos, player) then return 0 end
 		upgrade_autocrafter(pos)
 		local inv = minetest.get_meta(pos):get_inventory()
 		if listname == "recipe" then
@@ -305,6 +307,10 @@ minetest.register_node("pipeworks:autocrafter", {
 		return stack:get_count()
 	end,
 	allow_metadata_inventory_take = function(pos, listname, index, stack, player)
+		if not pipeworks.may_configure(pos, player) then
+			minetest.log("action", string.format("%s attempted to take from autocrafter at %s", player:get_player_name(), minetest.pos_to_string(pos)))
+			return 0
+		end
 		upgrade_autocrafter(pos)
 		local inv = minetest.get_meta(pos):get_inventory()
 		if listname == "recipe" then
@@ -319,6 +325,7 @@ minetest.register_node("pipeworks:autocrafter", {
 		return stack:get_count()
 	end,
 	allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
+		if not pipeworks.may_configure(pos, player) then return 0 end
 		upgrade_autocrafter(pos)
 		local inv = minetest.get_meta(pos):get_inventory()
 		local stack = inv:get_stack(from_list, from_index)
diff --git a/filter-injector.lua b/filter-injector.lua
index 20a9d44..620b7b3 100644
--- a/filter-injector.lua
+++ b/filter-injector.lua
@@ -181,12 +181,25 @@ for _, data in ipairs({
 		end,
 		after_dig_node = pipeworks.after_dig,
 		on_receive_fields = function(pos, formname, fields, sender)
+			if not pipeworks.may_configure(pos, sender) then return end
 			fs_helpers.on_receive_fields(pos, fields)
 			local meta = minetest.get_meta(pos)
 			meta:set_int("slotseq_index", 1)
 			set_filter_formspec(data, meta)
 			set_filter_infotext(data, meta)
 		end,
+		allow_metadata_inventory_put = function(pos, listname, index, stack, player)
+			if not pipeworks.may_configure(pos, player) then return 0 end
+			return stack:get_count()
+		end,
+		allow_metadata_inventory_take = function(pos, listname, index, stack, player)
+			if not pipeworks.may_configure(pos, player) then return 0 end
+			return stack:get_count()
+		end,
+		allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
+			if not pipeworks.may_configure(pos, player) then return 0 end
+			return count
+		end,
 		can_dig = function(pos, player)
 			local meta = minetest.get_meta(pos)
 			local inv = meta:get_inventory()
diff --git a/init.lua b/init.lua
index 5ed4074..9c2ebc8 100644
--- a/init.lua
+++ b/init.lua
@@ -57,6 +57,17 @@ function pipeworks.add_node_box(t, b)
 	end
 end
 
+function pipeworks.may_configure(pos, player)
+	local name = player:get_player_name()
+	local meta = minetest.get_meta(pos)
+	local owner = meta:get_string("owner")
+
+	if owner ~= "" then -- wielders and filters
+		return owner == name
+	end
+	return not minetest.is_protected(pos, name)
+end
+
 function pipeworks.node_is_owned(pos, placer)
 	local ownername = false
 	if type(IsPlayerNodeOwner) == "function" then					-- node_ownership mod
diff --git a/sorting_tubes.lua b/sorting_tubes.lua
index 8c1d49e..6f47f72 100644
--- a/sorting_tubes.lua
+++ b/sorting_tubes.lua
@@ -91,6 +91,7 @@ if pipeworks.enable_mese_tube then
 				end,
 				on_punch = update_formspec,
 				on_receive_fields = function(pos, formname, fields, sender)
+					if not pipeworks.may_configure(pos, sender) then return end
 					fs_helpers.on_receive_fields(pos, fields)
 					update_formspec(pos)
 				end,
@@ -99,6 +100,7 @@ if pipeworks.enable_mese_tube then
 					return true
 				end,
 				allow_metadata_inventory_put = function(pos, listname, index, stack, player)
+					if not pipeworks.may_configure(pos, player) then return 0 end
 					update_formspec(pos) -- For old tubes
 					local inv = minetest.get_meta(pos):get_inventory()
 					local stack_copy = ItemStack(stack)
@@ -107,12 +109,14 @@ if pipeworks.enable_mese_tube then
 					return 0
 				end,
 				allow_metadata_inventory_take = function(pos, listname, index, stack, player)
+					if not pipeworks.may_configure(pos, player) then return 0 end
 					update_formspec(pos) -- For old tubes
 					local inv = minetest.get_meta(pos):get_inventory()
 					inv:set_stack(listname, index, ItemStack(""))
 					return 0
 				end,
 				allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
+					if not pipeworks.may_configure(pos, player) then return 0 end
 					update_formspec(pos) -- For old tubes
 					local inv = minetest.get_meta(pos):get_inventory()
 					inv:set_stack(from_list, from_index, ItemStack(""))
diff --git a/teleport_tube.lua b/teleport_tube.lua
index 4a663d5..17beb06 100644
--- a/teleport_tube.lua
+++ b/teleport_tube.lua
@@ -154,8 +154,9 @@ pipeworks.register_tube("pipeworks:teleport_tube", {
 			meta:set_string("infotext", "unconfigured Teleportation Tube")
 		end,
 		on_receive_fields = function(pos,formname,fields,sender)
-			if not fields.channel then
-				return -- ignore escaping or clientside manipulation of the form
+			if not fields.channel -- ignore escaping or clientside manipulation of the form
+			or not pipeworks.may_configure(pos, sender) then
+				return
 			end
 			local new_channel = tostring(fields.channel):trim()
 
diff --git a/vacuum_tubes.lua b/vacuum_tubes.lua
index f987589..2c14781 100644
--- a/vacuum_tubes.lua
+++ b/vacuum_tubes.lua
@@ -47,6 +47,7 @@ if pipeworks.enable_mese_sand_tube then
 					meta:set_string("infotext", "Adjustable Vacuuming Pneumatic Tube Segment")
 				end,
 				on_receive_fields = function(pos,formname,fields,sender)
+					if not pipeworks.may_configure(pos, sender) then return end
 					local meta = minetest.get_meta(pos)
 					local dist = tonumber(fields.dist)
 					if dist then
diff --git a/wielder.lua b/wielder.lua
index 88f8e67..8a0e45b 100644
--- a/wielder.lua
+++ b/wielder.lua
@@ -230,27 +230,18 @@ local function register_wielder(data)
 				pipeworks.scan_for_tube_objects(pos)
 			end,
 			on_punch = data.fixup_node,
-			allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
-				local meta = minetest.get_meta(pos)
-				if player:get_player_name() ~= meta:get_string("owner") and meta:get_string("owner") ~= "" then
-					return 0
-				end
-				return count
-			end,
 			allow_metadata_inventory_put = function(pos, listname, index, stack, player)
-				local meta = minetest.get_meta(pos)
-				if player:get_player_name() ~= meta:get_string("owner") and meta:get_string("owner") ~= "" then
-					return 0
-				end
+				if not pipeworks.may_configure(pos, player) then return 0 end
 				return stack:get_count()
 			end,
 			allow_metadata_inventory_take = function(pos, listname, index, stack, player)
-				local meta = minetest.get_meta(pos)
-				if player:get_player_name() ~= meta:get_string("owner") and meta:get_string("owner") ~= "" then
-					return 0
-				end
+				if not pipeworks.may_configure(pos, player) then return 0 end
 				return stack:get_count()
 			end,
+			allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
+				if not pipeworks.may_configure(pos, player) then return 0 end
+				return count
+			end
 		})
 	end
 end
-- 
cgit v1.2.3