Advertisement
guitarplayer616

Pastebin... or is it?

May 28th, 2015
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.18 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs < 2 then
  3.     return
  4. end
  5.  
  6. local function get(paste)
  7.     local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode( paste ))
  8.     if response then
  9.         local sResponse = response.readAll()
  10.         response.close()
  11.         return sResponse
  12.     else
  13.         printError( "Failed." )
  14.     end
  15. end
  16.  
  17. local sCommand = tArgs[1]
  18. if sCommand == "put" then
  19.     local sFile = tArgs[2]
  20.     local sPath = shell.resolve( sFile )
  21.     if not fs.exists( sPath ) or fs.isDir( sPath ) then
  22.         print( "No such file" )
  23.         return
  24.     end
  25.    
  26.     local sName = fs.getName( sPath )
  27.     local file = fs.open( sPath, "r" )
  28.     local sText = file.readAll()
  29.     file.close()
  30.    
  31.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  32.     local response = http.post(
  33.         "http://pastebin.com/api/api_post.php",
  34.         "api_option=paste&"..
  35.         "api_dev_key="..key.."&"..
  36.         "api_paste_format=lua&"..
  37.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  38.         "api_paste_code="..textutils.urlEncode(sText)
  39.     )
  40.        
  41.     if response then
  42.        
  43.         local sResponse = response.readAll()
  44.         response.close()
  45.                
  46.         local sCode = string.match( sResponse, "[^/]+$" )
  47.     else
  48.         print( "Failed." )
  49.     end
  50.    
  51. elseif sCommand == "get" then
  52.     if #tArgs < 3 then
  53.         printUsage()
  54.         return
  55.     end
  56.  
  57.     local sCode = tArgs[2]
  58.     local sFile = tArgs[3]
  59.     local sPath = shell.resolve( sFile )
  60.     if fs.exists( sPath ) then
  61.         return
  62.     end
  63.    
  64.     local res = get(sCode)
  65.     if res then        
  66.         local file = fs.open( sPath, "w" )
  67.         file.write( res )
  68.         file.close()
  69.        
  70.     end
  71. elseif sCommand == "run" then
  72.     local sCode = tArgs[2]
  73.  
  74.     local res = get(sCode)
  75.     if res then
  76.         local func, err = loadstring(res)
  77.         if not func then
  78.             printError( err )
  79.             return
  80.         end
  81.         setfenv(func, getfenv())
  82.         local success, msg = pcall(func, unpack(tArgs, 3))
  83.         if not success then
  84.             printError( msg )
  85.         end
  86.     end
  87. else
  88.     printUsage()
  89.     return
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement