Advertisement
cyber01

Post in profile

Aug 3rd, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.39 KB | None | 0 0
  1. --[[ This program allows downloading and uploading from and to pastebin.com.
  2.      Authors: Sangar, Vexatos. Light edit by cyber01]]
  3. local component = require("component")
  4. local fs = require("filesystem")
  5. local internet = require("internet")
  6. local shell = require("shell")
  7. if not component.isAvailable("internet") then
  8.   io.stderr:write("This program requires an internet card to run.")
  9.   return
  10. end
  11.  
  12. local args, options = shell.parse(...)
  13.  
  14. -- This gets code from the website and stores it in the specified file.
  15. local function get(pasteId, filename)
  16.   local f, reason = io.open(filename, "w")
  17.   if not f then
  18.     io.stderr:write("Failed opening file for writing: " .. reason)
  19.     return
  20.   end
  21.  
  22.   io.write("Downloading from pastebin.com... ")
  23.   local url = "http://pastebin.com/raw.php?i=" .. pasteId
  24.   local result, response = pcall(internet.request, url)
  25.   if result then
  26.     io.write("success.\n")
  27.     for chunk in response do
  28.       if not options.k then
  29.         string.gsub(chunk, "\r\n", "\n")
  30.       end
  31.       f:write(chunk)
  32.     end
  33.  
  34.     f:close()
  35.     io.write("Saved data to " .. filename .. "\n")
  36.   else
  37.     io.write("failed.\n")
  38.     f:close()
  39.     fs.remove(filename)
  40.     io.stderr:write("HTTP request failed: " .. response .. "\n")
  41.   end
  42. end
  43.  
  44.  
  45.  
  46. -- This makes a string safe for being used in a URL.
  47. function encode(code)
  48.   if code then
  49.     code = string.gsub(code, "([^%w ])", function (c)
  50.       return string.format("%%%02X", string.byte(c))
  51.     end)
  52.     code = string.gsub(code, " ", "+")
  53.   end
  54.   return code
  55. end
  56.  
  57. -- This stores the program in a temporary file, which it will
  58. -- delete after the program was executed.
  59. function run(pasteId, ...)
  60.   local tmpFile = os.tmpname()
  61.   get(pasteId, tmpFile)
  62.   io.write("Running...\n")
  63.  
  64.   local success, reason = shell.execute(tmpFile, nil, ...)
  65.   if not success then
  66.     io.stderr:write(reason)
  67.   end
  68.   fs.remove(tmpFile)
  69. end
  70.  
  71. -- Uploads the specified file as a new paste to pastebin.com.
  72. function put(path)
  73.   devkey = "fd92bd40a84c127eeb6804b146793c97"
  74.   f = io.open("/etc/pastebin.conf","r")
  75.   local userkey = f:read()
  76.   local file, reason = io.open(path, "r")
  77.  
  78.   if not file then
  79.     io.stderr:write("Failed opening file for reading: " .. reason)
  80.     return
  81.   end
  82.  
  83.   local data = file:read("*a")
  84.   file:close()
  85.  
  86.   io.write("Uploading to pastebin.com... ")
  87.   local result, response = pcall(internet.request,
  88.         "http://pastebin.com/api/api_post.php",
  89.         "api_option=paste&" ..
  90.         "api_dev_key=" .. devkey .. "&" ..
  91.         "api_paste_format=lua&" ..
  92.         "api_user_key=" .. userkey .. "&" ..
  93.         "api_paste_expire_date=N&" ..
  94.         "api_paste_name=" .. encode(fs.name(path)) .. "&" ..
  95.         "api_paste_code=" .. encode(data))
  96.  
  97.   if result then
  98.     local info = ""
  99.     for chunk in response do
  100.       info = info .. chunk
  101.     end
  102.     if string.match(info, "^Bad API request, ") then
  103.       io.write("failed.\n")
  104.       io.write(info)
  105.     else
  106.       io.write("success.\n")
  107.       local pasteId = string.match(info, "[^/]+$")
  108.       io.write("Uploaded as " .. info .. "\n")
  109.       io.write('Run "pastebin get ' .. pasteId .. '" to download anywhere.')
  110.     end
  111.   else
  112.     io.write("failed.\n")
  113.     io.stderr:write(response)
  114.   end
  115. end
  116.  
  117. -- Получает ключ пользователя с pastebin.com.
  118. function getkey(login, pass)
  119.   io.write("Получение ключа с pastebin.com... ")
  120.   local result, response = pcall(internet.request,
  121.         "http://pastebin.com/api/api_login.php",
  122.         "api_dev_key=c5057ef0e6634227d486818f105fba2d&" ..
  123.         "api_user_name=" .. encode(login) .. "&" ..
  124.         "api_user_password=" .. encode(pass))
  125.  
  126.   if result then
  127.     local info = ""
  128.     for chunk in response do
  129.       info = info .. chunk
  130.     end
  131.     if string.match(info, "^Bad API request, ") then
  132.       io.write("безуспешно.\n")
  133.       io.write(info)
  134.     else
  135.       io.write("Успешно.\n")
  136.       local pasteId = string.match(info, "[^/]+$")
  137.       io.write("Ваш ключ " .. info .. "\n")
  138.       io.write('Теперь вы можете загружать код в свой аккаунт на pastebin.com')
  139.       f = io.open("/etc/pastebin.conf","a");
  140.       f:write(info)
  141.       f:flush()
  142.       f:close()
  143.     end
  144.   else
  145.     io.write("безуспешно.\n")
  146.     io.stderr:write(response)
  147.   end
  148. end
  149.  
  150. local command = args[1]
  151. if command == "put" then
  152.   if #args == 2 then
  153.     put(shell.resolve(args[2]))
  154.     return
  155.   end
  156. elseif command == "getkey" then
  157.   if #args == 3 then
  158.     getkey(args[2],args[3])
  159.     return
  160.   end
  161. elseif command == "get" then
  162.   if #args == 3 then
  163.     local path = shell.resolve(args[3])
  164.     if fs.exists(path) then
  165.       if not options.f or not os.remove(path) then
  166.         io.stderr:write("file already exists")
  167.         return
  168.       end
  169.     end
  170.     get(args[2], path)
  171.     return
  172.   end
  173. elseif command == "run" then
  174.   if #args >= 2 then
  175.     run(args[2], table.unpack(args, 3))
  176.     return
  177.   end
  178. end
  179.  
  180. -- If we come here there was some invalid input.
  181. io.write("Usages:\n")
  182. io.write("pastebin put [-f] <file>\n")
  183. io.write("pastebin get [-f] <id> <file>\n")
  184. io.write("pastebin getkey <login> <password>\n")
  185. io.write("pastebin run [-f] <id> [<arguments...>]\n")
  186. io.write(" -f: Force overwriting existing files.\n")
  187. io.write(" -k: keep line endings as-is (will convert\n")
  188. io.write("     Windows line endings to Unix otherwise).")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement