Advertisement
Guest User

pastebin.lua

a guest
Apr 6th, 2020
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.14 KB | None | 0 0
  1. --[[ This program allows downloading and uploading from and to pastebin.com.
  2.      Authors: Sangar, Vexatos ]]
  3. local component = require("component")
  4. local fs = require("filesystem")
  5. local internet = require("internet")
  6. local shell = require("shell")
  7.  
  8. if not component.isAvailable("internet") then
  9.   io.stderr:write("This program requires an internet card to run.")
  10.   return
  11. end
  12.  
  13. local args, options = shell.parse(...)
  14.  
  15. -- This gets code from the website and stores it in the specified file.
  16. local function get(pasteId, filename)
  17.   local f, reason = io.open(filename, "w")
  18.   if not f then
  19.     io.stderr:write("Failed opening file for writing: " .. reason)
  20.     return
  21.   end
  22.  
  23.   io.write("Downloading from pastebin.com... ")
  24.   local url = "http://pastebin.com/raw/" .. pasteId
  25.   local result, response = pcall(internet.request, url)
  26.   if result then
  27.     io.write("success.\n")
  28.     for chunk in response do
  29.       if not options.k then
  30.         string.gsub(chunk, "\r\n", "\n")
  31.       end
  32.       f:write(chunk)
  33.     end
  34.  
  35.     f:close()
  36.     io.write("Saved data to " .. filename .. "\n")
  37.   else
  38.     io.write("failed.\n")
  39.     f:close()
  40.     fs.remove(filename)
  41.     io.stderr:write("HTTP request failed: " .. response .. "\n")
  42.   end
  43. end
  44.  
  45. -- This makes a string safe for being used in a URL.
  46. function encode(code)
  47.   if code then
  48.     code = string.gsub(code, "([^%w ])", function (c)
  49.       return string.format("%%%02X", string.byte(c))
  50.     end)
  51.     code = string.gsub(code, " ", "+")
  52.   end
  53.   return code
  54. end
  55.  
  56. -- This stores the program in a temporary file, which it will
  57. -- delete after the program was executed.
  58. function run(pasteId, ...)
  59.   local tmpFile = os.tmpname()
  60.   get(pasteId, tmpFile)
  61.   io.write("Running...\n")
  62.  
  63.   local success, reason = shell.execute(tmpFile, nil, ...)
  64.   if not success then
  65.     io.stderr:write(reason)
  66.   end
  67.   fs.remove(tmpFile)
  68. end
  69.  
  70. -- Uploads the specified file as a new paste to pastebin.com.
  71. function put(path)
  72.   local config = {}
  73.   local configFile = loadfile("/etc/pastebin.conf", "t", config)
  74.   if configFile then
  75.     local result, reason = pcall(configFile)
  76.     if not result then
  77.       io.stderr:write("Failed loading config: " .. reason)
  78.     end
  79.   end
  80.   config.key = config.key or "fd92bd40a84c127eeb6804b146793c97"
  81.   local file, reason = io.open(path, "r")
  82.  
  83.   if not file then
  84.     io.stderr:write("Failed opening file for reading: " .. reason)
  85.     return
  86.   end
  87.  
  88.   local data = file:read("*a")
  89.   file:close()
  90.  
  91.   io.write("Uploading to pastebin.com... ")
  92.   local result, response = pcall(internet.request,
  93.         "http://pastebin.com/api/api_post.php",
  94.         "api_option=paste&" ..
  95.         "api_dev_key=" .. config.key .. "&" ..
  96.         "api_paste_format=lua&" ..
  97.         "api_paste_expire_date=N&" ..
  98.         "api_paste_name=" .. encode(fs.name(path)) .. "&" ..
  99.         "api_paste_code=" .. encode(data))
  100.  
  101.   if result then
  102.     local info = ""
  103.     for chunk in response do
  104.       info = info .. chunk
  105.     end
  106.     if string.match(info, "^Bad API request, ") then
  107.       io.write("failed.\n")
  108.       io.write(info)
  109.     else
  110.       io.write("success.\n")
  111.       local pasteId = string.match(info, "[^/]+$")
  112.       io.write("Uploaded as " .. info .. "\n")
  113.       io.write('Run "pastebin get ' .. pasteId .. '" to download anywhere.')
  114.     end
  115.   else
  116.     io.write("failed.\n")
  117.     io.stderr:write(response)
  118.   end
  119. end
  120.  
  121. local command = args[1]
  122. if command == "put" then
  123.   if #args == 2 then
  124.     put(shell.resolve(args[2]))
  125.     return
  126.   end
  127. elseif command == "get" then
  128.   if #args == 3 then
  129.     local path = shell.resolve(args[3])
  130.     if fs.exists(path) then
  131.       if not options.f or not os.remove(path) then
  132.         io.stderr:write("file already exists")
  133.         return
  134.       end
  135.     end
  136.     get(args[2], path)
  137.     return
  138.   end
  139. elseif command == "run" then
  140.   if #args >= 2 then
  141.     run(args[2], table.unpack(args, 3))
  142.     return
  143.   end
  144. end
  145.  
  146. -- If we come here there was some invalid input.
  147. io.write("Usages:\n")
  148. io.write("pastebin put [-f] <file>\n")
  149. io.write("pastebin get [-f] <id> <file>\n")
  150. io.write("pastebin run [-f] <id> [<arguments...>]\n")
  151. io.write(" -f: Force overwriting existing files.\n")
  152. io.write(" -k: keep line endings as-is (will convert\n")
  153. io.write("     Windows line endings to Unix otherwise).")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement