Guest User

Untitled

a guest
May 12th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.87 KB | None | 0 0
  1. aUser = {}
  2. aPass = {}
  3. nUserList = 0
  4.  
  5.  
  6. local function inputBox(len, x, y, mask)
  7.   local bInput = true
  8.   local sOutput = ""
  9.   term.setCursorPos(x,y)
  10.   while bInput == true do
  11.     local sEvent, param = os.pullEvent()
  12.     if sEvent == "key" then
  13.       if param == 28 then
  14.         if string.len(sOutput) > 0 then
  15.           bInput = false
  16.         end
  17.       elseif param == 14 then
  18.         sOutput = string.sub(sOutput,1,string.len(sOutput)-1)
  19.         term.setCursorPos(x+string.len(sOutput),y)
  20.         io.write(" ")
  21.         term.setCursorPos(x+string.len(sOutput),y)
  22.       end
  23.     end
  24.     if sEvent == "char" then
  25.       if string.len(sOutput) < len then
  26.         sOutput = sOutput .. param
  27.         term.setCursorPos(x,y)
  28.         if not mask then
  29.           io.write(sOutput)
  30.         else
  31.           for n=1,string.len(sOutput) do
  32.             io.write("*")
  33.           end
  34.         end
  35.       end
  36.     end
  37.   end
  38.   return sOutput
  39. end
  40.  
  41. local function drawBox(x, y, w, h)
  42.   for n=0,h-1 do
  43.     term.setCursorPos(x,y+n)
  44.     if n == 0 then
  45.       io.write("+")
  46.       for n2=1,w-2 do
  47.         io.write("-")
  48.       end
  49.       io.write("+")
  50.     elseif n == h-1 then
  51.       io.write("+")
  52.       for n2=1,w-2 do
  53.         io.write("-")
  54.       end
  55.       io.write("+")
  56.     else
  57.       io.write("|")
  58.       for n2=1,w-2 do
  59.         io.write(" ")
  60.       end
  61.       io.write("|")
  62.     end
  63.   end
  64. end
  65.  
  66. local function textAt(x,y, str)
  67.   term.setCursorPos(x,y)
  68.   io.write(str)
  69. end
  70.  
  71. local function form(str, msg, mask)
  72.   term.clear()
  73.   term.setCursorBlink(true)
  74.   if msg ~= "" then
  75.     textAt(26-(string.len(msg)/2),7,msg)
  76.   end
  77.   drawBox(16,9,20,3)
  78.   textAt(14-string.len(str),10,str..":")
  79.   local sData = inputBox(16, 18, 10, mask)
  80.   return sData
  81. end
  82.  
  83. local function checkPass()
  84.   return fs.exists("ACCOUNTS.F")
  85. end
  86.  
  87. local function loadPass()
  88.   nUserList = 0
  89.   aUser = {}
  90.   aPass = {}
  91.   local file = io.open( "ACCOUNTS.F", "r" )
  92.   local sLineU = file:read()
  93.   local sLineP = file:read()
  94.   while sLineU do
  95.     nUserList = nUserList + 1
  96.     table.insert(aUser, sLineU)
  97.     table.insert(aPass, sLineP)
  98.     sLineU = file:read()
  99.     sLineP = file:read()
  100.   end
  101.   file:close()
  102. end
  103.  
  104. local function writePass()
  105.   local file = io.open( "ACCOUNTS.F", "w" )
  106.   for n=1,nUserList do
  107.     file:write(aUser[n] .. "\n")
  108.     file:write(aPass[n] .. "\n")
  109.   end
  110.   file:close()
  111. end
  112.  
  113. local function login()
  114.   local nWrong = 0
  115.   local SFormMsg = "Please enter your username & password."
  116.   local bCorrect = false
  117.   local sUser
  118.   local sPass
  119.   while not bCorrect do
  120.     sUser = form("Username", SFormMsg, false)
  121.     sPass = form("Password", SFormMsg, true)
  122.     for n=1,nUserList do
  123.       if string.lower(sUser) == string.lower(aUser[n]) then
  124.         if sPass == aPass[n] then
  125.           bCorrect = true
  126.         end
  127.       end
  128.     end
  129.     if not bCorrect then
  130.       nWrong = nWrong + 1
  131.       if nWrong == 3 then
  132.         msg = "Too many failed attemps! Shutting down."
  133.         textAt(26-(string.len(msg)/2),13,msg)
  134.         sleep(5)
  135.         os.shutdown()
  136.       end
  137.       msg = "Username and/or password incorrect!"
  138.       textAt(26-(string.len(msg)/2),13,msg)
  139.       sleep(2)
  140.     end
  141.   end
  142.   return sUser, bCorrect
  143. end
  144.  
  145. local function newUser()
  146.   local sUser = ""
  147.   local sPass = ""
  148.   local sPassconf = "!!!!!!!!!!!!!!!!!!"
  149.   local bValid = false
  150.   local bConflict = false
  151.   local SFormMsg = "Account Creation: Enter new username."
  152.   while not bValid do
  153.     bValid = false
  154.     sUser = ""
  155.     while string.len(sUser) < 3 do
  156.       sUser = form("Username", SFormMsg, false)
  157.       if string.len(sUser) < 3 then
  158.         SFormMsg = "Username must have at least 3 characters."
  159.       end
  160.       bConflict = false
  161.       if nUserList > 0 then
  162.         for n=1,nUserList do
  163.           if string.lower(sUser) == string.lower(aUser[n]) then
  164.             SFormMsg = "Username already exists! Choose another."
  165.             bConflict = true
  166.           end
  167.         end
  168.       end
  169.       if not bConflict then
  170.         bValid = true
  171.       end
  172.     end
  173.   end
  174.   SFormMsg = "Account Creation: Enter new  password."
  175.   while sPass ~= sPassConf do
  176.     while string.len(sPass) < 5 do
  177.       sPass = form("Password", SFormMsg, true)
  178.       if string.len(sPass) < 5 then
  179.         SFormMsg = "Password must have at least 5 characters."
  180.       end
  181.     end
  182.     SFormMsg = "Account Creation: Re-enter your password."
  183.     sPassConf = form("Password", SFormMsg, true)
  184.     if sPass ~= sPassConf then
  185.       sPass = ""
  186.       sPassconf = "!!!!!!!!!!!!!!!!!!"
  187.       SFormMsg = "Passwords do not match! Create new password."
  188.     end
  189.   end
  190.   table.insert(aPass, sPass)
  191.   table.insert(aUser, sUser)
  192.   nUserList = nUserList + 1
  193.   writePass()
  194. end
  195.  
  196. local bNotFirst = checkPass()
  197. if bNotFirst then
  198.   loadPass()
  199.   x, y = login()
  200. else
  201.   newUser()
  202.   x, y = login()
  203. end
  204.  
  205. term.clear()
  206. term.setCursorPos(1,1)
  207. print(x)
  208. print(y)
Add Comment
Please, Sign In to add comment