Advertisement
Sirshark10

Untitled

Jun 24th, 2018
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 = syscall.getcwd() .. "/" .. sFile
  43.     if not fs.exists( sPath ) or fs.isDir( sPath ) then
  44.         print(sPath)
  45.         print(fs.exists(sPath))
  46.         print(fs.isDir(sPath))
  47.         print( "No such file" )
  48.         return
  49.     end
  50.    
  51.     -- Read in the file
  52.     local sName = fs.getName( sPath )
  53.     local file = fs.open( sPath, "r" )
  54.     local sText = file.readAll()
  55.     file.close()
  56.    
  57.     -- POST the contents to pastebin
  58.     write( "Connecting to pastebin.com... " )
  59.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  60.     local response = http.post(
  61.         "https://pastebin.com/api/api_post.php",
  62.         "api_option=paste&"..
  63.         "api_dev_key="..key.."&"..
  64.         "api_paste_format=lua&"..
  65.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  66.         "api_paste_code="..textutils.urlEncode(sText)
  67.     )
  68.        
  69.     if response then
  70.         print( "Success." )
  71.        
  72.         local sResponse = response.readAll()
  73.         response.close()
  74.                
  75.         local sCode = string.match( sResponse, "[^/]+$" )
  76.         print( "Uploaded as "..sResponse )
  77.         print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
  78.  
  79.     else
  80.         print( "Failed." )
  81.     end
  82.    
  83. elseif sCommand == "get" then
  84.     -- Download a file from pastebin.com
  85.     if #tArgs < 3 then
  86.         printUsage()
  87.         return
  88.     end
  89.  
  90.     -- Determine file to download
  91.     local sCode = tArgs[2]
  92.     local sFile = tArgs[3]
  93.     local sPath = shell.resolve( sFile )
  94.     if fs.exists( sPath ) then
  95.         print( "File already exists" )
  96.         return
  97.     end
  98.    
  99.     -- GET the contents from pastebin
  100.     local res = get(sCode)
  101.     if res then        
  102.         local file = fs.open( sPath, "w" )
  103.         file.write( res )
  104.         file.close()
  105.        
  106.         print( "Downloaded as "..sFile )
  107.     end
  108. elseif sCommand == "run" then
  109.     local sCode = tArgs[2]
  110.  
  111.     local res = get(sCode)
  112.     if res then
  113.         local func, err = load(res, sCode, "t", _ENV)
  114.         if not func then
  115.             printError( err )
  116.             return
  117.         end
  118.         local success, msg = pcall(func, table.unpack(tArgs, 3))
  119.         if not success then
  120.             printError( msg )
  121.         end
  122.     end
  123. else
  124.     printUsage()
  125.     return
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement