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

[1.3] Door Lock with Server

Started by Kolpa, 24 March 2012 - 01:53 PM
Kolpa #1
Posted 24 March 2012 - 02:53 PM
so this program is parted into a server and the door client

Client:
Spoiler


server= changeme
rednet.open("back")
while true do
textutils.slowPrint("please log in")
textutils.slowPrint("Username:")
user = io.read()
rednet.send(server, user)
textutils.slowPrint("waiting for server")
repeat
id, pass_s = rednet.receive()
until id == server
if pass_s == "fail" then
textutils.slowPrint("user not registered")
else
textutils.slowPrint("Password:")
pass_l = io.read()
if pass_s == pass_l then
textutils.slowPrint("acces granted for 10 seconds")
redstone.setOutput("right",true)
os.sleep(10)
redstone.setOutput("right",false)
term.clear()
term.setCursorPos(1,1)
else
textutils.slowPrint("acces denied")
term.clear()
term.setCursorPos(1,1)
end
end
end



Server:
Spoiler


client=changeme
usr="changeme"
pass="changeme"
rednet.open("back")
while true do
repeat
id, user = rednet.receive()
until id == client
if user == usr then
os.sleep(5)
rednet.send(client,pass)
else
os.sleep(5)
rednet.send(client,"fail")
end
end



ps: @Espen if u wanna improve have fun ^^
Espen #2
Posted 24 March 2012 - 03:53 PM
Alright Kolpa, as requested (kinda^^) I took a look at it and rewrote some stuff. I tried to leave the original code as much as possible.
I moved the password check to the server though. Because checking valid credentials should always happen server-side, or else someone could just rewrite their client to ignore the password-check.^^

Other smaller changes include setting locals wherever I could, removing redundant table accesses (os & io), etc.
If you have any questions about something in the code, don't hesitate to ask.
K, hope it's of any use, here goes:

Client:
Spoiler

local server = changeme
rednet.open("back")

while true do
  textutils.slowPrint("please log in")
  textutils.slowPrint("Username:")
  local user = read()
  textutils.slowPrint("Password:")
  local pass = read()
  rednet.send(server, user.."n"..pass)
  textutils.slowPrint("waiting for server")

  local id, access
  repeat
    id, access = rednet.receive()
  until id == server
  if access == "granted" then
    textutils.slowPrint("access granted for 10 seconds")
    redstone.setOutput("right", true)
    sleep(10)
    redstone.setOutput("right", false)
    term.clear()
    term.setCursorPos(1, 1)
  else
    textutils.slowPrint("access denied")
    sleep(2)
    term.clear()
    term.setCursorPos(1, 1)
  end
end

Server:
Spoiler

local client = changeme
local usr = "changeme"
local pass = "changeme"
rednet.open("back")

-- Helper function to split strings at the first line-break ("n").
-- Returns: Two strings representing the first and second half of the given string.
local function splitAtLineBreak( toSplit )
  for i = 1, #toSplit do
  local currChar = string.sub( toSplit, i, i )
  if currChar == "n" then
    firstHalf = string.sub( toSplit, 1, i - 1 )
    secondHalf = string.sub( toSplit, i + 1 )

    return firstHalf, secondHalf
  end  -- of IF
  end  -- of FOR
end

while true do
  local id, loginDetails
  repeat
    id, loginDetails = rednet.receive()
  until id == client

  local receivedUser, receivedPass = splitAtLineBreak( loginDetails )
  if receivedUser == usr and receivedPass == pass then
    sleep(5)
    rednet.send(client, "granted")
  else
    sleep(5)
    rednet.send(client, "denied")
  end
end

Cheers :(/>/>
Ian-Moone #3
Posted 24 March 2012 - 06:15 PM
looks good
Wolvan #4
Posted 24 March 2012 - 06:48 PM
Epic! I made something like this on a PServer already but this seems much more and working
Wirklich cooles Programm!


PS: Ich bin einen Rang gestiegen :(/>/> Next Rank! Oh yeah :)/>/>
Kolpa #5
Posted 24 March 2012 - 07:06 PM
Epic! I made something like this on a PServer already but this seems much more and working
Wirklich cooles Programm!


PS: Ich bin einen Rang gestiegen :(/>/> Next Rank! Oh yeah :)/>/>

danke ^^
PS: GRRRRRRRR
Kolpa #6
Posted 24 March 2012 - 07:08 PM
Alright Kolpa, as requested (kinda^^) I took a look at it and rewrote some stuff. I tried to leave the original code as much as possible.
I moved the password check to the server though. Because checking valid credentials should always happen server-side, or else someone could just rewrite their client to ignore the password-check.^^

Other smaller changes include setting locals wherever I could, removing redundant table accesses (os & io), etc.
If you have any questions about something in the code, don't hesitate to ask.
K, hope it's of any use, here goes:

Client:
Spoiler

local server = changeme
rednet.open("back")

while true do
  textutils.slowPrint("please log in")
  textutils.slowPrint("Username:")
  local user = read()
  textutils.slowPrint("Password:")
  local pass = read()
  rednet.send(server, user.."n"..pass)
  textutils.slowPrint("waiting for server")

  local id, access
  repeat
	id, access = rednet.receive()
  until id == server
  if access == "granted" then
	textutils.slowPrint("access granted for 10 seconds")
	redstone.setOutput("right", true)
	sleep(10)
	redstone.setOutput("right", false)
	term.clear()
	term.setCursorPos(1, 1)
  else
	textutils.slowPrint("access denied")
	sleep(2)
	term.clear()
	term.setCursorPos(1, 1)
  end
end

Server:
Spoiler

local client = changeme
local usr = "changeme"
local pass = "changeme"
rednet.open("back")

-- Helper function to split strings at the first line-break ("n").
-- Returns: Two strings representing the first and second half of the given string.
local function splitAtLineBreak( toSplit )
  for i = 1, #toSplit do
  local currChar = string.sub( toSplit, i, i )
  if currChar == "n" then
	firstHalf = string.sub( toSplit, 1, i - 1 )
	secondHalf = string.sub( toSplit, i + 1 )

	return firstHalf, secondHalf
  end  -- of IF
  end  -- of FOR
end

while true do
  local id, loginDetails
  repeat
	id, loginDetails = rednet.receive()
  until id == client

  local receivedUser, receivedPass = splitAtLineBreak( loginDetails )
  if receivedUser == usr and receivedPass == pass then
	sleep(5)
	rednet.send(client, "granted")
  else
	sleep(5)
	rednet.send(client, "denied")
  end
end

Cheers :(/>/>
thanks ^^ i actualy had a version of this on a server where u coudnt ctrl+t anymore so the rewrite thing was fixed but the server is down for now so i just posted a test version that i made 4 myself in ssp probably gonna edit if the server comes back on
Kolpa #7
Posted 24 March 2012 - 07:12 PM
looks good
ur pic makes the looks good epic :(/>/>
Kolpa #8
Posted 24 March 2012 - 07:16 PM
Alright Kolpa, as requested (kinda^^) I took a look at it and rewrote some stuff. I tried to leave the original code as much as possible.
I moved the password check to the server though. Because checking valid credentials should always happen server-side, or else someone could just rewrite their client to ignore the password-check.^^

Other smaller changes include setting locals wherever I could, removing redundant table accesses (os & io), etc.
If you have any questions about something in the code, don't hesitate to ask.
K, hope it's of any use, here goes:

Client:
Spoiler

local server = changeme
rednet.open("back")

while true do
  textutils.slowPrint("please log in")
  textutils.slowPrint("Username:")
  local user = read()
  textutils.slowPrint("Password:")
  local pass = read()
  rednet.send(server, user.."n"..pass)
  textutils.slowPrint("waiting for server")

  local id, access
  repeat
	id, access = rednet.receive()
  until id == server
  if access == "granted" then
	textutils.slowPrint("access granted for 10 seconds")
	redstone.setOutput("right", true)
	sleep(10)
	redstone.setOutput("right", false)
	term.clear()
	term.setCursorPos(1, 1)
  else
	textutils.slowPrint("access denied")
	sleep(2)
	term.clear()
	term.setCursorPos(1, 1)
  end
end

Server:
Spoiler

local client = changeme
local usr = "changeme"
local pass = "changeme"
rednet.open("back")

-- Helper function to split strings at the first line-break ("n").
-- Returns: Two strings representing the first and second half of the given string.
local function splitAtLineBreak( toSplit )
  for i = 1, #toSplit do
  local currChar = string.sub( toSplit, i, i )
  if currChar == "n" then
	firstHalf = string.sub( toSplit, 1, i - 1 )
	secondHalf = string.sub( toSplit, i + 1 )

	return firstHalf, secondHalf
  end  -- of IF
  end  -- of FOR
end

while true do
  local id, loginDetails
  repeat
	id, loginDetails = rednet.receive()
  until id == client

  local receivedUser, receivedPass = splitAtLineBreak( loginDetails )
  if receivedUser == usr and receivedPass == pass then
	sleep(5)
	rednet.send(client, "granted")
  else
	sleep(5)
	rednet.send(client, "denied")
  end
end

Cheers :(/>/>
something else what does the locale actualy do ? ^^ never used it so i have 0 idea
Espen #9
Posted 24 March 2012 - 07:55 PM
something else what does the locale actualy do ? ^^ never used it so i have 0 idea
If you don't explicitly need global variables, then you should always default to using local variables.
They limit the use of the variables to the scope they were defined in and downwards (e.g. even in nested functions within the same scope). Also they're faster than global variables.
The thing is, if you define a global variable in your program in CC, then even after your program has ended, that global variable is still set and if another program makes use of the same variable, this could lead to complications.

If you're interested, here you can read a little bit more about the difference bwtween locals and globals, as well as the reasons for choosing the former over the latter as a default: http://www.lua.org/pil/4.2.html
ShadowLamp #10
Posted 10 November 2014 - 04:19 PM
Could the client be added to this?


sleep(0)

side = "left"

function save(usr, pass)
  if not fs.exists(fs.combine("users", usr)) then
    if not fs.exists("users") then
          fs.makeDir("users")
    end
    fs.makeDir(fs.combine("users", usr))
  end
  local h = fs.open(fs.combine(fs.combine("users", usr), "password"), "w")
  if h then
    h.writeLine(pass)
    h.close()
  else
    error("Couldn't open file handle!")
  end
end

while true do
  term.clear()
  term.setCursorPos(1,1)
  term.write("Input Username: ")
  username = read()
  term.setCursorPos(1,2)
  term.write("Input Password: ")
  password = read("*")
    result = check(username,password)
    if result == true then
      redstone.setOutput(side,true)
      sleep(2)
      redstone.setOutput(side,false)
    elseif result == "admin" then
      term.clear()
      term.setCursorPos(1,1)
      term.write("Input new Account username: ")
      newUser = read()
      term.setCursorPos(1,2)
      term.write("Input passowrd for account "..newUser..": ")
      newPass = read("*")
      save(newUser,newPass)
    elseif result == false then
      print("Invalid password")
      sleep(2)
    elseif result == "nousr" then
      print("No such user")
      sleep(2)
    end
end
Edited on 10 November 2014 - 03:19 PM