Advertisement
PaymentOption

Door Lock

Jul 30th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.27 KB | None | 0 0
  1. oldpullEvent = os.pullEvent
  2. os.pullEvent = os.pullEventRaw
  3.  
  4. tArgs = { ... }
  5.  
  6. if #tArgs > 0 then
  7.         if #tArgs == 3 then
  8.                 local fileContents = ""
  9.                 file = fs.open(".lockInformation", "r")
  10.                 fileContents = file.readAll()
  11.                 file.close()
  12.                
  13.                 file = fs.open(".lockInformation", "w")
  14.                 file.write(fileContents.."\n"..tArgs[1].."\n"..tArgs[2].."\n"..tArgs[3])
  15.                 file.close()
  16.                
  17.                 print("Account '"..tArgs[1].."' added successfully.")
  18.         else
  19.                 print("Usage to add a user: ")
  20.                 print("<username> <password1> <password2>")
  21.         end
  22. else
  23.  
  24.         -- VARS --
  25.         tUsers = {} -- Format { sPassword, sPassword_Backup, sUsername }
  26.  
  27.         nGuesses = 0
  28.         bNeedsUpdate = false
  29.  
  30.         screenWidth, screenHeight = term.getSize()
  31.         VERSION = 1.7
  32.         -- END VARS --
  33.  
  34.         -- Helper Functions --
  35.         local function clear() term.clear(); term.setCursorPos(1, 1) end
  36.  
  37.         local function printBorder()
  38.                 for i=1, screenHeight do
  39.                         term.setCursorPos(1, i)
  40.                         if i == 1 or i == screenHeight then term.write(string.rep("*", screenWidth))
  41.                         else term.write("*"..string.rep(" ", screenWidth-2).."*") end
  42.                 end
  43.         end
  44.  
  45.         local function cPrint(height, string)
  46.                 term.setCursorPos(screenWidth/2 - string.len(string)/2, height); term.write(string)
  47.         end
  48.  
  49.         local function rPrint(height, string)
  50.                 term.setCursorPos(screenWidth - string.len(string), height); term.write(string)
  51.         end
  52.  
  53.         local function limitRead( nLength, cReplaceChar )
  54.           term.setCursorBlink( true )
  55.  
  56.           nLength = nLength or -1 -- -1 is unlimited
  57.           sReturnString = ""
  58.  
  59.           xPos, yPos = term.getCursorPos()
  60.  
  61.           while true do
  62.                            event, char = os.pullEvent()
  63.  
  64.                            if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check
  65.                                         if event == "char" then sReturnString = sReturnString .. char
  66.                                         elseif event == "key" then
  67.                                                 if char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter
  68.                                                 elseif char == 14 then -- Backspace
  69.                                                                 term.setCursorPos( xPos, yPos )
  70.                                                                 term.write( string.rep( " ", string.len( sReturnString ) ) )
  71.                                                                 sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 )
  72.                                                                 term.setCursorPos( xPos, yPos )
  73.  
  74.                                                                 if not cReplaceChar then term.write( sReturnString )
  75.                                                                 else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
  76.                                                 end
  77.                                         end
  78.                                         term.setCursorPos( xPos, yPos )
  79.                                         term.write( string.rep( " ", string.len( sReturnString ) ) )
  80.                                         term.setCursorPos( xPos, yPos )
  81.                                         if not cReplaceChar then term.write( sReturnString )
  82.                                         else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
  83.                         end
  84.         end
  85.         -- END Helper Functions --
  86.  
  87.         -- Updating Functions --
  88.         local function loadInformation()
  89.                 if not fs.exists(".lockInformation") then return false end
  90.                 tUsers = {}
  91.                
  92.                 local file = io.open(".lockInformation", "r")
  93.                 local fileSize = 0
  94.                
  95.                 for i in file:lines() do fileSize = fileSize+1 end
  96.                 file:close()
  97.                
  98.                 file = fs.open( ".lockInformation", "r" )
  99.                 for i=1, (fileSize/3) do
  100.                         tUsers[i] = { username = file.readLine(), password1 = file.readLine(), password2 = file.readLine() }
  101.                 end
  102.                 file.close()
  103.                
  104.                 print( #tUsers )
  105.                 return true
  106.         end
  107.  
  108.         local function loadVersion()
  109.                 if not fs.exists(".lockVersion") then
  110.                         file = fs.open(".lockVersion", "w")
  111.                         file.write(VERSION)
  112.                         file.close()
  113.                 end
  114.                
  115.                 local file = fs.open(".lockVersion", "r")
  116.                 VERSION = tonumber(file.readAll())
  117.                 file.close()
  118.         end
  119.  
  120.         local function update(nNewVer)
  121.                 local sCode = "Z3dhDRNq"
  122.                
  123.                 file = fs.open(".lockVersion", "w")
  124.                 file.write(nNewVer)
  125.                 file.close()
  126.                 loadVersion()
  127.                
  128.                 clear()
  129.                 printBorder()
  130.                 cPrint(6, "+-------------+")
  131.                 cPrint(7, "| Updating... |")
  132.                 cPrint(8, "+-------------+")
  133.                
  134.                 while true do
  135.                         local response = http.get(
  136.                                 "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  137.                                 )
  138.                                
  139.                         if response then
  140.                                 local sResponse = response.readAll()
  141.                                 response.close()
  142.                                
  143.                                 file = fs.open("/startup", "w")
  144.                                 file.write(sResponse)
  145.                                 file.close()
  146.                                 return
  147.                         else return end
  148.                 end
  149.         end
  150.  
  151.         local function checkForUpdate()
  152.                 local sCode = "uuBj6zdk"
  153.                
  154.                 clear()
  155.                 printBorder()
  156.                 cPrint(6, "+---------------------+")
  157.                 cPrint(7, "| Checking Version... |")
  158.                 cPrint(8, "+---------------------+")
  159.                
  160.                 while true do
  161.                         local response = http.get(
  162.                                 "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  163.                                 )
  164.                                
  165.                         if response then
  166.                                 local sResponse = response.readAll()
  167.                                 response.close()
  168.                                
  169.                                 if VERSION < tonumber(sResponse) then return tonumber(sResponse), true
  170.                                 else return false end
  171.                         else return false end
  172.                 end
  173.         end
  174.         -- END Updating Functions --
  175.  
  176.         -- User Input Functions --
  177.         local function getNewUserInfo()
  178.                 clear()
  179.                 printBorder()
  180.                
  181.                 cPrint(3, "+----------+")
  182.                 cPrint(4, "| New User |")
  183.                 cPrint(5, "+----------+")
  184.                
  185.                 cPrint(7, "Username: ")
  186.                 cPrint(8, "Password1: ")
  187.                 cPrint(9, "Password2: ")
  188.                 term.setCursorPos(screenWidth/2 - string.len("Password1: ")/2 - 8, 11); term.write("Confirm Password1: ") -- -8 to place the ':' so it is lined up with the above.
  189.                 term.setCursorPos(screenWidth/2 - string.len("Password1: ")/2 - 8, 12); term.write("Confirm Password2: ")
  190.                
  191.                 term.setCursorPos(screenWidth/2 - string.len("Username: ")/2 + string.len("Username: "), 7)
  192.                 local temp_Username = limitRead(12)
  193.                
  194.                 term.setCursorPos(screenWidth/2 - string.len("Password1: ")/2 + string.len("Password1: "), 8)
  195.                 local temp_Password1 = limitRead(12, "*")
  196.                
  197.                 term.setCursorPos(screenWidth/2 - string.len("Password2: ")/2 + string.len("Password2: "), 9)
  198.                 local temp_Password2 = limitRead(12, "*")
  199.                
  200.                 term.setCursorPos(screenWidth/2 - string.len("Password1: ")/2 + string.len("Password1: "), 11)
  201.                 local confirm_Password1 = limitRead(12, "*")
  202.                
  203.                 term.setCursorPos(screenWidth/2 - string.len("Password2: ")/2 + string.len("Password2: "), 12)
  204.                 local confirm_Password2 = limitRead(12, "*")
  205.                
  206.                 while true do
  207.                         if confirm_Password1 == temp_Password1 and confirm_Password2 == temp_Password2 then
  208.                                 local file = fs.open(".lockInformation", "w")
  209.                                 file.writeLine(temp_Username)
  210.                                 file.writeLine(temp_Password1)
  211.                                 file.writeLine(temp_Password2)
  212.                                 file.close()
  213.                                
  214.                                 return
  215.                         else
  216.                                 term.setCursorPos(screenWidth/2 - string.len("Password1: ")/2 + string.len("Password1: "), 8)
  217.                                 term.write(string.rep(" ", string.len(temp_Password1)))
  218.                                 term.setCursorPos(screenWidth/2 - string.len("Password2: ")/2 + string.len("Password2: "), 9)
  219.                                 term.write(string.rep(" ", string.len(temp_Password2)))
  220.                                 term.setCursorPos(screenWidth/2 - string.len("Password1: ")/2 + string.len("Password1: "), 11)
  221.                                 term.write(string.rep(" ", string.len(confirm_Password1)))
  222.                                 term.setCursorPos(screenWidth/2 - string.len("Password2: ")/2 + string.len("Password2: "), 12)
  223.                                 term.write(string.rep(" ", string.len(confirm_Password2)))
  224.                                
  225.                                 term.setCursorPos(screenWidth/2 - string.len("Password1: ")/2 + string.len("Password1: "), 8)
  226.                                 temp_Password1 = limitRead(12, "*")
  227.                                
  228.                                 term.setCursorPos(screenWidth/2 - string.len("Password2: ")/2 + string.len("Password2: "), 9)
  229.                                 temp_Password2 = limitRead(12, "*")
  230.                                
  231.                                 term.setCursorPos(screenWidth/2 - string.len("Password1: ")/2 + string.len("Password1: "), 11)
  232.                                 confirm_Password1 = limitRead(12, "*")
  233.                                
  234.                                 term.setCursorPos(screenWidth/2 - string.len("Password2: ")/2 + string.len("Password2: "), 12)
  235.                                 confirm_Password2 = limitRead(12, "*")
  236.                         end
  237.                 end
  238.         end
  239.  
  240.         local function determineUser() -- Basically login
  241.                 if loadInformation() then
  242.                         clear()
  243.                         printBorder()
  244.                        
  245.                         cPrint(4, "+-------------------------+")
  246.                         cPrint(5, "| Identification Required |")
  247.                         cPrint(6, "+-------------------------+")
  248.                         rPrint(screenHeight - 1, "Tries: "..nGuesses.."/5")
  249.                         term.setCursorPos(2, screenHeight - 1); term.write("Version: "..VERSION)
  250.                        
  251.                         cPrint(7, "Username: ")
  252.                         cPrint(8, "Password1: ")
  253.                         cPrint(9, "Password2: ")
  254.                        
  255.                         term.setCursorPos(screenWidth/2 - string.len("Username: ")/2 + string.len("Username: "), 7)
  256.                         local temp_Username = limitRead(12)
  257.                        
  258.                         term.setCursorPos(screenWidth/2 - string.len("Password1: ")/2 + string.len("Password1: "), 8)
  259.                         local temp_Password1 = limitRead(12, "*")
  260.                        
  261.                         term.setCursorPos(screenWidth/2 - string.len("Password2: ")/2 + string.len("Password2: "), 9)
  262.                         local temp_Password2 = limitRead(12, "*")
  263.                        
  264.                         for i=1, #tUsers do
  265.                                 if temp_Username == tUsers[i].username then
  266.                                         if temp_Password1 == tUsers[i].password1 and temp_Password2 == tUsers[i].password2 then return true
  267.                                         else return false end
  268.                                 end
  269.                         end
  270.                 else
  271.                         getNewUserInfo()
  272.                         cPrint(13, "+-----------------------+")
  273.                         cPrint(14, "|  Information Saved.   |")
  274.                         cPrint(15, "|  Rebooting to login.  |")
  275.                         cPrint(16, "+-----------------------+")
  276.                         sleep(2)
  277.                         os.reboot()
  278.                 end
  279.         end
  280.         -- END User Input Functions --
  281.  
  282.         loadVersion()
  283.         nNewVersion, bNeedsUpdate = checkForUpdate()
  284.         if bNeedsUpdate then update(nNewVersion); shell.run("startup") end
  285.         while true do
  286.                 if nGuesses == 5 then os.shutdown() end
  287.                
  288.                 if determineUser() then clear(); print(os.version()); break
  289.                 else
  290.                         cPrint(11, "+-----------------+")
  291.                         cPrint(12, "| Incorrect Input |")
  292.                         cPrint(13, "+-----------------+")
  293.                         sleep(2)
  294.                        
  295.                         nGuesses = nGuesses+1
  296.                 end
  297.         end
  298. end -- Ends the arguments 'else' statement.
  299.  
  300. os.pullEvent = oldpullEvent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement