Advertisement
boudragon2

Login System With User Access Levels

Sep 29th, 2013 (edited)
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.15 KB | None | 0 0
  1.     local function readN(len, replaceChar)
  2.       len = len or 12
  3.       local input=""
  4.       local key = 0
  5.       term.setCursorBlink(true)
  6.       repeat
  7.         local e,p1 = os.pullEvent()
  8.         if e=="char" then
  9.           if #input < len then
  10.             input = input .. p1
  11.             term.write(replaceChar or p1)
  12.           end
  13.         elseif e=="key" and p1==keys.backspace and #input > 0 then
  14.           input = input:sub(1,#input-1)
  15.           local x,y = term.getCursorPos()
  16.           term.setCursorPos(x-1,y)
  17.           term.write(" ")
  18.           term.setCursorPos(x-1,y)
  19.         end
  20.       until p1==keys.enter
  21.       term.setCursorBlink(false)
  22.       return input
  23.     end
  24.      
  25.     sid = 0
  26.     lockdown = 0
  27.     actc = ""
  28.     actpw = ""
  29.     actpc = ""
  30.     userlog = ""
  31.     userpas = ""
  32.     puselog = ""
  33.     prog = 0
  34.      
  35.     function clear()
  36.     term.clear()
  37.     term.setCursorPos(1,1)
  38.     end
  39.      
  40.     function menu(id, text)
  41.       if sid == id then
  42.         write">> "
  43.       else
  44.         write"   "
  45.       end
  46.       print(text)
  47.     end
  48.      
  49.     if not fs.exists("pending") then
  50.       shell.run("mkdir pending")
  51.       if not fs.exists("users") then
  52.         shell.run("mkdir users")
  53.       else
  54.       end
  55.     elseif not fs.exists("users") then
  56.       shell.run("mkdir users")
  57.     end
  58.      
  59.     if not fs.exists("motdu") then
  60.       file = fs.open("motdu", "w")
  61.       file.writeLine("CONTACT ADMIN TO SET MOTD.")
  62.       file.close()
  63.       if not fs.exists("motda") then
  64.         file = fs.open("motda", "w")
  65.         file.writeLine("PLEASE SET USER AND ADMIN MOTD MESSAGES.")
  66.         file.close()
  67.       end
  68.     else
  69.       if not fs.exists("motda") then
  70.         file = fs.open("motda", "w")
  71.         file.writeLine("PLEASE SET ADMIN MOTD MESSAGE.")
  72.         file.close()
  73.       end
  74.     end
  75.      
  76.     clear()
  77.     while prog == 0 do
  78.     --os.pullEvent = os.pullEventRaw
  79.     rep1 = 0
  80.     rep2 = 0
  81.     rep3 = 0
  82.     rep4 = 0
  83.     rep5 = 0
  84.     rep6 = 0
  85.     ioerror = 0
  86.     term.setCursorPos(1, 12)
  87.     term.clearLine()
  88.     term.setCursorPos(1, 13)
  89.     term.clearLine()
  90.     term.setCursorPos(1, 14)
  91.     term.clearLine()
  92.     term.setCursorPos(1, 15)
  93.     term.clearLine()
  94.     term.setCursorPos(1, 16)
  95.     term.clearLine()
  96.     term.setCursorPos(1, 17)
  97.     term.clearLine()
  98.     term.setCursorPos(1,1)
  99.     print("|################################################|")
  100.     print("|              BOUTECH SYSTEM LOGIN              |")
  101.     print("|                OS Version : 1.3                |")
  102.     print("|################################################|")
  103.     term.setCursorPos(13,7)
  104.     print("|------------------------|")
  105.     term.setCursorPos(13,8)
  106.     print("|                        |")
  107.     term.setCursorPos(13,9)
  108.     print("|                        |")
  109.     term.setCursorPos(13,10)
  110.     print("|                        |")
  111.     term.setCursorPos(13,11)
  112.     print("|------------------------|")
  113.     term.setCursorPos(16,8)
  114.     menu(0, "Login")
  115.     term.setCursorPos(16,9)
  116.     menu(1, "Create Account")
  117.     term.setCursorPos(16,10)
  118.     menu(2, "Shutdown")
  119.     event, key = os.pullEvent("key")
  120.      
  121.     if key == 200 then
  122.       if sid == 0 then
  123.         sid = sid+2
  124.       elseif sid == 1 then
  125.         sid = sid-1
  126.       elseif sid == 2 then
  127.         sid = sid-1
  128.       end
  129.     elseif key == 208 then
  130.       if sid == 2 then
  131.         sid = sid-2
  132.       elseif sid == 1 then
  133.         sid = sid+1
  134.       elseif sid == 0 then
  135.         sid = sid+1
  136.       end
  137.     elseif key == 28 then
  138.       if sid == 0 then
  139.         term.setCursorPos(13, 12)
  140.         print("|   Enter Credentials.   |")
  141.         term.setCursorPos(13, 13)
  142.         print("|                        |")
  143.         term.setCursorPos(13, 14)
  144.         print("|                        |")
  145.         term.setCursorPos(13, 15)
  146.         print("|                        |")
  147.         term.setCursorPos(13, 16)
  148.         print("|                        |")
  149.         term.setCursorPos(13, 17)
  150.         print("|------------------------|")
  151.         sleep(1)
  152.        
  153.         while rep5 == 0 do
  154.           sleep(1)
  155.           term.setCursorPos(1, 14)
  156.           term.clearLine()
  157.           term.setCursorPos(1, 16)
  158.           term.clearLine()
  159.           term.setCursorPos(13, 16)
  160.           print("|                        |")
  161.           term.setCursorPos(13, 14)
  162.           print("|                        |")
  163.           term.setCursorPos(15, 14)
  164.           write("Username: ")
  165.           userlog = readN()
  166.           if userlog == "adminovrride" then
  167.             sleep(1)
  168.             rnd = math.random(100, 500)
  169.             term.setCursorPos(1, 18)
  170.             term.clearLine()
  171.             term.setCursorPos (17, 18)
  172.             print("Override Code: ".. rnd)
  173.             apass = tostring((rnd + 11) * 83)
  174.             term.setCursorPos(1, 16)
  175.             term.clearLine()
  176.             term.setCursorPos(13, 16)
  177.             print("|                        |")
  178.             term.setCursorPos(15, 16)
  179.             write("Password: ")
  180.             ovrpass = readN(12, "*")
  181.             if ovrpass == apass then
  182.               term.setCursorPos(1, 18)
  183.               term.clearLine()
  184.               term.setCursorPos(15, 18)
  185.               print("Override Successful...")
  186.               sleep(1)
  187.               term.clear()
  188.               term.setCursorPos(1, 1)
  189.               rep5 = rep5+1
  190.               rep6 = rep6+1
  191.               prog = prog+1
  192.             else
  193.               term.setCursorPos(1, 18)
  194.               term.clearLine()
  195.               term.setCursorPos(15, 18)
  196.               print("  Override Failed...")
  197.               sleep(1)
  198.               rep5 = rep5+1
  199.               rep6 = rep6+1
  200.               sid = 0
  201.             end
  202.           else      
  203.             if userlog == "" then
  204.               term.setCursorPos(1, 18)
  205.               term.clearLine()
  206.               term.setCursorPos(18, 18)
  207.               print("Invalid Username")
  208.               sleep(1)
  209.               ioerror = ioerror+1
  210.               if ioerror == 3 then
  211.                 rep5 = rep5+1
  212.                 rep6 = rep6+1
  213.                 sid = 0
  214.               else
  215.               end
  216.             else
  217.               ioerror = 0
  218.               ulog = fs.exists("users/".. userlog)
  219.               if ulog == true then
  220.                 term.setCursorPos(1, 16)
  221.                 term.clearLine()
  222.                 term.setCursorPos(13, 16)
  223.                 print("|                        |")
  224.                 term.setCursorPos(15, 16)
  225.                 write("Password: ")
  226.                 sleep(1)
  227.                 userpas = readN(12, "*")
  228.                 if userpas == "" then
  229.                   term.setCursorPos(1, 18)
  230.                   term.clearLine()
  231.                   term.setCursorPos(15, 18)
  232.                   print("Invalid Password Entry")
  233.                   sleep(1)
  234.                   ioerror = ioerror+1
  235.                   term.setCursorPos(1, 18)
  236.                   term.clearLine()
  237.                   if ioerror == 3 then
  238.                     rep5 = rep5+1
  239.                     sid = 0
  240.                   else
  241.                   end
  242.                 else
  243.                   ulog = io.open("users/".. userlog, "r") or nil
  244.                   pass = ulog and ulog:read()
  245.                   ulog:close()
  246.                   if (pass and pass==userpas) then
  247.                     term.setCursorPos(1, 18)
  248.                     term.clearLine()
  249.                     term.setCursorPos(16, 18)
  250.                     print("  Login Success...")
  251.                     currentw = fs.open("current", "w")
  252.                     accessr = fs.open("users/".. userlog, "r")
  253.                     accessl = accessr.readLine() and accessr.readLine()
  254.                     currentw.writeLine(userlog)
  255.                     currentw.writeLine(accessl)
  256.                     currentw.close()
  257.                     accessr.close()
  258.                     rep5 = rep5+1
  259.                     rep6 = rep6+1
  260.                     prog = prog+1
  261.                     sid = 0
  262.                     sleep(1)
  263.                     shell.run("system")
  264.                   else
  265.                     if lockdown < 2 then
  266.                       term.setCursorPos(1, 18)
  267.                       term.clearLine()
  268.                       term.setCursorPos(16, 18)
  269.                       print("   Wrong Password")
  270.                       sleep(1)
  271.                       if userlog == puselog then
  272.                         lockdown = lockdown+1                    
  273.                       else
  274.                         lockdown = 0
  275.                         puselog = userlog
  276.                         lockdown = lockdown+1
  277.                       end
  278.                     else
  279.                       while rep6 == 0 do
  280.                         term.setCursorPos(15, 18)
  281.                         rnd = tostring(math.random(101, 999))
  282.                         if not fs.exists("pending/".. rnd) then
  283.                           fs.move("users/".. userlog, "pending/".. rnd)
  284.                           term.setCursorPos(1, 18)
  285.                           term.clearLine()
  286.                           term.setCursorPos(15, 18)
  287.                           print(" Lockdown Ref #: "..rnd)
  288.                           sleep(1)
  289.                           ulog = fs.open("pending/".. rnd, "a")
  290.                           ulog.writeLine("Username: ".. userlog)
  291.                           ulog.close()
  292.                           rep6 = rep6+1
  293.                           rep5 = rep5+1
  294.                           sid = 0
  295.                         else
  296.                         end
  297.                       end
  298.                     end
  299.                   end
  300.                 end
  301.               else
  302.                 term.setCursorPos(1, 18)
  303.                 term.clearLine()
  304.                 term.setCursorPos(14, 18)
  305.                 print("  Bad/Unknown Username")
  306.                 sleep(1)
  307.               end
  308.             end
  309.           end
  310.         end
  311.       elseif sid == 1 then
  312.         term.setCursorPos(13, 12)
  313.         print("|  Submit Account Info.  |")
  314.         term.setCursorPos(13, 13)
  315.         print("|                        |")
  316.         term.setCursorPos(13, 14)
  317.         print("|                        |")
  318.         term.setCursorPos(13, 15)
  319.         print("|                        |")
  320.         term.setCursorPos(13, 16)
  321.         print("|                        |")
  322.         term.setCursorPos(13, 17)
  323.         print("|------------------------|")
  324.            
  325.         while rep1 == 0 do
  326.           term.setCursorPos(1, 14)
  327.           term.clearLine()
  328.           term.setCursorPos(13, 14)
  329.           print("|                        |")
  330.           term.setCursorPos(15, 14)
  331.           write("Username: ")
  332.           sleep(1)
  333.           input = readN()
  334.           actc = input
  335.           actex = fs.exists("users/".. actc)
  336.           if input == "adminovrride" then
  337.             term.setCursorPos(1, 18)
  338.             term.clearLine()
  339.             term.setCursorPos(18, 18)
  340.             print("Invalid Username")
  341.             sleep(1)
  342.             ioerror = ioerror+1
  343.             if ioerror == 3 then
  344.               rep1 = 1
  345.               rep2 = 1
  346.               rep3 = 1
  347.               sid = 0
  348.             else
  349.             end
  350.           else
  351.             if input == "" then
  352.               term.setCursorPos(1, 18)
  353.               term.clearLine()
  354.               term.setCursorPos(18, 18)
  355.               print("Invalid Username")
  356.               sleep(1)
  357.               ioerror = ioerror+1
  358.               if ioerror == 3 then
  359.                 rep1 = 1
  360.                 rep2 = 1
  361.                 rep3 = 1
  362.                 sid = 0
  363.               else
  364.               end
  365.             elseif actex == true then
  366.               term.setCursorPos(1, 18)
  367.               term.clearLine()
  368.               term.setCursorPos(15, 18)
  369.               print (" Username Unavailable")
  370.               sleep(1)
  371.             else
  372.               ioerror = 0
  373.               rep1 = 1
  374.             end
  375.           end
  376.         end
  377.        
  378.         while rep2 == 0 do
  379.           while rep3 == 0 do
  380.             term.setCursorPos(15, 15)
  381.             write("Password: ")
  382.             sleep(1)
  383.             input = readN(12, "*")
  384.             if input == "" then
  385.               term.setCursorPos(1, 18)
  386.               term.clearLine()
  387.               term.setCursorPos(15, 18)
  388.               print("Invalid Password Entry")
  389.               sleep(1)
  390.               ioerror = ioerror+1
  391.               if ioerror == 3 then
  392.                 rep1 = 1
  393.                 rep2 = 1
  394.                 rep3 = 1
  395.               else
  396.               end
  397.             else
  398.               ioerror = 0
  399.               actpw = input
  400.               rep3 = 1
  401.               rep2 = 1
  402.             end
  403.           end
  404.          
  405.           term.setCursorPos(13, 16)
  406.           print("|                        |")
  407.           term.setCursorPos(15, 16)
  408.           write("Confirm : ")
  409.           sleep(1)
  410.           input = readN(12, "*")
  411.           actpc = input
  412.           if (actpc) == (actpw) then
  413.             while rep4 == 0 do
  414.               rnd = tostring(math.random(1011, 9999))
  415.               if not fs.exists("pending/".. rnd) then
  416.                 file = fs.open("pending/".. rnd, "w")
  417.                 file.writeLine(actc)
  418.                 file.writeLine(actpw)
  419.                 file.close()
  420.                 rep4 = rep4+1
  421.               else
  422.               end
  423.             end
  424.             term.setCursorPos(1, 18)
  425.             term.clearLine()
  426.             term.setCursorPos(13, 18)
  427.             print("  Pending... Ref #: ", rnd)
  428.             sleep(1)
  429.             ioerror = 0
  430.             rep2 = 1
  431.             sid = 0
  432.           else
  433.             term.setCursorPos(1, 18)
  434.             term.clearLine()
  435.             term.setCursorPos(15, 18)
  436.             print("Password Doesn't Match")
  437.             sleep(1)
  438.             rep2 = 0
  439.             ioerror = ioerror+1
  440.             if ioerror == 3 then
  441.               rep1 = 1
  442.               rep2 = 1
  443.               rep3 = 1
  444.             else
  445.             end
  446.           end
  447.         end
  448.        
  449.       elseif sid == 2 then
  450.         term.setCursorPos(13,14)
  451.         print("|########################|")
  452.         term.setCursorPos(13,15)
  453.         print("# SYSTEM SHUTTING DOWN!! #")
  454.         term.setCursorPos(13,16)
  455.         print("|########################|")
  456.         sleep(3)
  457.         os.shutdown()
  458.         end
  459.       end
  460.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement