Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local server = "http://mindblow.no-ip.org/pacman/api"
- local quiet = false
- local termcolor = term.isColor()
- --Quiet printing
- local oprint = print
- local function nprint(text)
- if not quiet then return oprint(text)
- else return false end
- end
- local print = nprint
- --End of quiet printing
- --API:
- local function st(color)
- if termcolor then
- term.setTextColor(color)
- end
- end
- local function sb(color)
- if termcolor then
- term.setBackgroundColor(color)
- end
- end
- local function clear()
- term.clear()
- term.setCursorPos(1,1)
- sb(colors.black)
- st(colors.white)
- end
- local function addToLog(name, path)
- local logfile = fs.open("pacman/list_installed", "a")
- logfile.write(name.."||AT||"..path.."\n")
- logfile.close()
- end
- local function downloadPastebin(name, code, run)
- st(colors.lime)
- print("Resource on pastebin. Connecting...")
- code = string.sub(code, 2)
- local resp = nil
- resp = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(code))
- if resp == nil then
- st(colors.red)
- print("Error while transfering file. Installation failed.")
- return
- end
- local saveas
- st(colors.lime)
- if run then
- print("File downloaded. This file is an install script - will be saved as "..name.."/install.")
- saveas = "install"
- else
- print("File downloaded. This file is not an install script - will be saved as "..name.."/"..name..".")
- saveas = name
- end
- fs.makeDir(name)
- if fs.exists(name.."/"..saveas) then
- st(colors.yellow)
- write("This file exists. Do you want to overwrite it?")
- st(colors.white)
- print(" (y/n)")
- key = nil
- while (key ~= "y" and key ~= "n") do
- e, key = os.pullEvent("char")
- end
- if key == "n" then
- st(colors.red)
- print("\n\nAborted!")
- return
- end
- fs.delete(name.."/"..saveas)
- end
- local file = fs.open(name.."/"..saveas, "w")
- file.write(resp.readAll())
- file.close()
- addToLog(name, name.."/"..saveas)
- st(colors.lime)
- write("Done! ")
- st(colors.lightBlue)
- write("Do you want to run it")
- if run then write(" (this is an install script...)") end
- write("?")
- st(colors.white)
- print(" (y/n)")
- key = nil
- while (key ~= "y" and key ~= "n") do
- e, key = os.pullEvent("char")
- end
- if key == "y" then
- print("OK. You can always run it again by typing \""..name.."/"..saveas.."..\"")
- shell.run(name.."/"..saveas)
- return
- else
- st(colors.white)
- print("OK. You can always run it by typing \""..name.."/"..saveas.."..\"")
- return
- end
- end
- local function downloadPacman(name, link, run)
- st(colors.lime)
- print("Resource on pacman server. Connecting...")
- local resp = nil
- resp = http.get(link)
- if resp == nil then
- st(colors.red)
- print("Error while transfering file. Installation failed.")
- return
- end
- local saveas
- st(colors.lime)
- if run then
- print("File downloaded. This file is an install script - will be saved as "..name.."/install.")
- saveas = "install"
- else
- print("File downloaded. This file is not an install script - will be saved as "..name.."/"..name..".")
- saveas = name
- end
- fs.makeDir(name)
- if fs.exists(name.."/"..saveas) then
- st(colors.yellow)
- write("This file exists. Do you want to overwrite it?")
- st(colors.white)
- print(" (y/n)")
- key = nil
- while (key ~= "y" and key ~= "n") do
- e, key = os.pullEvent("char")
- end
- if key == "n" then
- st(colors.red)
- print("\n\nAborted!")
- return
- end
- fs.delete(name.."/"..saveas)
- end
- local file = fs.open(name.."/"..saveas, "w")
- file.write(resp.readAll())
- file.close()
- addToLog(name, name.."/"..saveas)
- st(colors.lime)
- write("Done! ")
- st(colors.lightBlue)
- write("Do you want to run it")
- if run then write(" (this is an install script...)") end
- write("?")
- st(colors.white)
- print(" (y/n)")
- key = nil
- while (key ~= "y" and key ~= "n") do
- e, key = os.pullEvent("char")
- end
- if key == "y" then
- print("OK. You can always run it again by typing \""..name.."/"..saveas.."..\"")
- shell.run(name.."/"..saveas)
- return
- else
- st(colors.white)
- print("OK. You can always run it by typing \""..name.."/"..saveas.."..\"")
- return
- end
- end
- --End of API
- local args = {...}
- if #args < 1 then
- print("Manual: pacman help")
- return
- end
- clear()
- if args[1] == "i" or args[1] == "install" then
- write("You are about to install ")
- st(colors.lightBlue)
- write(args[2])
- st(colors.white)
- print(" package. Do you want to proceed? (y/n)")
- local key = nil
- while (key ~= "y" and key ~= "n") do
- e, key = os.pullEvent("char")
- end
- if key == "n" then
- st(colors.red)
- print("\n\nAborted!")
- return
- end
- clear()
- st(colors.lime)
- print("Connecting to server...")
- local resp = http.get(server.."?package="..string.lower(args[2]))
- if resp == nil then
- st(colors.red)
- print("Server not aviawable")
- return
- end
- local got = resp.readLine()
- if got == "404" then
- st(colors.red)
- print("Package does not exists")
- return
- elseif got == "410" then
- st(colors.red)
- print("Resource is gone. It might be disabled or it was deleted")
- return
- elseif got == "500" then
- st(colors.red)
- print("Unknown error. Try later.")
- return
- elseif got == "down" then
- st(colors.red)
- print("Server down for maintenance")
- return
- elseif string.find(got, "!") ~= 1 and string.find(got, "http://") ~= 1 then
- st(colors.red)
- print("Received address is not valid.")
- return
- end
- st(colors.white)
- print("Got resource link...")
- local run = resp.readLine()
- if string.find(got, "!") == 1 then
- downloadPastebin(args[2], got, run)
- else
- downloadPacman(args[2], got, run)
- end
- end
- if args[1] == "r" or args[1] == "remove" then
- if not fs.exists(args[2]) then
- st(colors.red)
- print("No such package!")
- return
- end
- write("You are about to remove ")
- st(colors.red)
- write(args[2])
- st(colors.white)
- print(" package. Do you want to proceed? (y/n)")
- local key = nil
- while (key ~= "y" and key ~= "n") do
- e, key = os.pullEvent("char")
- end
- if key == "n" then
- st(colors.red)
- print("\n\nAborted!")
- return
- end
- list = fs.open("pacman/list_installed", "r")
- local is = false
- local line = list.readLine()
- while line ~= nil do
- if string.find(line, args[2]) ~= nil then
- is = true
- break
- end
- line = list.readLine()
- end
- if not is then
- st(colors.yellow)
- write("Package "..args[2].." was not installed via pacman. Do you still want to proceed?")
- st(colors.white)
- print(" (y/n)")
- key = nil
- while (key ~= "y" and key ~= "n") do
- e, key = os.pullEvent("char")
- end
- if key == "n" then
- st(colors.red)
- print("\n\nAborted!")
- return
- end
- end
- fs.delete(args[2])
- st(colors.lime)
- print("Deleted!")
- return
- end
- if args[1] == "help" then
- print([[
- Welcome to pacman!
- Commands:
- - pacman [i|install] package
- to install a package
- - pacman [r|remove] package
- to remove a package
- - pacman list
- to list all packages
- - pacman help
- to see help page
- ]])
- return
- end
- print("Manual: pacman help")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement