Advertisement
Guest User

pastebin

a guest
Jan 26th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. local fs = require("filesystem")
  2. local internet = require("internet")
  3. local component = require("component")
  4. local shell = require("shell")
  5. local term = require("term")
  6.  
  7. local args, options = shell.parse(...)
  8. local function printUsage()
  9.   print("Usages:")
  10.   print("pastebin put [-f] <file>")
  11.   print("pastebin get [-f] <id> <file>")
  12.   print("pastebin run [-f] <id> <arguments>")
  13.   print(" -f: Force overwriting existing files.")
  14.   print(" -k: keep line endings as-is (will convert")
  15.   print("     Windows line endings to Unix otherwise).")
  16. end
  17. if #args < 2 then
  18.   printUsage()
  19.   return
  20. end
  21.  
  22. if not component.isAvailable("internet") then
  23.   print( "Error: Pastebin requires an Internet Card to run" )
  24.   return
  25. end
  26.  
  27. local function get(paste, filename)
  28. term.write( "Connecting to pastebin.com... " )
  29. local f, reason = io.open(filename, "w")
  30. if not f then
  31.   print("failed opening file for writing: " .. reason)
  32.   return
  33. end
  34.  
  35. local url = "http://pastebin.com/raw.php?i=" .. paste
  36. local result, response = pcall(internet.request, url)
  37. if result then
  38.   print("Success.")
  39.   for chunk in response do
  40.     if not options.k then
  41.       string.gsub(chunk, "\r\n", "\n")
  42.     end
  43.     f:write(chunk)
  44.   end
  45.  
  46.   f:close()
  47.   print("saved data to " .. filename)
  48. else
  49.   f:close()
  50.   fs.remove(filename)
  51.   print("http request failed: " .. response)
  52. end
  53. end
  54.  
  55. function encode( code )
  56.     if code then
  57.         code = string.gsub (code, "([^%w ])",
  58.         function (c)
  59.             return string.format ("%%%02X", string.byte(c))
  60.         end)
  61.         code = string.gsub (code, " ", "+")
  62.     end
  63.     return code
  64. end
  65.  
  66. function run(paste)
  67. local tmpFile = "/tmp/tmp_pastebin.lua"
  68. get(paste,shell.resolve(tmpFile))
  69. term.clear()
  70. print("Running...")
  71.  
  72. local success, msg = shell.execute(tmpFile, _ENV, table.unpack(args, 3))
  73.     if not success then
  74.       print( msg )
  75.     end
  76.     fs.remove(tmpFile)
  77. end
  78.  
  79. function put(file)
  80.   --local fKey = io.open("/etc/pastebin.key","r")
  81.   --local key = fKey:read("*a")
  82.   --fKey:close()
  83.   local path = shell.resolve( file )
  84.   local sName = fs.name( path )
  85.   local key = "fd92bd40a84c127eeb6804b146793c97"
  86.   local fText, reason = io.open(path,"r")
  87.  
  88.   if not fText then
  89.     print("failed opening file for reading: " .. reason)
  90.     return
  91.   end
  92.  
  93.   local sText = fText:read("*a")
  94.   fText:close()
  95.  
  96.   local result,response = pcall(internet.request,
  97.         "http://pastebin.com/api/api_post.php",
  98.         "api_option=paste&"..
  99.         "api_dev_key="..key.."&"..
  100.         "api_paste_format=lua&"..
  101.         "api_paste_expire_date=N&"..
  102.         "api_paste_name="..encode(sName).."&"..
  103.         "api_paste_code="..encode(sText)
  104.     )
  105.    
  106.     if response then
  107.         print( "Success." )
  108.         local sResponse = ""
  109.         for chunk in response do
  110.           sResponse = sResponse..chunk
  111.         end        
  112.         local sCode = string.match( sResponse, "[^/]+$" )
  113.         print( "Uploaded as "..sResponse )
  114.         print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
  115.  
  116.     else
  117.         print( "Failed: "..response )
  118.     end
  119. end
  120.  
  121. local sCommand = args[1]
  122. local kArg = args[2]
  123. if #args >=2 then
  124.   if sCommand=="put" then
  125.     if #args == 2 then
  126.       local file = kArg
  127.       put(file)
  128.     else
  129.       printUsage()
  130.       return
  131.     end
  132.   elseif sCommand == "get" then
  133.     if #args == 3 then
  134.       local sFile = shell.resolve(args[3])
  135.       if fs.exists(sFile) then
  136.         if not options.f or not os.remove(sFile) then
  137.           print("file already exists")
  138.           return
  139.         end
  140.       end
  141.       get(kArg,sFile)
  142.     else
  143.       printUsage()
  144.       return
  145.     end
  146.   elseif sCommand == "run" then
  147.       run(kArg)
  148.   end
  149. else
  150.   printUsage()
  151.   return
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement