Advertisement
HPWebcamAble

[CC][1.2] Password (Program and Shortcut)

Dec 7th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.51 KB | None | 0 0
  1. --[[
  2. Coded by HPWebcamAble
  3.  
  4. You need to use my Program Organizer to install this program
  5. You'll find the description there
  6.  
  7. Program Requirements:
  8. -Advanced Computer
  9.  
  10.  
  11. Version: 1.2
  12. Notes:
  13.   -Cleaned up the code a bit
  14.   -Bug fixes
  15.   -Ported to Advanced Computers
  16. ]]
  17.  
  18. --Variables--
  19. w,h = term.getSize()
  20. args = {...}
  21. local cUser --The correct username
  22. local cPass --and password
  23. local uUser --The password and username entered by the user
  24. local uPass
  25. local codes = { --pastebin codes, or what to put in the file (only used with missing files)
  26.   ["/password/bg"] = {"3Hsa4f63"},
  27.   ["/password/login"] = {nil,"admin\nadmin"},
  28.   ["/password/safemode"] = {nil,"true"}
  29. }
  30. --Functions--
  31. function color(txt,bck)
  32.   if txt then
  33.     term.setTextColor(txt)
  34.   end
  35.   if bck then
  36.     term.setBackgroundColor(bck)
  37.   end
  38. end
  39. function clear() shell.run("clear") end
  40. function tw(...) term.write(...) end
  41. function cp(...) term.setCursorPos(...) end
  42.  
  43. function printC(text,y)
  44.   if not y then
  45.     error("printC:for text "..text..", y value expected")
  46.   end
  47.   tLenght = #tostring(text)
  48.   local sStart = math.ceil(w/2-tLenght/2)
  49.   local sEnd = sStart + tLenght
  50.   cp(sStart,y)
  51.   tw(text)
  52.   return sStart,sEnd
  53. end
  54.  
  55. function refreshLogin()
  56.   f = fs.open("/password/login","r")
  57.   if not f then
  58.     color(colors.red)
  59.     print("Failed to open login file")
  60.     print("Try restarting Minecraft")
  61.     return
  62.   end
  63.   cUser = f.readLine()
  64.   cPass = f.readLine()
  65.   f.close()
  66. end
  67.  
  68. function scrollRead(x,y,maxX,insertText) --This basically scrolls but not very well
  69.   if insertText then
  70.     cPos = #insertText+1
  71.     cInput = insertText
  72.   else
  73.     cPos = 1
  74.     cInput = ""
  75.   end
  76.   term.setCursorBlink(true)
  77.   while true do
  78.     term.setCursorPos(x,y)
  79.     term.write(string.rep(" ",maxX))
  80.     term.setCursorPos(x,y)
  81.     if string.len(cInput) > maxX-1 then
  82.       term.write(string.sub(cInput,(maxX-1)*-1))
  83.     else
  84.       term.write(cInput)
  85.     end
  86.     if cPos > maxX-1 then
  87.       term.setCursorPos(x+maxX-1,y)
  88.     else  
  89.      term.setCursorPos(x+cPos-1,y)
  90.     end
  91.     if safeMode then --This is not usually part of this method, only for this program
  92.       event,p1 = os.pullEvent()
  93.     else
  94.       event,p1 = os.pullEventRaw()
  95.     end
  96.         if event == "char" then
  97.             cInput = string.sub(cInput,1,cPos)..p1..string.sub(cInput,cPos+1)
  98.             cPos = cPos+1          
  99.         elseif event == "key" then
  100.       if p1 == keys.enter then
  101.                 break
  102.       elseif p1 == keys.backspace then
  103.                 if cPos > 1 then
  104.                     cInput = string.sub(cInput,1,cPos-2)..string.sub(cInput,cPos)
  105.                     cPos = cPos - 1
  106.                 end
  107.             elseif p1 == keys["end"] then
  108.                 cPos = string.len(cInput)+1
  109.       end    
  110.     end
  111.   end
  112.   term.setCursorBlink(false)
  113.   return cInput
  114. end
  115.  
  116. function get(sCode,sFile) --This function is a modified portion of the Pastebin program
  117.   local sPath = shell.resolve( sFile )
  118.   if fs.exists( sPath ) then
  119.     return false
  120.   end
  121.   local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode ))
  122.   if response then              
  123.     local sResponse = response.readAll()
  124.     response.close()            
  125.     local file = fs.open( sPath, "w" )
  126.     file.write( sResponse )
  127.     file.close()
  128.     return true          
  129.   else
  130.     return false
  131.   end    
  132. end
  133.  
  134. function main()--Having this as a function allows error catching
  135.   if fs.exists("startup") then
  136.     f = fs.open("startup","r")
  137.     if f.readLine() ~= [[shell.run("pass elock")]] then
  138.       if fs.exists("~startup") then
  139.         shell.run("delete ~startup")
  140.       else
  141.         shell.run("rename startup ~startup")
  142.       end
  143.     end
  144.     f.close()
  145.   end
  146.   f = fs.open("startup","w")
  147.   f.write([[shell.run("pass elock")]])
  148.   f.close()
  149.  
  150.   while true do
  151.     color(nil,colors.white)
  152.     clear()
  153.     paintutils.drawImage(paintutils.loadImage("/password/bg"),1,1)
  154.     color(colors.lightGray,colors.white)
  155.     printC("Computer ID:"..os.getComputerID(),h)
  156.     color(colors.black,colors.lightGray)
  157.     cp(11,9)
  158.     tw("Username:")
  159.     cp(11,11)
  160.     tw("Password:")
  161.     color(colors.black,colors.white)
  162.     if uUser then
  163.       cp(20,9)
  164.       tw(uUser)
  165.       uPass = scrollRead(20,11,21)
  166.       if uPass ~= cPass or uUser ~= cUser then
  167.         uPass = nil
  168.         uUser = nil
  169.         color(colors.red,colors.lightGray)
  170.         printC("Access denied",12)
  171.         sleep(1)
  172.       else
  173.         break
  174.       end
  175.     else
  176.       uUser = scrollRead(20,9,21)
  177.     end
  178.   end
  179.  
  180.   color(colors.yellow,colors.black)
  181.   clear()
  182.   print(os.version())
  183.   color(colors.blue)
  184.   print("Welcome")
  185.   shell.run("delete startup")
  186.   if fs.exists("~startup") then
  187.     shell.run("rename ~startup startup")
  188.   end
  189. end
  190.  
  191.  
  192. --Program--
  193. if w ~= 51 and h ~= 19 or not term.isColor() then
  194.   print("This program can only run on an advanced computer")
  195. end
  196.  
  197. for a,b in pairs(codes) do
  198.   if not fs.exists(a) then
  199.     color(colors.red)
  200.     print("Missing "..a)
  201.     if codes[a][1] then
  202.       if not get(codes[a][1],a) then
  203.         color(colors.red)
  204.         print("Unable to download "..a.." from pastebin")
  205.         return
  206.       else
  207.         color(colors.white)
  208.         print("Downloaded "..a)
  209.       end
  210.     else
  211.       f = fs.open(a,"w")
  212.       f.write(codes[a][2])
  213.       f.close()
  214.       color(colors.white)
  215.       print("Created "..a)
  216.     end
  217.   end
  218. end
  219.  
  220. color(colors.white)
  221. if #args < 1 or #args > 2 or args[1] == "help" then
  222.   print("Usages:")
  223.   print("pass lock")
  224.   print("pass edit [user:pass]")
  225.   print("pass SafeMode (To toggle safemode)")
  226.   return
  227. end
  228.  
  229. f = fs.open("/password/safemode","r")
  230. firstLine = f.readLine()
  231. f.close()
  232. if firstLine == "true" then
  233.   safeMode = true
  234. elseif firstLine == "false" then
  235.   safeMode = false
  236. else
  237.   print("The safemode file isn't true or false!")
  238.   f = fs.open("/password/safemode","w")
  239.   f.write("true")
  240.   f.close()
  241.   print("Fixed")
  242.   color(colors.yellow)
  243.   print("Please retry your operation...")
  244.   return
  245. end
  246.  
  247. refreshLogin()
  248.  
  249. if cUser == nil or cUser == "" or cPass == nil or cPass == "" then
  250.   print("Login file is blank")
  251.   print("Fixing...")
  252.   f = fs.open("/password/login","w")
  253.   f.write("new\nnew")
  254.   f.close()
  255.   print("Success")
  256.   color(colors.yellow)
  257.   print("Please retry your operation...")
  258.   return
  259. end
  260.  
  261. if args[1] == "lock" then --Lock the computer
  262.   clear()
  263.   color(colors.yellow)
  264.   printC("HPWebcamAble's",1)
  265.   printC("Password Program",2)
  266.   cp(1,3)
  267.   print(" ")
  268.   color(colors.blue)
  269.   if safeMode then
  270.     print("SafeMode is on, so you can bypass the login by holding Ctrl+t. Type 'pass SafeMode' in the console to disable it.")
  271.   else
  272.     print("Note: SafeMode is off")
  273.   end
  274.   print(" ")
  275.   color(colors.white)
  276.   print("Current Username:"..cUser)
  277.   print("Current Password:"..cPass)
  278.   print(" ")
  279.   print("Enter - Continue (Lock)")
  280.   print("Tab - Cancel")
  281.   while true do
  282.     event,key = os.pullEvent("key")
  283.     if key == keys.enter then
  284.       break
  285.     elseif key == keys.tab then
  286.       color(colors.red)
  287.       print("Cancelled")
  288.       return
  289.     end
  290.   end
  291.   state,err = pcall(function() main() end) --Catch errors
  292. elseif args[1] == "edit" then --Edit login
  293.   if args[2] ~= "user" and args[2] ~= "pass" then
  294.     print("Usages:")
  295.     print("pass edit [user:pass]")
  296.     return
  297.   end
  298.   color(colors.white)
  299.   if args[2] == "pass" then --Change the password
  300.     print("Enter new password:")
  301.     tw("> ")
  302.     repeat
  303.       input = read()
  304.     until input ~= ""
  305.     if input == cPass then
  306.       color(colors.red)
  307.       print("That's the current password")
  308.     else
  309.       f = fs.open("/password/login","w")
  310.       f.write(cUser.."\n"..input)
  311.       f.close()
  312.       refreshLogin()
  313.       color(colors.lime)
  314.       print("Password changed")
  315.       return
  316.     end
  317.   else --Change the username
  318.     print("Enter new username:")
  319.     tw("> ")
  320.     repeat
  321.       input = read()
  322.     until input ~= ""
  323.     if input == cUser then
  324.       color(colors.red)
  325.       print("That's the current username")
  326.       return
  327.     else
  328.       f = fs.open("/password/login","w")
  329.       f.write(input.."\n"..cPass)
  330.       f.close()
  331.       refreshLogin()
  332.       color(colors.lime)
  333.       print("Username changed")
  334.       return
  335.     end
  336.   end
  337. elseif args[1] == "elock" then --Lock without confirmation
  338.   state,err = pcall(function() main() end) --Catch errors
  339. elseif string.lower(args[1]) == "safemode" then
  340.   print("SafeMode: "..tostring(safeMode))
  341.   if safeMode then
  342.     color(colors.red)
  343.     print("Turning SafeMode off prevents the login from being bypassed")
  344.   end
  345.   color(colors.white)
  346.   print("Toggle SafeMode?")
  347.   print("Y/N")
  348.   while true do
  349.     event,key = os.pullEvent("key")
  350.     if key == keys.y then
  351.       safeMode = not safeMode
  352.       f = fs.open("/password/safemode","w")
  353.       f.write(tostring(safeMode))
  354.       f.close()
  355.       color(colors.lime)
  356.       print("SafeMode toggled")
  357.       os.pullEvent("char")
  358.       error()
  359.     elseif key ==keys.n then
  360.       color(colors.red)
  361.       print("Cancelled")
  362.       os.pullEvent("char")
  363.       error()
  364.     end
  365.   end
  366. else
  367.   print("Usages:")
  368.   print("pass lock")
  369.   print("pass SafeMode (To toggle safemode)")
  370.   print("pass edit [user:pass]")
  371.   return
  372. end
  373.  
  374. if err then
  375.   if string.find(err,"Terminated") then
  376.     term.setCursorBlink(false)
  377.     color(colors.white,colors.black)
  378.     clear()
  379.     print("Program terminated")
  380.     shell.run("delete startup") --No need to check for the file; if the program has run, it should be there
  381.   else
  382.     color(colors.red,colors.black)
  383.     clear()
  384.     printC("Error",1)
  385.     cp(1,3)
  386.     color(colors.white)
  387.     print(err)
  388.   end
  389. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement