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

Am I no longer able to block the 'Terminate' event?

Started by Darky_Alan, 12 March 2013 - 04:14 PM
Darky_Alan #1
Posted 12 March 2013 - 05:14 PM
On a previous script I wrote for a door lock, I was able to negate the user from activating 'Terminate' and getting past my password. I haven't ued CC in close to a year possibly (I might be exagerating but it really had been a while, maybe like 4 to 6 months)

Anywho this was the original script:

function clear()
term.clear()
term.setCursorPos(1,1)
end
function lock()
local PASSWORD = "PASSWORD GOES HERE"
local temp = os.pullEvent
local function dkeys(swap)
  disable = {
  [0] = function() os.pullEvent = temp end,
  [1] = function() os.pullEvent = os.pullEventRaw end
}
disable[swap]()
end
clear()
dkeys(0)
print("Enter password:")
write("> ")
local input = read("*")
if input == PASSWORD then
  clear()
  dkeys(0)
  print("Access granted!")
  sleep(1)
  clear()
  textutils.slowPrint("Unlocking...")
  print("Welcome!")
  redstone.setOutput("back", true)
  sleep(4)
  redstone.setOutput("back", false)
  os.reboot()
else
  clear()
  print("Access denied!")
  textutils.slowPrint("Logging username...")
  sleep(1)
  os.reboot()
end
end
lock()

This section in specific allowed me to stop a user from terminating my 'startup' program.

local function dkeys(swap)
  disable = {
  [0] = function() os.pullEvent = temp end,
  [1] = function() os.pullEvent = os.pullEventRaw end
}
disable[swap]()

Have there been changes to the terminate event? My script use to work like a charm, is there some new way to block the user from terminating my program?
Cranium #2
Posted 12 March 2013 - 05:22 PM
Nope. That has not changed. Preventing terminate is still os.pullEvent = os.pullEventRaw in one way or another.
Darky_Alan #3
Posted 12 March 2013 - 05:27 PM
For some reason I am fully able to simply CTRL + T and terminate my startup, is there somethign wrong with my script? If it helps I'm using advanced computers.
Cranium #4
Posted 12 March 2013 - 05:38 PM
It's cause you want to put the items within that function outside of a function so it gets called first.

local oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw
--at the end of your code:
os.pullEvent = oldPull
eleure #5
Posted 12 March 2013 - 05:57 PM
a cleaner version of your code for cranium's benefit.

function clear()
  term.clear()
  term.setCursorPos(1,1)
end

function lock()
  local PASSWORD = "PASSWORD GOES HERE"
  local temp = os.pullEvent
  local function dkeys(swap)
	disable = {
	  [0] = function() os.pullEvent = temp end,
	  [1] = function() os.pullEvent = os.pullEventRaw end
	}
  disable[swap]()
  end

  clear()
  dkeys(0)
  print("Enter password:")
  write("> ")
  local input = read("*")

  if input == PASSWORD then
	clear()
	dkeys(0)
	print("Access granted!")
	sleep(1)
	clear()
	textutils.slowPrint("Unlocking...")
	print("Welcome!")
	redstone.setOutput("back", true)
	sleep(4)
	redstone.setOutput("back", false)
	os.reboot()
  else
	clear()
	print("Access denied!")
	textutils.slowPrint("Logging username...")
	sleep(1)
	os.reboot()
  end
end
lock()

your only issue is that you're mixing up what dkeys(0) and dkeys(1) do. you should be calling dkeys(1) to disable terminate and dkeys(0) to enable it again.

edit: wow, the spacing is wrong for me because of some formatting applied after submit. that's a terrible feature
Darky_Alan #6
Posted 12 March 2013 - 06:09 PM
Thanks, I was mixing them up.


clear()
  dkeys(1)
  print("Enter password:")
  write("> ")
  local input = read("*")
  if input == PASSWORD then
		clear()
		dkeys(0)
		print("Access granted!")
		sleep(1)
		clear()
		textutils.slowPrint("Unlocking...")
		print("Welcome!")
		redstone.setOutput("back", true)
		sleep(4)
		redstone.setOutput("back", false)
		os.reboot()

Fixed it.