Advertisement
Guest User

dep-get

a guest
Feb 28th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.23 KB | None | 0 0
  1. local function modRead()
  2.   local text = ""
  3.   while true do
  4.     e = {os.pullEvent()}
  5.     if e[1] == "key" then
  6.       if e[2] == keys.enter then
  7.         return text
  8.       elseif e[2] == keys.backspace then
  9.         text = text:sub(1,#text-1)
  10.       end
  11.     elseif e[1] == "char" then
  12.       text = text..e[2]
  13.     end
  14.   end
  15.   term.setCursorBlink(false)
  16. end
  17. local server = "http://luca.spdns.eu/dep-get/cc/"
  18. local function getPage(page)
  19.   local uri = server..page
  20.   local stream = http.get(uri)
  21.   local content = stream.readAll()
  22.   local res = stream.getResponseCode()
  23.   stream.close()
  24.   return content,res
  25. end
  26. local function contains(table,key)
  27.   for i,v in pairs(table) do
  28.     if v == key then
  29.       return true
  30.     end
  31.   end
  32.   return false
  33. end
  34. local function installAPI(api)
  35.   print("Installing API "..api)
  36.   local depsRaw = getPage("depget.php?file="..api)
  37.   table.insert(_G.depapis,api)
  38.   for dep in (depsRaw.."\n"):gmatch("([^\n]*)\n") do
  39.     if dep ~= "" and not contains(_G.depapi,dep) then
  40.       installAPI(dep)
  41.     end
  42.   end
  43.   local content = getPage("get.php?file="..api)
  44.   local f = fs.open("/usr/apis/"..api,"w")
  45.   f.write(content)
  46.   f.close()
  47.   os.loadAPI("/usr/apis/"..api)
  48. end
  49. local function install(program)
  50.   print("Installing "..program)
  51.   local depsRaw = getPage("depget.php?file="..program)
  52.   if not contains(_G.depinstalled,program) then
  53.     for dep in (depsRaw.."\n"):gmatch("([^\n]*)\n") do
  54.       if dep ~= "" and not contains(_G.depapis,dep) then
  55.            installAPI(dep)
  56.       end
  57.     end
  58.     local content = getPage("get.php?file="..program)
  59.     local f = fs.open("/usr/bin/"..program,"w")
  60.     f.write(content)
  61.     f.close()
  62.   else
  63.     print(program.." is already installed!")
  64.   end
  65. end
  66. local tArgs = {...}
  67. if #tArgs == 0 or tArgs[1] == "help" then
  68.   print("Dep-Get help:")
  69.   print("  dep-get list <filter>")
  70.   print("  dep-get startup")
  71.   print("  dep-get download <file>")
  72.   print("  dep-get install <program>")
  73.   print("  dep-get upload <user> <file>")
  74.   print("  This dep has Super Cow Powers")
  75. elseif tArgs[1] == "moo" then
  76.   print("        (__)")
  77.   print("        (oo)")
  78.   print("  /------\\/")
  79.   print(" / |    ||")
  80.   print("*  /\\   /\\")
  81.   print("   ~~   ~~")
  82.   print("...\"Have you mooed today?\"...")
  83. elseif tArgs[1] == "list" then
  84.   local filter = false
  85.   local filterText = ""
  86.   if tArgs[2] then
  87.     filter = true
  88.     filterText = tArgs[2]
  89.   end
  90.   local content = getPage("list.php")
  91.   term.clear()
  92.   local files = {}
  93.   for f in (content.."\n"):gmatch("([^\n]*)\n") do
  94.     if filter then
  95.       if f:lower():match(filterText:lower()) then
  96.         table.insert(files,f)
  97.       end
  98.     else
  99.       table.insert(files,f)
  100.     end
  101.   end
  102.   local y = 1
  103.   local _,my = term.getSize()
  104.   for i,v in pairs(files) do
  105.     term.setCursorPos(1,y)
  106.     write(v)
  107.     y = y + 1
  108.     if y == my then
  109.       term.setCursorPos(1,y)
  110.       write("--Press a key to continue(Q to quit)--")
  111.       local e,k = os.pullEvent("key")
  112.       if k == keys.q then
  113.         sleep(.05)
  114.         break
  115.       else
  116.         term.clear()
  117.         y = 1
  118.       end
  119.     end
  120.   end
  121.   print()
  122.   sleep(.05)
  123. elseif tArgs[1] == "startup" then
  124.   shell.setPath(shell.path()..":/usr/bin")
  125.   _G.depapis = fs.list("/usr/apis")
  126.   _G.depinstalled = fs.list("/usr/bin")
  127.   for i,v in pairs(_G.depapis) do
  128.     os.loadAPI("/usr/apis/"..v)
  129.   end
  130. elseif tArgs[1] == "download" then
  131.   local file = tArgs[2]
  132.   local content = getPage("get.php?file="..file)
  133.   local f = fs.open(file,"w")
  134.   f.write(content)
  135.   f.close()
  136.   print("File downloaded raw!")
  137. elseif tArgs[1] == "install" then
  138.   local file = tArgs[2]
  139.   install(file)
  140. elseif tArgs[1] == "upload" then
  141.   local user = tArgs[2]
  142.   local file = tArgs[3]
  143.   write("Password for "..user.."(hidden): ")
  144.   local pass = modRead()
  145.   print()
  146.   local data = "?user="..user.."&"
  147.   data = data.."password="..pass.."&"
  148.   data = data.."file="..textutils.urlEncode(file).."&"
  149.   write("Reading in "..file.."...")
  150.   local f = fs.open(file,"r")
  151.   local cont = f.readAll()
  152.   print("done")
  153.   write("Uploading...")
  154.   data = data.."filecontent="..textutils.urlEncode(cont)
  155.   local stream = http.get(server.."upload.php"..data)
  156.   local res = stream.readAll()
  157.   stream.close()
  158.   print(res)
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement