Advertisement
HPWebcamAble

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

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