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

Computer Lock Program (simple)

Started by Shuudoushi, 24 September 2012 - 04:32 AM
Shuudoushi #1
Posted 24 September 2012 - 06:32 AM
I have made this simple program for locking your computer with a password. This will make it so only you (and whom ever you give the password to) can access the files on it. Now enough talking here is the code! (For more easy reading http://pastebin.com/Hcj7upAe)


local password = "<password>" -- Change this to what you want your password to be.
local oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw

while true do
 term.clear()
 term.setCursorPos(1,1)
 write("Password:")
 local input = read("*")
 if input == password then
  print("Welcome!")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
  break
 else
  print("Password incorect!")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
  print("Goodbye!")
  sleep(2)
  os.shutdown()
 end
end

os.pullEvent = oldPull

You will need to rename it startup and if you want, make a VERY simple program called "lock" that looks like this.


shell.run("startup")

This will allow you to lock your computer easily!

Hope you like! If you need any help or have some pointers let me know!

EDIT: I have taken Mtdj2's advise and moved the anti-terminate code outside of the loop.
EDIT: Compressed to coding down a bit to take up less space.
Mtdj2 #2
Posted 24 September 2012 - 10:59 AM
A thing: Move the function define:

while true do
function os.pullEvent()
local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
  os.reboot() -- Change this to os.shutdown() if you want the computer to shutdown instead
end
  return event, p1, p2, p3, p4, p5
end
term.clear()
Make it like:

function os.pullEvent()
local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then end
return event, p1, p2, p3, p4, p5
end
while true do
term.clear()
You could put in a block of code to do instead of termination, like you did:

if event == "terminate" then os.shutdown() end
If you dont move the function out of the while loop, you will get lag, and you will declare the function many times.
Hope i helped making it better.
Mr. Fang #3
Posted 24 September 2012 - 01:42 PM
Do you want to have other passwords to open the door? This would keep your passcode for yourself.
The code would start lke this:

local password = "<password>"
local password = "<second passcode>"
....Rest of Coding....

So basically, you would just accept two different inputs.

(Vote up if this helped)
Fatal_Exception #4
Posted 24 September 2012 - 02:07 PM
No, that just changes the value of password. Only the second password will work.
Mr. Fang #5
Posted 24 September 2012 - 05:57 PM
No, that just changes the value of password. Only the second password will work.
Wait really? It worked for me when I did it.
Cranium #6
Posted 24 September 2012 - 08:07 PM
Wait really? It worked for me when I did it.
No. It didn't.
that code sets the value of password once, then instantly change the value. If you want separate passwords, you want something like this:

local password1 = "password1"
local password2 = "password2"
while true do
local input = read("*")
if input == password1 or input == password2 then
print("password successful"
end
end
Shuudoushi #7
Posted 24 September 2012 - 09:13 PM
A thing: Move the function define:

while true do
function os.pullEvent()
local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
  os.reboot() -- Change this to os.shutdown() if you want the computer to shutdown instead
end
  return event, p1, p2, p3, p4, p5
end
term.clear()
Make it like:

function os.pullEvent()
local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then end
return event, p1, p2, p3, p4, p5
end
while true do
term.clear()
You could put in a block of code to do instead of termination, like you did:

if event == "terminate" then os.shutdown() end
If you dont move the function out of the while loop, you will get lag, and you will declare the function many times.
Hope i helped making it better.

I'm not going to lie some of the code like the code for rebooting the computer instead of terminating the program i copied and pasted from someone else :P/>/>. I am still quite the LUA newbie and atm i can only do simple stuff like this. But i will test this out and if it works out well on the Tekkit server I help manage then the code will be updated ;)/>/>.

Wait really? It worked for me when I did it.
No. It didn't.
that code sets the value of password once, then instantly change the value. If you want separate passwords, you want something like this:

local password1 = "password1"
local password2 = "password2"
while true do
local input = read("*")
if input == password1 or input == password2 then
print("password successful")
end
end

Cranium is right you will have to use a elseif or what Cranium has put above. Other wise the program will just want to use the second password and only that password. And also the program isn't for a door o.O This program locks the COMPUTER IT'S SELF. That way you can keep people from griefing your programs and the like. The only way for someone to "hack" your computer while you are useing this program (after you have placed a label on the computer that is) is to use FTP or the consel to bypass all protocalls. But i dont think co-owner, owner, or any admins are going to be griefing your computer. (btw if you label your computer before you break it into a drop item it will retain all data [going off of CC 1.33] label your computer with: )


label set <name> -- For computer.
label set <side> <name> -- For floppies.