Advertisement
faeranne

pastebin.lua

Dec 4th, 2017 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.98 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. local term = require("term")
  8. local string = require("string")
  9.  
  10. if not component.isAvailable("internet") then
  11.   io.stderr:write("This program requires an internet card to run.")
  12.   return
  13. end
  14.  
  15. local args, options = shell.parse(...)
  16.  
  17. -- This gets code from the website and stores it in the specified file.
  18. local function get(pasteId, filename)
  19.   local f, reason = io.open(filename, "w")
  20.   if not f then
  21.     io.stderr:write("Failed opening file for writing: " .. reason)
  22.     return
  23.   end
  24.  
  25.   io.write("Downloading from pastebin.com... ")
  26.   local url = "https://pastebin.com/raw.php?i=" .. pasteId
  27.   local result, response = pcall(internet.request, url)
  28.   if result then
  29.     io.write("success.\n")
  30.     for chunk in response do
  31.       if not options.k then
  32.         string.gsub(chunk, "\r\n", "\n")
  33.       end
  34.       f:write(chunk)
  35.     end
  36.  
  37.     f:close()
  38.     io.write("Saved data to " .. filename .. "\n")
  39.   else
  40.     io.write("failed.\n")
  41.     f:close()
  42.     fs.remove(filename)
  43.     io.stderr:write("HTTP request failed: " .. response .. "\n")
  44.   end
  45. end
  46.  
  47. -- This makes a string safe for being used in a URL.
  48. function encode(code)
  49.   if code then
  50.     code = string.gsub(code, "([^%w ])", function (c)
  51.       return string.format("%%%02X", string.byte(c))
  52.     end)
  53.     code = string.gsub(code, " ", "+")
  54.   end
  55.   return code
  56. end
  57.  
  58. -- This stores the program in a temporary file, which it will
  59. -- delete after the program was executed.
  60. function run(pasteId, ...)
  61.   local tmpFile = os.tmpname()
  62.   get(pasteId, tmpFile)
  63.   io.write("Running...\n")
  64.  
  65.   local success, reason = shell.execute(tmpFile, nil, ...)
  66.   if not success then
  67.     io.stderr:write(reason)
  68.   end
  69.   fs.remove(tmpFile)
  70. end
  71.  
  72. -- Uploads the specified file as a new paste to pastebin.com.
  73. function put(path)
  74.   local config = {}
  75.   local configFile = loadfile("/etc/pastebin.conf", "t", config)
  76.   if configFile then
  77.     local result, reason = pcall(configFile)
  78.     if not result then
  79.       io.stderr:write("Failed loading config: " .. reason)
  80.     end
  81.   end
  82.   local userKeyFile, reason = io.open("/etc/pastebin_user.key", "r")
  83.   if userKeyFile then
  84.     config.userKey = userKeyFile:read("*a")
  85.     userKeyFile:close()
  86.   else
  87.     config.userKey = ""
  88.   end
  89.   config.key = config.key or "fd92bd40a84c127eeb6804b146793c97"
  90.   local file, reason = io.open(path, "r")
  91.  
  92.   if not file then
  93.     io.stderr:write("Failed opening file for reading: " .. reason)
  94.     return
  95.   end
  96.  
  97.   local data = file:read("*a")
  98.   file:close()
  99.  
  100.   io.write("Uploading to pastebin.com... ")
  101.   local result, response = pcall(internet.request,
  102.         "https://pastebin.com/api/api_post.php",
  103.         "api_option=paste&" ..
  104.         "api_dev_key=" .. config.key .. "&" ..
  105.         "api_user_key=" .. config.userKey .. "&" ..
  106.         "api_paste_format=lua&" ..
  107.         "api_paste_expire_date=N&" ..
  108.         "api_paste_name=" .. encode(fs.name(path)) .. "&" ..
  109.         "api_paste_code=" .. encode(data))
  110.  
  111.   if result then
  112.     local info = ""
  113.     for chunk in response do
  114.       info = info .. chunk
  115.     end
  116.     if string.match(info, "^Bad API request, ") then
  117.       io.write("failed.\n")
  118.       io.write(info)
  119.     else
  120.       io.write("success.\n")
  121.       local pasteId = string.match(info, "[^/]+$")
  122.       io.write("Uploaded as " .. info .. "\n")
  123.       io.write('Run "pastebin get ' .. pasteId .. '" to download anywhere.\n')
  124.     end
  125.   else
  126.     io.write("failed.\n")
  127.     io.stderr:write(response)
  128.   end
  129. end
  130.  
  131. function login(username)
  132.   local config = {}
  133.   local configFile = loadfile("/etc/pastebin.conf", "t", config)
  134.   if configFile then
  135.     local result, reason = pcall(configFile)
  136.     if not result then
  137.       io.stderr:write("Failed loading config: " .. reason)
  138.     end
  139.   end
  140.   config.key = config.key or "fd92bd40a84c127eeb6804b146793c97"
  141.   if not username then
  142.     io.write("Username: ")
  143.     username = io.read();
  144.   end
  145.   io.write(username)
  146.   io.write("Password: ")
  147.   password = term.read({pwchar='*'})
  148.   password = string.sub(password,0,string.len(password)-1)
  149.   io.write(password)
  150.   local result, response = pcall(internet.request,
  151.         "https://pastebin.com/api/api_login.php",
  152.         "api_dev_key=" .. config.key .. "&" ..
  153.         "api_user_name=" .. encode(username) .. "&" ..
  154.         "api_user_password=" .. encode(password))
  155.   if result then
  156.     local info = ""
  157.     for chunk in response do
  158.       info = info .. chunk
  159.     end
  160.     if string.match(info, "^Bad API request, ") then
  161.       io.write("failed.\n")
  162.       io.write(info)
  163.     else
  164.       io.write("success.\n")
  165.       io.write("User key is " .. info .. "\n")
  166.       local f, reason = io.open("/etc/pastebin_user.key", "w")
  167.       f:write(info)
  168.       f:close()
  169.     end
  170.   else
  171.     io.write("failed.\n")
  172.     io.stderr:write(response)
  173.   end
  174. end
  175.  
  176. local command = args[1]
  177. if command == "put" then
  178.   if #args == 2 then
  179.     put(args[2])
  180.     return
  181.   end
  182. elseif command == "get" then
  183.   if #args == 3 then
  184.     local path = args[3]
  185.     if fs.exists(path) then
  186.       if not options.f or not os.remove(path) then
  187.         io.stderr:write("file already exists")
  188.         return
  189.       end
  190.     end
  191.     get(args[2], path)
  192.     return
  193.   end
  194. elseif command == "run" then
  195.   if #args >= 2 then
  196.     run(args[2], table.unpack(args, 3))
  197.     return
  198.   end
  199. elseif command == "login" then
  200.   if #args >= 2 then
  201.     login(args[2])
  202.     return
  203.   end
  204. end
  205.  
  206. -- If we come here there was some invalid input.
  207. io.write("Usages:\n")
  208. io.write("pastebin put [-f] \n")
  209. io.write("pastebin get [-f]  \n")
  210. io.write("pastebin run [-f]  []\n")
  211. io.write("pastebin login \n")
  212. io.write(" -f: Force overwriting existing files.\n")
  213. io.write(" -k: keep line endings as-is (will convert\n")
  214. io.write("     Windows line endings to Unix otherwise).")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement