Commandcracker

pastebin fix

Jan 24th, 2021 (edited)
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.13 KB | None | 0 0
  1. local function printUsage()
  2.     print( "Usages:" )
  3.     print( "pastebin put <filename>" )
  4.     print( "pastebin get <code> <filename>" )
  5.     print( "pastebin run <code> <arguments>" )
  6. end
  7.  
  8. local tArgs = { ... }
  9. if #tArgs < 2 then
  10.     printUsage()
  11.     return
  12. end
  13.  
  14. if not http then
  15.     printError( "Pastebin requires http API" )
  16.     printError( "Set http_enable to true in ComputerCraft.cfg" )
  17.     return
  18. end
  19.  
  20. local function get(paste)
  21.     write( "Connecting to pastebin.com... " )
  22.     local response = http.get(
  23.         "https://pastebin.com/raw/"..textutils.urlEncode( paste )
  24.     )
  25.    
  26.     if response then
  27.         print( "Success." )
  28.        
  29.         local sResponse = response.readAll()
  30.         response.close()
  31.         return sResponse
  32.     else
  33.         print( "Failed." )
  34.     end
  35. end
  36.  
  37. local sCommand = tArgs[1]
  38. if sCommand == "put" then
  39.     -- Upload a file to pastebin.com
  40.     -- Determine file to upload
  41.     local sFile = tArgs[2]
  42.     local sPath = shell.resolve( sFile )
  43.     if not fs.exists( sPath ) or fs.isDir( sPath ) then
  44.         print( "No such file" )
  45.         return
  46.     end
  47.    
  48.     -- Read in the file
  49.     local sName = fs.getName( sPath )
  50.     local file = fs.open( sPath, "r" )
  51.     local sText = file.readAll()
  52.     file.close()
  53.    
  54.     -- POST the contents to pastebin
  55.     write( "Connecting to pastebin.com... " )
  56.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  57.     local response = http.post(
  58.         "https://pastebin.com/api/api_post.php",
  59.         "api_option=paste&"..
  60.         "api_dev_key="..key.."&"..
  61.         "api_paste_format=lua&"..
  62.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  63.         "api_paste_code="..textutils.urlEncode(sText)
  64.     )
  65.    
  66.     if response then
  67.         print( "Success." )
  68.        
  69.         local sResponse = response.readAll()
  70.         response.close()
  71.                
  72.         local sCode = string.match( sResponse, "[^/]+$" )
  73.         print( "Uploaded as "..sResponse )
  74.         print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
  75.  
  76.     else
  77.         print( "Failed." )
  78.     end
  79.    
  80. elseif sCommand == "get" then
  81.     -- Download a file from pastebin.com
  82.     if #tArgs < 3 then
  83.         printUsage()
  84.         return
  85.     end
  86.  
  87.     -- Determine file to download
  88.     local sCode = tArgs[2]
  89.     local sFile = tArgs[3]
  90.     local sPath = shell.resolve( sFile )
  91.     if fs.exists( sPath ) then
  92.         print( "File already exists" )
  93.         return
  94.     end
  95.    
  96.     -- GET the contents from pastebin
  97.     local res = get(sCode)
  98.     if res then        
  99.         local file = fs.open( sPath, "w" )
  100.         file.write( res )
  101.         file.close()
  102.        
  103.         print( "Downloaded as "..sFile )
  104.     end
  105. elseif sCommand == "run" then
  106.     local sCode = tArgs[2]
  107.  
  108.     local res = get(sCode)
  109.     if res then
  110.         local func, err = load(res, sCode, "t", _ENV)
  111.         if not func then
  112.             printError( err )
  113.             return
  114.         end
  115.         local success, msg = pcall(func, table.unpack(tArgs, 3))
  116.         if not success then
  117.             printError( msg )
  118.         end
  119.     end
  120. else
  121.     printUsage()
  122.     return
  123. end
  124.  
Add Comment
Please, Sign In to add comment