This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
crackroach's profile picture

[HELP]

Started by crackroach, 04 December 2012 - 08:38 AM
crackroach #1
Posted 04 December 2012 - 09:38 AM
I'm trying to do a security check for one of my program. But the command peripheral.getType() need a side as a parameter. I'v tried to do a table containing all the sides' but sadly it does'nt accept it… can you help me?


while true do

local side = {"top", "bottom", "right", "left", "back", "front"}
local peripheralType = {"modem", "computer", "turtle", "drive"}
local intrusionTest = peripheral.getType( "side" )

if intrusionTest == peripheralType then
if intusionTest == side[5] and peripheralType[1] then
sleep(0.2)
else
bank.lock()
end
else
sleep(0.2)
end
end
Lyqyd #2
Posted 04 December 2012 - 09:53 AM
You're using tables incorrectly. You need to pass peripheral.getType a string containing the name of a side, not a string containing the name of a table containing the names of several sides. Try using side[1] etc. You can also loop through a table using pairs(), ipairs(), or 1 and the length of the table in a for loop, as you may have seen elsewhere. The rest of your code has similar issues.
ChunLing #3
Posted 04 December 2012 - 09:55 AM
You also don't need the side table yourself, just use rs.getSides().

Describe the desired behavior of this program, I can't quite figure out what you're trying to do. Do you want to exclude a modem on the back from being considered an intruder?
Edited on 04 December 2012 - 08:58 AM
crackroach #4
Posted 04 December 2012 - 01:14 PM
You also don't need the side table yourself, just use rs.getSides().

Describe the desired behavior of this program, I can't quite figure out what you're trying to do. Do you want to exclude a modem on the back from being considered an intruder?

It's supposed to be password locked(it is not a vulgar lock though :P/> ), that's why i need to restrain peripheral uses. The tables were written badly because it was my last hope of making it work without having to set up 24 'IF'… can you help me making it work? If you do can you please leave traces on your work?

Thanks
crackroach

Here is the full code(it's WIP by the way and it's also a 'dofile')
Spoiler


bank = {}



local oSleep = function()
sleep(180)
end


function bank.unlock( n )
local name = read()
local file
file = fs.open("saves/"..name, "r")
local fileD = {}
local line = file.readLine()
repeat
table.insert(fileD, line)
line = file.readLine()
until line == nil

if file == true then
local password = read()
readPass = fileD[2]

if readPass == password then
n = n - n
os.reboot()
else
term.setCursorPos(11, 11)
print("Wrong password")
term.setCursorPos(11, 12)
print("Setting extra security")
sleep(2)
term.setCursorPos(1, 11)
term.clearLine()
term.setCursorPos(1, 12)
term.clearLine()
n = n + 118
return n
end
else
term.setCursorPos(11, 11)
print("Wrong username")
term.setCursorPos(11, 12)
print("Setting extra security")
sleep(2)
term.setCursorPos(1, 11)
term.clearLine()
term.setCursorPos(1, 12)
term.clearLine()
n = n + 118
return n
end
end

function bank.lock()
term.clear()
tb.wC("Error "..errorType)
write("\n")
tB.wC("System going in safe mode")
sleep(2)
term.clear()
tB.wC("SYSTEM LOCKED")
n = 180
for qwerty = 1, n do
n = n - 1
local touch, me = os.pullEvent(key)

if touch == "key" then
if me == 29 and 30 and 32 and 50 then
bank.unlocks( n )
end
end
end
end

while true do

local side = {"top", "bottom", "right", "left", "back", "front"}
local peripheralType = {"modem", "computer", "turtle", "drive"}
local intrusionTest = peripheral.getType( "side" )

if intrusionTest == peripheralType then
if intusionTest == side[5] and peripheralType[1] then
sleep(0.2)
else
bank.lock()
end
else
sleep(0.2)
end
end
Edited on 04 December 2012 - 01:32 PM
ChunLing #5
Posted 04 December 2012 - 03:56 PM
Eh, I could do that but I need to know whether you're trying to permit or restrict that modem on the back. If you're allowing a modem on the back, I don't see how it could be any harm to have a modem anywhere else, so I'll proceed with the idea that a modem is not a problem.

Here's something quick, see what you can do with it. First, a function to use to check for prohibited peripherals:
local function intrusionTest()
    local prohibit = {"computer","turtle","drive"}
    for k,v in pairs(rs.getSides()) do
        for i = 1,#peripheralType do
            if peripheral.getType(v) == prohibit[i] then return v,prohibit[i] end
        end
    end
return nil end
It should scan for peripherals matching the types in the prohibit list. If it finds one, it returns the side and the type found. It only need to find one to return. Otherwise it returns nil.

Then we call this somewhere and use the result to decide to do something. Like:
if intrusionTest() then bank.lock() end

Or you can assign some variables to hold the returns and check them, if you allow those peripherals in some places and not others…though what the side has to do with it I don't know.
crackroach #6
Posted 04 December 2012 - 04:17 PM
Eh, I could do that but I need to know whether you're trying to permit or restrict that modem on the back. If you're allowing a modem on the back, I don't see how it could be any harm to have a modem anywhere else, so I'll proceed with the idea that a modem is not a problem.

Here's something quick, see what you can do with it. First, a function to use to check for prohibited peripherals:
local function intrusionTest()
	local prohibit = {"computer","turtle","drive"}
	for k,v in pairs(rs.getSides()) do
		for i = 1,#peripheralType do
			if peripheral.getType(v) == prohibit[i] then return v,prohibit[i] end
		end
	end
return nil end
It should scan for peripherals matching the types in the prohibit list. If it finds one, it returns the side and the type found. It only need to find one to return. Otherwise it returns nil.

Then we call this somewhere and use the result to decide to do something. Like:
if intrusionTest() then bank.lock() end

Or you can assign some variables to hold the returns and check them, if you allow those peripherals in some places and not others…though what the side has to do with it I don't know.

Thanks, it's pretty comprehensive. I thought about the modem problem and i came to the conclusion that only computer, turtle and drive can be harmful, so your example is functional. I guess the side has nothing to do with anything now, i wanted to put only one modem in the back, but the problem is solved.

I need an explanation for something though: if i put that function in a 'dofile' will it work? and that function should be a while loop in order to scan every second or so, right?
ChunLing #7
Posted 04 December 2012 - 09:25 PM
The function definition needs to be somewhere near the top of your program, how often you call it is up to you, but every half second (or even less) would probably be best. If a turtle is programmed to sneak in and reboot your computer, I have no idea how long you have between the time you could detect it and the time that it'll shut you down, but it's probably not long. Do some tests to see how short a loop you need for it to work reliably.

Given that this needs to run so often, you might want to define prohibit outside the function so it doesn't have to be reallocated each time. Though that's really a minor issue until you get up into running it hundreds of times a second.
crackroach #8
Posted 05 December 2012 - 04:26 AM
The function definition needs to be somewhere near the top of your program, how often you call it is up to you, but every half second (or even less) would probably be best. If a turtle is programmed to sneak in and reboot your computer, I have no idea how long you have between the time you could detect it and the time that it'll shut you down, but it's probably not long. Do some tests to see how short a loop you need for it to work reliably.

Given that this needs to run so often, you might want to define prohibit outside the function so it doesn't have to be reallocated each time. Though that's really a minor issue until you get up into running it hundreds of times a second.
I tried calling it parallel with my other function, it doesn't work, if i put it in a while loop, it doesn't work. The problem is that the intrusionTest function takes all the place. I've been on the CC wiki, i found this os.pullEvent(peripheral). I'll try to put something together and i'll post it here. we never know :lol:/>

Thanks for your help
ChunLing #9
Posted 05 December 2012 - 10:41 AM
This function is an "instant return" type, it shouldn't be used in parallel with other functions. It also shouldn't be in it's own while loop (same reason, basically). You could alter it to make it so that it had it's own loop and continued running until it found a peripheral on the prohibit list, and then maybe it would work in parallel, though that's not how I would usually design things.
crackroach #10
Posted 05 December 2012 - 06:24 PM
This function is an "instant return" type, it shouldn't be used in parallel with other functions. It also shouldn't be in it's own while loop (same reason, basically). You could alter it to make it so that it had it's own loop and continued running until it found a peripheral on the prohibit list, and then maybe it would work in parallel, though that's not how I would usually design things.
How would you do it? honestly i'm out of ideas…
ChunLing #11
Posted 05 December 2012 - 06:41 PM
I guess that depends on how the rest of the program is designed…which I don't know in detail. Making this into a function that loops every time a peripheral event occurs until it does detect a prohibited peripheral and then running it in parallel with your main program loop should work, it's just that I would do it differently if I were making the program.

But let's do it as a parallel friendly function.

local function intrusionTest()
  local prohibit = {"computer","turtle","drive"}
  while true do
    os.pullEvent("peripheral")
	for k,v in pairs(rs.getSides()) do
	  for i = 1,#peripheralType do
	    if peripheral.getType(v) == prohibit[i] then return v,prohibit[i] end
	  end
	end
  end
end
Now the function only ever ends if it finds a prohibited device, as long as it doesn't it just waits for a peripheral event and checks again. Note that this solves a problem I mentioned earlier, in that the local declaration of prohibit doesn't have to be repeated every time the detection loop runs, so there's that.

Normally, if I were writing a program of this sort, I'd try to make an os.pullEvent loop the heart of the program. That would maximize the amount of control I'd have over everything. It also tends to mean that you have to write all your own input functions, though. So a bit of a hassle, if the function above will work.
crackroach #12
Posted 05 December 2012 - 06:57 PM
I guess that depends on how the rest of the program is designed…which I don't know in detail. Making this into a function that loops every time a peripheral event occurs until it does detect a prohibited peripheral and then running it in parallel with your main program loop should work, it's just that I would do it differently if I were making the program.

But let's do it as a parallel friendly function.

local function intrusionTest()
  local prohibit = {"computer","turtle","drive"}
  while true do
	os.pullEvent("peripheral")
	for k,v in pairs(rs.getSides()) do
	  for i = 1,#peripheralType do
		if peripheral.getType(v) == prohibit[i] then return v,prohibit[i] end
	  end
	end
  end
end
Now the function only ever ends if it finds a prohibited device, as long as it doesn't it just waits for a peripheral event and checks again. Note that this solves a problem I mentioned earlier, in that the local declaration of prohibit doesn't have to be repeated every time the detection loop runs, so there's that.

Normally, if I were writing a program of this sort, I'd try to make an os.pullEvent loop the heart of the program. That would maximize the amount of control I'd have over everything. It also tends to mean that you have to write all your own input functions, though. So a bit of a hassle, if the function above will work.
Would'nt it be simplier if that function could operate on is own. What i mean by that is simply activate it and forget about it…

My program is designed like every function call an other when it is abut to end. like so:

--gross representation
function three(a, B)/>
   c = "stuff"
   print(a, b, c)
end

function two(a)
   b = "random stuff"
   three(a, B)/>
end

function one()
   a = "other stuff"
   two(a)
end

one()
ChunLing #13
Posted 05 December 2012 - 09:49 PM
Huh. Well, in that case, with the hopefully more parallel friendly version above, you can use parallel.waitForAny(one, intrusionTest) where you're now using one(). If you want to alter intrusionTest to work more like the structure you've outlined, then replace "return v,prohibit" with "bank.lock()".
crackroach #14
Posted 06 December 2012 - 05:55 AM
Huh. Well, in that case, with the hopefully more parallel friendly version above, you can use parallel.waitForAny(one, intrusionTest) where you're now using one(). If you want to alter intrusionTest to work more like the structure you've outlined, then replace "return v,prohibit" with "bank.lock()".

Ok, thanks for your help and patience.