Bimgo

CC-Server code

Feb 18th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.19 KB | None | 0 0
  1. local myid = os.computerID()
  2. local doorList = { 11,13 } --id of the stations
  3. local passwordForDoor = { "notch","minecraft" } --password for each stations
  4.  
  5. mon=peripheral.wrap("bottom") - position of screen
  6. print("Access Terminal")
  7. rednet.open("back") -- open rednet modem
  8.  
  9. print("Computer id for Access Terminal is "..tostring(myid)) --show the name of terminal and ID
  10.  
  11. function findIndexForId(id) -- comparaison of password
  12.    for i,v in ipairs(doorList) do
  13.       if id == v then
  14.          return i
  15.       end
  16.    end
  17.    return 0
  18. end
  19.  
  20. --[[ for future implementation ]]--
  21. function setPasswordForLock(id,password)
  22.    local i = findIndexForId(id)
  23.    
  24.    if i == 0 then
  25.       return false
  26.    end
  27.    
  28.    passwordForDoor[i] = password
  29.    print("in setPasswordForLock"..id..password)
  30.    return true
  31. end
  32.  
  33. function checkPasswordForLock(id,password)
  34.    local i = findIndexForId(id)
  35.    if i == 0 then
  36.       return -1
  37.    end
  38.    if passwordForDoor[i] == password then
  39.       return 1
  40.    else
  41.       return 0
  42.    end
  43. end
  44.  
  45. --[[ Not needed yet, for later when we allow remove password changes ]]-
  46. function saveData()
  47.   local myfile = io.open("/doorpw.dat","w")
  48.   print(tostring(myfile))
  49.   print("1")
  50.   for i,pw in ipairs(passwordForDoor) do
  51.      myfile:write(pw)
  52.   end
  53.   print("4")
  54.   myfile:close()
  55. end
  56. ----------------------------PROGRAM------------------------------------------------
  57. local isValid = 0
  58.  
  59. while true do
  60.    local timeString = textutils.formatTime(os.time(),false) -- time
  61.  
  62.    senderId, message, distance = rednet.receive()
  63.      
  64.    isValid = checkPasswordForLock(senderId, message) --value is -1, 0 or 1
  65.  
  66.    if isValid == -1 then
  67.       print("server "..senderId.." sent us a request but is not in our list") --if sender of message is not in list
  68.    elseif isValid == 1 then
  69.       rednet.send(senderId, "Valid")
  70.       mon.scroll(1)
  71.       mon.setCursorPos(1,4)
  72.       mon.write("Access from "..senderId.." at "..timeString) --access granted for senderID at time
  73.    else
  74.       rednet.send(senderId, "Not Valid")
  75.       mon.scroll(1)
  76.       mon.setCursorPos(1,4)
  77.       mon.write("Failure from "..senderId.." at "..timeString) --access not granted, tentative of hacking
  78.   end
  79. end
Advertisement
Add Comment
Please, Sign In to add comment