Advertisement
guitarplayer616

Pastebin CC FIX 1.7+

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