summaryrefslogtreecommitdiff
path: root/init.lua
blob: 7e7f6eade1b6ef9c7497abd2f421a4d9dc6b83eb (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
math.randomseed(os.time())

local function tempname()
	local name = "/tmp/das-"
	name = name..math.random(1000,9999)
	return(name)
end

local on_digiline_receive = function (pos, node, channel, msg)
	local meta = minetest.get_meta(pos)
	local setchan = meta:get_string("channel")
	local dev = meta:get_string("device")
	local intemp = tempname()
	local outtemp = tempname()
	local ret = {}
	local sendlen = 0
	if channel ~= setchan then return end
	if type(msg) ~= "table" then return end
	local cdb = ""
	for i=1,string.len(msg.cdb),1 do
		local byte = string.sub(msg.cdb,i,i)
		cdb = string.format("%s %02X",cdb,string.byte(byte))
	end
	local cmd = "sudo sg_raw -vvvR "..dev..cdb
	if msg.request then
		cmd = cmd.." -r "..msg.request.." -o "..intemp
	end
	if msg.data then
		sendlen = string.len(msg.data)
		local outfile = io.open(outtemp,"w")
		outfile:write(msg.data)
		outfile:close()
		cmd = cmd.." -s "..sendlen.." -i "..outtemp
	end
	print(cmd)
	ret.sk = os.execute(cmd)
	if msg.request then
		local infile = io.open(intemp,"r")
		if infile then
			local indata = infile:read("*all")
			infile:close()
			os.execute("sudo rm "..intemp)
			ret.data = indata
		end
	end
	if msg.data then
		os.execute("sudo rm "..outtemp)
	end
	digiline:receptor_send(pos, digiline.rules.default, channel, ret)
end

minetest.register_node("das:scsi_gateway", {
	tiles = {
	"das_logo.png"
	},
	digiline = 
	{
		receptor = {},
		effector = {action = on_digiline_receive}
	},
	groups = {cracky=2},
	description = "DAS<->SCSI Gateway",
	on_construct = function(pos)
		local meta = minetest.get_meta(pos)
		meta:set_string("formspec","size[8,4;]field[1,1;6,2;channel;Channel;${channel}]field[1,2;6,2;device;Device;${device}]button_exit[2.25,3;3,1;submit;Save]")
		meta:set_string("device","/dev/sda")
		meta:set_string("channel","scsi")
	end,
	on_receive_fields = function(pos, formname, fields, sender)
		local name = sender:get_player_name()
		if minetest.is_protected(pos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then
			minetest.record_protection_violation(pos,name)
			return
		end
		local meta = minetest.get_meta(pos)
		if fields.channel then meta:set_string("channel",fields.channel) end
		if fields.device then meta:set_string("device",fields.device) end
	end,
	sounds = default.node_sound_stone_defaults()
})