Advertisement
krakaen

Pastebin Program Fix - ComputerCraft

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