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

Set password on first launch

Started by azdaspaz818, 16 April 2012 - 07:25 AM
azdaspaz818 #1
Posted 16 April 2012 - 09:25 AM
I don't think this has been asked before, but is it possible to ask the user to set a password on first launch? I am a part of a thriving multiplayer server with towny installed and I wish to have a chest area protected by computers. Any ideas how I might achieve such a thing?
libraryaddict #2
Posted 16 April 2012 - 11:15 AM
Ok, I could either give you bits of my API or I could give you a "kinda" method :)/>/>
Ill try the "kinda"

if fs.exists("FirstStartPass") then dofile("FirstStartPass") else write("Password: ") Password = read() Heh = io.open("FirstStartPass", "w") Heh:write(Password) Heh:close() end

This makes a new file called FirstStartPass.

If I quoted my API at you it could be done inside the file.
But idk if there is any short way to get that in the first line.

If you are interested in the API tho.
Look at this.

function removeLineNo(filename, ...) -- Say the file you want removed. Then the line numbers of the lines you want removed.
  local LinesRemove = {...}
  local fp = io.open( filename, "r" )
  local content = {}
  local i = 1
  for line in fp:lines() do
	content[#content+1] = line
	i = i + 1
  end
  fp:close()
  for n=1,#LinesRemove do
	table.remove(content,LinesRemove[n])
	for e=1,#LinesRemove do
	  LinesRemove[e] = LinesRemove[e]-1
	end
  end
  local fp = io.open( filename, "w" )
  for i = 1, #content do
	fp:write( string.format( "%sn", content[i] ) )
  end
  fp:close()
end

function insertLine(file, lineno, Insertline) -- say the file, Then the line number. Then the line to be inserted.
  local lolol = io.open(file, "r")
  local content = {}
  local i = 1
  for line in lolol:lines() do
	if i == lineno then
	  content[#content+1] = Insertline
	  i = i + 1
	end
	content[#content+1] = line
	i = i + 1
  end
  lolol:close()
  local lolol = io.open( file, "w" )
  for i = 1, #content do
	lolol:write( string.format( "%sn", content[i] ) )
  end
  lolol:close()
end

with the api you can do it with
write("Password: ") Bla = read() insertLine("startup", 2, Bla)removeLineNo("startup", 1)
azdaspaz818 #3
Posted 17 April 2012 - 09:11 AM
I tried the above suggestion to no avail :)/>/>. I am now wondering how to save a file containing the password, then reload that file every time the program runs. I have set up an RS NOR latch to indicate the first launch, now I just need to know how to save and load passwords. Any help is much appreciated.
Luanub #4
Posted 17 April 2012 - 09:43 AM
Here is an example of what you need to do to save the file, and then open and read the file. How many passwords are you planning on storing? If more then one it will take slightly a bit more work then what I am going to post.

Save File

if not fs.exists("password.txt") then -- will cause this to only run once. Will skip it if it finds the file.
print ("Enter Password")
local password = read("*")
local file = io.open("password.txt", "w")
file:write(password)
file:close()
end

Read the password so it can be used..

local file = io.open("password.txt", "r")
passwd = file:read() -- capture file contents as the var passwd
file:close()
while true do -- run this infinitely
print ("Enter Password:")
password = read("*")
if password == passwd then -- check against user input
-- do stuff
break -- stop the infinite loop so you can do stuff
else
print ("invalid password entered")
end
end
Edited on 17 April 2012 - 07:50 AM
libraryaddict #5
Posted 17 April 2012 - 10:50 AM
Eh.
My way if it worked would leave no trace.

Not really sure why it didnt work.
Aside from a space missing and only touching startup.

Try luanub's way.

A edited version to store more then one password is

Save file
if not fs.exists("password.txt") then -- will cause this to only run once. Will skip it if it finds the file. print ("Enter Password") local password = read("*") local file = io.open("password.txt", "a") file:write("n"..password) file:close() end


And the read.

  local fp = io.open( filename, "r" )
  for line in fp:lines() do
		if line == password then Password() end
  end
  fp:close()

And for Password()
However you could just place your code there instead of Password()
Functions mean its a block of code you can call on anywhere without typing it out.


function Password()
  Put your code here!
end
azdaspaz818 #6
Posted 17 April 2012 - 11:32 AM
You are both very helpful! Thank-you both so much :)/>/>. Consider it solved.