Advertisement
jesusthekiller

pacman

Mar 30th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.13 KB | None | 0 0
  1. local server = "http://mindblow.no-ip.org/pacman/api"
  2. local quiet = false
  3.  
  4. local termcolor = term.isColor()
  5.  
  6. --Quiet printing
  7.  
  8. local oprint = print
  9.  
  10. local function nprint(text)
  11.     if not quiet then return oprint(text)
  12.     else return false end
  13. end
  14.  
  15. local print = nprint
  16.  
  17. --End of quiet printing
  18.  
  19. --API:
  20.  
  21. local function st(color)
  22.     if termcolor then
  23.         term.setTextColor(color)
  24.     end
  25. end
  26.  
  27. local function sb(color)
  28.     if termcolor then
  29.         term.setBackgroundColor(color)
  30.     end
  31. end
  32.  
  33. local function clear()
  34.     term.clear()
  35.     term.setCursorPos(1,1)
  36.     sb(colors.black)
  37.     st(colors.white)
  38. end
  39.  
  40. local function addToLog(name, path)
  41.     local logfile = fs.open("pacman/list_installed", "a")
  42.     logfile.write(name.."||AT||"..path.."\n")
  43.     logfile.close()
  44. end
  45.  
  46. local function downloadPastebin(name, code, run)
  47.     st(colors.lime)
  48.     print("Resource on pastebin. Connecting...")
  49.    
  50.     code = string.sub(code, 2)
  51.    
  52.     local resp = nil
  53.     resp = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code))
  54.    
  55.     if resp == nil then
  56.         st(colors.red)
  57.         print("Error while transfering file. Installation failed.")
  58.         return
  59.     end
  60.    
  61.     local saveas
  62.    
  63.     st(colors.lime)
  64.     if run then
  65.         print("File downloaded. This file is an install script - will be saved as "..name.."/install.")
  66.         saveas = "install"
  67.     else
  68.         print("File downloaded. This file is not an install script - will be saved as "..name.."/"..name..".")
  69.         saveas = name
  70.     end
  71.    
  72.     fs.makeDir(name)
  73.    
  74.     if fs.exists(name.."/"..saveas) then
  75.         st(colors.yellow)
  76.         write("This file exists. Do you want to overwrite it?")
  77.         st(colors.white)
  78.         print(" (y/n)")
  79.        
  80.         key = nil
  81.         while (key ~= "y" and key ~= "n") do
  82.             e, key = os.pullEvent("char")
  83.         end
  84.        
  85.         if key == "n" then
  86.             st(colors.red)
  87.             print("\n\nAborted!")
  88.             return
  89.         end
  90.        
  91.         fs.delete(name.."/"..saveas)
  92.     end
  93.    
  94.     local file = fs.open(name.."/"..saveas, "w")
  95.     file.write(resp.readAll())
  96.    
  97.     file.close()
  98.    
  99.     addToLog(name, name.."/"..saveas)
  100.    
  101.     st(colors.lime)
  102.     write("Done! ")
  103.     st(colors.lightBlue)
  104.     write("Do you want to run it")
  105.     if run then write(" (this is an install script...)") end
  106.     write("?")
  107.     st(colors.white)
  108.     print(" (y/n)")
  109.    
  110.     key = nil
  111.     while (key ~= "y" and key ~= "n") do
  112.         e, key = os.pullEvent("char")
  113.     end
  114.    
  115.     if key == "y" then
  116.         print("OK. You can always run it again by typing \""..name.."/"..saveas.."..\"")
  117.         shell.run(name.."/"..saveas)
  118.         return
  119.     else
  120.         st(colors.white)
  121.         print("OK. You can always run it by typing \""..name.."/"..saveas.."..\"")
  122.         return
  123.     end
  124. end
  125.  
  126. local function downloadPacman(name, link, run)
  127.     st(colors.lime)
  128.     print("Resource on pacman server. Connecting...")
  129.    
  130.     local resp = nil
  131.     resp = http.get(link)
  132.    
  133.     if resp == nil then
  134.         st(colors.red)
  135.         print("Error while transfering file. Installation failed.")
  136.         return
  137.     end
  138.    
  139.     local saveas
  140.    
  141.     st(colors.lime)
  142.     if run then
  143.         print("File downloaded. This file is an install script - will be saved as "..name.."/install.")
  144.         saveas = "install"
  145.     else
  146.         print("File downloaded. This file is not an install script - will be saved as "..name.."/"..name..".")
  147.         saveas = name
  148.     end
  149.    
  150.     fs.makeDir(name)
  151.    
  152.     if fs.exists(name.."/"..saveas) then
  153.         st(colors.yellow)
  154.         write("This file exists. Do you want to overwrite it?")
  155.         st(colors.white)
  156.         print(" (y/n)")
  157.        
  158.         key = nil
  159.         while (key ~= "y" and key ~= "n") do
  160.             e, key = os.pullEvent("char")
  161.         end
  162.        
  163.         if key == "n" then
  164.             st(colors.red)
  165.             print("\n\nAborted!")
  166.             return
  167.         end
  168.        
  169.         fs.delete(name.."/"..saveas)
  170.     end
  171.    
  172.     local file = fs.open(name.."/"..saveas, "w")
  173.     file.write(resp.readAll())
  174.    
  175.     file.close()
  176.    
  177.     addToLog(name, name.."/"..saveas)
  178.    
  179.     st(colors.lime)
  180.     write("Done! ")
  181.     st(colors.lightBlue)
  182.     write("Do you want to run it")
  183.     if run then write(" (this is an install script...)") end
  184.     write("?")
  185.     st(colors.white)
  186.     print(" (y/n)")
  187.    
  188.     key = nil
  189.     while (key ~= "y" and key ~= "n") do
  190.         e, key = os.pullEvent("char")
  191.     end
  192.    
  193.     if key == "y" then
  194.         print("OK. You can always run it again by typing \""..name.."/"..saveas.."..\"")
  195.         shell.run(name.."/"..saveas)
  196.         return
  197.     else
  198.         st(colors.white)
  199.         print("OK. You can always run it by typing \""..name.."/"..saveas.."..\"")
  200.         return
  201.     end
  202. end
  203.  
  204. --End of API
  205.  
  206. local args = {...}
  207.  
  208. if #args < 1 then
  209.     print("Manual: pacman help")
  210.     return
  211. end
  212.  
  213. clear()
  214.  
  215. if args[1] == "i" or args[1] == "install" then
  216.     write("You are about to install ")
  217.     st(colors.lightBlue)
  218.     write(args[2])
  219.     st(colors.white)
  220.     print(" package. Do you want to proceed? (y/n)")
  221.    
  222.     local key = nil
  223.     while (key ~= "y" and key ~= "n") do
  224.         e, key = os.pullEvent("char")
  225.     end
  226.    
  227.     if key == "n" then
  228.         st(colors.red)
  229.         print("\n\nAborted!")
  230.         return
  231.     end
  232.    
  233.     clear()
  234.     st(colors.lime)
  235.     print("Connecting to server...")
  236.    
  237.     local resp = http.get(server.."?package="..string.lower(args[2]))
  238.    
  239.     if resp == nil then
  240.         st(colors.red)
  241.         print("Server not aviawable")
  242.         return
  243.     end
  244.    
  245.     local got = resp.readLine()
  246.    
  247.     if got == "404" then
  248.         st(colors.red)
  249.         print("Package does not exists")
  250.         return
  251.     elseif got == "410" then
  252.         st(colors.red)
  253.         print("Resource is gone. It might be disabled or it was deleted")
  254.         return
  255.     elseif got == "500" then
  256.         st(colors.red)
  257.         print("Unknown error. Try later.")
  258.         return
  259.     elseif got == "down" then
  260.         st(colors.red)
  261.         print("Server down for maintenance")
  262.         return
  263.     elseif string.find(got, "!") ~= 1 and string.find(got, "http://") ~= 1 then
  264.         st(colors.red)
  265.         print("Received address is not valid.")
  266.         return
  267.     end
  268.    
  269.     st(colors.white)
  270.     print("Got resource link...")
  271.    
  272.     local run = resp.readLine()
  273.    
  274.     if string.find(got, "!") == 1 then
  275.         downloadPastebin(args[2], got, run)
  276.     else
  277.         downloadPacman(args[2], got, run)
  278.     end
  279. end
  280.  
  281. if args[1] == "r" or args[1] == "remove" then
  282.     if not fs.exists(args[2]) then
  283.         st(colors.red)
  284.         print("No such package!")
  285.         return
  286.     end
  287.    
  288.     write("You are about to remove ")
  289.     st(colors.red)
  290.     write(args[2])
  291.     st(colors.white)
  292.     print(" package. Do you want to proceed? (y/n)")
  293.    
  294.     local key = nil
  295.     while (key ~= "y" and key ~= "n") do
  296.         e, key = os.pullEvent("char")
  297.     end
  298.    
  299.     if key == "n" then
  300.         st(colors.red)
  301.         print("\n\nAborted!")
  302.         return
  303.     end
  304.    
  305.     list = fs.open("pacman/list_installed", "r")
  306.    
  307.     local is = false
  308.    
  309.     local line = list.readLine()
  310.    
  311.     while line ~= nil do
  312.         if string.find(line, args[2]) ~= nil then
  313.             is = true
  314.             break
  315.         end
  316.         line = list.readLine()
  317.     end
  318.    
  319.     if not is then
  320.         st(colors.yellow)
  321.         write("Package "..args[2].." was not installed via pacman. Do you still want to proceed?")
  322.         st(colors.white)
  323.         print(" (y/n)")
  324.        
  325.         key = nil
  326.         while (key ~= "y" and key ~= "n") do
  327.             e, key = os.pullEvent("char")
  328.         end
  329.        
  330.         if key == "n" then
  331.             st(colors.red)
  332.             print("\n\nAborted!")
  333.             return
  334.         end
  335.     end
  336.    
  337.     fs.delete(args[2])
  338.    
  339.     st(colors.lime)
  340.     print("Deleted!")
  341.     return
  342. end
  343.  
  344. if args[1] == "help" then
  345.     print([[
  346.     Welcome to pacman!
  347.    
  348.     Commands:
  349.     - pacman [i|install] package
  350.       to install a package
  351.     - pacman [r|remove] package
  352.       to remove a package
  353.     - pacman list
  354.       to list all packages
  355.     - pacman help
  356.       to see help page
  357.     ]])
  358.     return
  359. end
  360.  
  361. print("Manual: pacman help")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement